Skip to content

Commit aad4fc9

Browse files
committed
Fix: Prevent macro-expanded extern crates from shadowing extern arguments
1 parent 198328a commit aad4fc9

File tree

12 files changed

+74
-135
lines changed

12 files changed

+74
-135
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10361036
self.r.dcx().emit_err(
10371037
errors::MacroExpandedExternCrateCannotShadowExternArguments { span: item.span },
10381038
);
1039+
return; // Fix: Stop processing this item to avoid inconsistent resolution state.
10391040
}
10401041

10411042
use indexmap::map::Entry;

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
@@ -1186,6 +1186,7 @@ pub struct Resolver<'ra, 'tcx> {
11861186
privacy_errors: Vec<PrivacyError<'ra>> = Vec::new(),
11871187
/// Ambiguity errors are delayed for deduplication.
11881188
ambiguity_errors: Vec<AmbiguityError<'ra>> = Vec::new(),
1189+
issue_145575_hack_applied: bool = false,
11891190
/// `use` injections are delayed for better placement and deduplication.
11901191
use_injections: Vec<UseError<'tcx>> = Vec::new(),
11911192
/// Crate-local macro expanded `macro_export` referred to by a module-relative path.

src/tools/clippy/tests/ui/crashes/ice-6255.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ macro_rules! define_other_core {
99
}
1010

1111
fn main() {
12-
core::panic!(); //~ ERROR: `core` is ambiguous
12+
core::panic!();
1313
}
1414

1515
define_other_core!();

src/tools/clippy/tests/ui/crashes/ice-6255.stderr

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,5 @@ LL | define_other_core!();
99
|
1010
= note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error[E0659]: `core` is ambiguous
13-
--> tests/ui/crashes/ice-6255.rs:12:5
14-
|
15-
LL | core::panic!();
16-
| ^^^^ ambiguous name
17-
|
18-
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
19-
= note: `core` could refer to a built-in crate
20-
note: `core` could also refer to the crate imported here
21-
--> tests/ui/crashes/ice-6255.rs:6:9
22-
|
23-
LL | extern crate std as core;
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
25-
...
26-
LL | define_other_core!();
27-
| -------------------- in this macro invocation
28-
= help: use `crate::core` to refer to this crate unambiguously
29-
= note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info)
30-
31-
error: aborting due to 2 previous errors
12+
error: aborting due to 1 previous error
3213

33-
For more information about this error, try `rustc --explain E0659`.

tests/ui/imports/issue-109148.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ macro_rules! m {
1010

1111
m!();
1212

13-
use std::mem; //~ ERROR `std` is ambiguous
14-
use ::std::mem as _; //~ ERROR `std` is ambiguous
13+
use std::mem;
14+
use ::std::mem as _;
1515

1616
fn main() {}

tests/ui/imports/issue-109148.stderr

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,5 @@ LL | m!();
99
|
1010
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error[E0659]: `std` is ambiguous
13-
--> $DIR/issue-109148.rs:13:5
14-
|
15-
LL | use std::mem;
16-
| ^^^ ambiguous name
17-
|
18-
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
19-
= note: `std` could refer to a built-in crate
20-
note: `std` could also refer to the crate imported here
21-
--> $DIR/issue-109148.rs:6:9
22-
|
23-
LL | extern crate core as std;
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
25-
...
26-
LL | m!();
27-
| ---- in this macro invocation
28-
= help: use `crate::std` to refer to this crate unambiguously
29-
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
30-
31-
error[E0659]: `std` is ambiguous
32-
--> $DIR/issue-109148.rs:14:7
33-
|
34-
LL | use ::std::mem as _;
35-
| ^^^ ambiguous name
36-
|
37-
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
38-
= note: `std` could refer to a built-in crate
39-
note: `std` could also refer to the crate imported here
40-
--> $DIR/issue-109148.rs:6:9
41-
|
42-
LL | extern crate core as std;
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
44-
...
45-
LL | m!();
46-
| ---- in this macro invocation
47-
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
48-
49-
error: aborting due to 3 previous errors
12+
error: aborting due to 1 previous error
5013

51-
For more information about this error, try `rustc --explain E0659`.

tests/ui/macros/issue-78325-inconsistent-resolution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ macro_rules! define_other_core {
88
}
99

1010
fn main() {
11-
core::panic!(); //~ ERROR `core` is ambiguous
12-
::core::panic!(); //~ ERROR `core` is ambiguous
11+
core::panic!();
12+
::core::panic!();
1313
}
1414

1515
define_other_core!();

tests/ui/macros/issue-78325-inconsistent-resolution.stderr

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,5 @@ LL | define_other_core!();
99
|
1010
= note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error[E0659]: `core` is ambiguous
13-
--> $DIR/issue-78325-inconsistent-resolution.rs:11:5
14-
|
15-
LL | core::panic!();
16-
| ^^^^ ambiguous name
17-
|
18-
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
19-
= note: `core` could refer to a built-in crate
20-
note: `core` could also refer to the crate imported here
21-
--> $DIR/issue-78325-inconsistent-resolution.rs:5:9
22-
|
23-
LL | extern crate std as core;
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
25-
...
26-
LL | define_other_core!();
27-
| -------------------- in this macro invocation
28-
= help: use `crate::core` to refer to this crate unambiguously
29-
= note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info)
30-
31-
error[E0659]: `core` is ambiguous
32-
--> $DIR/issue-78325-inconsistent-resolution.rs:12:7
33-
|
34-
LL | ::core::panic!();
35-
| ^^^^ ambiguous name
36-
|
37-
= note: ambiguous because of a conflict between a macro-expanded name and a less macro-expanded name from outer scope during import or macro resolution
38-
= note: `core` could refer to a built-in crate
39-
note: `core` could also refer to the crate imported here
40-
--> $DIR/issue-78325-inconsistent-resolution.rs:5:9
41-
|
42-
LL | extern crate std as core;
43-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
44-
...
45-
LL | define_other_core!();
46-
| -------------------- in this macro invocation
47-
= note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info)
48-
49-
error: aborting due to 3 previous errors
12+
error: aborting due to 1 previous error
5013

51-
For more information about this error, try `rustc --explain E0659`.

0 commit comments

Comments
 (0)