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

non-exhaustive pattern false positive #17031

Open avandecreme opened 5 months ago

avandecreme commented 5 months ago

rust-analyzer version: v0.3.1916 (taken from the VSCode plugin page, the given command does nothing on my machine)

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

editor or extension: VSCode with rust-analyzer v0.3.1916

relevant settings: NA

repository link (if public, optional): NA

code snippet to reproduce:

pub trait Foo {
    type Bar;
    fn foo(bar: Self::Bar);
}

pub struct FooImpl;

const foo_impl: () = {
    impl Foo for FooImpl {
        type Bar = ();

        fn foo(_bar: Self::Bar) {
            let () = _bar; // non-exhaustive pattern: `_` not covered reported here
        }
    }
};

This code compiles fine with rustc.

Any of the following changes make the error go away:

  1. Renaming _bar by bar
  2. Replacing fn foo(_bar: Self::Bar) by fn foo(_bar: ())
  3. Removing the const foo_impl: () = {...} wrapper

The original issue for me is when using proptest_derive, I get the error with this code:

#[derive(Arbitrary)]
pub enum Foo {
    Bar,
    Baz
}

The generated code looks similar to the repro above.

ShoyuVanilla commented 3 months ago

I just started looking into this and got

image

---- handlers::non_exhaustive_let::tests::regression_issue_17031 stdout ----
thread 'handlers::non_exhaustive_let::tests::regression_issue_17031' panicked at crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs:393:21:
internal error: entered unreachable code: Unexpected type for `Struct` constructor: AssocTypeId(0)<[?0 := AdtId(StructId(StructId(0)))<[]>]>
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

interesting. Seems like a projection issue

Veykril commented 3 months ago

Does arbitrary still emit named const items?🤨 They should really switch to const _ so we can support it properly

ShoyuVanilla commented 3 months ago

It seems not have been so for 2 years 🤔

https://github.com/rust-fuzz/arbitrary/blame/main/derive/src/lib.rs#L57

avandecreme commented 3 months ago

My test case is generated by proptest_derive, not arbitrary.

Thanks for looking into it!

ShoyuVanilla commented 3 months ago

proptest has been fixed recently 👍 https://github.com/proptest-rs/proptest/commit/b1be99d2c3225325ef08072ef5f23f231e75a5fa#diff-1e85e98bbdbda751a72c78db697f0e85029d9600354a12d075e081a284f68816L121 Anyway, I'll try fixing the cases like in the example code of this issue

ibraheemdev commented 2 months ago

Not sure if something changed recently but I'm getting thread 'Worker' panicked at crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs:401:21:\ninternal error: entered unreachable code: Unexpected type forStructconstructor: AssocTypeId(8088) very consistently immediately after opening certain files.

It also seems to happen if I pause while typing a for (a, b statement. Will try to reproduce.