Skip to content
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

Handle the case where start is greater than end in list.index() #2022

Merged
merged 2 commits into from
Jun 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
5 changes: 4 additions & 1 deletion src/libasr/codegen/llvm_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2787,7 +2787,10 @@ namespace LCompilers {

llvm::Value* cond = builder->CreateICmpEQ(
LLVM::CreateLoad(*builder, i), end_point);
llvm_utils->create_if_else(cond, [&]() {
llvm::Value* start_greater_than_end = builder->CreateICmpSGE(
LLVM::CreateLoad(*builder, i), end_point);
llvm::Value* condition = builder->CreateOr(cond, start_greater_than_end);
llvm_utils->create_if_else(condition, [&]() {
std::string message = "The list does not contain the element: ";
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("ValueError: %s%d\n");
llvm::Value *fmt_ptr2 = builder->CreateGlobalStringPtr(message);
Expand Down
8 changes: 8 additions & 0 deletions tests/errors/test_list_index4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from lpython import i32

def test_list_index4_error():
a: list[i32]
a = [1, 2, 3]
print(a.index(2,2,1))

test_list_index4_error()
13 changes: 13 additions & 0 deletions tests/reference/runtime-test_list_index4-c31dfdb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "runtime-test_list_index4-c31dfdb",
"cmd": "lpython {infile}",
"infile": "tests/errors/test_list_index4.py",
"infile_hash": "4513031129335b8c83686936c12f6b24516c14a79ad5c3b9a9d98113",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "runtime-test_list_index4-c31dfdb.stderr",
"stderr_hash": "3e3eee1ba7f96c1edaeed1c7b93f54b397dbe3b08d20c9b720c291a5",
"returncode": 1
}
1 change: 1 addition & 0 deletions tests/reference/runtime-test_list_index4-c31dfdb.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ValueError: The list does not contain the element: 2
4 changes: 4 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,10 @@ run = true
filename = "errors/test_list_index3.py"
run = true

[[test]]
filename = "errors/test_list_index4.py"
run = true

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