Skip to content

Fixed scoping issues of for loops in global scope #2672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ RUN(NAME loop_07 LABELS cpython llvm llvm_jit c)
RUN(NAME loop_08 LABELS cpython llvm llvm_jit c)
RUN(NAME loop_09 LABELS cpython llvm llvm_jit)
RUN(NAME loop_10 LABELS cpython llvm llvm_jit)
RUN(NAME loop_11 LABELS cpython llvm llvm_jit)
RUN(NAME if_01 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64)
RUN(NAME if_02 LABELS cpython llvm llvm_jit c wasm wasm_x86 wasm_x64)
RUN(NAME if_03 FAIL LABELS cpython llvm llvm_jit c NOFAST)
Expand Down
42 changes: 42 additions & 0 deletions integration_tests/loop_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from lpython import i32

#checking for loops in the global scope
sum: i32 = 0
i: i32
for i in [1, 2, 3, 4]:
print(i)
sum += i
print("sum = ",sum)
assert sum == 10

alphabets: str = ""
c: str
for c in "abcde":
print(c)
alphabets += c
print("alphabets = ",alphabets)
assert alphabets == "abcde"

alphabets = ""
s : str = "abcde"
for c in s[1:4]:
print(c)
alphabets += c
print("alphabets = ",alphabets)
assert alphabets == "bcd"

sum = 0
num_list : list[i32] = [1, 2, 3, 4]
for i in num_list[1:3]:
print(i)
sum += i
print("sum = ",sum)
assert sum == 5

sum = 0
nested_list : list[list[i32]] = [[1, 2, 3, 4]]
for i in nested_list[0]:
print(i)
sum += i
print("sum = ",sum)
assert sum == 10
16 changes: 11 additions & 5 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5516,9 +5516,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
loop_src_var_name = AST::down_cast<AST::Name_t>(sbt->m_value)->m_id;
visit_Subscript(*sbt);
ASR::expr_t *target = ASRUtils::EXPR(tmp);
ASR::symbol_t *loop_src_var_symbol = current_scope->resolve_symbol(loop_src_var_name);
ASR::ttype_t *loop_src_var_ttype = ASRUtils::symbol_type(loop_src_var_symbol);

ASR::ttype_t *loop_src_var_ttype = ASRUtils::expr_type(target);
// Create a temporary variable that will contain the evaluated value of Subscript
std::string tmp_assign_name = current_scope->get_unique_name("__tmp_assign_for_loop", false);
SetChar variable_dependencies_vec;
Expand All @@ -5536,7 +5534,11 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc,
ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)),
target, nullptr);
current_body->push_back(al, ASRUtils::STMT(assign));
if (current_body != nullptr) {
current_body->push_back(al, ASRUtils::STMT(assign));
} else {
global_init.push_back(al, assign);
}
loop_end = for_iterable_helper(tmp_assign_name, x.base.base.loc, explicit_iter_name);
for_iter_type = loop_end;
LCOMPILERS_ASSERT(loop_end);
Expand Down Expand Up @@ -5568,7 +5570,11 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc,
ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, tmp_assign_variable_sym)),
target, nullptr);
current_body->push_back(al, ASRUtils::STMT(assign));
if (current_body != nullptr) {
current_body->push_back(al, ASRUtils::STMT(assign));
} else {
global_init.push_back(al, assign);
}
loop_end = for_iterable_helper(tmp_assign_name, x.base.base.loc, explicit_iter_name);
for_iter_type = loop_end;
LCOMPILERS_ASSERT(loop_end);
Expand Down
Loading