Skip to content

Commit 80b6b58

Browse files
committed
[mypyc] feat: support constant folding in IRBuilder.extract_int
1 parent 053c054 commit 80b6b58

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

mypyc/irbuild/builder.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
object_rprimitive,
107107
str_rprimitive,
108108
)
109+
from mypyc.irbuild.constant_fold import constant_fold_expr
109110
from mypyc.irbuild.context import FuncInfo, ImplicitClass
110111
from mypyc.irbuild.ll_builder import LowLevelIRBuilder
111112
from mypyc.irbuild.mapper import Mapper
@@ -965,12 +966,8 @@ def maybe_spill_assignable(self, value: Value) -> Register | AssignmentTarget:
965966
return reg
966967

967968
def extract_int(self, e: Expression) -> int | None:
968-
if isinstance(e, IntExpr):
969-
return e.value
970-
elif isinstance(e, UnaryExpr) and e.op == "-" and isinstance(e.expr, IntExpr):
971-
return -e.expr.value
972-
else:
973-
return None
969+
folded = constant_fold_expr(self, e)
970+
return folded if isinstance(folded, int) else None
974971

975972
def get_sequence_type(self, expr: Expression) -> RType:
976973
return self.get_sequence_type_from_type(self.types[expr])

0 commit comments

Comments
 (0)