Skip to content

Commit 139071c

Browse files
BobTheBuidlerpre-commit-ci[bot]p-sawicki
authored
[mypyc] feat: support negative index in TupleGet op (#19990)
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 #19972 so I think we should consider this PR a prerequisite to that one --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Piotr Sawicki <[email protected]>
1 parent d2a8800 commit 139071c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

mypyc/ir/ops.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,10 +1045,17 @@ 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+
), f"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 (
1057+
self.index <= src_len - 1
1058+
), f"Index out of range.\nsource type: {src.type}\nindex: {index}"
10521059
self.type = src.type.types[index]
10531060
self.is_borrowed = borrow
10541061

0 commit comments

Comments
 (0)