Check pattern refutability the same way exhaustiveness is checked

This commit is contained in:
Jakub Wieczorek 2014-05-31 15:13:46 +02:00
parent 6750eb5a05
commit f5e513b2b2
3 changed files with 44 additions and 95 deletions

View File

@ -750,12 +750,15 @@ fn check_local(cx: &mut MatchCheckCtxt, loc: &Local) {
LocalFor => "`for` loop" LocalFor => "`for` loop"
}; };
let mut spans = vec![]; match is_refutable(cx, loc.pat) {
find_refutable(cx, &*loc.pat, &mut spans); Some(pat) => {
let msg = format!(
for span in spans.iter() { "refutable pattern in {} binding: {} not covered",
cx.tcx.sess.span_err(*span, name, pat_to_str(&*pat)
format!("refutable pattern in {} binding", name).as_slice()); );
cx.tcx.sess.span_err(loc.pat.span, msg.as_slice());
},
None => ()
} }
// Check legality of move bindings. // Check legality of move bindings.
@ -769,67 +772,27 @@ fn check_fn(cx: &mut MatchCheckCtxt,
sp: Span) { sp: Span) {
visit::walk_fn(cx, kind, decl, body, sp, ()); visit::walk_fn(cx, kind, decl, body, sp, ());
for input in decl.inputs.iter() { for input in decl.inputs.iter() {
let mut spans = vec![]; match is_refutable(cx, input.pat) {
find_refutable(cx, &*input.pat, &mut spans); Some(pat) => {
let msg = format!(
for span in spans.iter() { "refutable pattern in function argument: {} not covered",
cx.tcx.sess.span_err(*span, pat_to_str(&*pat)
"refutable pattern in function argument"); );
cx.tcx.sess.span_err(input.pat.span, msg.as_slice());
},
None => ()
} }
} }
} }
fn find_refutable(cx: &MatchCheckCtxt, pat: &Pat, spans: &mut Vec<Span>) { fn is_refutable(cx: &MatchCheckCtxt, pat: Gc<Pat>) -> Option<Gc<Pat>> {
macro_rules! this_pattern { let pats = vec!(vec!(pat));
() => { is_useful(cx, &pats, [wild()])
{ .useful()
spans.push(pat.span); .map(|pats| {
return assert_eq!(pats.len(), 1);
} pats.get(0).clone()
} })
}
let opt_def = cx.tcx.def_map.borrow().find_copy(&pat.id);
match opt_def {
Some(DefVariant(enum_id, _, _)) => {
if ty::enum_variants(cx.tcx, enum_id).len() != 1u {
this_pattern!()
}
}
Some(DefStatic(..)) => this_pattern!(),
_ => ()
}
match pat.node {
PatBox(ref sub) | PatRegion(ref sub) | PatIdent(_, _, Some(ref sub)) => {
find_refutable(cx, &**sub, spans)
}
PatWild | PatWildMulti | PatIdent(_, _, None) => {}
PatLit(lit) => {
match lit.node {
ExprLit(lit) => {
match lit.node {
LitNil => {} // `()`
_ => this_pattern!(),
}
}
_ => this_pattern!(),
}
}
PatRange(_, _) => { this_pattern!() }
PatStruct(_, ref fields, _) => {
for f in fields.iter() {
find_refutable(cx, &*f.pat, spans);
}
}
PatTup(ref elts) | PatEnum(_, Some(ref elts))=> {
for elt in elts.iter() {
find_refutable(cx, &**elt, spans)
}
}
PatEnum(_,_) => {}
PatVec(..) => { this_pattern!() }
PatMac(_) => cx.tcx.sess.bug("unexpanded macro"),
}
} }
// Legality of move bindings checking // Legality of move bindings checking

View File

@ -1,32 +0,0 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn func(
(
1, //~ ERROR refutable pattern in function argument
(
Some( //~ ERROR refutable pattern in function argument
1), // nested, so no warning.
2..3 //~ ERROR refutable pattern in function argument
)
): (int, (Option<int>, int))
) {}
fn main() {
let (
1, //~ ERROR refutable pattern in local binding
(
Some( //~ ERROR refutable pattern in local binding
1), // nested, so no warning.
2..3 //~ ERROR refutable pattern in local binding
)
) = (1, (None, 2));
}

View File

@ -0,0 +1,18 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn func((1, (Some(1), 2..3)): (int, (Option<int>, int))) { }
//~^ ERROR refutable pattern in function argument
fn main() {
let (1, (Some(1), 2..3)) = (1, (None, 2));
//~^ ERROR refutable pattern in local binding
}