Skip to content

Commit 83ae5b2

Browse files
committed
make extern crate x as y under macro as an error when y is module
1 parent 8291d68 commit 83ae5b2

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,12 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
895895
self.r.potentially_unused_imports.push(import);
896896
let imported_binding = self.r.import(binding, import);
897897
if parent == self.r.graph_root {
898-
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
899-
if expansion != LocalExpnId::ROOT && orig_name.is_some() && !entry.is_import() {
898+
let ident = ident.normalize_to_macros_2_0();
899+
if let Some(entry) = self.r.extern_prelude.get(&ident) {
900+
if expansion != LocalExpnId::ROOT
901+
&& orig_name.is_some()
902+
&& entry.binding.map_or(true, |binding| binding.module().is_some())
903+
{
900904
self.r.dcx().emit_err(
901905
errors::MacroExpandedExternCrateCannotShadowExternArguments {
902906
span: item.span,
@@ -912,7 +916,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
912916
let entry = self
913917
.r
914918
.extern_prelude
915-
.entry(ident.normalize_to_macros_2_0())
919+
.entry(ident)
916920
.or_insert(ExternPreludeEntry { binding: None, introduced_by_item: true });
917921
// Binding from `extern crate` item in source code can replace
918922
// a binding from `--extern` on command line here.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ edition: 2021
2+
3+
// issue#128813
4+
5+
extern crate core;
6+
7+
fn f() {
8+
use ::core;
9+
}
10+
11+
macro_rules! m {
12+
() => {
13+
extern crate std as core;
14+
//~^ ERROR: macro-expanded `extern crate` items cannot shadow names passed with `--extern`
15+
};
16+
}
17+
18+
m!();
19+
20+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: macro-expanded `extern crate` items cannot shadow names passed with `--extern`
2+
--> $DIR/multiple-extern-core-mod-by-macro.rs:13:9
3+
|
4+
LL | extern crate std as core;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
...
7+
LL | m!();
8+
| ---- in this macro invocation
9+
|
10+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)