diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 06acba4749dd..7bc8d280824f 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -205,10 +205,6 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { }) } - fn assoc_type_required_generic_args_suggestion(&self, assoc_type_def_id: DefId) -> String { - self.r.item_required_generic_args_suggestion(assoc_type_def_id) - } - /// This does best-effort work to generate suggestions for associated types. fn suggest_assoc_type_from_bounds( &mut self, @@ -323,7 +319,10 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { record_from_generics(self, generics); } - if let Some(assoc) = self.diag_metadata.current_impl_item { + if let Some(item) = self.diag_metadata.current_item + && matches!(item.kind, ItemKind::Impl(..)) + && let Some(assoc) = self.diag_metadata.current_impl_item + { let generics = match &assoc.kind { AssocItemKind::Const(box ast::ConstItem { generics, .. }) | AssocItemKind::Fn(box ast::Fn { generics, .. }) @@ -346,7 +345,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { let assoc_segment = format!( "{}{}", assoc_name, - self.assoc_type_required_generic_args_suggestion(assoc_type_def_id) + self.r.item_required_generic_args_suggestion(assoc_type_def_id) ); suggestions.insert(format!("{ty_param}::{assoc_segment}")); } else { @@ -354,7 +353,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { let assoc_segment = format!( "{}{}", assoc_name, - self.assoc_type_required_generic_args_suggestion(assoc_type_def_id) + self.r.item_required_generic_args_suggestion(assoc_type_def_id) ); for trait_path in trait_paths { suggestions @@ -371,24 +370,13 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { let mut suggestions: Vec = suggestions.into_iter().collect(); suggestions.sort(); - match suggestions.as_slice() { - [one] => { - err.span_suggestion_verbose( - ident_span, - "use the associated type", - one, - Applicability::MaybeIncorrect, - ); - } - _ => { - err.span_suggestions( - ident_span, - "use an associated type", - suggestions, - Applicability::MaybeIncorrect, - ); - } - }; + err.span_suggestions_with_style( + ident_span, + "you might have meant to use an associated type of the same name", + suggestions, + Applicability::MaybeIncorrect, + SuggestionStyle::ShowAlways, + ); true } diff --git a/tests/ui/associated-types/associated-types-eq-1.stderr b/tests/ui/associated-types/associated-types-eq-1.stderr index 53ad1aefe5d3..54e50f36fb6a 100644 --- a/tests/ui/associated-types/associated-types-eq-1.stderr +++ b/tests/ui/associated-types/associated-types-eq-1.stderr @@ -4,7 +4,7 @@ error[E0425]: cannot find type `A` in this scope LL | let _: A = x.boo(); | ^ | -help: use the associated type +help: you might have meant to use an associated type of the same name | LL | let _: I::A = x.boo(); | +++ diff --git a/tests/ui/associated-types/suggest-assoc-type-from-bounds.rs b/tests/ui/associated-types/suggest-assoc-type-from-bounds.rs index 0eb56ad96e36..8b349f325cd7 100644 --- a/tests/ui/associated-types/suggest-assoc-type-from-bounds.rs +++ b/tests/ui/associated-types/suggest-assoc-type-from-bounds.rs @@ -40,4 +40,25 @@ fn i() { let _: Assoc = todo!(); //~ ERROR cannot find type `Assoc` in this scope } +fn j() { + struct Local; + impl Local { + fn method() { + let _: Assoc = todo!(); //~ ERROR cannot find type `Assoc` in this scope + } + } + + let _ = std::marker::PhantomData::; +} + +pub struct S; +impl S { + fn method() { + fn inner() { + let _: Assoc = todo!(); //~ ERROR cannot find type `Assoc` in this scope + } + inner(); + } +} + fn main() {} diff --git a/tests/ui/associated-types/suggest-assoc-type-from-bounds.stderr b/tests/ui/associated-types/suggest-assoc-type-from-bounds.stderr index c46c89c119e1..b5ce2d91ca4d 100644 --- a/tests/ui/associated-types/suggest-assoc-type-from-bounds.stderr +++ b/tests/ui/associated-types/suggest-assoc-type-from-bounds.stderr @@ -4,7 +4,7 @@ error[E0425]: cannot find type `Assoc` in this scope LL | let _: Assoc = todo!(); | ^^^^^ | -help: use an associated type +help: you might have meant to use an associated type of the same name | LL | let _: >::Assoc = todo!(); | +++++++++++++++++++ @@ -17,7 +17,7 @@ error[E0425]: cannot find type `A` in this scope LL | let _: A = todo!(); | ^ | -help: use an associated type +help: you might have meant to use an associated type of the same name | LL | let _: >::A = todo!(); | ++++++++++++++++++ @@ -34,7 +34,7 @@ error[E0425]: cannot find type `Assoc` in this scope LL | let _: Assoc = todo!(); | ^^^^^ | -help: use an associated type +help: you might have meant to use an associated type of the same name | LL | let _: ::Assoc = todo!(); | ++++++++++++++ @@ -47,12 +47,29 @@ error[E0425]: cannot find type `Assoc` in this scope LL | let _: Assoc = todo!(); | ^^^^^ | -help: use the associated type +help: you might have meant to use an associated type of the same name | LL - let _: Assoc = todo!(); LL + let _: T::Assoc<'_> = todo!(); | -error: aborting due to 4 previous errors +error[E0425]: cannot find type `Assoc` in this scope + --> $DIR/suggest-assoc-type-from-bounds.rs:47:20 + | +LL | let _: Assoc = todo!(); + | ^^^^^ + | +help: you might have meant to use an associated type of the same name + | +LL | let _: U::Assoc = todo!(); + | +++ + +error[E0425]: cannot find type `Assoc` in this scope + --> $DIR/suggest-assoc-type-from-bounds.rs:58:20 + | +LL | let _: Assoc = todo!(); + | ^^^^^ not found in this scope + +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0425`. diff --git a/tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.stderr b/tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.stderr index 184a5aaf0488..c217672b0050 100644 --- a/tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.stderr +++ b/tests/ui/typeck/sugg-swap-equality-in-macro-issue-139050.stderr @@ -4,7 +4,7 @@ error[E0425]: cannot find type `Item` in this scope LL | Item: Eq + Debug, | ^^^^ | -help: use the associated type +help: you might have meant to use an associated type of the same name | LL | I::Item: Eq + Debug, | +++