Skip to content

Commit

Permalink
Handle the case where start is greater than end in list.index() (lc…
Browse files Browse the repository at this point in the history
  • Loading branch information
faze-geek authored Jun 25, 2023
1 parent 97c61a6 commit e2b03ca
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
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

0 comments on commit e2b03ca

Please sign in to comment.