Skip to content

E0308 doesn't provide a help message in loops when using let-else #142602

Open
@ada4a

Description

@ada4a

Code

pub fn main() {
    let a = loop {
        if true {
            break;
        }
        let Some(_) = Some(5) else {
            break 3;
        };
    };
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:7:19
  |
7 |             break 3;
  |                   ^ expected `()`, found integer

For more information about this error, try `rustc --explain E0308`.

Desired output

error[E0308]: mismatched types
 --> src/lib.rs:9:19
  |
4 |             break;
  |             ----- expected because of this `break`
...
7 |             break 3;
  |                   ^ expected `()`, found integer

For more information about this error, try `rustc --explain E0308`.

Rationale and extra context

Usually, the error comes with a nice help message showing why another type was expected. But when using let-else, no help message is shown.

PS: in all repros, replacing if true { break; } with just break; works just as well -- but it also causes an unreachable_code warning, which I felt like muddied the waters.

Other cases

// the following 3 changes bring the help message back:

// replacing `let-else` with logically identical `if-let`
pub fn main() {
    let a = loop {
        if true {
            break;
        }
        if let Some(_n) = Some(5) {
            //
        } else {
            break 3;
        };
    };
}

// replacing `let-else` with `if`
pub fn main() {
    let a = loop {
        if true {
            break;
        }
        if true {
            break 3;
        }
    };
}

// having `let-else` contain the _first_ `break` of the loop
pub fn main() {
    let a = loop {
        let Some(_) = Some(5) else {
            break;
        };
        if true {
            break 3;
        }
    };
}

// having two `let-else`s doesn't work though
pub fn main() {
    let a = loop {
        let Some(_) = Some(5) else {
            break;
        };
        let Some(_) = Some(4) else {
            break 3;
        };
    };
}

Rust Version

rustc 1.86.0 (05f9846f8 2025-03-31) (built from a source tarball)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Anything else?

All the repros were tested on Rust Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.F-let_elseIssues related to let-else statements (RFC 3137)T-compilerRelevant to the compiler 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