Skip to content

Poor diagnostics on missing semicolon after match statement #72634

Closed

Description

I tried this code (playground):

fn stuff() -> i32 {
    match 1i32 {
        2i32 => 3i32,
        _ => panic!("wrong"),
    }
    4i32
}

I expected to see this happen:
A compiler error about missing ; between } and 4i32

Instead, this happened:

error[E0308]: `match` arms have incompatible types
 --> src/lib.rs:4:14
  |
2 | /     match 1i32 {
3 | |         2i32 => 3i32,
  | |                 ---- this is found to be of type `i32`
4 | |         _ => panic!("wrong"),
  | |              ^^^^^^^^^^^^^^^ expected `i32`, found `()`
5 | |     }
  | |_____- `match` arms have incompatible types
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

This is very confusing, because I "know" that panic!() return type is ! which is coercible to anything else (or whatever is the technical explanation for "we can really ignore the return type of panic!"), so it should not ever be present in error messages about mistyping stuff.

Meta

rustc --version --verbose:

rustc 1.44.0-beta.3 (0f0d70055 2020-05-11)
binary: rustc
commit-hash: 0f0d70055530bdbf3f0acb7b8ad25aa4a6ad8ea9
commit-date: 2020-05-11
host: x86_64-pc-windows-msvc
release: 1.44.0-beta.3
LLVM version: 9.0

This issue is also present in 1.34.2 (from a year ago), and in two-week-old nightly (rustc 1.45.0-nightly (99cb9cc 2020-05-11))

p.s.: When deminimizing the issue, i realized that it's even more confusing when match is the last statement in a for loop (as it was in my real code), i.e.:

fn stuff2(){
    for _ in 0..5{
        match 1i32 {
            2i32 => 3i32,
            _ => panic!("wrong"),
        }
    }
}

gives the same error message about incompatible match arms.

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 lintsA-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.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