Skip to content

Commit fce0b35

Browse files
authored
Fix bug in printing a list (#2654)
* Fix bug in printing a list * Update tests * Add integration test * Fix bugs * Update references * Fix tests for C backend
1 parent ac21bb2 commit fce0b35

6 files changed

+1613
-1067
lines changed

Diff for: integration_tests/test_list_11.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
from lpython import i32
22

3+
l: list[i32] = [1, 2]
4+
5+
def add_item(i: i32) -> list[i32]:
6+
l.append(i)
7+
return l
8+
9+
310
def return_empty_list_of_tuples() -> list[i32]:
411
return []
512

@@ -19,12 +26,21 @@ def test_iterate_over_string():
1926
assert s == temp[i]
2027
i+=1
2128

29+
def test_issue_2639():
30+
print(add_item(3))
31+
32+
assert len(l) == 3
33+
assert l[0] == 1
34+
assert l[1] == 2
35+
assert l[2] == 3
36+
2237
def main0():
2338
x: list[i32] = return_empty_list_of_tuples()
2439
print(len(x))
2540

2641
assert len(x) == 0
2742
test_issue_1882()
2843
test_iterate_over_string()
44+
test_issue_2639()
2945

3046
main0()

Diff for: src/libasr/pass/print_list_tuple.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,23 @@ class PrintListTupleVisitor
114114
list_iter_var_name, al, current_scope, int_type);
115115
}
116116

117+
std::string list_var_name;
118+
ASR::expr_t *list_var;
119+
{
120+
list_var_name =
121+
current_scope->get_unique_name("__list_var", false);
122+
list_var = PassUtils::create_auxiliary_variable(loc,
123+
list_var_name, al, current_scope, ASRUtils::expr_type(list_expr));
124+
}
125+
126+
ASR::stmt_t *assign_stmt = ASRUtils::STMT(
127+
ASR::make_Assignment_t(al, loc, list_var, list_expr, nullptr));
128+
117129
ASR::expr_t *list_item = ASRUtils::EXPR(
118-
ASR::make_ListItem_t(al, loc, list_expr,
130+
ASR::make_ListItem_t(al, loc, list_var,
119131
list_iter_var, listC->m_type, nullptr));
120132
ASR::expr_t *list_len = ASRUtils::EXPR(ASR::make_ListLen_t(
121-
al, loc, list_expr, int_type, nullptr));
133+
al, loc, list_var, int_type, nullptr));
122134
ASR::expr_t *constant_one = ASRUtils::EXPR(
123135
ASR::make_IntegerConstant_t(al, loc, 1, int_type));
124136
ASR::expr_t *list_len_minus_one =
@@ -199,6 +211,7 @@ class PrintListTupleVisitor
199211
al, loc, nullptr, loop_head, loop_body.p, loop_body.size(), nullptr, 0));
200212

201213
{
214+
print_pass_result_tmp.push_back(al, assign_stmt);
202215
print_pass_result_tmp.push_back(al, print_open_bracket);
203216
print_pass_result_tmp.push_back(al, loop);
204217
print_pass_result_tmp.push_back(al, print_close_bracket);

Diff for: tests/reference/pass_print_list_tuple-print_02-09600eb.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "pass_print_list_tuple-print_02-09600eb.stdout",
9-
"stdout_hash": "b518803746ffd1666ff29f4bfa2347eb621d81af5e52dc36964cd249",
9+
"stdout_hash": "2831d417b5508b57e5e64c51339eb96f4d9aaf3559ee19c31dd0bb3c",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)