@@ -4021,6 +4021,31 @@ def testfunc(n):
40214021 self .assertIn ("_TO_BOOL_DICT" , uops )
40224022 self .assertNotIn ("_TO_BOOL" , uops )
40234023
4024+ def test_guard_type_version_resolves_type_for_to_bool (self ):
4025+ # Tests the _GUARD_TYPE_VERSION + _RECORD_TOS_TYPE flow:
4026+ # TO_BOOL_GENERIC records the type, then _GUARD_TYPE_VERSION
4027+ # resolves it (even if the version cache has a collision),
4028+ # enabling the optimizer to specialize _TO_BOOL → _TO_BOOL_DICT.
4029+ def testfunc (n ):
4030+ d = {"key" : "value" }
4031+ count = 0
4032+ for _ in range (n ):
4033+ if d :
4034+ count += 1
4035+ d ["key" ] = "value" # mutation keeps dict non-trivial
4036+ return count
4037+
4038+ res , ex = self ._run_with_optimizer (testfunc , TIER2_THRESHOLD )
4039+ self .assertEqual (res , TIER2_THRESHOLD )
4040+ self .assertIsNotNone (ex )
4041+ uops = get_opnames (ex )
4042+ # The optimizer should resolve the dict type from recorded type
4043+ # info and specialize _TO_BOOL into _TO_BOOL_DICT
4044+ self .assertIn ("_TO_BOOL_DICT" , uops )
4045+ self .assertNotIn ("_TO_BOOL" , uops )
4046+ # _GUARD_TYPE_VERSION should be present (guards the dict type)
4047+ self .assertIn ("_GUARD_TYPE_VERSION" , uops )
4048+
40244049 def test_attr_promotion_failure (self ):
40254050 # We're not testing for any specific uops here, just
40264051 # testing it doesn't crash.
0 commit comments