Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions mypyc/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1045,10 +1045,17 @@ class TupleGet(RegisterOp):

def __init__(self, src: Value, index: int, line: int = -1, *, borrow: bool = False) -> None:
super().__init__(line)
assert isinstance(
src.type, RTuple
), "TupleGet only operates on tuples, not {type(src.type).__name__}"
src_len = len(src.type.types)
self.src = src
self.index = index
assert isinstance(src.type, RTuple), "TupleGet only operates on tuples"
assert index >= 0
if index < 0:
self.index += src_len
assert (
self.index <= src_len - 1
), f"Index out of range.\nsource type: {src.type}\nindex: {index}"
self.type = src.type.types[index]
self.is_borrowed = borrow

Expand Down
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):
folded_index = constant_fold_expr(builder, index)
if isinstance(folded_index, int):
length = len(base.type.types)
if -length <= folded_index <= length - 1:
return builder.add(TupleGet(base, folded_index, expr.line))

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