Reject empty matches on inhabited types

Closes #3096
This commit is contained in:
Tim Chevalier
2012-08-15 14:28:52 -07:00
parent a83414b6e8
commit c0140f5c34
3 changed files with 18 additions and 3 deletions

View File

@@ -4,6 +4,7 @@ import const_eval::{eval_const_expr, const_val, const_int,
compare_const_vals}; compare_const_vals};
import syntax::codemap::span; import syntax::codemap::span;
import syntax::print::pprust::pat_to_str; import syntax::print::pprust::pat_to_str;
import util::ppaux::ty_to_str;
import pat_util::*; import pat_util::*;
import syntax::visit; import syntax::visit;
import driver::session::session; import driver::session::session;
@@ -29,10 +30,15 @@ fn check_expr(tcx: ty::ctxt, ex: @expr, &&s: (), v: visit::vt<()>) {
// Check for empty enum, because is_useful only works on inhabited // Check for empty enum, because is_useful only works on inhabited
// types. // types.
let pat_ty = node_id_to_type(tcx, scrut.id); let pat_ty = node_id_to_type(tcx, scrut.id);
if type_is_empty(tcx, pat_ty) && arms.is_empty() { if arms.is_empty() {
// Vacuously exhaustive if !type_is_empty(tcx, pat_ty) {
return; // We know the type is inhabited, so this must be wrong
tcx.sess.span_err(ex.span, #fmt("non-exhaustive patterns: \
type %s is non-empty", ty_to_str(tcx, pat_ty)));
} }
// If the type *is* empty, it's vacuously exhaustive
return;
}
match ty::get(pat_ty).struct { match ty::get(pat_ty).struct {
ty_enum(did, _) => { ty_enum(did, _) => {
if (*enum_variants(tcx, did)).is_empty() && arms.is_empty() { if (*enum_variants(tcx, did)).is_empty() && arms.is_empty() {

View File

@@ -0,0 +1,3 @@
fn main() {
match () { } //~ ERROR non-exhaustive
}

View File

@@ -0,0 +1,6 @@
enum bottom { }
fn main() {
let x = ptr::addr_of(()) as *bottom;
match x { } //~ ERROR non-exhaustive patterns
}