Skip to content

Commit 970cd4f

Browse files
[mypyc] feat: add IndexOp support to constant_fold_expr
1 parent c71fef0 commit 970cd4f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

mypyc/irbuild/constant_fold.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue |
7272
value = constant_fold_expr(builder, expr.expr)
7373
if value is not None and not isinstance(value, bytes):
7474
return constant_fold_unary_op(expr.op, value)
75+
elif isinstance(expr, IndexOp):
76+
base = constant_fold_expr(builder, expr.base)
77+
if base is not None:
78+
index = constant_fold_expr(builder, expr.base)
79+
if index is not None:
80+
try:
81+
return base[index]
82+
except Exception:
83+
return None
7584
return None
7685

7786

0 commit comments

Comments
 (0)