Skip to content

Bad error message on returning match with incompatible match arms #24568

Closed
@Binero

Description

@Binero

When returning the result of a match expression with incompatible arms, rust gives a rather obscure error message:

fn main() {
    is_one(1)
}

fn is_one(a: u8) {
    match a {
        1 => true,
        _ => panic!("It's not one!"),
    }
}
<anon>:6:5: 9:6 error: match arms have incompatible types:
 expected `bool`,
    found `()`
(expected bool,
    found ()) [E0308]
<anon>:6     match a {
<anon>:7         1 => true,
<anon>:8         _ => panic!("It's not one!"),
<anon>:9     }
<anon>:8:14: 8:38 note: match arm with an incompatible type
<anon>:8         _ => panic!("It's not one!"),
                      ^~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101
Program ended.

http://is.gd/J3qEpq

This is likely to be because panic! takes on the return type of the function, which is (), and than complains that it doesn't match the first arm's return type. This error message makes sense, but left me quite puzzled figuring out what was wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions