Skip to content

Commit d98eaad

Browse files
committed
resolve: Improve diagnostics for ambiguities in extern prelude
1 parent 772493d commit d98eaad

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,14 +1864,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18641864
}
18651865
}
18661866

1867-
fn ambiguity_diagnostics(&self, ambiguity_error: &AmbiguityError<'_>) -> AmbiguityErrorDiag {
1867+
fn ambiguity_diagnostics(&self, ambiguity_error: &AmbiguityError<'ra>) -> AmbiguityErrorDiag {
18681868
let AmbiguityError { kind, ident, b1, b2, misc1, misc2, .. } = *ambiguity_error;
1869+
let extern_prelude_ambiguity = || {
1870+
self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)).is_some_and(|entry| {
1871+
entry.item_binding == Some(b1) && entry.flag_binding.get() == Some(b2)
1872+
})
1873+
};
18691874
let (b1, b2, misc1, misc2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() {
18701875
// We have to print the span-less alternative first, otherwise formatting looks bad.
18711876
(b2, b1, misc2, misc1, true)
18721877
} else {
18731878
(b1, b2, misc1, misc2, false)
18741879
};
1880+
18751881
let could_refer_to = |b: NameBinding<'_>, misc: AmbiguityErrorMisc, also: &str| {
18761882
let what = self.binding_description(b, ident, misc == AmbiguityErrorMisc::FromPrelude);
18771883
let note_msg = format!("`{ident}` could{also} refer to {what}");
@@ -1887,7 +1893,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18871893
"consider adding an explicit import of `{ident}` to disambiguate"
18881894
))
18891895
}
1890-
if b.is_extern_crate() && ident.span.at_least_rust_2018() {
1896+
if b.is_extern_crate() && ident.span.at_least_rust_2018() && !extern_prelude_ambiguity()
1897+
{
18911898
help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously"))
18921899
}
18931900
match misc {

tests/ui/imports/issue-109148.stderr

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ LL | use std::mem;
1717
|
1818
= 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
1919
= note: `std` could refer to a built-in crate
20-
= help: use `::std` to refer to this crate unambiguously
2120
note: `std` could also refer to the crate imported here
2221
--> $DIR/issue-109148.rs:6:9
2322
|
@@ -26,8 +25,7 @@ LL | extern crate core as std;
2625
...
2726
LL | m!();
2827
| ---- in this macro invocation
29-
= help: use `::std` to refer to this crate unambiguously
30-
= help: or use `crate::std` to refer to this crate unambiguously
28+
= help: use `crate::std` to refer to this crate unambiguously
3129
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
3230

3331
error[E0659]: `std` is ambiguous
@@ -38,7 +36,6 @@ LL | use ::std::mem as _;
3836
|
3937
= 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
4038
= note: `std` could refer to a built-in crate
41-
= help: use `::std` to refer to this crate unambiguously
4239
note: `std` could also refer to the crate imported here
4340
--> $DIR/issue-109148.rs:6:9
4441
|
@@ -47,7 +44,6 @@ LL | extern crate core as std;
4744
...
4845
LL | m!();
4946
| ---- in this macro invocation
50-
= help: use `::std` to refer to this crate unambiguously
5147
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
5248

5349
error: aborting due to 3 previous errors

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ LL | core::panic!();
1717
|
1818
= 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
1919
= note: `core` could refer to a built-in crate
20-
= help: use `::core` to refer to this crate unambiguously
2120
note: `core` could also refer to the crate imported here
2221
--> $DIR/issue-78325-inconsistent-resolution.rs:5:9
2322
|
@@ -26,8 +25,7 @@ LL | extern crate std as core;
2625
...
2726
LL | define_other_core!();
2827
| -------------------- in this macro invocation
29-
= help: use `::core` to refer to this crate unambiguously
30-
= help: or use `crate::core` to refer to this crate unambiguously
28+
= help: use `crate::core` to refer to this crate unambiguously
3129
= note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info)
3230

3331
error[E0659]: `core` is ambiguous
@@ -38,7 +36,6 @@ LL | ::core::panic!();
3836
|
3937
= 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
4038
= note: `core` could refer to a built-in crate
41-
= help: use `::core` to refer to this crate unambiguously
4239
note: `core` could also refer to the crate imported here
4340
--> $DIR/issue-78325-inconsistent-resolution.rs:5:9
4441
|
@@ -47,7 +44,6 @@ LL | extern crate std as core;
4744
...
4845
LL | define_other_core!();
4946
| -------------------- in this macro invocation
50-
= help: use `::core` to refer to this crate unambiguously
5147
= note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info)
5248

5349
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)