Skip to content

Commit 5ed3308

Browse files
committed
resolve: Cleanup ::name suggestions for ambiguities
using the now available scope information
1 parent 7c13c74 commit 5ed3308

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,22 +1996,27 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19961996

19971997
fn ambiguity_diagnostic(&self, ambiguity_error: &AmbiguityError<'ra>) -> errors::Ambiguity {
19981998
let AmbiguityError { kind, ident, b1, b2, scope1, scope2, .. } = *ambiguity_error;
1999-
let extern_prelude_ambiguity = || {
2000-
// Note: b1 may come from a module scope, as an extern crate item in module.
2001-
matches!(scope2, Scope::ExternPreludeFlags)
2002-
&& self
2003-
.extern_prelude
2004-
.get(&Macros20NormalizedIdent::new(ident))
2005-
.is_some_and(|entry| entry.item_decl.map(|(b, _)| b) == Some(b1))
1999+
let extern_prelude_flag = |scope| matches!(scope, Scope::ExternPreludeFlags);
2000+
let extern_prelude_item = |scope, decl| {
2001+
// Declaration comes from extern prelude item scope,
2002+
matches!(scope, Scope::ExternPreludeItems)
2003+
// or from a module scope, as an extern crate item in the root module.
2004+
|| matches!(scope, Scope::ModuleNonGlobs(..))
2005+
&& self
2006+
.extern_prelude
2007+
.get(&Macros20NormalizedIdent::new(ident))
2008+
.is_some_and(|entry| entry.item_decl.map(|(d, _)| d) == Some(decl))
20062009
};
2010+
let extern_prelude_ambiguity =
2011+
extern_prelude_flag(scope2) && extern_prelude_item(scope1, b1);
20072012
let (b1, b2, scope1, scope2, swapped) = if b2.span.is_dummy() && !b1.span.is_dummy() {
20082013
// We have to print the span-less alternative first, otherwise formatting looks bad.
20092014
(b2, b1, scope2, scope1, true)
20102015
} else {
20112016
(b1, b2, scope1, scope2, false)
20122017
};
20132018

2014-
let could_refer_to = |b: Decl<'_>, scope: Scope<'ra>, also: &str| {
2019+
let could_refer_to = |b: Decl<'ra>, scope: Scope<'ra>, also: &str| {
20152020
let what = self.decl_description(b, ident, scope);
20162021
let note_msg = format!("`{ident}` could{also} refer to {what}");
20172022

@@ -2026,7 +2031,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20262031
"consider adding an explicit import of `{ident}` to disambiguate"
20272032
))
20282033
}
2029-
if b.is_extern_crate() && ident.span.at_least_rust_2018() && !extern_prelude_ambiguity()
2034+
2035+
if !extern_prelude_ambiguity
2036+
&& (extern_prelude_flag(scope) || extern_prelude_item(scope, b))
20302037
{
20312038
help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously"))
20322039
}

tests/ui/imports/extern-prelude-extern-crate-restricted-shadowing.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ LL | extern crate std as Vec;
2424
...
2525
LL | define_vec!();
2626
| ------------- in this macro invocation
27+
= help: use `::Vec` to refer to this crate unambiguously
2728
note: `Vec` could also refer to a struct from prelude
2829
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
2930
= note: this error originates in the macro `define_vec` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui/imports/issue-114682-5.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ note: `issue_114682_5_extern_1` could also refer to the crate defined here
4545
|
4646
LL | pub use crate::*;
4747
| ^^^^^
48-
= help: use `::issue_114682_5_extern_1` to refer to this crate unambiguously
4948
= note: `#[deny(ambiguous_glob_imports)]` (part of `#[deny(future_incompatible)]`) on by default
5049

5150
error: aborting due to 3 previous errors
@@ -72,6 +71,5 @@ note: `issue_114682_5_extern_1` could also refer to the crate defined here
7271
|
7372
LL | pub use crate::*;
7473
| ^^^^^
75-
= help: use `::issue_114682_5_extern_1` to refer to this crate unambiguously
7674
= note: `#[deny(ambiguous_glob_imports)]` (part of `#[deny(future_incompatible)]`) on by default
7775

tests/ui/macros/macro-path-prelude-shadowing.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | std::panic!();
66
|
77
= note: ambiguous because of a conflict between a name from a glob import and an outer scope during import or macro resolution
88
= note: `std` could refer to a built-in crate
9+
= help: use `::std` to refer to this crate unambiguously
910
note: `std` could also refer to the module imported here
1011
--> $DIR/macro-path-prelude-shadowing.rs:28:9
1112
|

0 commit comments

Comments
 (0)