rust-lang / rust-analyzer

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

non-exhaustive pattern false positive #17031

Open avandecreme opened 7 months ago

avandecreme commented 7 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 5 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 5 months ago

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

ShoyuVanilla commented 5 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 5 months ago

My test case is generated by proptest_derive, not arbitrary.

Thanks for looking into it!

ShoyuVanilla commented 5 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 4 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.

edwin0cheng commented 1 week ago

I see the same error above these days too:

thread 'Worker' panicked at crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs:407:21:
internal error: entered unreachable code: Unexpected type for `Struct` constructor: AssocTypeId(5805)<[?0 := 1<[?0 := AssocTypeId(5702)<[?0 := AdtId(StructId(StructId(8412)))<[]>]>]>]>

After this error occurred, I have to reload vscode.

ShoyuVanilla commented 1 week ago

I see the same error above these days too:

thread 'Worker' panicked at crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs:407:21: internal error: entered unreachable code: Unexpected type for Struct constructor: AssocTypeId(5805)<[?0 := 1<[?0 := AssocTypeId(5702)<[?0 := AdtId(StructId(StructId(8412)))<[]>]>]>]>

After this error occurred, I have to reload vscode.

I think that this occurs when opening certain file. Could you let me know which file or project triggers this if you're working on a public codebase?

edwin0cheng commented 1 week ago

I think that this occurs when opening certain file. Could you let me know which file or project triggers this if you're working on a public codebase?

Sorry, it is a private codebase and I am quite busy to find a MRE now. I just wanted to leave a comment here to show that this bug still occurs. :)

ShoyuVanilla commented 1 week ago

Sorry, it is a private codebase and I am quite busy to find a MRE now. I just wanted to leave a comment here to show that this bug still occurs. :)

No worries. At least the error message you wrote gives some information πŸ˜„