Skip to content

let else: wrong diagnostic span on return type mismatch #96961

Closed

Description

The following code [playground]

#![feature(let_else)]

fn foo() -> u32 {
    println!("foo");

    let () = () else {
        return 12;
    };

    println!("foo");
}

fails to build with the following:

error[E0308]: mismatched types
 --> src/lib.rs:6:5
  |
6 | /     let () = () else {
7 | |         return 12;
8 | |     };
  | |______^ expected `u32`, found `()`

note the span points to just the let .. else, but there's nothing wrong with it, the problem is the function return type doesn't match. I'd have expected the error to look the same as if the let else wasn't there, which is:

error[E0308]: mismatched types
 --> src/lib.rs:3:13
  |
3 | fn foo() -> u32 {
  |    ---      ^^^ expected `u32`, found `()`
  |    |
  |    implicitly returns `()` as its body has no tail or `return` expression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsF-let_elseIssues related to let-else statements (RFC 3137)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions