Skip to content

Using let pattern with ref results in 'mismatched types' when combined with 'panic!()' #118113

@Aaron1011

Description

@Aaron1011

I tried this code:

struct Foo {
    bar: u8
}

fn main() {
    let Foo { ref bar } = panic!();
}

I expected to see this happen: The code should compile successfully by coercing the ! from panic!() to Foo.

Instead, this happened: rustc produces the following error:

error[E0308]: mismatched types
 --> src/main.rs:6:9
  |
6 |     let Foo { ref bar } = panic!();
  |         ^^^^^^^^^^^^^^^   -------- this expression has type `!`
  |         |
  |         expected `!`, found `Foo`
  |
  = note: expected type `!`
           found struct `Foo`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (bin "playground") due to previous error

The code compiles successfully if the ref keyword is removed.

Meta

rustc --version --verbose:

rustc 1.76.0-nightly (2b603f95a 2023-11-12)
binary: rustc
commit-hash: 2b603f95a48f10f931a61dd208fe3e5ffd64e491
commit-date: 2023-11-12
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions