From 14acce0cb64cca2290e7042fbca8d8030f5c16d7 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 20 May 2025 16:55:09 -0300 Subject: [PATCH] Do not call name() on rpitit assoc_item (cherry picked from commit 66d47c16873fc04a02cb0bd977ec7f3d50f228f1) --- .../src/error_reporting/traits/suggestions.rs | 23 +++++++++++-------- .../in-trait/not-inferred-generic.rs | 13 +++++++++++ .../in-trait/not-inferred-generic.stderr | 21 +++++++++++++++++ 3 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 tests/ui/impl-trait/in-trait/not-inferred-generic.rs create mode 100644 tests/ui/impl-trait/in-trait/not-inferred-generic.stderr diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index 8801397b7754..6863857f9ecb 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -2124,16 +2124,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { accessed through a specific `impl`", self.tcx.def_kind_descr(assoc_item.as_def_kind(), item_def_id) )); - err.span_suggestion( - span, - "use the fully qualified path to an implementation", - format!( - "::{}", - self.tcx.def_path_str(trait_ref), - assoc_item.name() - ), - Applicability::HasPlaceholders, - ); + + if !assoc_item.is_impl_trait_in_trait() { + err.span_suggestion( + span, + "use the fully qualified path to an implementation", + format!( + "::{}", + self.tcx.def_path_str(trait_ref), + assoc_item.name() + ), + Applicability::HasPlaceholders, + ); + } } } } diff --git a/tests/ui/impl-trait/in-trait/not-inferred-generic.rs b/tests/ui/impl-trait/in-trait/not-inferred-generic.rs new file mode 100644 index 000000000000..3879ea0e6262 --- /dev/null +++ b/tests/ui/impl-trait/in-trait/not-inferred-generic.rs @@ -0,0 +1,13 @@ +trait TypedClient { + fn publish_typed(&self) -> impl Sized + where + F: Clone; +} +impl TypedClient for () { + fn publish_typed(&self) -> impl Sized {} +} + +fn main() { + ().publish_typed(); + //~^ ERROR type annotations needed [E0283] +} diff --git a/tests/ui/impl-trait/in-trait/not-inferred-generic.stderr b/tests/ui/impl-trait/in-trait/not-inferred-generic.stderr new file mode 100644 index 000000000000..07f029d3bb7d --- /dev/null +++ b/tests/ui/impl-trait/in-trait/not-inferred-generic.stderr @@ -0,0 +1,21 @@ +error[E0283]: type annotations needed + --> $DIR/not-inferred-generic.rs:11:8 + | +LL | ().publish_typed(); + | ^^^^^^^^^^^^^ cannot infer type of the type parameter `F` declared on the method `publish_typed` + | + = note: cannot satisfy `_: Clone` + = note: associated types cannot be accessed directly on a `trait`, they can only be accessed through a specific `impl` +note: required by a bound in `TypedClient::publish_typed::{anon_assoc#0}` + --> $DIR/not-inferred-generic.rs:4:12 + | +LL | F: Clone; + | ^^^^^ required by this bound in `TypedClient::publish_typed::{anon_assoc#0}` +help: consider specifying the generic argument + | +LL | ().publish_typed::(); + | +++++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0283`.