Rollup merge of #142477 - JonathanBrouwer:associated-type-suggestion, r=WaffleLapkin

Fix incorrect suggestion when calling an associated type with a type anchor

`sugg_span` here is the span of the call expression.
That span here is the `<Self>::Assoc`, which is exactly what we need here (even though I would expect it to include the arguments, but I guess it doesn't)

r? ``@WaffleLapkin``
One commit with failing tests and one that fixes it for reviewability

closes rust-lang/rust#142473
This commit is contained in:
Matthias Krüger 2025-06-14 11:27:12 +02:00 committed by GitHub
commit 4bf7765388
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 60 additions and 1 deletions

View file

@ -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,

View file

@ -0,0 +1,22 @@
// issue: <https://github.com/rust-lang/rust/issues/142473>
//
//@ run-rustfix
#![allow(unused)]
struct T();
trait Trait {
type Assoc;
fn f();
}
impl Trait for () {
type Assoc = T;
fn f() {
T();
//~^ ERROR no associated item named `Assoc` found for unit type `()` in the current scope
}
}
fn main() {}

View file

@ -0,0 +1,22 @@
// issue: <https://github.com/rust-lang/rust/issues/142473>
//
//@ run-rustfix
#![allow(unused)]
struct T();
trait Trait {
type Assoc;
fn f();
}
impl Trait for () {
type Assoc = T;
fn f() {
<Self>::Assoc();
//~^ ERROR no associated item named `Assoc` found for unit type `()` in the current scope
}
}
fn main() {}

View file

@ -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 | <Self>::Assoc();
| ^^^^^ associated item not found in `()`
|
help: to construct a value of type `T`, use the explicit path
|
LL - <Self>::Assoc();
LL + T();
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0599`.