Skip to content

Commit 826e0ad

Browse files
[mypyc] feat: support constant folding in translate_index_expr [1/1] (#19972)
This PR attempts to constant fold the index value in `translate_index_expr` I'm not sure any test changes are warranted for a small PR of this nature. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f8ecfe5 commit 826e0ad

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

mypyc/irbuild/expression.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,12 @@ def transform_index_expr(builder: IRBuilder, expr: IndexExpr) -> Value:
590590

591591
base = builder.accept(expr.base, can_borrow=can_borrow_base)
592592

593-
if isinstance(base.type, RTuple) and isinstance(index, IntExpr):
594-
return builder.add(TupleGet(base, index.value, expr.line))
593+
if isinstance(base.type, RTuple):
594+
folded_index = constant_fold_expr(builder, index)
595+
if isinstance(folded_index, int):
596+
length = len(base.type.types)
597+
if -length <= folded_index <= length - 1:
598+
return builder.add(TupleGet(base, folded_index, expr.line))
595599

596600
if isinstance(index, SliceExpr):
597601
value = try_gen_slice_op(builder, base, index)

0 commit comments

Comments
 (0)