Skip to content

Commit 5a97ed0

Browse files
authored
Merge pull request #1904 from certik/import_fix
Allow duplicate imports
2 parents d20a094 + e0fe3a9 commit 5a97ed0

File tree

7 files changed

+144
-102
lines changed

7 files changed

+144
-102
lines changed

integration_tests/modules_02.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from modules_02b import f
1+
from modules_02b import f, f
22
from lpython import i32
33

44
def main0():

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4245,14 +4245,28 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
42454245
}
42464246

42474247
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
4248+
int i=-1;
42484249
for (auto &remote_sym : mod_symbols) {
4250+
i++;
42494251
if( procedures_db.is_function_to_be_ignored(msym, remote_sym) ) {
42504252
continue ;
42514253
}
42524254
std::string new_sym_name = ASRUtils::get_mangled_name(m, remote_sym);
42534255
ASR::symbol_t *t = import_from_module(al, m, current_scope, msym,
42544256
remote_sym, new_sym_name, x.base.base.loc, true);
4255-
current_scope->add_symbol(new_sym_name, t);
4257+
if (current_scope->get_scope().find(new_sym_name) != current_scope->get_scope().end()) {
4258+
ASR::symbol_t *old_sym = current_scope->get_scope().find(new_sym_name)->second;
4259+
diag.add(diag::Diagnostic(
4260+
"The symbol '" + new_sym_name + "' imported from " + std::string(m->m_name) +" will shadow the existing symbol '" + new_sym_name + "'",
4261+
diag::Level::Warning, diag::Stage::Semantic, {
4262+
diag::Label("new symbol", {x.m_names[i].loc}),
4263+
diag::Label("old symbol implementation", {old_sym->base.loc}),
4264+
})
4265+
);
4266+
current_scope->overwrite_symbol(new_sym_name, t);
4267+
} else {
4268+
current_scope->add_symbol(new_sym_name, t);
4269+
}
42564270
}
42574271

42584272
tmp = nullptr;

tests/reference/asr-modules_02-ec92e6f.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"basename": "asr-modules_02-ec92e6f",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/modules_02.py",
5-
"infile_hash": "dcb00ac27cbbcdec61d81f1df9e852ba81a2197e7804ec89cab76e44",
5+
"infile_hash": "c3ce0b2b9780f27f787297f75e5477e990481b962dcee420809540e0",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-modules_02-ec92e6f.stdout",
99
"stdout_hash": "2b9767d477601b562ca13e1d770d84bafad8f2647fb778d52fc20d0d",
10-
"stderr": null,
11-
"stderr_hash": null,
10+
"stderr": "asr-modules_02-ec92e6f.stderr",
11+
"stderr_hash": "910bed77716460a8f8712eac5e5818c55b7744f45a4a36aa257f3527",
1212
"returncode": 0
1313
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: The symbol 'f' imported from modules_02b will shadow the existing symbol 'f'
2+
--> tests/../integration_tests/modules_02.py:1:28
3+
|
4+
1 | from modules_02b import f, f
5+
| ^ new symbol
6+
7+
--> tests/../integration_tests/modules_02b.py:3:1 - 5:16
8+
|
9+
3 | def f():
10+
| ^^^^^^^^...
11+
...
12+
|
13+
5 | print("f()")
14+
| ...^^^^^^^^^^^^^^^^ old symbol implementation

tests/reference/asr_json-modules_02-70a491a.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
"basename": "asr_json-modules_02-70a491a",
33
"cmd": "lpython --show-asr --json {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/modules_02.py",
5-
"infile_hash": "dcb00ac27cbbcdec61d81f1df9e852ba81a2197e7804ec89cab76e44",
5+
"infile_hash": "c3ce0b2b9780f27f787297f75e5477e990481b962dcee420809540e0",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr_json-modules_02-70a491a.stdout",
9-
"stdout_hash": "f01c2568a6bbb12f504e47ab4ca342362c2b277b186ea8a6d02f8ba9",
10-
"stderr": null,
11-
"stderr_hash": null,
9+
"stdout_hash": "baae33b908a2ae0da9c7dd50b6307a768b0360e514987d70d3e37dc4",
10+
"stderr": "asr_json-modules_02-70a491a.stderr",
11+
"stderr_hash": "67b8bf471b878cb687ed200fe5d207b6b4acb9d1e1096a930576fba9",
1212
"returncode": 0
1313
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: The symbol 'f' imported from modules_02b will shadow the existing symbol 'f'
2+
--> tests/../integration_tests/modules_02.py:1:28
3+
|
4+
1 | from modules_02b import f, f
5+
| ^ new symbol
6+
7+
--> tests/../integration_tests/modules_02b.py:3:1 - 5:16
8+
|
9+
3 | def f():
10+
| ^^^^^^^^...
11+
...
12+
|
13+
5 | print("f()")
14+
| ...^^^^^^^^^^^^^^^^ old symbol implementation

0 commit comments

Comments
 (0)