Skip to content

Commit 4a43dea

Browse files
authored
Merge pull request #2237 from Shaikh-Ubaid/fix_asr_explicit_iter_var
ASR: Fix explicit iter variable
2 parents b3add2a + 3a25540 commit 4a43dea

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ RUN(NAME loop_03 LABELS cpython llvm c wasm wasm_x64)
484484
RUN(NAME loop_04 LABELS cpython llvm c)
485485
RUN(NAME loop_05 LABELS cpython llvm c)
486486
RUN(NAME loop_06 LABELS cpython llvm c NOFAST)
487+
RUN(NAME loop_07 LABELS cpython llvm c)
487488
RUN(NAME if_01 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
488489
RUN(NAME if_02 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
489490
RUN(NAME if_03 FAIL LABELS cpython llvm c NOFAST)
@@ -717,7 +718,7 @@ RUN(NAME test_package_01 LABELS cpython llvm NOFAST)
717718
RUN(NAME test_pkg_lpdraw LABELS cpython llvm wasm)
718719
RUN(NAME test_pkg_lnn_01 LABELS cpython llvm NOFAST)
719720
RUN(NAME test_pkg_lnn_02 LABELS cpython llvm NOFAST)
720-
RUN(NAME test_pkg_lpconvexhull LABELS cpython c)
721+
RUN(NAME test_pkg_lpconvexhull LABELS cpython llvm c NOFAST)
721722

722723
RUN(NAME generics_01 LABELS cpython llvm c)
723724
RUN(NAME generics_02 LABELS cpython llvm c)

integration_tests/loop_07.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from lpython import i32
2+
3+
def main0():
4+
points: list[tuple[i32, i32]] = [(445, 193), (138, 28), (418, 279)]
5+
point: tuple[i32, i32]
6+
x_sum: i32 = 0
7+
y_sum: i32 = 0
8+
for point in points:
9+
print(point)
10+
x_sum += point[0]
11+
y_sum += point[1]
12+
13+
print(x_sum, y_sum)
14+
assert x_sum == 1001
15+
assert y_sum == 500
16+
17+
main0()

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5503,9 +5503,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
55035503
if (!is_explicit_iterator_required) {
55045504
a_kind = ASRUtils::extract_kind_from_ttype_t(ASRUtils::expr_type(target));
55055505
}
5506-
ASR::ttype_t *a_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, a_kind));
5507-
ASR::expr_t *constant_one = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(
5508-
al, x.base.base.loc, 1, a_type));
55095506
ASR::do_loop_head_t head = make_do_loop_head(loop_start, loop_end, inc, a_kind,
55105507
x.base.base.loc);
55115508

@@ -5520,18 +5517,22 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
55205517
// add an assignment instruction to body to assign value of loop_src_var at an index to the loop_target_var
55215518
LCOMPILERS_ASSERT(current_scope->get_symbol(explicit_iter_name) != nullptr);
55225519
auto explicit_iter_var = ASR::make_Var_t(al, x.base.base.loc, current_scope->get_symbol(explicit_iter_name));
5520+
ASR::ttype_t *a_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc, a_kind));
5521+
ASR::expr_t *constant_one = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(
5522+
al, x.base.base.loc, 1, a_type));
55235523
auto index_plus_one = ASR::make_IntegerBinOp_t(al, x.base.base.loc, ASRUtils::EXPR(explicit_iter_var),
55245524
ASR::binopType::Add, constant_one, a_type, nullptr);
5525-
auto loop_src_var = ASR::make_Var_t(al, x.base.base.loc, current_scope->resolve_symbol(loop_src_var_name));
5525+
ASR::expr_t* loop_src_var = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, current_scope->resolve_symbol(loop_src_var_name)));
5526+
ASR::ttype_t* loop_src_var_ttype = ASRUtils::expr_type(loop_src_var);
55265527
ASR::asr_t* loop_src_var_element = nullptr;
55275528
if (ASR::is_a<ASR::StringLen_t>(*for_iter_type)) {
55285529
loop_src_var_element = ASR::make_StringItem_t(
5529-
al, x.base.base.loc, ASRUtils::EXPR(loop_src_var),
5530-
ASRUtils::EXPR(index_plus_one), a_type, nullptr);
5530+
al, x.base.base.loc, loop_src_var,
5531+
ASRUtils::EXPR(index_plus_one), ASRUtils::get_contained_type(loop_src_var_ttype), nullptr);
55315532
} else if (ASR::is_a<ASR::ListLen_t>(*for_iter_type)) {
55325533
loop_src_var_element = ASR::make_ListItem_t(
5533-
al, x.base.base.loc, ASRUtils::EXPR(loop_src_var),
5534-
ASRUtils::EXPR(explicit_iter_var), a_type, nullptr);
5534+
al, x.base.base.loc, loop_src_var,
5535+
ASRUtils::EXPR(explicit_iter_var), ASRUtils::get_contained_type(loop_src_var_ttype), nullptr);
55355536
}
55365537
auto loop_target_assignment = ASR::make_Assignment_t(al, x.base.base.loc, target, ASRUtils::EXPR(loop_src_var_element), nullptr);
55375538
body.push_back(al, ASRUtils::STMT(loop_target_assignment));

tests/reference/asr-test_builtin_str-580e920.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-test_builtin_str-580e920.stdout",
9-
"stdout_hash": "5e1687a9cce6c997ff5c0ff9c15dc4435f1abbd581fb77d647d9afe4",
9+
"stdout_hash": "5869436dc7dad9581fe5088ceb646bb304ddc9f8ef790e6550687c9b",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-test_builtin_str-580e920.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@
586586
(Integer 4)
587587
()
588588
)
589-
(Integer 4)
589+
(Character 1 -2 ())
590590
()
591591
)
592592
()

0 commit comments

Comments
 (0)