@@ -1437,6 +1437,68 @@ def crash_addition():
14371437
14381438 crash_addition ()
14391439
1440+ def test_narrow_type_to_constant_bool_false (self ):
1441+ def f (n ):
1442+ trace = []
1443+ for i in range (n ):
1444+ # f is always False, but we can only prove that it's a bool:
1445+ f = i == TIER2_THRESHOLD
1446+ trace .append ("A" )
1447+ if not f : # Kept.
1448+ trace .append ("B" )
1449+ if not f : # Removed!
1450+ trace .append ("C" )
1451+ trace .append ("D" )
1452+ if f : # Removed!
1453+ trace .append ("X" )
1454+ trace .append ("E" )
1455+ trace .append ("F" )
1456+ if f : # Removed!
1457+ trace .append ("X" )
1458+ trace .append ("G" )
1459+ return trace
1460+
1461+ trace , ex = self ._run_with_optimizer (f , TIER2_THRESHOLD )
1462+ self .assertEqual (trace , list ("ABCDEFG" ) * TIER2_THRESHOLD )
1463+ self .assertIsNotNone (ex )
1464+ uops = get_opnames (ex )
1465+ # Only one guard remains:
1466+ self .assertEqual (uops .count ("_GUARD_IS_FALSE_POP" ), 1 )
1467+ self .assertEqual (uops .count ("_GUARD_IS_TRUE_POP" ), 0 )
1468+ # But all of the appends we care about are still there:
1469+ self .assertEqual (uops .count ("_CALL_LIST_APPEND" ), len ("ABCDEFG" ))
1470+
1471+ def test_narrow_type_to_constant_bool_true (self ):
1472+ def f (n ):
1473+ trace = []
1474+ for i in range (n ):
1475+ # f is always True, but we can only prove that it's a bool:
1476+ f = i != TIER2_THRESHOLD
1477+ trace .append ("A" )
1478+ if f : # Kept.
1479+ trace .append ("B" )
1480+ if not f : # Removed!
1481+ trace .append ("X" )
1482+ trace .append ("C" )
1483+ if f : # Removed!
1484+ trace .append ("D" )
1485+ trace .append ("E" )
1486+ trace .append ("F" )
1487+ if not f : # Removed!
1488+ trace .append ("X" )
1489+ trace .append ("G" )
1490+ return trace
1491+
1492+ trace , ex = self ._run_with_optimizer (f , TIER2_THRESHOLD )
1493+ self .assertEqual (trace , list ("ABCDEFG" ) * TIER2_THRESHOLD )
1494+ self .assertIsNotNone (ex )
1495+ uops = get_opnames (ex )
1496+ # Only one guard remains:
1497+ self .assertEqual (uops .count ("_GUARD_IS_FALSE_POP" ), 0 )
1498+ self .assertEqual (uops .count ("_GUARD_IS_TRUE_POP" ), 1 )
1499+ # But all of the appends we care about are still there:
1500+ self .assertEqual (uops .count ("_CALL_LIST_APPEND" ), len ("ABCDEFG" ))
1501+
14401502
14411503def global_identity (x ):
14421504 return x
0 commit comments