Skip to content

Commit 55b8337

Browse files
committed
Emit a warning when shadowing
1 parent 1493fb5 commit 55b8337

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

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_or_overwrite_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;

0 commit comments

Comments
 (0)