Skip to content

Commit 54c505f

Browse files
Fix ICE when macro-expanded extern crate shadows std
1 parent 1c00e98 commit 54c505f

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

compiler/rustc_resolve/src/imports.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10481048
message,
10491049
} => {
10501050
if no_ambiguity {
1051-
assert!(import.imported_module.get().is_none());
1051+
if !self.issue_145575_hack_applied {
1052+
assert!(import.imported_module.get().is_none());
1053+
}
10521054
self.report_error(
10531055
span,
10541056
ResolutionError::FailedToResolve {
@@ -1072,7 +1074,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10721074
..
10731075
} => {
10741076
if no_ambiguity {
1075-
assert!(import.imported_module.get().is_none());
1077+
if !self.issue_145575_hack_applied {
1078+
assert!(import.imported_module.get().is_none());
1079+
}
10761080
let module = if let Some(ModuleOrUniformRoot::Module(m)) = module {
10771081
m.opt_def_id()
10781082
} else {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ edition: 2024
2+
// This test ensures that `extern crate` with attribute shadowing std does not cause ICE.
3+
// Issue link: https://github.com/rust-lang/rust/issues/152895
4+
5+
#![crate_type = "lib"]
6+
7+
#[foobar] //~ ERROR cannot find attribute `foobar` in this scope
8+
extern crate core as std; //~ ERROR macro-expanded `extern crate` items cannot shadow names passed with `--extern`
9+
mod inner {
10+
use std::collections::hash_map::HashMap; //~ ERROR cannot find `collections` in `std`
11+
use std::vec::IntoIter; //~ ERROR unresolved import `std::vec`
12+
13+
use crate::*;
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: macro-expanded `extern crate` items cannot shadow names passed with `--extern`
2+
--> $DIR/ice-on-shadowing-std-with-attr.rs:8:1
3+
|
4+
LL | extern crate core as std;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error[E0433]: cannot find `collections` in `std`
8+
--> $DIR/ice-on-shadowing-std-with-attr.rs:10:14
9+
|
10+
LL | use std::collections::hash_map::HashMap;
11+
| ^^^^^^^^^^^ could not find `collections` in `std`
12+
13+
error[E0432]: unresolved import `std::vec`
14+
--> $DIR/ice-on-shadowing-std-with-attr.rs:11:14
15+
|
16+
LL | use std::vec::IntoIter;
17+
| ^^^ could not find `vec` in `std`
18+
19+
error: cannot find attribute `foobar` in this scope
20+
--> $DIR/ice-on-shadowing-std-with-attr.rs:7:3
21+
|
22+
LL | #[foobar]
23+
| ^^^^^^
24+
25+
error: aborting due to 4 previous errors
26+
27+
Some errors have detailed explanations: E0432, E0433.
28+
For more information about an error, try `rustc --explain E0432`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ edition: 2024
2+
// This test ensures that a macro-expanded `extern crate` shadowing std does not cause ICE.
3+
// Issue link: https://github.com/rust-lang/rust/issues/152895
4+
5+
#![crate_type = "lib"]
6+
7+
mod inner {
8+
use std::collections::hash_map::HashMap; //~ ERROR cannot find `collections` in `std`
9+
use std::vec::IntoIter; //~ ERROR unresolved import `std::vec`
10+
11+
use crate::*;
12+
}
13+
14+
macro_rules! define_other_core {
15+
() => {
16+
extern crate core as std; //~ ERROR macro-expanded `extern crate` items cannot shadow names passed with `--extern`
17+
};
18+
}
19+
define_other_core! {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error: macro-expanded `extern crate` items cannot shadow names passed with `--extern`
2+
--> $DIR/ice-on-shadowing-std-with-macro.rs:16:9
3+
|
4+
LL | extern crate core as std;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
...
7+
LL | define_other_core! {}
8+
| --------------------- in this macro invocation
9+
|
10+
= note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error[E0433]: cannot find `collections` in `std`
13+
--> $DIR/ice-on-shadowing-std-with-macro.rs:8:14
14+
|
15+
LL | use std::collections::hash_map::HashMap;
16+
| ^^^^^^^^^^^ could not find `collections` in `std`
17+
18+
error[E0432]: unresolved import `std::vec`
19+
--> $DIR/ice-on-shadowing-std-with-macro.rs:9:14
20+
|
21+
LL | use std::vec::IntoIter;
22+
| ^^^ could not find `vec` in `std`
23+
24+
error: aborting due to 3 previous errors
25+
26+
Some errors have detailed explanations: E0432, E0433.
27+
For more information about an error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)