Skip to content
Open
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions mypyc/irbuild/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,12 @@ def transform_index_expr(builder: IRBuilder, expr: IndexExpr) -> Value:

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

if isinstance(base.type, RTuple) and isinstance(index, IntExpr):
return builder.add(TupleGet(base, index.value, expr.line))
if (
isinstance(base.type, RTuple)
and isinstance(folded_index := constant_fold_expr(builder, index), int)
and folded_index >= 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for index overflow (it may generate an error elsewhere, but we don't want to crash the compiler if it's ignored).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

I also just thought about reverse indexing, I'll implement that too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to adapt TupleGet before we can pass negative values here.

I've implemented that in a separate PR #19990 , lets pause this one until we get that merged in so we don't have to come back to this section later

):
return builder.add(TupleGet(base, folded_index, expr.line))

if isinstance(index, SliceExpr):
value = try_gen_slice_op(builder, base, index)
Expand Down