Skip to content

Commit d7121dd

Browse files
tanay-manubaidsk
andauthored
Fixed scoping issues of for loops in global scope (#2672)
* Fixed scoping issues of for loops in global scope * Covered subscripts * Considered edge case of nested list * Added white-spaces * Added integration test * Removed c backend from test loop_7 * Update src/lpython/semantics/python_ast_to_asr.cpp Co-authored-by: Shaikh Ubaid <shaikhubaid769@gmail.com> * Split new test from loop_07 to loop_11 and others * Added print statements before assert * Removed explicit cast to str --------- Co-authored-by: Shaikh Ubaid <shaikhubaid769@gmail.com>
1 parent 973c500 commit d7121dd

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ RUN(NAME loop_07 LABELS cpython llvm llvm_jit c)
518518
RUN(NAME loop_08 LABELS cpython llvm llvm_jit c)
519519
RUN(NAME loop_09 LABELS cpython llvm llvm_jit)
520520
RUN(NAME loop_10 LABELS cpython llvm llvm_jit)
521+
RUN(NAME loop_11 LABELS cpython llvm llvm_jit)
521522
RUN(NAME if_01 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64)
522523
RUN(NAME if_02 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64)
523524
RUN(NAME if_03 FAIL LABELS cpython llvm llvm_jit c NOFAST)

integration_tests/loop_11.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from lpython import i32
2+
3+
#checking for loops in the global scope
4+
sum: i32 = 0
5+
i: i32
6+
for i in [1, 2, 3, 4]:
7+
print(i)
8+
sum += i
9+
print("sum = ",sum)
10+
assert sum == 10
11+
12+
alphabets: str = ""
13+
c: str
14+
for c in "abcde":
15+
print(c)
16+
alphabets += c
17+
print("alphabets = ",alphabets)
18+
assert alphabets == "abcde"
19+
20+
alphabets = ""
21+
s : str = "abcde"
22+
for c in s[1:4]:
23+
print(c)
24+
alphabets += c
25+
print("alphabets = ",alphabets)
26+
assert alphabets == "bcd"
27+
28+
sum = 0
29+
num_list : list[i32] = [1, 2, 3, 4]
30+
for i in num_list[1:3]:
31+
print(i)
32+
sum += i
33+
print("sum = ",sum)
34+
assert sum == 5
35+
36+
sum = 0
37+
nested_list : list[list[i32]] = [[1, 2, 3, 4]]
38+
for i in nested_list[0]:
39+
print(i)
40+
sum += i
41+
print("sum = ",sum)
42+
assert sum == 10

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5516,9 +5516,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
55165516
loop_src_var_name = AST::down_cast<AST::Name_t>(sbt->m_value)->m_id;
55175517
visit_Subscript(*sbt);
55185518
ASR::expr_t *target = ASRUtils::EXPR(tmp);
5519-
ASR::symbol_t *loop_src_var_symbol = current_scope->resolve_symbol(loop_src_var_name);
5520-
ASR::ttype_t *loop_src_var_ttype = ASRUtils::symbol_type(loop_src_var_symbol);
5521-
5519+
ASR::ttype_t *loop_src_var_ttype = ASRUtils::expr_type(target);
55225520
// Create a temporary variable that will contain the evaluated value of Subscript
55235521
std::string tmp_assign_name = current_scope->get_unique_name("__tmp_assign_for_loop", false);
55245522
SetChar variable_dependencies_vec;
@@ -5536,7 +5534,11 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
55365534
ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc,
55375535
ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)),
55385536
target, nullptr);
5539-
current_body->push_back(al, ASRUtils::STMT(assign));
5537+
if (current_body != nullptr) {
5538+
current_body->push_back(al, ASRUtils::STMT(assign));
5539+
} else {
5540+
global_init.push_back(al, assign);
5541+
}
55405542
loop_end = for_iterable_helper(tmp_assign_name, x.base.base.loc, explicit_iter_name);
55415543
for_iter_type = loop_end;
55425544
LCOMPILERS_ASSERT(loop_end);
@@ -5568,7 +5570,11 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
55685570
ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc,
55695571
ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)),
55705572
target, nullptr);
5571-
current_body->push_back(al, ASRUtils::STMT(assign));
5573+
if (current_body != nullptr) {
5574+
current_body->push_back(al, ASRUtils::STMT(assign));
5575+
} else {
5576+
global_init.push_back(al, assign);
5577+
}
55725578
loop_end = for_iterable_helper(tmp_assign_name, x.base.base.loc, explicit_iter_name);
55735579
for_iter_type = loop_end;
55745580
LCOMPILERS_ASSERT(loop_end);

0 commit comments

Comments
 (0)