@@ -1437,6 +1437,67 @@ def crash_addition():
1437
1437
1438
1438
crash_addition ()
1439
1439
1440
+ def test_narrow_type_to_constant_int_zero (self ):
1441
+ def f (n ):
1442
+ trace = []
1443
+ for i in range (n ):
1444
+ # f is always (int) 0, but we can only prove that it's a integer:
1445
+ f = i - i # at this point python knows f is an int, but doesnt know that it is 0 (we know it is 0 though)
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"))
1440
1501
1441
1502
def global_identity (x ):
1442
1503
return x
0 commit comments