Skip to content

Added support for negative constant indexing in tuples #1534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ RUN(NAME test_list_10 LABELS cpython llvm c)
RUN(NAME test_list_section LABELS cpython llvm c)
RUN(NAME test_tuple_01 LABELS cpython llvm c)
RUN(NAME test_tuple_02 LABELS cpython llvm c)
RUN(NAME test_tuple_03 LABELS cpython llvm)
RUN(NAME test_dict_01 LABELS cpython llvm)
RUN(NAME test_dict_02 LABELS cpython llvm)
RUN(NAME test_dict_03 LABELS cpython llvm)
Expand Down
10 changes: 10 additions & 0 deletions integration_tests/test_tuple_03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from ltypes import i32, f64

def f():
t1: tuple[i32, f64, str] = (1, 2.0, "3")

assert t1[-1] == "3"
assert t1[-2] == 2.0
assert t1[-3] == 1

f()
24 changes: 23 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3125,8 +3125,30 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
ASR::down_cast<ASR::List_t>(type)->m_type, nullptr);
return false;
} else if (ASR::is_a<ASR::Tuple_t>(*type)) {
int i = 0;
index = ASRUtils::EXPR(tmp);
int i = ASR::down_cast<ASR::IntegerConstant_t>(ASRUtils::EXPR(tmp))->m_n;
ASR::expr_t* val = ASRUtils::expr_value(index);
if (!val) {
throw SemanticError("Runtime Indexing with " + ASRUtils::type_to_str_python(type) +
" is not possible.", loc);
}

if (ASR::is_a<ASR::IntegerConstant_t>(*val)) {
i = ASR::down_cast<ASR::IntegerConstant_t>(val)->m_n;
int tuple_size = ASR::down_cast<ASR::Tuple_t>(type)->n_type;
if (i < 0) {
i = tuple_size + i;
ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
4, nullptr, 0));
index = ASRUtils::EXPR(ASR::make_IntegerConstant_t(
al, loc, i, int_type));
}
if (i >= tuple_size || i < 0) {
throw SemanticError("Tuple index out of bounds", loc);
}
} else {
throw SemanticError("Tuple indices must be constant integers", loc);
}
tmp = make_TupleItem_t(al, loc, value, index,
ASR::down_cast<ASR::Tuple_t>(type)->m_type[i], nullptr);
return false;
Expand Down
6 changes: 6 additions & 0 deletions tests/errors/test_tuple2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def main0():
t: tuple[i32, i32, i32] = (1, 2, 3)
x: i32 = -1
print(t[x])

main0()
5 changes: 5 additions & 0 deletions tests/errors/test_tuple3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def main0():
t: tuple[i32, i32, i32] = (1, 2, 3)
print(t[-4])

main0()
13 changes: 13 additions & 0 deletions tests/reference/asr-test_tuple2-b046610.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_tuple2-b046610",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_tuple2.py",
"infile_hash": "84aa4ecdd21f5d633975d08f6927a7d336c47a31c2fd2bc42cae1c7b",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_tuple2-b046610.stderr",
"stderr_hash": "1e22bbde9c9fdc171314f1158dcac6bf6373d94e00c9878f66d7b667",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_tuple2-b046610.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Runtime Indexing with tuple[i32, i32, i32] is not possible.
--> tests/errors/test_tuple2.py:4:11
|
4 | print(t[x])
| ^^^^
13 changes: 13 additions & 0 deletions tests/reference/asr-test_tuple3-19fa47f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-test_tuple3-19fa47f",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/test_tuple3.py",
"infile_hash": "cfa12cc4d8456bfd45e77cc49c5e8d2417181a1443cdaaf8b15c0528",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-test_tuple3-19fa47f.stderr",
"stderr_hash": "780d951c4bfa3476ee84d16d9a89c5a9255691b270bfb312d974608e",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-test_tuple3-19fa47f.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Tuple index out of bounds
--> tests/errors/test_tuple3.py:3:11
|
3 | print(t[-4])
| ^^^^^
8 changes: 8 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,14 @@ asr = true
filename = "errors/test_tuple1.py"
asr = true

[[test]]
filename = "errors/test_tuple2.py"
asr = true

[[test]]
filename = "errors/test_tuple3.py"
asr = true

[[test]]
filename = "errors/test_for2.py"
asr = true
Expand Down