Skip to content

Commit d1e337b

Browse files
committed
Prohibit macro-expanded extern crate items shadowing crates passed with --extern
1 parent 7976aa3 commit d1e337b

5 files changed

+51
-5
lines changed

src/librustc_resolve/build_reduced_graph.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,23 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
446446
let binding =
447447
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
448448
if ptr::eq(self.current_module, self.graph_root) {
449-
self.extern_prelude.entry(ident.modern()).or_insert(ExternPreludeEntry {
449+
if let Some(entry) = self.extern_prelude.get(&ident.modern()) {
450+
if expansion != Mark::root() && orig_name.is_some() &&
451+
entry.extern_crate_item.is_none() {
452+
self.session.span_err(item.span, "macro-expanded `extern crate` items \
453+
cannot shadow names passed with \
454+
`--extern`");
455+
}
456+
}
457+
let entry = self.extern_prelude.entry(ident.modern())
458+
.or_insert(ExternPreludeEntry {
450459
extern_crate_item: None,
451460
introduced_by_item: true,
452-
}).extern_crate_item = Some(binding);
461+
});
462+
entry.extern_crate_item = Some(binding);
463+
if orig_name.is_some() {
464+
entry.introduced_by_item = true;
465+
}
453466
}
454467
let directive = self.arenas.alloc_import_directive(ImportDirective {
455468
root_id: item.id,

src/test/ui/feature-gates/feature-gate-extern_crate_item_prelude.rs

+7
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ mod import_absolute {
3636
//~^ ERROR use of extern prelude names introduced with `extern crate` items is unstable
3737
}
3838

39+
extern crate alloc as core;
40+
41+
mod unrelated_crate_renamed {
42+
type A = core::boxed::Box<u8>;
43+
//~^ ERROR use of extern prelude names introduced with `extern crate` items is unstable
44+
}
45+
3946
fn main() {}

src/test/ui/feature-gates/feature-gate-extern_crate_item_prelude.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ LL | type A = ::alloc::boxed::Box<u8>;
6262
|
6363
= help: add #![feature(extern_crate_item_prelude)] to the crate attributes to enable
6464

65-
error: aborting due to 8 previous errors
65+
error[E0658]: use of extern prelude names introduced with `extern crate` items is unstable (see issue #54658)
66+
--> $DIR/feature-gate-extern_crate_item_prelude.rs:42:14
67+
|
68+
LL | type A = core::boxed::Box<u8>;
69+
| ^^^^
70+
|
71+
= help: add #![feature(extern_crate_item_prelude)] to the crate attributes to enable
72+
73+
error: aborting due to 9 previous errors
6674

6775
For more information about this error, try `rustc --explain E0658`.

src/test/ui/imports/extern-prelude-extern-crate-fail.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// aux-build:two_macros.rs
2+
// compile-flags:--extern non_existent
23

34
mod n {
45
extern crate two_macros;
@@ -10,4 +11,12 @@ mod m {
1011
}
1112
}
1213

14+
macro_rules! define_std_as_non_existent {
15+
() => {
16+
extern crate std as non_existent;
17+
//~^ ERROR `extern crate` items cannot shadow names passed with `--extern`
18+
}
19+
}
20+
define_std_as_non_existent!();
21+
1322
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
error: macro-expanded `extern crate` items cannot shadow names passed with `--extern`
2+
--> $DIR/extern-prelude-extern-crate-fail.rs:16:9
3+
|
4+
LL | extern crate std as non_existent;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
...
7+
LL | define_std_as_non_existent!();
8+
| ------------------------------ in this macro invocation
9+
110
error[E0433]: failed to resolve. Use of undeclared type or module `two_macros`
2-
--> $DIR/extern-prelude-extern-crate-fail.rs:9:9
11+
--> $DIR/extern-prelude-extern-crate-fail.rs:10:9
312
|
413
LL | two_macros::m!(); //~ ERROR failed to resolve. Use of undeclared type or module `two_macros`
514
| ^^^^^^^^^^ Use of undeclared type or module `two_macros`
615

7-
error: aborting due to previous error
16+
error: aborting due to 2 previous errors
817

918
For more information about this error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)