diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 704c316bce650..f0527740a58c7 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -1048,7 +1048,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { message, } => { if no_ambiguity { - assert!(import.imported_module.get().is_none()); + if !self.issue_145575_hack_applied { + assert!(import.imported_module.get().is_none()); + } self.report_error( span, ResolutionError::FailedToResolve { @@ -1072,7 +1074,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { .. } => { if no_ambiguity { - assert!(import.imported_module.get().is_none()); + if !self.issue_145575_hack_applied { + assert!(import.imported_module.get().is_none()); + } let module = if let Some(ModuleOrUniformRoot::Module(m)) = module { m.opt_def_id() } else { diff --git a/tests/ui/resolve/ice-on-shadowing-std-with-attr.rs b/tests/ui/resolve/ice-on-shadowing-std-with-attr.rs new file mode 100644 index 0000000000000..67f499e774e23 --- /dev/null +++ b/tests/ui/resolve/ice-on-shadowing-std-with-attr.rs @@ -0,0 +1,14 @@ +//@ edition: 2024 +// This test ensures that `extern crate` with attribute shadowing std does not cause ICE. +// Issue link: https://github.com/rust-lang/rust/issues/152895 + +#![crate_type = "lib"] + +#[foobar] //~ ERROR cannot find attribute `foobar` in this scope +extern crate core as std; //~ ERROR macro-expanded `extern crate` items cannot shadow names passed with `--extern` +mod inner { + use std::collections::hash_map::HashMap; //~ ERROR cannot find `collections` in `std` + use std::vec::IntoIter; //~ ERROR unresolved import `std::vec` + + use crate::*; +} diff --git a/tests/ui/resolve/ice-on-shadowing-std-with-attr.stderr b/tests/ui/resolve/ice-on-shadowing-std-with-attr.stderr new file mode 100644 index 0000000000000..f04852999b325 --- /dev/null +++ b/tests/ui/resolve/ice-on-shadowing-std-with-attr.stderr @@ -0,0 +1,28 @@ +error: macro-expanded `extern crate` items cannot shadow names passed with `--extern` + --> $DIR/ice-on-shadowing-std-with-attr.rs:8:1 + | +LL | extern crate core as std; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0433]: cannot find `collections` in `std` + --> $DIR/ice-on-shadowing-std-with-attr.rs:10:14 + | +LL | use std::collections::hash_map::HashMap; + | ^^^^^^^^^^^ could not find `collections` in `std` + +error[E0432]: unresolved import `std::vec` + --> $DIR/ice-on-shadowing-std-with-attr.rs:11:14 + | +LL | use std::vec::IntoIter; + | ^^^ could not find `vec` in `std` + +error: cannot find attribute `foobar` in this scope + --> $DIR/ice-on-shadowing-std-with-attr.rs:7:3 + | +LL | #[foobar] + | ^^^^^^ + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0432, E0433. +For more information about an error, try `rustc --explain E0432`. diff --git a/tests/ui/resolve/ice-on-shadowing-std-with-macro.rs b/tests/ui/resolve/ice-on-shadowing-std-with-macro.rs new file mode 100644 index 0000000000000..fdefa7e8ce582 --- /dev/null +++ b/tests/ui/resolve/ice-on-shadowing-std-with-macro.rs @@ -0,0 +1,19 @@ +//@ edition: 2024 +// This test ensures that a macro-expanded `extern crate` shadowing std does not cause ICE. +// Issue link: https://github.com/rust-lang/rust/issues/152895 + +#![crate_type = "lib"] + +mod inner { + use std::collections::hash_map::HashMap; //~ ERROR cannot find `collections` in `std` + use std::vec::IntoIter; //~ ERROR unresolved import `std::vec` + + use crate::*; +} + +macro_rules! define_other_core { + () => { + extern crate core as std; //~ ERROR macro-expanded `extern crate` items cannot shadow names passed with `--extern` + }; +} +define_other_core! {} diff --git a/tests/ui/resolve/ice-on-shadowing-std-with-macro.stderr b/tests/ui/resolve/ice-on-shadowing-std-with-macro.stderr new file mode 100644 index 0000000000000..8bdf809775516 --- /dev/null +++ b/tests/ui/resolve/ice-on-shadowing-std-with-macro.stderr @@ -0,0 +1,27 @@ +error: macro-expanded `extern crate` items cannot shadow names passed with `--extern` + --> $DIR/ice-on-shadowing-std-with-macro.rs:16:9 + | +LL | extern crate core as std; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ +... +LL | define_other_core! {} + | --------------------- in this macro invocation + | + = note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0433]: cannot find `collections` in `std` + --> $DIR/ice-on-shadowing-std-with-macro.rs:8:14 + | +LL | use std::collections::hash_map::HashMap; + | ^^^^^^^^^^^ could not find `collections` in `std` + +error[E0432]: unresolved import `std::vec` + --> $DIR/ice-on-shadowing-std-with-macro.rs:9:14 + | +LL | use std::vec::IntoIter; + | ^^^ could not find `vec` in `std` + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0432, E0433. +For more information about an error, try `rustc --explain E0432`.