rust-lang / rust-lang/rust

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

Open
#118,113 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coercions C-bug T-compiler T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in src/main.rs with the reported rustc --version --verbose details, and review rustc --explain E0308 for context on the diagnostic. Trace the compiler handling of the let pattern, ref binding, and panic!() expression; done means the example compiles without the mismatched-types error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.