Skip to content

Commit 3f11f26

Browse files
[mypyc] feat: support negative index in TupleGet op
This PR modifies `TupleGet.__init__` to automatically convert negative indexes to positive indexes instead of crashing at the assert This won't change functionality on its own, since none of the existing calling locations can pass a negative value, but will allow us to pass negative values in python#19972 so I think we should consider this PR a prerequisite to that one
1 parent a62f273 commit 3f11f26

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

mypyc/ir/ops.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,10 +1045,15 @@ class TupleGet(RegisterOp):
10451045

10461046
def __init__(self, src: Value, index: int, line: int = -1, *, borrow: bool = False) -> None:
10471047
super().__init__(line)
1048+
assert isinstance(
1049+
src.type, RTuple
1050+
), "TupleGet only operates on tuples, not {type(src.type).__name__}"
1051+
src_len = len(src.type.types)
10481052
self.src = src
10491053
self.index = index
1050-
assert isinstance(src.type, RTuple), "TupleGet only operates on tuples"
1051-
assert index >= 0
1054+
if index < 0:
1055+
self.index += src_len
1056+
assert self.index <= src_len - 1, f"Index out of range.\nsource type: {src.type}\nindex: {index}"
10521057
self.type = src.type.types[index]
10531058
self.is_borrowed = borrow
10541059

0 commit comments

Comments
 (0)