Empty match expressions can be ill-typed. · Issue #24590 · rust-lang/rust (original) (raw)

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: