Rust-analyzer seems to suggest bad syntax for associated type

If I put the following in src/lib.rs

pub trait Foo {
    type Bar: Default;
}

pub fn quux<T: Foo>() -> T::Bar {
    let y = Default::default();

    y
}

and open the project directory in Visual Studio Code (1.47.2), rust-analyzer (0.2.336, installed from the marketplace) annotates y with the type

Foo::Bar<T>

which seems wrong to me -- surely the correct syntax is <T as Foo>::Bar? If I click to insert the suggested type annotation, rustc indeed complains:

error[E0223]: ambiguous associated type
 --> src/lib.rs:6:12
  |
6 |     let y: Foo::Bar<T> = Default::default();
  |            ^^^^^^^^^^^ help: use fully-qualified syntax: `<Type as Foo>::Bar`

Is this a known issue? Am I missing something?

Sounds weird. I would check the issue tracker of the project.

1 Like

I didn't see any obvious duplicates in the tracker so I've opened https://github.com/rust-analyzer/rust-analyzer/issues/6191. Thanks for the sanity check!

ETA: a maintainer has confirmed the issue and outlined a fix, so I'm going to mark this as solved.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.