Skip to content

Commit fb13fc0

Browse files
Add runtime information (like index range, missing element) in the error message
1 parent 6cf03b9 commit fb13fc0

File tree

6 files changed

+19
-15
lines changed

6 files changed

+19
-15
lines changed

src/libasr/codegen/llvm_utils.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -969,10 +969,16 @@ namespace LFortran {
969969
builder->CreateCondBr(cond, thenBB, elseBB);
970970
builder->SetInsertPoint(thenBB);
971971
{
972-
std::string message = "list index out of range";
973-
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("IndexError: %s\n");
974-
llvm::Value *fmt_ptr2 = builder->CreateGlobalStringPtr(message);
975-
print_error(context, module, *builder, {fmt_ptr, fmt_ptr2});
972+
std::string index_error = "IndexError: %s%d%s%d\n",
973+
message1 = "List index is out of range. Index range is (0, ",
974+
message2 = "), but the given index is ";
975+
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr(index_error);
976+
llvm::Value *fmt_ptr1 = builder->CreateGlobalStringPtr(message1);
977+
llvm::Value *fmt_ptr2 = builder->CreateGlobalStringPtr(message2);
978+
llvm::Value *end_minus_one = builder->CreateSub(end_point,
979+
llvm::ConstantInt::get(context, llvm::APInt(32, 1)));
980+
print_error(context, module, *builder, {fmt_ptr, fmt_ptr1,
981+
end_minus_one, fmt_ptr2, pos});
976982
int exit_code_int = 1;
977983
llvm::Value *exit_code = llvm::ConstantInt::get(context,
978984
llvm::APInt(32, exit_code_int));
@@ -2417,12 +2423,10 @@ namespace LFortran {
24172423
builder->CreateCondBr(cond, thenBB, elseBB);
24182424
builder->SetInsertPoint(thenBB);
24192425
{
2420-
// TODO: Allow runtime information like the exact element which is
2421-
// not found.
2422-
std::string message = "The list does not contain the element";
2423-
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("ValueError: %s\n");
2426+
std::string message = "The list does not contain the element: ";
2427+
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("ValueError: %s%d\n");
24242428
llvm::Value *fmt_ptr2 = builder->CreateGlobalStringPtr(message);
2425-
print_error(context, module, *builder, {fmt_ptr, fmt_ptr2});
2429+
print_error(context, module, *builder, {fmt_ptr, fmt_ptr2, item});
24262430
int exit_code_int = 1;
24272431
llvm::Value *exit_code = llvm::ConstantInt::get(context,
24282432
llvm::APInt(32, exit_code_int));

tests/reference/runtime-test_list_01-3ee9b3e.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "runtime-test_list_01-3ee9b3e.stderr",
11-
"stderr_hash": "3bc6c29af7798c771cee99ac69fb9beacafd9e95479b08e47032ecf1",
11+
"stderr_hash": "c9cbdc4df1e7fbe0a6e73db7ac7809f71102fb524c8aa86139271006",
1212
"returncode": 1
1313
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ValueError: The list does not contain the element
1+
ValueError: The list does not contain the element: 4

tests/reference/runtime-test_list_02-5f7db5f.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"basename": "runtime-test_list_02-5f7db5f",
33
"cmd": "lpython {infile}",
44
"infile": "tests/runtime_errors/test_list_02.py",
5-
"infile_hash": "73803ffe5a87f1ac9a76ea16075d8fbfabb4968b40c7265952a529e1",
5+
"infile_hash": "f74ffd3eeb1f4a61b105d50f030fcd9e1095cc70d0a618763bbf367b",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "runtime-test_list_02-5f7db5f.stderr",
11-
"stderr_hash": "6c43f8d578ad68cecf9f508bf6469418796f38489b3eb524f98391d7",
11+
"stderr_hash": "f45ba86e610eeb2cc11fc176120e56d153a896f486e9b04d94fa3de1",
1212
"returncode": 1
1313
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
IndexError: list index out of range
1+
IndexError: List index is out of range. Index range is (0, 2), but the given index is 3

tests/runtime_errors/test_list_02.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
def test_list():
55
x: list[i32]
66
x = [1, 2, 3]
7-
x[4] = 0
7+
x[3] = 0
88

99
test_list()

0 commit comments

Comments
 (0)