From 799cec4a44c8e815fabf28649c8819e6723f6826 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Mon, 13 Mar 2023 10:58:39 +0530 Subject: [PATCH] Handle initialisation at declaration inside for loop --- integration_tests/structs_21.py | 9 +++++++++ src/libasr/codegen/asr_to_c_cpp.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/integration_tests/structs_21.py b/integration_tests/structs_21.py index f8d3b4cfc7c..24c672c149d 100644 --- a/integration_tests/structs_21.py +++ b/integration_tests/structs_21.py @@ -18,10 +18,19 @@ def f(c: CPtr): print(A) assert A == 3 +def g(c: CPtr): + i: i32 + for i in range(3): + p: Pointer[S] = c_p_pointer(c, S) + A: i32 = p.a + print(A) + assert A == 3 + def main(): s: S = S(3) p: CPtr = empty_c_void_p() p_c_pointer(pointer(s, S), p) f(p) + g(p) main() diff --git a/src/libasr/codegen/asr_to_c_cpp.h b/src/libasr/codegen/asr_to_c_cpp.h index c7d556a6287..f12bcfb255f 100644 --- a/src/libasr/codegen/asr_to_c_cpp.h +++ b/src/libasr/codegen/asr_to_c_cpp.h @@ -324,6 +324,8 @@ R"(#include std::string close_paranthesis = indent + "}\n"; indent += std::string(indentation_spaces, ' '); indentation_level += 1; + SymbolTable* current_scope_copy = current_scope; + current_scope = block->m_symtab; std::vector var_order = ASRUtils::determine_variable_declaration_order(block->m_symtab); for (auto &item : var_order) { ASR::symbol_t* var_sym = block->m_symtab->get_symbol(item); @@ -343,6 +345,7 @@ R"(#include decl += check_tmp_buffer(); src = open_paranthesis + decl + body + close_paranthesis; indentation_level -= 1; + current_scope = current_scope_copy; } // Returns the declaration, no semi colon at the end