Skip to content

Commit 1934337

Browse files
authored
Rollup merge of rust-lang#149860 - Delta17920:fix/149821-root-cause, r=petrochenkov
Fix: Prevent macro-expanded extern crates from shadowing extern arguments prevents an ICE by fixing a logic bug in `build_reduced_graph.rs`. the bug caused the compiler to correctly detect and report a shadowing error for a macro-expanded `extern crate` but then continue processing the invalid item, corrupting the resolver's internal state (`extern_prelude`) and leading to a crash in later resolution passes the fix adds an early return after the shadowing error is reported to ensure the invalid item is not added to the resolution graph. Fixes rust-lang#149821
2 parents e985d18 + 0748492 commit 1934337

File tree

5 files changed

+65
-31
lines changed

5 files changed

+65
-31
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -771,36 +771,39 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
771771
} else {
772772
None
773773
};
774-
// Skip ambiguity errors for extern flag bindings "overridden"
775-
// by extern item bindings.
776-
// FIXME: Remove with lang team approval.
777-
let issue_145575_hack = Some(binding) == extern_prelude_flag_binding
778-
&& extern_prelude_item_binding.is_some()
779-
&& extern_prelude_item_binding != Some(innermost_binding);
780-
if let Some(kind) = ambiguity_error_kind
781-
&& !issue_145575_hack
782-
{
783-
let misc = |f: Flags| {
784-
if f.contains(Flags::MISC_SUGGEST_CRATE) {
785-
AmbiguityErrorMisc::SuggestCrate
786-
} else if f.contains(Flags::MISC_SUGGEST_SELF) {
787-
AmbiguityErrorMisc::SuggestSelf
788-
} else if f.contains(Flags::MISC_FROM_PRELUDE) {
789-
AmbiguityErrorMisc::FromPrelude
790-
} else {
791-
AmbiguityErrorMisc::None
792-
}
793-
};
794-
self.ambiguity_errors.push(AmbiguityError {
795-
kind,
796-
ident: orig_ident,
797-
b1: innermost_binding,
798-
b2: binding,
799-
warning: false,
800-
misc1: misc(innermost_flags),
801-
misc2: misc(flags),
802-
});
803-
return true;
774+
if let Some(kind) = ambiguity_error_kind {
775+
// Skip ambiguity errors for extern flag bindings "overridden"
776+
// by extern item bindings.
777+
// FIXME: Remove with lang team approval.
778+
let issue_145575_hack = Some(binding) == extern_prelude_flag_binding
779+
&& extern_prelude_item_binding.is_some()
780+
&& extern_prelude_item_binding != Some(innermost_binding);
781+
782+
if issue_145575_hack {
783+
self.issue_145575_hack_applied = true;
784+
} else {
785+
let misc = |f: Flags| {
786+
if f.contains(Flags::MISC_SUGGEST_CRATE) {
787+
AmbiguityErrorMisc::SuggestCrate
788+
} else if f.contains(Flags::MISC_SUGGEST_SELF) {
789+
AmbiguityErrorMisc::SuggestSelf
790+
} else if f.contains(Flags::MISC_FROM_PRELUDE) {
791+
AmbiguityErrorMisc::FromPrelude
792+
} else {
793+
AmbiguityErrorMisc::None
794+
}
795+
};
796+
self.ambiguity_errors.push(AmbiguityError {
797+
kind,
798+
ident: orig_ident,
799+
b1: innermost_binding,
800+
b2: binding,
801+
warning: false,
802+
misc1: misc(innermost_flags),
803+
misc2: misc(flags),
804+
});
805+
return true;
806+
}
804807
}
805808

806809
false

compiler/rustc_resolve/src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11701170
return;
11711171
}
11721172
if let Some(initial_res) = initial_res {
1173-
if res != initial_res {
1173+
if res != initial_res && !this.issue_145575_hack_applied {
11741174
span_bug!(import.span, "inconsistent resolution for an import");
11751175
}
11761176
} else if this.privacy_errors.is_empty() {

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,7 @@ pub struct Resolver<'ra, 'tcx> {
11881188
privacy_errors: Vec<PrivacyError<'ra>> = Vec::new(),
11891189
/// Ambiguity errors are delayed for deduplication.
11901190
ambiguity_errors: Vec<AmbiguityError<'ra>> = Vec::new(),
1191+
issue_145575_hack_applied: bool = false,
11911192
/// `use` injections are delayed for better placement and deduplication.
11921193
use_injections: Vec<UseError<'tcx>> = Vec::new(),
11931194
/// Crate-local macro expanded `macro_export` referred to by a module-relative path.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ edition: 2024
2+
3+
mod m {
4+
use crate::*;
5+
use core;
6+
}
7+
8+
macro_rules! define_other_core {
9+
() => {
10+
extern crate std as core;
11+
//~^ ERROR macro-expanded `extern crate` items cannot shadow names passed with `--extern`
12+
};
13+
}
14+
15+
define_other_core! {}
16+
17+
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/ice-inconsistent-resolution-149821.rs:10:9
3+
|
4+
LL | extern crate std as core;
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: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)