Skip to content

Wrong type deduced when implementing same method for different types #3144

@x-hgg-x

Description

@x-hgg-x

Rust-analyzer deduces the wrong type when for a given generic type, there are multiple implementations of a same method with the same signature.

pub struct Wrapper<T>(T);
pub struct Foo<T>(T);
pub struct Bar<T>(T);

impl<T> Wrapper<Foo<T>> {
    pub fn new(foo_: T) -> Self {
        Wrapper(Foo(foo_))
    }
}

impl<T> Wrapper<Bar<T>> {
    pub fn new(bar_: T) -> Self {
        Wrapper(Bar(bar_))
    }
}

fn main() {
    let _a = Wrapper::<Foo<f32>>::new(1.0);
    let _b = Wrapper::<Bar<f32>>::new(1.0);
}

In this example, type of _b is incorreclty deduced to Wrapper::<Foo<f64>> even with the turbofish type hinting.

Sans titre

I can add myself the type of _b, but the argument inlay hint for the new method will still be incorrect (displaying foo_ instead of bar_, and the "Go to definition" functionality will still go the the wrong definition.

It seems that rust-analyzer looks only at the first definition of the new method, even if there are better matches below.

I'm using the latest release of rust-analyzer (2020-02-11) with the latest version of the vscode extension.

I have first encountered this problem when using the method palette::rgb::Srgba::new from the palette crate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions