]> git.lizzy.rs Git - rust.git/commitdiff
Prohibit macro-expanded `extern crate` items shadowing crates passed with `--extern`
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Tue, 23 Oct 2018 22:03:47 +0000 (01:03 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Tue, 23 Oct 2018 22:33:35 +0000 (01:33 +0300)
src/librustc_resolve/build_reduced_graph.rs
src/test/ui/feature-gates/feature-gate-extern_crate_item_prelude.rs
src/test/ui/feature-gates/feature-gate-extern_crate_item_prelude.stderr
src/test/ui/imports/extern-prelude-extern-crate-fail.rs
src/test/ui/imports/extern-prelude-extern-crate-fail.stderr

index 777a5ab79d090f7ca625a88bee119cfd9341402b..aa7bfeae5f48b034013c64a9e089fb4f80b5e52c 100644 (file)
@@ -446,10 +446,23 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
                 let binding =
                     (module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
                 if ptr::eq(self.current_module, self.graph_root) {
-                    self.extern_prelude.entry(ident.modern()).or_insert(ExternPreludeEntry {
+                    if let Some(entry) = self.extern_prelude.get(&ident.modern()) {
+                        if expansion != Mark::root() && orig_name.is_some() &&
+                           entry.extern_crate_item.is_none() {
+                            self.session.span_err(item.span, "macro-expanded `extern crate` items \
+                                                              cannot shadow names passed with \
+                                                              `--extern`");
+                        }
+                    }
+                    let entry = self.extern_prelude.entry(ident.modern())
+                                                   .or_insert(ExternPreludeEntry {
                         extern_crate_item: None,
                         introduced_by_item: true,
-                    }).extern_crate_item = Some(binding);
+                    });
+                    entry.extern_crate_item = Some(binding);
+                    if orig_name.is_some() {
+                        entry.introduced_by_item = true;
+                    }
                 }
                 let directive = self.arenas.alloc_import_directive(ImportDirective {
                     root_id: item.id,
index eb7c52c3d0f322e433e316b2b96b1c76849bace4..a043b6c2e610740f104b33b937124c8c19cd55b5 100644 (file)
@@ -36,4 +36,11 @@ mod import_absolute {
     //~^ ERROR use of extern prelude names introduced with `extern crate` items is unstable
 }
 
+extern crate alloc as core;
+
+mod unrelated_crate_renamed {
+    type A = core::boxed::Box<u8>;
+    //~^ ERROR use of extern prelude names introduced with `extern crate` items is unstable
+}
+
 fn main() {}
index 4dec8a35bcab1dc7de76dbaed1a193c1fd4fdeee..cabfb56d7a840b5913110774390b241fceae17d0 100644 (file)
@@ -62,6 +62,14 @@ LL |         type A = ::alloc::boxed::Box<u8>;
    |
    = help: add #![feature(extern_crate_item_prelude)] to the crate attributes to enable
 
-error: aborting due to 8 previous errors
+error[E0658]: use of extern prelude names introduced with `extern crate` items is unstable (see issue #54658)
+  --> $DIR/feature-gate-extern_crate_item_prelude.rs:42:14
+   |
+LL |     type A = core::boxed::Box<u8>;
+   |              ^^^^
+   |
+   = help: add #![feature(extern_crate_item_prelude)] to the crate attributes to enable
+
+error: aborting due to 9 previous errors
 
 For more information about this error, try `rustc --explain E0658`.
index 22248af6d3a2d41037c98bf3815b7737a35a4514..57b097c9df318091b62b3dfc20fa5a00da718524 100644 (file)
@@ -1,4 +1,5 @@
 // aux-build:two_macros.rs
+// compile-flags:--extern non_existent
 
 mod n {
     extern crate two_macros;
@@ -10,4 +11,12 @@ fn check() {
     }
 }
 
+macro_rules! define_std_as_non_existent {
+    () => {
+        extern crate std as non_existent;
+        //~^ ERROR `extern crate` items cannot shadow names passed with `--extern`
+    }
+}
+define_std_as_non_existent!();
+
 fn main() {}
index 464812f1f690215c7469134ae45382194e230196..8f68d2af34ca3d66e66935f7972267e181a634bc 100644 (file)
@@ -1,9 +1,18 @@
+error: macro-expanded `extern crate` items cannot shadow names passed with `--extern`
+  --> $DIR/extern-prelude-extern-crate-fail.rs:16:9
+   |
+LL |         extern crate std as non_existent;
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+...
+LL | define_std_as_non_existent!();
+   | ------------------------------ in this macro invocation
+
 error[E0433]: failed to resolve. Use of undeclared type or module `two_macros`
-  --> $DIR/extern-prelude-extern-crate-fail.rs:9:9
+  --> $DIR/extern-prelude-extern-crate-fail.rs:10:9
    |
 LL |         two_macros::m!(); //~ ERROR failed to resolve. Use of undeclared type or module `two_macros`
    |         ^^^^^^^^^^ Use of undeclared type or module `two_macros`
 
-error: aborting due to previous error
+error: aborting due to 2 previous errors
 
 For more information about this error, try `rustc --explain E0433`.