From c9995d2492dc94ea92ac6639ac8757199da749d0 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Sat, 14 Jun 2025 00:08:02 +0200 Subject: [PATCH 1/2] Failing test Signed-off-by: Jonathan Brouwer --- .../associated-type-call.fixed | 22 +++++++++++++++++++ .../associated-types/associated-type-call.rs | 22 +++++++++++++++++++ .../associated-type-call.stderr | 15 +++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 tests/ui/associated-types/associated-type-call.fixed create mode 100644 tests/ui/associated-types/associated-type-call.rs create mode 100644 tests/ui/associated-types/associated-type-call.stderr diff --git a/tests/ui/associated-types/associated-type-call.fixed b/tests/ui/associated-types/associated-type-call.fixed new file mode 100644 index 0000000000000..a3bc910f6111c --- /dev/null +++ b/tests/ui/associated-types/associated-type-call.fixed @@ -0,0 +1,22 @@ +// issue: +// +//@ run-rustfix +#![allow(unused)] +struct T(); + +trait Trait { + type Assoc; + + fn f(); +} + +impl Trait for () { + type Assoc = T; + + fn f() { + +// +//@ run-rustfix +#![allow(unused)] +struct T(); + +trait Trait { + type Assoc; + + fn f(); +} + +impl Trait for () { + type Assoc = T; + + fn f() { + ::Assoc(); + //~^ ERROR no associated item named `Assoc` found for unit type `()` in the current scope + } +} + +fn main() {} diff --git a/tests/ui/associated-types/associated-type-call.stderr b/tests/ui/associated-types/associated-type-call.stderr new file mode 100644 index 0000000000000..634009433c830 --- /dev/null +++ b/tests/ui/associated-types/associated-type-call.stderr @@ -0,0 +1,15 @@ +error[E0599]: no associated item named `Assoc` found for unit type `()` in the current scope + --> $DIR/associated-type-call.rs:17:17 + | +LL | ::Assoc(); + | ^^^^^ associated item not found in `()` + | +help: to construct a value of type `T`, use the explicit path + | +LL - ::Assoc(); +LL + Date: Sat, 14 Jun 2025 00:08:28 +0200 Subject: [PATCH 2/2] Fix incorrect suggestion when calling an associated type with a type anchor Signed-off-by: Jonathan Brouwer --- compiler/rustc_hir_typeck/src/method/suggest.rs | 2 +- tests/ui/associated-types/associated-type-call.fixed | 2 +- tests/ui/associated-types/associated-type-call.stderr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 6cb53bbd7452e..288d915e85c65 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -724,7 +724,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { let def_path = tcx.def_path_str(adt_def.did()); err.span_suggestion( - ty.span.to(item_ident.span), + sugg_span, format!("to construct a value of type `{}`, use the explicit path", def_path), def_path, Applicability::MachineApplicable, diff --git a/tests/ui/associated-types/associated-type-call.fixed b/tests/ui/associated-types/associated-type-call.fixed index a3bc910f6111c..d450b3b82c96f 100644 --- a/tests/ui/associated-types/associated-type-call.fixed +++ b/tests/ui/associated-types/associated-type-call.fixed @@ -14,7 +14,7 @@ impl Trait for () { type Assoc = T; fn f() { - ::Assoc(); help: to construct a value of type `T`, use the explicit path | LL - ::Assoc(); -LL +