Skip to content

Commit c0140f5

Browse files
committed
Reject empty matches on inhabited types
Closes #3096
1 parent a83414b commit c0140f5

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/rustc/middle/check_alt.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import const_eval::{eval_const_expr, const_val, const_int,
44
compare_const_vals};
55
import syntax::codemap::span;
66
import syntax::print::pprust::pat_to_str;
7+
import util::ppaux::ty_to_str;
78
import pat_util::*;
89
import syntax::visit;
910
import driver::session::session;
@@ -29,10 +30,15 @@ fn check_expr(tcx: ty::ctxt, ex: @expr, &&s: (), v: visit::vt<()>) {
2930
// Check for empty enum, because is_useful only works on inhabited
3031
// types.
3132
let pat_ty = node_id_to_type(tcx, scrut.id);
32-
if type_is_empty(tcx, pat_ty) && arms.is_empty() {
33-
// Vacuously exhaustive
34-
return;
33+
if arms.is_empty() {
34+
if !type_is_empty(tcx, pat_ty) {
35+
// We know the type is inhabited, so this must be wrong
36+
tcx.sess.span_err(ex.span, #fmt("non-exhaustive patterns: \
37+
type %s is non-empty", ty_to_str(tcx, pat_ty)));
3538
}
39+
// If the type *is* empty, it's vacuously exhaustive
40+
return;
41+
}
3642
match ty::get(pat_ty).struct {
3743
ty_enum(did, _) => {
3844
if (*enum_variants(tcx, did)).is_empty() && arms.is_empty() {

src/test/compile-fail/issue-3096-1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
match () { } //~ ERROR non-exhaustive
3+
}

src/test/compile-fail/issue-3096-2.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
enum bottom { }
2+
3+
fn main() {
4+
let x = ptr::addr_of(()) as *bottom;
5+
match x { } //~ ERROR non-exhaustive patterns
6+
}

0 commit comments

Comments
 (0)