Skip to content

Commit 692844c

Browse files
Update constant_fold.py
1 parent 3f18ed0 commit 692844c

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

mypyc/irbuild/constant_fold.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
MemberExpr,
2424
NameExpr,
2525
OpExpr,
26+
SliceExpr,
2627
StrExpr,
2728
UnaryExpr,
2829
Var,
@@ -76,7 +77,32 @@ def constant_fold_expr(builder: IRBuilder, expr: Expression) -> ConstantValue |
7677
elif isinstance(expr, IndexExpr):
7778
base = constant_fold_expr(builder, expr.base)
7879
if base is not None:
79-
index = constant_fold_expr(builder, expr.base)
80+
index_expr = expr.index
81+
if isinstance(index_expr, SliceExpr):
82+
if index_expr.begin_index is None:
83+
begin_index = None
84+
else:
85+
begin_index = constant_fold_expr(builder, index_expr.begin_index)
86+
if begin_index is None:
87+
return None
88+
if index_expr.end_index is None:
89+
end_index = None
90+
else:
91+
end_index = constant_fold_expr(builder, index_expr.end_index)
92+
if end_index is None:
93+
return None
94+
if index_expr.stride is None:
95+
stride = None
96+
else:
97+
stride = constant_fold_expr(builder, index_expr.stride)
98+
if stride is None:
99+
return None
100+
try:
101+
return base[begin_index:end_index:stride]
102+
except Exception:
103+
return None
104+
105+
index = constant_fold_expr(builder, index_expr)
80106
if index is not None:
81107
try:
82108
return base[index] # type: ignore [index]

0 commit comments

Comments
 (0)