Skip to content

Commit f84afdd

Browse files
authored
Merge pull request #2252 from Smit-create/i-2153
ASR: Fix Import as
2 parents 1f85474 + 36ba561 commit f84afdd

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ RUN(NAME test_import_04 IMPORT_PATH ..
556556
LABELS cpython llvm c)
557557
RUN(NAME test_import_05 LABELS cpython llvm c wasm wasm_x86 wasm_x64)
558558
RUN(NAME test_import_06 LABELS cpython llvm)
559+
RUN(NAME test_import_07 LABELS cpython llvm c)
559560
RUN(NAME test_math LABELS cpython llvm NOFAST)
560561
RUN(NAME test_numpy_01 LABELS cpython llvm c)
561562
RUN(NAME test_numpy_02 LABELS cpython llvm c)

integration_tests/test_import_07.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# test issue 2153
2+
from test_import_07_module import f as fa
3+
4+
def main0():
5+
assert fa(3) == 6
6+
assert fa(10) == 20
7+
8+
main0()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from lpython import i32
2+
3+
def f(x: i32) -> i32:
4+
return 2 * x

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4472,10 +4472,6 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
44724472
throw SemanticError("Not implemented: The import statement must currently specify the module name", x.base.base.loc);
44734473
}
44744474
std::string msym = x.m_module; // Module name
4475-
std::vector<std::string> mod_symbols;
4476-
for (size_t i=0; i<x.n_names; i++) {
4477-
mod_symbols.push_back(x.m_names[i].m_name);
4478-
}
44794475

44804476
// Get the module, for now assuming it is not loaded, so we load it:
44814477
ASR::symbol_t *t = nullptr; // current_scope->parent->resolve_symbol(msym);
@@ -4518,13 +4514,17 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
45184514
}
45194515

45204516
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
4521-
int i=-1;
4522-
for (auto &remote_sym : mod_symbols) {
4517+
int i = -1;
4518+
for (size_t j=0; j<x.n_names; j++) {
4519+
std::string remote_sym = x.m_names[j].m_name;
45234520
i++;
45244521
if( procedures_db.is_function_to_be_ignored(msym, remote_sym) ) {
45254522
continue ;
45264523
}
45274524
std::string new_sym_name = ASRUtils::get_mangled_name(m, remote_sym);
4525+
if (x.m_names[j].m_asname) {
4526+
new_sym_name = ASRUtils::get_mangled_name(m, x.m_names[j].m_asname);
4527+
}
45284528
ASR::symbol_t *t = import_from_module(al, m, current_scope, msym,
45294529
remote_sym, new_sym_name, x.m_names[i].loc, true);
45304530
if (current_scope->get_scope().find(new_sym_name) != current_scope->get_scope().end()) {
@@ -4914,7 +4914,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
49144914
// Handled by SymbolTableVisitor already
49154915
std::string mod_name = x.m_module;
49164916
for (size_t i = 0; i < x.n_names; i++) {
4917-
imported_functions[x.m_names[i].m_name] = mod_name;
4917+
if (x.m_names[i].m_asname) {
4918+
imported_functions[x.m_names[i].m_asname] = mod_name;
4919+
}
4920+
else {
4921+
imported_functions[x.m_names[i].m_name] = mod_name;
4922+
}
49184923
}
49194924
ASR::symbol_t *mod_sym = current_scope->resolve_symbol(mod_name);
49204925
if (mod_sym) {

0 commit comments

Comments
 (0)