Skip to content

Empty match expressions can be ill-typed. #24590

@michaelsproul

Description

@michaelsproul

I've been playing around with empty match expressions, which are only allowed if the type being matched on is empty, and I've noticed that it's possible to compile (seemingly) ill-typed code like the following:

enum Empty {}

fn main() {
    let x: &Empty = unsafe {
        &*(0 as *const Empty)
    };

    let y: u32 = match *x {};
    println!("{}", y);
}

I would have expected this not to compile as match *x {} can only sensibly evaluate to (), yet y has type u32. The compiler only reports that the variable y is unused.

$ rustc empty.rs
empty.rs:8:9: 8:10 warning: unused variable: `y`, #[warn(unused_variables)] on by default
empty.rs:8     let y: u32 = match *x {};

Running it on Linux x86_64 results in a segfault followed by an illegal instruction.

Some solutions I see are:

  • Ensure empty match expressions have type unit ().
  • Disallow empty match expressions entirely (although they could be useful in generated code).
  • Do nothing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions