Skip to content

Error when rustc reports none, related to associated types on supertraits/trait aliases #13410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Artemis21 opened this issue Oct 13, 2022 · 4 comments
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug

Comments

@Artemis21
Copy link

Artemis21 commented Oct 13, 2022

rust-analyzer version: 0.3.1238-standalone and also 1.66.0-nightly (a6b7274 2022-10-10)

rustc version: rustc 1.66.0-nightly (a6b7274a4 2022-10-10)

#![feature(trait_alias)]
trait Foo {
    type A;

    fn foo(&self, _: impl Fn() -> Self::A) {}
}

impl Foo for () {
    type A = ();
}

trait EmptyFoo = Foo<A = ()>;

fn empty() -> impl EmptyFoo {}

fn main() {
    empty().foo(|| ());
}

This produces an error on the penultimate line in rust analyzer: expected <impl EmptyFoo as Foo>::A, found (). This is confusing, since the associated type A of trait Foo for any type implementing EmptyFoo is (). It conflicts with rustc, which compiles and runs the code without error.

If the parameter type for foo is just Self::A (and we pass () as the parameter), there is no error.

@Artemis21
Copy link
Author

trait Foo {
    type A;

    fn foo(&self, _: impl Fn() -> Self::A) {}
}

trait EmptyFoo: Foo<A = ()> {}

impl<T: Foo<A = ()>> EmptyFoo for T {}

impl Foo for () {
    type A = ();
}

fn empty() -> impl EmptyFoo {}

fn main() {
    empty().foo(|| ());
}

The same issue occurs without the unstable trait aliases feature, so it affects stable rust.

@Artemis21 Artemis21 changed the title Error when rustc reports none, related to associated types on trait aliases Error when rustc reports none, related to associated types on supertraits/trait aliases Oct 15, 2022
@Veykril Veykril added A-ty type system / type inference / traits / method resolution C-bug Category: bug labels Oct 15, 2022
@lowr
Copy link
Contributor

lowr commented Oct 16, 2022

We haven't implemented trait aliases properly (#2773). #2773 (comment) describes how we currently handle them, but it differs from the correct semantics.

That said, we should be able to deduce the associated type in both snippets (even with our current handling of trait aliases). I think this is on chalk but need to investigate further.

@Artemis21
Copy link
Author

Alright, I'll see if I can try this with chalk directly. (See the second snippet that doesn't use trait aliases.)

@sethp
Copy link
Contributor

sethp commented Apr 13, 2023

I'm seeing something similar, I think, with the latest nightly: rustc 1.70.0-nightly (4087deacc 2023-04-12)

    fn no_alias() -> impl nom::Parser<(), (), ()> {
        |_| todo!()
    }

    trait UnitParserAlias = nom::Parser<(), (), ()>;
    fn alias() -> impl UnitParserAlias {
        |_| todo!()
    }

    let mut ok = no_alias();
    ok.parse(());

    let mut nope = alias();
    nope.parse(()); // this `parse` method compiles fine, but isn't found by rust-analyzer

a screenshot of the code snippet in vscode, with an underline underneath ok's .parse but none for nope's .parse

Notice how rust-analyzer can't tell that parse is an &mut method in the second example (there's no underline); it also doesn't auto-complete or display documentation on hover or offer go-to-definition.

Explicitly using a supertrait as suggested by the previous implementation seems to avoid the analysis failure:

    trait UnitSuperParser: nom::Parser<(), (), ()> {}
    impl<T> UnitSuperParser for T where T: nom::Parser<(), (), ()> {}
    fn super_trait() -> impl UnitSuperParser {
        |_| todo!()
    }
    let mut ok2 = super_trait();
    ok2.parse(());

a screenshot of the second code snippet; parse is underlined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ty type system / type inference / traits / method resolution C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

4 participants