diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2025-08-13 15:46:11 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2025-08-13 17:45:40 +0300 |
| commit | d98eaad5096a516f9a1601c61e7e9b6e9f25e695 (patch) | |
| tree | 08a80faf3faf3f8b78345d74d990dc03e0ec147f | |
| parent | 772493d51d24dabbffdc54aaf48859eec85d544e (diff) | |
| download | rust-d98eaad5096a516f9a1601c61e7e9b6e9f25e695.tar.gz rust-d98eaad5096a516f9a1601c61e7e9b6e9f25e695.zip | |
resolve: Improve diagnostics for ambiguities in extern prelude
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 11 | ||||
| -rw-r--r-- | tests/ui/imports/issue-109148.stderr | 6 | ||||
| -rw-r--r-- | tests/ui/macros/issue-78325-inconsistent-resolution.stderr | 6 |
3 files changed, 11 insertions, 12 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index e5321c7f3be..e420e68264b 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1864,14 +1864,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } } - fn ambiguity_diagnostics(&self, ambiguity_error: &AmbiguityError<'_>) -> AmbiguityErrorDiag { + fn ambiguity_diagnostics(&self, ambiguity_error: &AmbiguityError<'ra>) -> AmbiguityErrorDiag { let AmbiguityError { kind, ident, b1, b2, misc1, misc2, .. } = *ambiguity_error; + let extern_prelude_ambiguity = || { + self.extern_prelude.get(&Macros20NormalizedIdent::new(ident)).is_some_and(|entry| { + entry.item_binding == Some(b1) && entry.flag_binding.get() == Some(b2) + }) + }; let (b1, b2, misc1, misc2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() { // We have to print the span-less alternative first, otherwise formatting looks bad. (b2, b1, misc2, misc1, true) } else { (b1, b2, misc1, misc2, false) }; + let could_refer_to = |b: NameBinding<'_>, misc: AmbiguityErrorMisc, also: &str| { let what = self.binding_description(b, ident, misc == AmbiguityErrorMisc::FromPrelude); let note_msg = format!("`{ident}` could{also} refer to {what}"); @@ -1887,7 +1893,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { "consider adding an explicit import of `{ident}` to disambiguate" )) } - if b.is_extern_crate() && ident.span.at_least_rust_2018() { + if b.is_extern_crate() && ident.span.at_least_rust_2018() && !extern_prelude_ambiguity() + { help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously")) } match misc { diff --git a/tests/ui/imports/issue-109148.stderr b/tests/ui/imports/issue-109148.stderr index eead173ba7a..ee047385ae3 100644 --- a/tests/ui/imports/issue-109148.stderr +++ b/tests/ui/imports/issue-109148.stderr @@ -17,7 +17,6 @@ LL | use std::mem; | = 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 = note: `std` could refer to a built-in crate - = help: use `::std` to refer to this crate unambiguously note: `std` could also refer to the crate imported here --> $DIR/issue-109148.rs:6:9 | @@ -26,8 +25,7 @@ LL | extern crate core as std; ... LL | m!(); | ---- in this macro invocation - = help: use `::std` to refer to this crate unambiguously - = help: or use `crate::std` to refer to this crate unambiguously + = help: use `crate::std` to refer to this crate unambiguously = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0659]: `std` is ambiguous @@ -38,7 +36,6 @@ LL | use ::std::mem as _; | = 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 = note: `std` could refer to a built-in crate - = help: use `::std` to refer to this crate unambiguously note: `std` could also refer to the crate imported here --> $DIR/issue-109148.rs:6:9 | @@ -47,7 +44,6 @@ LL | extern crate core as std; ... LL | m!(); | ---- in this macro invocation - = help: use `::std` to refer to this crate unambiguously = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 3 previous errors diff --git a/tests/ui/macros/issue-78325-inconsistent-resolution.stderr b/tests/ui/macros/issue-78325-inconsistent-resolution.stderr index 5574a1f0491..7c745040640 100644 --- a/tests/ui/macros/issue-78325-inconsistent-resolution.stderr +++ b/tests/ui/macros/issue-78325-inconsistent-resolution.stderr @@ -17,7 +17,6 @@ LL | core::panic!(); | = 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 = note: `core` could refer to a built-in crate - = help: use `::core` to refer to this crate unambiguously note: `core` could also refer to the crate imported here --> $DIR/issue-78325-inconsistent-resolution.rs:5:9 | @@ -26,8 +25,7 @@ LL | extern crate std as core; ... LL | define_other_core!(); | -------------------- in this macro invocation - = help: use `::core` to refer to this crate unambiguously - = help: or use `crate::core` to refer to this crate unambiguously + = help: use `crate::core` to refer to this crate unambiguously = note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0659]: `core` is ambiguous @@ -38,7 +36,6 @@ LL | ::core::panic!(); | = 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 = note: `core` could refer to a built-in crate - = help: use `::core` to refer to this crate unambiguously note: `core` could also refer to the crate imported here --> $DIR/issue-78325-inconsistent-resolution.rs:5:9 | @@ -47,7 +44,6 @@ LL | extern crate std as core; ... LL | define_other_core!(); | -------------------- in this macro invocation - = help: use `::core` to refer to this crate unambiguously = note: this error originates in the macro `define_other_core` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 3 previous errors |
