Open
Description
What works
def main0():
l: list[i32] = [1, 2, 3]
x: i32 = 2
print(l[1], l[-1], l[x])
main0()
(lf) anutosh491@spbhat68:~/lpython/lpython$ ./src/bin/lpython examples/expr2.py
2 3 3
What doesn't work
(lf) anutosh491@spbhat68:~/lpython/lpython$ cat examples/expr2.py
def main0():
l: list[i32] = [1, 2, 3]
x: i32 = -2
print(l[x])
main0()
(lf) anutosh491@spbhat68:~/lpython/lpython$ ./src/bin/lpython examples/expr2.py
IndexError: List index is out of range. Index range is (0, 2), but the given index is -2
(lf) anutosh491@spbhat68:~/lpython/lpython$ python examples/expr2.py
2
So basically what's happening is , it follows the expected code flow and through the code block responsible for handling negative indexing . But here the val
comes out to be a null pointer when indexing is done through a variable .
lpython/src/lpython/semantics/python_ast_to_asr.cpp
Lines 3109 to 3114 in 8f62ac4
The positive index remains unaffected though (it skips this block ) and still works out well through the code which follows
tmp = make_ListItem_t(al, loc, value, index,
ASR::down_cast<ASR::List_t>(type)->m_type, nullptr);
return false;
Metadata
Metadata
Assignees
Labels
No labels