Skip to content

Commit fb0428c

Browse files
Merge pull request #1672 from Thirumalai-Shaktivel/var_init
2 parents b0027d5 + 01e1216 commit fb0428c

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,4 @@ RUN(NAME global_syms_03_b LABELS cpython llvm c)
444444
RUN(NAME global_syms_03_c LABELS cpython llvm c)
445445
RUN(NAME global_syms_04 LABELS cpython llvm c wasm wasm_x64)
446446
RUN(NAME global_syms_05 LABELS cpython llvm c)
447+
RUN(NAME global_syms_06 LABELS cpython llvm c)

integration_tests/global_syms_06.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from lpython import i32
2+
3+
def test() -> i32:
4+
temp: i32 = 0
5+
return temp
6+
7+
x: i32 = test()
8+
i: i32 = 10
9+
j: i32 = i

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,15 +2249,15 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
22492249
ASR::symbol_t* v_sym = ASR::down_cast<ASR::symbol_t>(v);
22502250
ASR::Variable_t* v_variable = ASR::down_cast<ASR::Variable_t>(v_sym);
22512251

2252-
if( init_expr && (current_body || ASR::is_a<ASR::List_t>(*type)) &&
2253-
(is_runtime_expression || !is_variable_const)) {
2252+
if( init_expr && (current_body || ASR::is_a<ASR::List_t>(*type) ||
2253+
is_runtime_expression) && !is_variable_const) {
22542254
ASR::expr_t* v_expr = ASRUtils::EXPR(ASR::make_Var_t(al, loc, v_sym));
22552255
cast_helper(v_expr, init_expr, true);
22562256
ASR::asr_t* assign = ASR::make_Assignment_t(al, loc, v_expr,
22572257
init_expr, nullptr);
22582258
if (current_body) {
22592259
current_body->push_back(al, ASRUtils::STMT(assign));
2260-
} else if (ASR::is_a<ASR::List_t>(*type)) {
2260+
} else if (ASR::is_a<ASR::List_t>(*type) || is_runtime_expression) {
22612261
global_init.push_back(al, assign);
22622262
}
22632263

@@ -3983,10 +3983,23 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
39833983
// Erase the function in TranslationUnit
39843984
unit->m_global_scope->erase_symbol(func_name);
39853985
}
3986+
global_init.p = nullptr;
3987+
global_init.n = 0;
3988+
}
3989+
3990+
if (global_init.n > 0) {
3991+
// copy all the item's from `items` (global_statements)
3992+
// into `global_init`
3993+
for (auto &i: items) {
3994+
global_init.push_back(al, i);
3995+
}
3996+
unit->m_items = global_init.p;
3997+
unit->n_items = global_init.size();
3998+
} else {
3999+
unit->m_items = items.p;
4000+
unit->n_items = items.size();
39864001
}
39874002

3988-
unit->m_items = items.p;
3989-
unit->n_items = items.size();
39904003
if (items.n > 0 && main_module_sym) {
39914004
std::string func_name = "global_statements";
39924005
// Wrap all the global statements into a Function

0 commit comments

Comments
 (0)