diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index a820f700869b2..955f3501fccc5 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1271,6 +1271,7 @@ impl<'a> Resolver<'a> { let mut seen_modules = FxHashSet::default(); let mut worklist = vec![(start_module, Vec::::new(), true)]; let mut worklist_via_import = vec![]; + let mut seen_exact_match = false; while let Some((in_module, path_segments, accessible)) = match worklist.pop() { None => worklist_via_import.pop(), @@ -1313,6 +1314,15 @@ impl<'a> Resolver<'a> { } } + // Note if we find an in-scope exact match on the identifier we are looking for + if ident.name == lookup_ident.name + && ns == namespace + && ptr::eq(in_module, parent_scope.module) + { + seen_exact_match = true; + return; + } + // collect results based on the filter function // avoid suggesting anything from the same module in which we are resolving // avoid suggesting anything with a hygienic name @@ -1406,6 +1416,12 @@ impl<'a> Resolver<'a> { }) } + // If we've seen an exact match on the ident, assume correct intent from the user + // (ie. the user meant to use the ident like this) and clear any import candidates + if seen_exact_match { + candidates.clear(); + } + // If only some candidates are accessible, take just them if !candidates.iter().all(|v: &ImportSuggestion| !v.accessible) { candidates = candidates.into_iter().filter(|x| x.accessible).collect(); diff --git a/src/test/ui/hygiene/globs.stderr b/src/test/ui/hygiene/globs.stderr index bcfcc28adf776..b7198a55e45ef 100644 --- a/src/test/ui/hygiene/globs.stderr +++ b/src/test/ui/hygiene/globs.stderr @@ -37,8 +37,6 @@ LL | n!(f); LL | n!(f); | ^ not found in this scope | - = note: consider importing this function: - foo::f = note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0425]: cannot find function `f` in this scope @@ -50,8 +48,6 @@ LL | n!(f); LL | f | ^ not found in this scope | - = note: consider importing this function: - foo::f = note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 4 previous errors diff --git a/src/test/ui/resolve/issue-3907.stderr b/src/test/ui/resolve/issue-3907.stderr index 6fc61cae84339..6f6f3affdb462 100644 --- a/src/test/ui/resolve/issue-3907.stderr +++ b/src/test/ui/resolve/issue-3907.stderr @@ -8,10 +8,6 @@ help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` | LL | trait Foo = dyn issue_3907::Foo; | -help: consider importing this trait instead - | -LL | use issue_3907::Foo; - | error: aborting due to previous error diff --git a/src/test/ui/span/issue-35987.stderr b/src/test/ui/span/issue-35987.stderr index 2bc3ff4c3b619..2d32754173d55 100644 --- a/src/test/ui/span/issue-35987.stderr +++ b/src/test/ui/span/issue-35987.stderr @@ -3,11 +3,6 @@ error[E0404]: expected trait, found type parameter `Add` | LL | impl Add for Foo { | ^^^ not a trait - | -help: consider importing this trait instead - | -LL | use std::ops::Add; - | error: aborting due to previous error diff --git a/src/test/ui/suggestions/init-enum-as-struct.rs b/src/test/ui/suggestions/init-enum-as-struct.rs new file mode 100644 index 0000000000000..10f4eba106b8a --- /dev/null +++ b/src/test/ui/suggestions/init-enum-as-struct.rs @@ -0,0 +1,23 @@ +pub type Foos = i32; + +pub enum Foo { + A, + B, + C, +} + +mod module0 { + pub struct Foo { + a: i32, + } +} +mod module1 { + pub struct Foo {} +} +mod module2 { + pub enum Foo {} +} + +fn main() { + let foo = Foo { b: 0 }; //~ expected struct, variant or union type, found enum `Foo` +} diff --git a/src/test/ui/suggestions/init-enum-as-struct.stderr b/src/test/ui/suggestions/init-enum-as-struct.stderr new file mode 100644 index 0000000000000..b9a0b9c5fd5c1 --- /dev/null +++ b/src/test/ui/suggestions/init-enum-as-struct.stderr @@ -0,0 +1,12 @@ +error[E0574]: expected struct, variant or union type, found enum `Foo` + --> $DIR/init-enum-as-struct.rs:22:15 + | +LL | pub type Foos = i32; + | -------------------- similarly named type alias `Foos` defined here +... +LL | let foo = Foo { b: 0 }; + | ^^^ help: a type alias with a similar name exists: `Foos` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0574`.