Skip to content

Commit b4e6589

Browse files
Fix: Restore visit_ImportFrom and finalize aliasing logic
1 parent 90b2ce3 commit b4e6589

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4872,8 +4872,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
48724872
}
48734873
}
48744874

4875-
// --- RESTORED visit_ImportFrom ---
4876-
// --- 1. The Missing Function (RESTORE THIS) ---
4875+
// --- 1. RESTORED & FIXED: visit_ImportFrom ---
48774876
void visit_ImportFrom(const AST::ImportFrom_t &x) {
48784877
if (!x.m_module) {
48794878
throw SemanticError("Not implemented: The import statement must currently specify the module name", x.base.base.loc);
@@ -4890,7 +4889,9 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
48904889
paths.push_back(rl_path);
48914890
paths.push_back(parent_dir);
48924891

4893-
bool lpython, enum_py, copy, sympy;
4892+
// FIX: Initialize bools to false to avoid undefined behavior
4893+
bool lpython = false, enum_py = false, copy = false, sympy = false;
4894+
48944895
set_module_symbol(msym, paths);
48954896
t = (ASR::symbol_t*)(load_module(al, global_scope,
48964897
msym, x.base.base.loc, diag, lm, false, paths, lpython, enum_py, copy, sympy,
@@ -4928,26 +4929,29 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
49284929
if (x.m_names[j].m_asname) {
49294930
new_sym_name = ASRUtils::get_mangled_name(m, x.m_names[j].m_asname);
49304931
}
4931-
ASR::symbol_t *t = import_from_module(al, m, current_scope, msym,
4932+
4933+
// FIX: Renamed 't' to 'imported_sym' to avoid shadowing the outer module pointer 't'
4934+
ASR::symbol_t *imported_sym = import_from_module(al, m, current_scope, msym,
49324935
remote_sym, new_sym_name, x.m_names[i].loc, true);
4936+
49334937
if (current_scope->get_scope().find(new_sym_name) != current_scope->get_scope().end()) {
49344938
ASR::symbol_t *old_sym = current_scope->get_scope().find(new_sym_name)->second;
49354939
diag.add(diag::Diagnostic(
49364940
"The symbol '" + new_sym_name + "' imported from " + std::string(m->m_name) +" will shadow the existing symbol '" + new_sym_name + "'",
49374941
diag::Level::Warning, diag::Stage::Semantic, {
49384942
diag::Label("old symbol", {old_sym->base.loc}),
4939-
diag::Label("new symbol", {t->base.loc}),
4943+
diag::Label("new symbol", {imported_sym->base.loc}),
49404944
})
49414945
);
4942-
current_scope->overwrite_symbol(new_sym_name, t);
4946+
current_scope->overwrite_symbol(new_sym_name, imported_sym);
49434947
} else {
4944-
current_scope->add_symbol(new_sym_name, t);
4948+
current_scope->add_symbol(new_sym_name, imported_sym);
49454949
}
49464950
}
49474951
tmp = nullptr;
49484952
}
49494953

4950-
// --- 2. Your Fixed Function (CORRECT VERSION WITHOUT 'ELSE') ---
4954+
// --- 2. FIXED: visit_Import (Safe Bools + No Else) ---
49514955
void visit_Import(const AST::Import_t &x) {
49524956
ASR::symbol_t *t = nullptr;
49534957
std::string rl_path = get_runtime_library_dir();
@@ -4962,7 +4966,9 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
49624966
std::string mod_sym = x.m_names[i].m_name;
49634967
char* alias = x.m_names[i].m_asname;
49644968

4965-
bool lpython, enum_py, copy, sympy;
4969+
// FIX: Initialize bools
4970+
bool lpython = false, enum_py = false, copy = false, sympy = false;
4971+
49664972
set_module_symbol(mod_sym, paths);
49674973

49684974
t = (ASR::symbol_t*)(load_module(al, global_scope,
@@ -4990,18 +4996,27 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
49904996
current_module_dependencies.push_back(al, s2c(al, mod_sym));
49914997
}
49924998

4993-
// ALIAS LOGIC (No else block!)
4999+
// ALIAS LOGIC
49945000
if (alias) {
49955001
std::string alias_str = std::string(alias);
49965002
if (current_scope->get_symbol(alias_str) == nullptr) {
5003+
// Note: Using the signature that matches your local libasr version
49975004
ASR::asr_t *ext_sym = ASR::make_ExternalSymbol_t(
4998-
al, x.base.base.loc, current_scope,
4999-
s2c(al, alias_str), t, s2c(al, mod_sym),
5000-
nullptr, 0, s2c(al, mod_sym), ASR::accessType::Public
5005+
al,
5006+
x.base.base.loc,
5007+
current_scope,
5008+
s2c(al, alias_str),
5009+
t,
5010+
s2c(al, mod_sym),
5011+
nullptr,
5012+
0,
5013+
s2c(al, mod_sym),
5014+
ASR::accessType::Public
50015015
);
50025016
current_scope->add_symbol(alias_str, ASR::down_cast<ASR::symbol_t>(ext_sym));
50035017
}
50045018
}
5019+
// NO ELSE BLOCK: Standard imports are handled by load_module logic implicitly.
50055020
}
50065021
}
50075022

0 commit comments

Comments
 (0)