diff options
| author | León Orell Valerian Liehr <me@fmease.dev> | 2025-04-23 21:14:25 +0200 |
|---|---|---|
| committer | León Orell Valerian Liehr <me@fmease.dev> | 2025-05-06 17:04:03 +0200 |
| commit | 8c37c8c3e6133ae431bc4a5aec1c7b21a3134969 (patch) | |
| tree | eca9ef16972d0adce3ed4cf92e0fff67b51b737f | |
| parent | 9e1832998d0c47ee11cd28e0458e68d9e7186b0f (diff) | |
| download | rust-8c37c8c3e6133ae431bc4a5aec1c7b21a3134969.tar.gz rust-8c37c8c3e6133ae431bc4a5aec1c7b21a3134969.zip | |
Preserve generic args in suggestions for ambiguous associated items
Most notably, this preserves the `(..)` of ambiguous RTN paths.
15 files changed, 140 insertions, 47 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index 60dc84716c7..45fee0fa402 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -3,7 +3,8 @@ use rustc_data_structures::sorted_map::SortedMap; use rustc_data_structures::unord::UnordMap; use rustc_errors::codes::*; use rustc_errors::{ - Applicability, Diag, ErrorGuaranteed, MultiSpan, listify, pluralize, struct_span_code_err, + Applicability, Diag, ErrorGuaranteed, MultiSpan, SuggestionStyle, listify, pluralize, + struct_span_code_err, }; use rustc_hir::def::{CtorOf, DefKind, Res}; use rustc_hir::def_id::DefId; @@ -443,7 +444,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { span, &type_names, &[path_str], - item_segment.ident.name, + item_segment.ident, assoc_tag, ) } @@ -552,12 +553,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let traits: Vec<_> = self.probe_traits_that_match_assoc_ty(self_ty, ident); - // Don't print `ty::Error` to the user. self.report_ambiguous_assoc_item_path( span, &[self_ty.to_string()], &traits, - ident.name, + ident, assoc_tag, ) } @@ -568,7 +568,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { span: Span, types: &[String], traits: &[String], - name: Symbol, + ident: Ident, assoc_tag: ty::AssocTag, ) -> ErrorGuaranteed { let kind_str = assoc_tag_str(assoc_tag); @@ -588,6 +588,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { Applicability::MachineApplicable, ); } else { + let sugg_sp = span.until(ident.span); + let mut types = types.to_vec(); types.sort(); let mut traits = traits.to_vec(); @@ -595,76 +597,79 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { match (&types[..], &traits[..]) { ([], []) => { err.span_suggestion_verbose( - span, + sugg_sp, format!( "if there were a type named `Type` that implements a trait named \ - `Trait` with associated {kind_str} `{name}`, you could use the \ + `Trait` with associated {kind_str} `{ident}`, you could use the \ fully-qualified path", ), - format!("<Type as Trait>::{name}"), + "<Type as Trait>::", Applicability::HasPlaceholders, ); } ([], [trait_str]) => { err.span_suggestion_verbose( - span, + sugg_sp, format!( "if there were a type named `Example` that implemented `{trait_str}`, \ you could use the fully-qualified path", ), - format!("<Example as {trait_str}>::{name}"), + format!("<Example as {trait_str}>::"), Applicability::HasPlaceholders, ); } ([], traits) => { - err.span_suggestions( - span, + err.span_suggestions_with_style( + sugg_sp, format!( "if there were a type named `Example` that implemented one of the \ - traits with associated {kind_str} `{name}`, you could use the \ + traits with associated {kind_str} `{ident}`, you could use the \ fully-qualified path", ), - traits.iter().map(|trait_str| format!("<Example as {trait_str}>::{name}")), + traits.iter().map(|trait_str| format!("<Example as {trait_str}>::")), Applicability::HasPlaceholders, + SuggestionStyle::ShowAlways, ); } ([type_str], []) => { err.span_suggestion_verbose( - span, + sugg_sp, format!( - "if there were a trait named `Example` with associated {kind_str} `{name}` \ + "if there were a trait named `Example` with associated {kind_str} `{ident}` \ implemented for `{type_str}`, you could use the fully-qualified path", ), - format!("<{type_str} as Example>::{name}"), + format!("<{type_str} as Example>::"), Applicability::HasPlaceholders, ); } (types, []) => { - err.span_suggestions( - span, + err.span_suggestions_with_style( + sugg_sp, format!( - "if there were a trait named `Example` with associated {kind_str} `{name}` \ + "if there were a trait named `Example` with associated {kind_str} `{ident}` \ implemented for one of the types, you could use the fully-qualified \ path", ), types .into_iter() - .map(|type_str| format!("<{type_str} as Example>::{name}")), + .map(|type_str| format!("<{type_str} as Example>::")), Applicability::HasPlaceholders, + SuggestionStyle::ShowAlways, ); } (types, traits) => { let mut suggestions = vec![]; for type_str in types { for trait_str in traits { - suggestions.push(format!("<{type_str} as {trait_str}>::{name}")); + suggestions.push(format!("<{type_str} as {trait_str}>::")); } } - err.span_suggestions( - span, + err.span_suggestions_with_style( + sugg_sp, "use fully-qualified syntax", suggestions, Applicability::MachineApplicable, + SuggestionStyle::ShowAlways, ); } } diff --git a/tests/ui/associated-inherent-types/not-found-self-type-differs-shadowing-trait-item.uncovered.stderr b/tests/ui/associated-inherent-types/not-found-self-type-differs-shadowing-trait-item.uncovered.stderr index 978305c2ce3..3e914e0538d 100644 --- a/tests/ui/associated-inherent-types/not-found-self-type-differs-shadowing-trait-item.uncovered.stderr +++ b/tests/ui/associated-inherent-types/not-found-self-type-differs-shadowing-trait-item.uncovered.stderr @@ -2,7 +2,13 @@ error[E0223]: ambiguous associated type --> $DIR/not-found-self-type-differs-shadowing-trait-item.rs:28:12 | LL | let _: S::<bool>::Pr = (); - | ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<S<bool> as Tr>::Pr` + | ^^^^^^^^^^^^^ + | +help: use fully-qualified syntax + | +LL - let _: S::<bool>::Pr = (); +LL + let _: <S<bool> as Tr>::Pr = (); + | error: aborting due to 1 previous error diff --git a/tests/ui/associated-item/ambiguous-associated-type-with-generics.stderr b/tests/ui/associated-item/ambiguous-associated-type-with-generics.stderr index 9e1dd739807..fcf6b4c3e73 100644 --- a/tests/ui/associated-item/ambiguous-associated-type-with-generics.stderr +++ b/tests/ui/associated-item/ambiguous-associated-type-with-generics.stderr @@ -2,7 +2,12 @@ error[E0223]: ambiguous associated type --> $DIR/ambiguous-associated-type-with-generics.rs:13:13 | LL | let _x: <dyn Trait<i32>>::Ty; - | ^^^^^^^^^^^^^^^^^^^^ help: use fully-qualified syntax: `<dyn Trait<i32> as Assoc>::Ty` + | ^^^^^^^^^^^^^^^^^^^^ + | +help: use fully-qualified syntax + | +LL | let _x: <dyn Trait<i32> as Assoc>::Ty; + | ++++++++ error: aborting due to 1 previous error diff --git a/tests/ui/associated-item/associated-item-duplicate-names-3.stderr b/tests/ui/associated-item/associated-item-duplicate-names-3.stderr index a2346e292ac..84a9da09988 100644 --- a/tests/ui/associated-item/associated-item-duplicate-names-3.stderr +++ b/tests/ui/associated-item/associated-item-duplicate-names-3.stderr @@ -13,7 +13,13 @@ error[E0223]: ambiguous associated type --> $DIR/associated-item-duplicate-names-3.rs:18:12 | LL | let x: Baz::Bar = 5; - | ^^^^^^^^ help: use fully-qualified syntax: `<Baz as Foo>::Bar` + | ^^^^^^^^ + | +help: use fully-qualified syntax + | +LL - let x: Baz::Bar = 5; +LL + let x: <Baz as Foo>::Bar = 5; + | error: aborting due to 2 previous errors diff --git a/tests/ui/associated-type-bounds/return-type-notation/path-no-qself.stderr b/tests/ui/associated-type-bounds/return-type-notation/path-no-qself.stderr index aad6dfc496b..a13fdbda1bf 100644 --- a/tests/ui/associated-type-bounds/return-type-notation/path-no-qself.stderr +++ b/tests/ui/associated-type-bounds/return-type-notation/path-no-qself.stderr @@ -7,7 +7,7 @@ LL | Trait::method(..): Send, help: if there were a type named `Example` that implemented `Trait`, you could use the fully-qualified path | LL - Trait::method(..): Send, -LL + <Example as Trait>::method: Send, +LL + <Example as Trait>::method(..): Send, | error: aborting due to 1 previous error diff --git a/tests/ui/associated-type-bounds/return-type-notation/path-non-param-qself.stderr b/tests/ui/associated-type-bounds/return-type-notation/path-non-param-qself.stderr index 6b7ded3f285..4c4c2c24079 100644 --- a/tests/ui/associated-type-bounds/return-type-notation/path-non-param-qself.stderr +++ b/tests/ui/associated-type-bounds/return-type-notation/path-non-param-qself.stderr @@ -6,9 +6,8 @@ LL | <()>::method(..): Send, | help: if there were a trait named `Example` with associated function `method` implemented for `()`, you could use the fully-qualified path | -LL - <()>::method(..): Send, -LL + <() as Example>::method: Send, - | +LL | <() as Example>::method(..): Send, + | ++++++++++ error[E0223]: ambiguous associated function --> $DIR/path-non-param-qself.rs:13:5 @@ -19,7 +18,7 @@ LL | i32::method(..): Send, help: if there were a trait named `Example` with associated function `method` implemented for `i32`, you could use the fully-qualified path | LL - i32::method(..): Send, -LL + <i32 as Example>::method: Send, +LL + <i32 as Example>::method(..): Send, | error[E0223]: ambiguous associated function @@ -31,7 +30,7 @@ LL | Adt::method(..): Send, help: if there were a trait named `Example` with associated function `method` implemented for `Adt`, you could use the fully-qualified path | LL - Adt::method(..): Send, -LL + <Adt as Example>::method: Send, +LL + <Adt as Example>::method(..): Send, | error: aborting due to 3 previous errors diff --git a/tests/ui/associated-types/associated-types-in-ambiguous-context.stderr b/tests/ui/associated-types/associated-types-in-ambiguous-context.stderr index 1be8db5ddf4..a7647cf26aa 100644 --- a/tests/ui/associated-types/associated-types-in-ambiguous-context.stderr +++ b/tests/ui/associated-types/associated-types-in-ambiguous-context.stderr @@ -14,7 +14,13 @@ error[E0223]: ambiguous associated type --> $DIR/associated-types-in-ambiguous-context.rs:22:17 | LL | trait Foo where Foo::Assoc: Bar { - | ^^^^^^^^^^ help: use fully-qualified syntax: `<Self as Foo>::Assoc` + | ^^^^^^^^^^ + | +help: use fully-qualified syntax + | +LL - trait Foo where Foo::Assoc: Bar { +LL + trait Foo where <Self as Foo>::Assoc: Bar { + | error[E0223]: ambiguous associated type --> $DIR/associated-types-in-ambiguous-context.rs:27:10 @@ -42,7 +48,13 @@ error[E0223]: ambiguous associated type --> $DIR/associated-types-in-ambiguous-context.rs:13:23 | LL | fn grab(&self) -> Grab::Value; - | ^^^^^^^^^^^ help: use fully-qualified syntax: `<Self as Grab>::Value` + | ^^^^^^^^^^^ + | +help: use fully-qualified syntax + | +LL - fn grab(&self) -> Grab::Value; +LL + fn grab(&self) -> <Self as Grab>::Value; + | error[E0223]: ambiguous associated type --> $DIR/associated-types-in-ambiguous-context.rs:16:22 diff --git a/tests/ui/error-codes/E0223.stderr b/tests/ui/error-codes/E0223.stderr index e985a4c9bf0..fbfdce5689a 100644 --- a/tests/ui/error-codes/E0223.stderr +++ b/tests/ui/error-codes/E0223.stderr @@ -2,7 +2,13 @@ error[E0223]: ambiguous associated type --> $DIR/E0223.rs:8:14 | LL | let foo: MyTrait::X; - | ^^^^^^^^^^ help: use fully-qualified syntax: `<MyStruct as MyTrait>::X` + | ^^^^^^^^^^ + | +help: use fully-qualified syntax + | +LL - let foo: MyTrait::X; +LL + let foo: <MyStruct as MyTrait>::X; + | error: aborting due to 1 previous error diff --git a/tests/ui/lint/bare-trait-objects-path.stderr b/tests/ui/lint/bare-trait-objects-path.stderr index fbb647c37c5..e611abd31f3 100644 --- a/tests/ui/lint/bare-trait-objects-path.stderr +++ b/tests/ui/lint/bare-trait-objects-path.stderr @@ -55,7 +55,13 @@ error[E0223]: ambiguous associated type --> $DIR/bare-trait-objects-path.rs:23:12 | LL | let _: Dyn::Ty; - | ^^^^^^^ help: use fully-qualified syntax: `<dyn Dyn as Assoc>::Ty` + | ^^^^^^^ + | +help: use fully-qualified syntax + | +LL - let _: Dyn::Ty; +LL + let _: <dyn Dyn as Assoc>::Ty; + | error: aborting due to 1 previous error; 4 warnings emitted diff --git a/tests/ui/qualified/qualified-path-params-2.stderr b/tests/ui/qualified/qualified-path-params-2.stderr index 6641e81013f..e70cdbdc3f4 100644 --- a/tests/ui/qualified/qualified-path-params-2.stderr +++ b/tests/ui/qualified/qualified-path-params-2.stderr @@ -7,7 +7,7 @@ LL | type A = <S as Tr>::A::f<u8>; help: if there were a trait named `Example` with associated type `f` implemented for `<S as Tr>::A`, you could use the fully-qualified path | LL - type A = <S as Tr>::A::f<u8>; -LL + type A = <<S as Tr>::A as Example>::f; +LL + type A = <<S as Tr>::A as Example>::f<u8>; | error: aborting due to 1 previous error diff --git a/tests/ui/self/self-impl.stderr b/tests/ui/self/self-impl.stderr index 18ffd15427f..1bda307d0eb 100644 --- a/tests/ui/self/self-impl.stderr +++ b/tests/ui/self/self-impl.stderr @@ -2,13 +2,25 @@ error[E0223]: ambiguous associated type --> $DIR/self-impl.rs:23:16 | LL | let _: <Self>::Baz = true; - | ^^^^^^^^^^^ help: use fully-qualified syntax: `<Bar as Foo>::Baz` + | ^^^^^^^^^^^ + | +help: use fully-qualified syntax + | +LL - let _: <Self>::Baz = true; +LL + let _: <Bar as Foo>::Baz = true; + | error[E0223]: ambiguous associated type --> $DIR/self-impl.rs:25:16 | LL | let _: Self::Baz = true; - | ^^^^^^^^^ help: use fully-qualified syntax: `<Bar as Foo>::Baz` + | ^^^^^^^^^ + | +help: use fully-qualified syntax + | +LL - let _: Self::Baz = true; +LL + let _: <Bar as Foo>::Baz = true; + | error: aborting due to 2 previous errors diff --git a/tests/ui/structs/struct-path-associated-type.stderr b/tests/ui/structs/struct-path-associated-type.stderr index de396e875b0..110362293ac 100644 --- a/tests/ui/structs/struct-path-associated-type.stderr +++ b/tests/ui/structs/struct-path-associated-type.stderr @@ -48,19 +48,37 @@ error[E0223]: ambiguous associated type --> $DIR/struct-path-associated-type.rs:32:13 | LL | let s = S::A {}; - | ^^^^ help: use fully-qualified syntax: `<S as Tr>::A` + | ^^^^ + | +help: use fully-qualified syntax + | +LL - let s = S::A {}; +LL + let s = <S as Tr>::A {}; + | error[E0223]: ambiguous associated type --> $DIR/struct-path-associated-type.rs:33:13 | LL | let z = S::A::<u8> {}; - | ^^^^ help: use fully-qualified syntax: `<S as Tr>::A` + | ^^^^ + | +help: use fully-qualified syntax + | +LL - let z = S::A::<u8> {}; +LL + let z = <S as Tr>::A::<u8> {}; + | error[E0223]: ambiguous associated type --> $DIR/struct-path-associated-type.rs:35:9 | LL | S::A {} => {} - | ^^^^ help: use fully-qualified syntax: `<S as Tr>::A` + | ^^^^ + | +help: use fully-qualified syntax + | +LL - S::A {} => {} +LL + <S as Tr>::A {} => {} + | error: aborting due to 8 previous errors diff --git a/tests/ui/traits/item-privacy.stderr b/tests/ui/traits/item-privacy.stderr index 4fd9ef91192..1d3d8cb9843 100644 --- a/tests/ui/traits/item-privacy.stderr +++ b/tests/ui/traits/item-privacy.stderr @@ -230,13 +230,25 @@ error[E0223]: ambiguous associated type --> $DIR/item-privacy.rs:119:12 | LL | let _: S::B; - | ^^^^ help: use fully-qualified syntax: `<S as assoc_ty::B>::B` + | ^^^^ + | +help: use fully-qualified syntax + | +LL - let _: S::B; +LL + let _: <S as assoc_ty::B>::B; + | error[E0223]: ambiguous associated type --> $DIR/item-privacy.rs:120:12 | LL | let _: S::C; - | ^^^^ help: use fully-qualified syntax: `<S as assoc_ty::C>::C` + | ^^^^ + | +help: use fully-qualified syntax + | +LL - let _: S::C; +LL + let _: <S as assoc_ty::C>::C; + | error[E0624]: associated type `A` is private --> $DIR/item-privacy.rs:122:12 diff --git a/tests/ui/typeck/ice-unexpected-region-123863.stderr b/tests/ui/typeck/ice-unexpected-region-123863.stderr index 8a4d767c143..e5050b4d316 100644 --- a/tests/ui/typeck/ice-unexpected-region-123863.stderr +++ b/tests/ui/typeck/ice-unexpected-region-123863.stderr @@ -39,7 +39,7 @@ LL | Inner::concat_strs::<"a">::A help: if there were a trait named `Example` with associated type `concat_strs` implemented for `Inner<_>`, you could use the fully-qualified path | LL - Inner::concat_strs::<"a">::A -LL + <Inner<_> as Example>::concat_strs::A +LL + <Inner<_> as Example>::concat_strs::<"a">::A | error: aborting due to 3 previous errors diff --git a/tests/ui/typeck/issue-107087.stderr b/tests/ui/typeck/issue-107087.stderr index 289c8d161ae..157ba5a1672 100644 --- a/tests/ui/typeck/issue-107087.stderr +++ b/tests/ui/typeck/issue-107087.stderr @@ -2,7 +2,13 @@ error[E0223]: ambiguous associated type --> $DIR/issue-107087.rs:16:5 | LL | A::B::<>::C - | ^^^^^^^^ help: use fully-qualified syntax: `<A<_> as Foo>::B` + | ^^^^^^^^ + | +help: use fully-qualified syntax + | +LL - A::B::<>::C +LL + <A<_> as Foo>::B::<>::C + | error: aborting due to 1 previous error |
