rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.14k stars 1.58k forks source link

A false positive error diagnostic when using experimental diagnostics #17336

Open komar007 opened 4 months ago

komar007 commented 4 months ago

This happens only with experimental diagnostics enabled, but I thought I'd post it in case it helps in any way. Here is a rather minimal reproduction of a false error in the assertion at the very bottom of the code. The code compiles without errors, clippy doesn't complain.

rust-analyzer version: rust-analyzer 1.76.0 (07dca48 2024-02-04)

rustc version: rustc 1.76.0 (07dca489a 2024-02-04)

editor or extension: neovim

relevant settings: rust-analyzer.diagnostics.experimental.enable

code snippet to reproduce:

trait Tr {
    type Output;
    fn f(self) -> (Self::Output, impl Tr);
}

struct S<T, N: Tr> {
    x: T,
    n: N,
}

impl<T, N: Tr> Tr for S<T, N> {
    type Output = T;

    fn f(self) -> (Self::Output, N) {
        (self.x, self.n)
    }
}

struct E {}

impl Tr for E {
    type Output = ();

    fn f(self) -> (Self::Output, impl Tr) {
        ((), E {})
    }
}

fn main() {
    let s = S {
        x: 10,
        n: S { x: true, n: E {} },
    };
    let (_, s) = s.f();
    let (x, _) = s.f();
    assert!(x); // rust-analyzer says: expected bool, found <<impl Tr as Tr>::Output as Not>::Output
}

It looks like it doesn't recognize the assert! macro as matching x, which is bool in this case. Also if you check the type of x, it says <impl Tr as Tr>::Output, which is technically correct, but not very useful in this case.

Veykril commented 4 months ago

Please update your rust-analyzer to the latest version, the diagnostic is silenced in that case (we still fail to normalize the output here)

komar007 commented 4 months ago

@Veykril thanks a lot, it's solved indeed.

Why check the latest version if you can spend an hour trying to find a minimal reproduction... It was fun anyway:)