Provide a witness pattern for non-exhaustive patterns

Fixed #4321
This commit is contained in:
Jakub Wieczorek 2014-06-07 14:17:01 +02:00
parent f5e513b2b2
commit 34407dcdbb
7 changed files with 502 additions and 552 deletions

View File

@ -10,23 +10,57 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use middle::const_eval::{compare_const_vals, lookup_const_by_id}; use middle::const_eval::{compare_const_vals, const_bool, const_float, const_val};
use middle::const_eval::{eval_const_expr, const_val, const_bool, const_float}; use middle::const_eval::{eval_const_expr, lookup_const_by_id};
use middle::def::*; use middle::def::*;
use middle::pat_util::*; use middle::pat_util::*;
use middle::ty::*; use middle::ty::*;
use middle::ty; use middle::ty;
use util::ppaux::ty_to_str;
use std::cmp;
use std::gc::{Gc, GC}; use std::gc::{Gc, GC};
use std::iter; use std::iter;
use syntax::ast::*; use syntax::ast::*;
use syntax::ast_util::{is_unguarded, walk_pat}; use syntax::ast_util::{is_unguarded, walk_pat};
use syntax::codemap::{DUMMY_SP, Span}; use syntax::codemap::{Span, Spanned, DUMMY_SP};
use syntax::parse::token; use syntax::owned_slice::OwnedSlice;
use syntax::print::pprust::pat_to_str;
use syntax::visit; use syntax::visit;
use syntax::visit::{Visitor, FnKind}; use syntax::visit::{Visitor, FnKind};
use util::ppaux::ty_to_str;
type Matrix = Vec<Vec<Gc<Pat>>>;
#[deriving(Clone)]
enum Usefulness {
Useful(Vec<Gc<Pat>>),
NotUseful
}
enum WitnessPreference {
ConstructWitness,
LeaveOutWitness
}
impl Usefulness {
fn useful(self) -> Option<Vec<Gc<Pat>>> {
match self {
Useful(pats) => Some(pats),
_ => None
}
}
}
fn def_to_path(tcx: &ty::ctxt, id: DefId) -> Path {
ty::with_path(tcx, id, |path| Path {
global: false,
segments: path.map(|elem| PathSegment {
identifier: Ident::new(elem.name()),
lifetimes: vec!(),
types: OwnedSlice::empty()
}).collect(),
span: DUMMY_SP,
})
}
struct MatchCheckCtxt<'a> { struct MatchCheckCtxt<'a> {
tcx: &'a ty::ctxt, tcx: &'a ty::ctxt,
@ -81,14 +115,14 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &Expr) {
// If the type *is* empty, it's vacuously exhaustive // If the type *is* empty, it's vacuously exhaustive
return; return;
} }
let m: matrix = arms let m: Matrix = arms
.iter() .iter()
.filter(|&arm| is_unguarded(arm)) .filter(|&arm| is_unguarded(arm))
.flat_map(|arm| arm.pats.iter()) .flat_map(|arm| arm.pats.iter())
.map(|pat| vec!(pat.clone())) .map(|pat| vec!(pat.clone()))
.collect(); .collect();
check_exhaustive(cx, ex.span, &m); check_exhaustive(cx, ex.span, &m);
} },
_ => () _ => ()
} }
} }
@ -98,7 +132,6 @@ fn check_arms(cx: &MatchCheckCtxt, arms: &[Arm]) {
let mut seen = Vec::new(); let mut seen = Vec::new();
for arm in arms.iter() { for arm in arms.iter() {
for pat in arm.pats.iter() { for pat in arm.pats.iter() {
// Check that we do not match against a static NaN (#6804) // Check that we do not match against a static NaN (#6804)
let pat_matches_nan: |&Pat| -> bool = |p| { let pat_matches_nan: |&Pat| -> bool = |p| {
let opt_def = cx.tcx.def_map.borrow().find_copy(&p.id); let opt_def = cx.tcx.def_map.borrow().find_copy(&p.id);
@ -123,10 +156,8 @@ fn check_arms(cx: &MatchCheckCtxt, arms: &[Arm]) {
}); });
let v = vec!(*pat); let v = vec!(*pat);
match is_useful(cx, &seen, v.as_slice()) { match is_useful(cx, &seen, v.as_slice(), LeaveOutWitness) {
not_useful => { NotUseful => cx.tcx.sess.span_err(pat.span, "unreachable pattern"),
cx.tcx.sess.span_err(pat.span, "unreachable pattern");
}
_ => () _ => ()
} }
if arm.guard.is_none() { seen.push(v); } if arm.guard.is_none() { seen.push(v); }
@ -141,66 +172,22 @@ fn raw_pat(p: Gc<Pat>) -> Gc<Pat> {
} }
} }
fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, m: &matrix) { fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, m: &Matrix) {
let ext = match is_useful(cx, m, [wild()]) { match is_useful(cx, m, [wild()], ConstructWitness) {
not_useful => { NotUseful => {
// This is good, wildcard pattern isn't reachable // This is good, wildcard pattern isn't reachable
return; return;
} }
useful_ => None, Useful(pats) => {
useful(ty, ref ctor) => { let witness = match pats.as_slice() {
match ty::get(ty).sty { [ref witness] => witness.clone(),
ty::ty_bool => { [] => wild(),
match *ctor { _ => unreachable!()
val(const_bool(true)) => Some("true".to_string()),
val(const_bool(false)) => Some("false".to_string()),
_ => None
}
}
ty::ty_enum(id, _) => {
let vid = match *ctor {
variant(id) => id,
_ => fail!("check_exhaustive: non-variant ctor"),
}; };
let variants = ty::enum_variants(cx.tcx, id); let msg = format!("non-exhaustive patterns: {0} not covered", pat_to_str(&*witness));
match variants.iter().find(|v| v.id == vid) {
Some(v) => {
Some(token::get_ident(v.name).get()
.to_str()
.into_string())
}
None => {
fail!("check_exhaustive: bad variant in ctor")
}
}
}
ty::ty_vec(..) | ty::ty_rptr(..) => {
match *ctor {
vec(n) => {
Some(format!("vectors of length {}", n))
}
_ => None
}
}
_ => None
}
}
};
let msg = format!("non-exhaustive patterns{}", match ext {
Some(ref s) => format!(": {} not covered", *s),
None => "".to_string()
});
cx.tcx.sess.span_err(sp, msg.as_slice()); cx.tcx.sess.span_err(sp, msg.as_slice());
} }
}
type matrix = Vec<Vec<Gc<Pat>>>;
#[deriving(Clone)]
enum useful {
useful(ty::t, ctor),
useful_,
not_useful,
} }
#[deriving(Clone, PartialEq)] #[deriving(Clone, PartialEq)]
@ -212,6 +199,129 @@ enum ctor {
vec(uint) vec(uint)
} }
fn const_val_to_expr(value: &const_val) -> Gc<Expr> {
let node = match value {
&const_bool(b) => LitBool(b),
_ => unreachable!()
};
box(GC) Expr {
id: 0,
node: ExprLit(box(GC) Spanned { node: node, span: DUMMY_SP }),
span: DUMMY_SP
}
}
fn construct_witness(cx: &MatchCheckCtxt, ctor: &ctor, pats: Vec<Gc<Pat>>, lty: ty::t) -> Gc<Pat> {
let pat = match ty::get(lty).sty {
ty::ty_tup(_) => PatTup(pats),
ty::ty_enum(_, _) => {
let vid = match ctor {
&variant(vid) => vid,
_ => unreachable!()
};
PatEnum(def_to_path(cx.tcx, vid), Some(pats))
},
ty::ty_struct(cid, _) => {
let fields = ty::lookup_struct_fields(cx.tcx, cid);
let field_pats = fields.move_iter()
.zip(pats.iter())
.map(|(field, pat)| FieldPat {
ident: Ident::new(field.name),
pat: pat.clone()
}).collect();
PatStruct(def_to_path(cx.tcx, cid), field_pats, false)
},
ty::ty_rptr(_, ty::mt { ty: ty, .. }) => {
match ty::get(ty).sty {
ty::ty_vec(_, None) => match ctor {
&vec(_) => PatVec(pats, None, vec!()),
_ => unreachable!()
},
_ => {
assert_eq!(pats.len(), 1);
PatRegion(pats.get(0).clone())
}
}
},
ty::ty_box(_) => {
assert_eq!(pats.len(), 1);
PatBox(pats.get(0).clone())
},
_ => {
match ctor {
&vec(_) => PatVec(pats, None, vec!()),
&val(ref v) => PatLit(const_val_to_expr(v)),
_ => PatWild
}
}
};
box(GC) Pat {
id: 0,
node: pat,
span: DUMMY_SP
}
}
fn missing_constructor(cx: &MatchCheckCtxt, m: &Matrix, left_ty: ty::t) -> Option<ctor> {
let used_constructors: Vec<ctor> = m.iter()
.filter_map(|r| pat_ctor_id(cx, left_ty, *r.get(0)))
.collect();
all_constructors(cx, m, left_ty)
.move_iter()
.find(|c| !used_constructors.contains(c))
}
fn all_constructors(cx: &MatchCheckCtxt, m: &Matrix, left_ty: ty::t) -> Vec<ctor> {
fn vec_constructors(m: &Matrix) -> Vec<ctor> {
let max_vec_len = m.iter().map(|r| match r.get(0).node {
PatVec(ref before, _, ref after) => before.len() + after.len(),
_ => 0u
}).max().unwrap_or(0u);
let contains_slice = m.iter().any(|r| match r.get(0).node {
PatVec(_, ref slice, _) => slice.is_some(),
_ => false
});
let lengths = iter::range_inclusive(0u, if contains_slice {
max_vec_len
} else {
max_vec_len + 1
});
lengths.map(|len| vec(len)).collect()
}
match ty::get(left_ty).sty {
ty::ty_bool =>
[true, false].iter().map(|b| val(const_bool(*b))).collect(),
ty::ty_rptr(_, ty::mt { ty: ty, .. }) => match ty::get(ty).sty {
ty::ty_vec(_, None) => vec_constructors(m),
_ => vec!(single)
},
ty::ty_enum(eid, _) =>
ty::enum_variants(cx.tcx, eid).iter().map(|va| variant(va.id)).collect(),
ty::ty_vec(_, None) =>
vec_constructors(m),
ty::ty_vec(_, Some(n)) =>
vec!(vec(n)),
ty::ty_nil if !m.iter().all(|r| is_wild(cx, *r.get(0))) =>
vec!(),
_ =>
vec!(single)
}
}
// Algorithm from http://moscova.inria.fr/~maranget/papers/warn/index.html // Algorithm from http://moscova.inria.fr/~maranget/papers/warn/index.html
// //
// Whether a vector `v` of patterns is 'useful' in relation to a set of such // Whether a vector `v` of patterns is 'useful' in relation to a set of such
@ -225,12 +335,13 @@ enum ctor {
// Note: is_useful doesn't work on empty types, as the paper notes. // Note: is_useful doesn't work on empty types, as the paper notes.
// So it assumes that v is non-empty. // So it assumes that v is non-empty.
fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[Gc<Pat>]) -> useful { fn is_useful(cx: &MatchCheckCtxt, m: &Matrix, v: &[Gc<Pat>],
witness: WitnessPreference) -> Usefulness {
if m.len() == 0u { if m.len() == 0u {
return useful_; return Useful(vec!());
} }
if m.get(0).len() == 0u { if m.get(0).len() == 0u {
return not_useful return NotUseful;
} }
let real_pat = match m.iter().find(|r| r.get(0).id != 0) { let real_pat = match m.iter().find(|r| r.get(0).id != 0) {
Some(r) => { Some(r) => {
@ -241,310 +352,146 @@ fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[Gc<Pat>]) -> useful {
_ => *r.get(0) _ => *r.get(0)
} }
} }
None if v.len() == 0 => return not_useful, None if v.len() == 0 => return NotUseful,
None => v[0] None => v[0]
}; };
let left_ty = if real_pat.id == 0 { ty::mk_nil() } let left_ty = if real_pat.id == 0 {
else { ty::node_id_to_type(cx.tcx, real_pat.id) }; ty::mk_nil()
} else {
ty::pat_ty(cx.tcx, &*real_pat)
};
match pat_ctor_id(cx, v[0]) { match pat_ctor_id(cx, left_ty, v[0]) {
None => match missing_constructor(cx, m, left_ty) {
None => { None => {
match missing_ctor(cx, m, left_ty) { all_constructors(cx, m, left_ty).move_iter().filter_map(|c| {
None => { is_useful_specialized(cx, m, v, c.clone(),
match ty::get(left_ty).sty { left_ty, witness).useful().map(|pats| {
ty::ty_bool => { Useful(match witness {
match is_useful_specialized(cx, m, v, ConstructWitness => {
val(const_bool(true)), let arity = constructor_arity(cx, &c, left_ty);
0u, left_ty){ let subpats = {
not_useful => { let pat_slice = pats.as_slice();
is_useful_specialized(cx, m, v, Vec::from_fn(arity, |i| {
val(const_bool(false)), pat_slice.get(i).map(|p| p.clone())
0u, left_ty) .unwrap_or_else(|| wild())
})
};
let mut result = vec!(construct_witness(cx, &c, subpats, left_ty));
result.extend(pats.move_iter().skip(arity));
result
} }
u => u, LeaveOutWitness => vec!()
})
})
}).nth(0).unwrap_or(NotUseful)
},
Some(ctor) => {
let matrix = &m.iter().filter_map(|r| default(cx, r.as_slice())).collect();
match is_useful(cx, matrix, v.tail(), witness) {
Useful(pats) => Useful(match witness {
ConstructWitness => {
let arity = constructor_arity(cx, &ctor, left_ty);
let wild_pats = Vec::from_elem(arity, wild());
let enum_pat = construct_witness(cx, &ctor, wild_pats, left_ty);
(vec!(enum_pat)).append(pats.as_slice())
} }
LeaveOutWitness => vec!()
}),
result => result
} }
ty::ty_enum(eid, _) => {
for va in (*ty::enum_variants(cx.tcx, eid)).iter() {
match is_useful_specialized(cx, m, v, variant(va.id),
va.args.len(), left_ty) {
not_useful => (),
u => return u,
}
}
not_useful
}
ty::ty_vec(_, Some(n)) => {
is_useful_specialized(cx, m, v, vec(n), n, left_ty)
}
ty::ty_vec(..) => fail!("impossible case"),
ty::ty_rptr(_, ty::mt{ty: ty, ..}) | ty::ty_uniq(ty) => match ty::get(ty).sty {
ty::ty_vec(_, None) => {
let max_len = m.iter().rev().fold(0, |max_len, r| {
match r.get(0).node {
PatVec(ref before, _, ref after) => {
cmp::max(before.len() + after.len(), max_len)
}
_ => max_len
}
});
for n in iter::range(0u, max_len + 1) {
match is_useful_specialized(cx, m, v, vec(n), n, left_ty) {
not_useful => (),
u => return u,
}
}
not_useful
}
_ => {
let arity = ctor_arity(cx, &single, left_ty);
is_useful_specialized(cx, m, v, single, arity, left_ty)
} }
}, },
_ => {
let arity = ctor_arity(cx, &single, left_ty); Some(v0_ctor) => is_useful_specialized(cx, m, v, v0_ctor, left_ty, witness)
is_useful_specialized(cx, m, v, single, arity, left_ty)
}
}
}
Some(ctor) => {
match is_useful(cx,
&m.iter().filter_map(|r| {
default(cx, r.as_slice())
}).collect::<matrix>(),
v.tail()) {
useful_ => useful(left_ty, ctor),
u => u,
}
}
}
}
Some(v0_ctor) => {
let arity = ctor_arity(cx, &v0_ctor, left_ty);
is_useful_specialized(cx, m, v, v0_ctor, arity, left_ty)
}
} }
} }
fn is_useful_specialized(cx: &MatchCheckCtxt, fn is_useful_specialized(cx: &MatchCheckCtxt, m: &Matrix, v: &[Gc<Pat>],
m: &matrix, ctor: ctor, lty: ty::t, witness: WitnessPreference) -> Usefulness {
v: &[Gc<Pat>], let arity = constructor_arity(cx, &ctor, lty);
ctor: ctor, let matrix = m.iter().filter_map(|r| {
arity: uint, specialize(cx, r.as_slice(), &ctor, arity)
lty: ty::t) }).collect();
-> useful { match specialize(cx, v, &ctor, arity) {
let ms = m.iter().filter_map(|r| { Some(v) => is_useful(cx, &matrix, v.as_slice(), witness),
specialize(cx, r.as_slice(), &ctor, arity, lty) None => NotUseful
}).collect::<matrix>();
let could_be_useful = match specialize(cx, v, &ctor, arity, lty) {
Some(v) => is_useful(cx, &ms, v.as_slice()),
None => return not_useful,
};
match could_be_useful {
useful_ => useful(lty, ctor),
u => u,
} }
} }
fn pat_ctor_id(cx: &MatchCheckCtxt, p: Gc<Pat>) -> Option<ctor> { fn pat_ctor_id(cx: &MatchCheckCtxt, left_ty: ty::t, p: Gc<Pat>) -> Option<ctor> {
let pat = raw_pat(p); let pat = raw_pat(p);
match pat.node { match pat.node {
PatWild | PatWildMulti => { None } PatIdent(..) | PatEnum(..) | PatStruct(..) =>
PatIdent(_, _, _) | PatEnum(_, _) => { match cx.tcx.def_map.borrow().find(&pat.id) {
let opt_def = cx.tcx.def_map.borrow().find_copy(&pat.id); Some(&DefStatic(did, false)) => {
match opt_def {
Some(DefVariant(_, id, _)) => Some(variant(id)),
Some(DefStatic(did, false)) => {
let const_expr = lookup_const_by_id(cx.tcx, did).unwrap(); let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
Some(val(eval_const_expr(cx.tcx, &*const_expr))) Some(val(eval_const_expr(cx.tcx, &*const_expr)))
},
Some(&DefVariant(_, id, _)) =>
Some(variant(id)),
_ => match pat.node {
PatEnum(..) | PatStruct(..) => Some(single),
PatIdent(..) => None,
_ => unreachable!()
} }
_ => None },
} PatLit(expr) =>
} Some(val(eval_const_expr(cx.tcx, &*expr))),
PatLit(ref expr) => { Some(val(eval_const_expr(cx.tcx, &**expr))) } PatRange(lo, hi) =>
PatRange(ref lo, ref hi) => { Some(range(eval_const_expr(cx.tcx, &*lo), eval_const_expr(cx.tcx, &*hi))),
Some(range(eval_const_expr(cx.tcx, &**lo), eval_const_expr(cx.tcx, &**hi))) PatVec(ref before, _, ref after) => match ty::get(left_ty).sty {
} ty::ty_vec(_, Some(n)) =>
PatStruct(..) => { Some(vec(n)),
match cx.tcx.def_map.borrow().find(&pat.id) { _ =>
Some(&DefVariant(_, id, _)) => Some(variant(id)), Some(vec(before.len() + after.len()))
_ => Some(single) },
} PatBox(_) | PatTup(_) | PatRegion(..) =>
} Some(single),
PatBox(_) | PatTup(_) | PatRegion(..) => { PatWild | PatWildMulti =>
Some(single) None,
} PatMac(_) =>
PatVec(ref before, slice, ref after) => { cx.tcx.sess.bug("unexpanded macro")
match slice {
Some(_) => None,
None => Some(vec(before.len() + after.len()))
}
}
PatMac(_) => cx.tcx.sess.bug("unexpanded macro"),
} }
} }
fn is_wild(cx: &MatchCheckCtxt, p: Gc<Pat>) -> bool { fn is_wild(cx: &MatchCheckCtxt, p: Gc<Pat>) -> bool {
let pat = raw_pat(p); let pat = raw_pat(p);
match pat.node { match pat.node {
PatWild | PatWildMulti => { true } PatWild | PatWildMulti => true,
PatIdent(_, _, _) => { PatIdent(_, _, _) => {
match cx.tcx.def_map.borrow().find(&pat.id) { match cx.tcx.def_map.borrow().find(&pat.id) {
Some(&DefVariant(_, _, _)) | Some(&DefStatic(..)) => { false } Some(&DefVariant(_, _, _)) | Some(&DefStatic(..)) => false,
_ => { true } _ => true
} }
} }
_ => { false } _ => false
}
}
fn missing_ctor(cx: &MatchCheckCtxt,
m: &matrix,
left_ty: ty::t)
-> Option<ctor> {
return match ty::get(left_ty).sty {
ty::ty_box(_) | ty::ty_tup(_) |
ty::ty_struct(..) => check_matrix_for_wild(cx, m),
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty: ty, ..}) => match ty::get(ty).sty {
ty::ty_vec(_, None) => ctor_for_slice(m),
ty::ty_str => Some(single),
_ => check_matrix_for_wild(cx, m),
},
ty::ty_enum(eid, _) => {
let pat_ctors: Vec<ctor> = m
.iter()
.filter_map(|r| pat_ctor_id(cx, *r.get(0)))
.collect();
let variants = ty::enum_variants(cx.tcx, eid);
variants.iter().map(|v| variant(v.id)).find(|c| !pat_ctors.contains(c))
}
ty::ty_nil => None,
ty::ty_bool => {
let mut true_found = false;
let mut false_found = false;
for r in m.iter() {
match pat_ctor_id(cx, *r.get(0)) {
None => (),
Some(val(const_bool(true))) => true_found = true,
Some(val(const_bool(false))) => false_found = true,
_ => fail!("impossible case")
}
}
if true_found && false_found { None }
else if true_found { Some(val(const_bool(false))) }
else { Some(val(const_bool(true))) }
}
ty::ty_vec(_, Some(n)) => {
let mut missing = true;
let mut wrong = false;
for r in m.iter() {
match r.get(0).node {
PatVec(ref before, ref slice, ref after) => {
let count = before.len() + after.len();
if (count < n && slice.is_none()) || count > n {
wrong = true;
}
if count == n || (count < n && slice.is_some()) {
missing = false;
}
}
_ => {}
}
}
match (wrong, missing) {
(true, _) => Some(vec(n)), // should be compile-time error
(_, true) => Some(vec(n)),
_ => None
}
}
ty::ty_vec(..) => fail!("impossible case"),
_ => Some(single)
};
fn check_matrix_for_wild(cx: &MatchCheckCtxt, m: &matrix) -> Option<ctor> {
for r in m.iter() {
if !is_wild(cx, *r.get(0)) { return None; }
}
return Some(single);
}
// For slice and ~[T].
fn ctor_for_slice(m: &matrix) -> Option<ctor> {
// Find the lengths and slices of all vector patterns.
let mut vec_pat_lens = m.iter().filter_map(|r| {
match r.get(0).node {
PatVec(ref before, ref slice, ref after) => {
Some((before.len() + after.len(), slice.is_some()))
}
_ => None
}
}).collect::<Vec<(uint, bool)> >();
// Sort them by length such that for patterns of the same length,
// those with a destructured slice come first.
vec_pat_lens.sort_by(|&(len1, slice1), &(len2, slice2)| {
if len1 == len2 {
slice2.cmp(&slice1)
} else {
len1.cmp(&len2)
}
});
vec_pat_lens.dedup();
let mut found_slice = false;
let mut next = 0;
let mut missing = None;
for &(length, slice) in vec_pat_lens.iter() {
if length != next {
missing = Some(next);
break;
}
if slice {
found_slice = true;
break;
}
next += 1;
}
// We found patterns of all lengths within <0, next), yet there was no
// pattern with a slice - therefore, we report vec(next) as missing.
if !found_slice {
missing = Some(next);
}
match missing {
Some(k) => Some(vec(k)),
None => None
}
}
}
fn ctor_arity(cx: &MatchCheckCtxt, ctor: &ctor, ty: ty::t) -> uint {
fn vec_ctor_arity(ctor: &ctor) -> uint {
match *ctor {
vec(n) => n,
_ => 0u
} }
} }
fn constructor_arity(cx: &MatchCheckCtxt, ctor: &ctor, ty: ty::t) -> uint {
match ty::get(ty).sty { match ty::get(ty).sty {
ty::ty_tup(ref fs) => fs.len(), ty::ty_tup(ref fs) => fs.len(),
ty::ty_box(_) => 1u, ty::ty_box(_) | ty::ty_uniq(_) => 1u,
ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty: ty, ..}) => match ty::get(ty).sty { ty::ty_rptr(_, ty::mt { ty: ty, .. }) => match ty::get(ty).sty {
ty::ty_vec(_, None) => vec_ctor_arity(ctor), ty::ty_vec(_, None) => match *ctor {
_ => 1u, vec(n) => n,
_ => 0u
},
_ => 1u
}, },
ty::ty_enum(eid, _) => { ty::ty_enum(eid, _) => {
let id = match *ctor { match *ctor {
variant(id) => id, variant(id) => enum_variant_with_id(cx.tcx, eid, id).args.len(),
_ => fail!("impossible case") _ => unreachable!()
};
match ty::enum_variants(cx.tcx, eid).iter().find(|v| v.id == id ) {
Some(v) => v.args.len(),
None => fail!("impossible case")
} }
} }
ty::ty_struct(cid, _) => ty::lookup_struct_fields(cx.tcx, cid).len(), ty::ty_struct(cid, _) => ty::lookup_struct_fields(cx.tcx, cid).len(),
ty::ty_vec(_, Some(_)) => vec_ctor_arity(ctor), ty::ty_vec(_, _) => match *ctor {
vec(n) => n,
_ => 0u
},
_ => 0u _ => 0u
} }
} }
@ -553,10 +500,6 @@ fn wild() -> Gc<Pat> {
box(GC) Pat {id: 0, node: PatWild, span: DUMMY_SP} box(GC) Pat {id: 0, node: PatWild, span: DUMMY_SP}
} }
fn wild_multi() -> Gc<Pat> {
box(GC) Pat {id: 0, node: PatWildMulti, span: DUMMY_SP}
}
fn range_covered_by_constructor(ctor_id: &ctor, from: &const_val, to: &const_val) -> Option<bool> { fn range_covered_by_constructor(ctor_id: &ctor, from: &const_val, to: &const_val) -> Option<bool> {
let (c_from, c_to) = match *ctor_id { let (c_from, c_to) = match *ctor_id {
val(ref value) => (value, value), val(ref value) => (value, value),
@ -572,19 +515,17 @@ fn range_covered_by_constructor(ctor_id: &ctor, from: &const_val, to: &const_val
} }
} }
fn specialize(cx: &MatchCheckCtxt, fn specialize(cx: &MatchCheckCtxt, r: &[Gc<Pat>],
r: &[Gc<Pat>], ctor_id: &ctor, arity: uint) -> Option<Vec<Gc<Pat>>> {
ctor_id: &ctor, let &Pat {
arity: uint, id: ref pat_id, node: ref n, span: ref pat_span
left_ty: ty::t) } = &(*raw_pat(r[0]));
-> Option<Vec<Gc<Pat>>> {
let &Pat{id: ref pat_id, node: ref n, span: ref pat_span} = &(*raw_pat(r[0]));
let head: Option<Vec<Gc<Pat>>> = match n { let head: Option<Vec<Gc<Pat>>> = match n {
&PatWild => { &PatWild => {
Some(Vec::from_elem(arity, wild())) Some(Vec::from_elem(arity, wild()))
} }
&PatWildMulti => { &PatWildMulti => {
Some(Vec::from_elem(arity, wild_multi())) Some(Vec::from_elem(arity, wild()))
} }
&PatIdent(_, _, _) => { &PatIdent(_, _, _) => {
let opt_def = cx.tcx.def_map.borrow().find_copy(pat_id); let opt_def = cx.tcx.def_map.borrow().find_copy(pat_id);
@ -638,30 +579,18 @@ fn specialize(cx: &MatchCheckCtxt,
_ => None _ => None
} }
} }
&PatStruct(_, ref pattern_fields, _) => { &PatStruct(_, ref pattern_fields, _) => {
// Is this a struct or an enum variant? // Is this a struct or an enum variant?
let def = cx.tcx.def_map.borrow().get_copy(pat_id); let def = cx.tcx.def_map.borrow().get_copy(pat_id);
let class_id = match def { let class_id = match def {
DefVariant(_, variant_id, _) => { DefVariant(_, variant_id, _) => if variant(variant_id) == *ctor_id {
if variant(variant_id) == *ctor_id {
Some(variant_id) Some(variant_id)
} else { } else {
None None
} },
} DefStruct(struct_id) => Some(struct_id),
_ => { _ => None
match ty::get(left_ty).sty {
ty::ty_struct(cid, _) => Some(cid),
_ => {
cx.tcx.sess.span_bug(
*pat_span,
format!("struct pattern resolved to {}, \
not a struct",
ty_to_str(cx.tcx,
left_ty)).as_slice());
}
}
}
}; };
class_id.map(|variant_id| { class_id.map(|variant_id| {
let struct_fields = ty::lookup_struct_fields(cx.tcx, variant_id); let struct_fields = ty::lookup_struct_fields(cx.tcx, variant_id);
@ -673,14 +602,14 @@ fn specialize(cx: &MatchCheckCtxt,
}).collect(); }).collect();
args args
}) })
}
&PatTup(ref args) =>
Some(args.clone()),
&PatBox(ref inner) | &PatRegion(ref inner) =>
Some(vec!(inner.clone())),
}
&PatTup(ref args) => {
Some(args.clone())
}
&PatBox(ref inner) | &PatRegion(ref inner) => {
Some(vec!(inner.clone()))
}
&PatLit(ref expr) => { &PatLit(ref expr) => {
let expr_value = eval_const_expr(cx.tcx, &**expr); let expr_value = eval_const_expr(cx.tcx, &**expr);
match range_covered_by_constructor(ctor_id, &expr_value, &expr_value) { match range_covered_by_constructor(ctor_id, &expr_value, &expr_value) {
@ -692,6 +621,7 @@ fn specialize(cx: &MatchCheckCtxt,
} }
} }
} }
&PatRange(ref from, ref to) => { &PatRange(ref from, ref to) => {
let from_value = eval_const_expr(cx.tcx, &**from); let from_value = eval_const_expr(cx.tcx, &**from);
let to_value = eval_const_expr(cx.tcx, &**to); let to_value = eval_const_expr(cx.tcx, &**to);
@ -704,6 +634,7 @@ fn specialize(cx: &MatchCheckCtxt,
} }
} }
} }
&PatVec(ref before, ref slice, ref after) => { &PatVec(ref before, ref slice, ref after) => {
match *ctor_id { match *ctor_id {
vec(_) => { vec(_) => {
@ -726,6 +657,7 @@ fn specialize(cx: &MatchCheckCtxt,
_ => None _ => None
} }
} }
&PatMac(_) => { &PatMac(_) => {
cx.tcx.sess.span_err(*pat_span, "unexpanded macro"); cx.tcx.sess.span_err(*pat_span, "unexpanded macro");
None None
@ -787,7 +719,7 @@ fn check_fn(cx: &mut MatchCheckCtxt,
fn is_refutable(cx: &MatchCheckCtxt, pat: Gc<Pat>) -> Option<Gc<Pat>> { fn is_refutable(cx: &MatchCheckCtxt, pat: Gc<Pat>) -> Option<Gc<Pat>> {
let pats = vec!(vec!(pat)); let pats = vec!(vec!(pat));
is_useful(cx, &pats, [wild()]) is_useful(cx, &pats, [wild()], ConstructWitness)
.useful() .useful()
.map(|pats| { .map(|pats| {
assert_eq!(pats.len(), 1); assert_eq!(pats.len(), 1);

View File

@ -9,7 +9,8 @@
// except according to those terms. // except according to those terms.
fn foo(a: Option<uint>, b: Option<uint>) { fn foo(a: Option<uint>, b: Option<uint>) {
match (a,b) { //~ ERROR: non-exhaustive patterns: None not covered match (a,b) {
//~^ ERROR: non-exhaustive patterns: (core::option::None, core::option::None) not covered
(Some(a), Some(b)) if a == b => { } (Some(a), Some(b)) if a == b => { }
(Some(_), None) | (Some(_), None) |
(None, Some(_)) => { } (None, Some(_)) => { }

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 main() {
let tup = (true, true);
println!("foo {:}", match tup { //~ ERROR non-exhaustive patterns: (true, false) not covered
(false, false) => "foo",
(false, true) => "bar",
(true, true) => "baz"
});
}

View File

@ -8,13 +8,12 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// error-pattern: non-exhaustive patterns
enum t { a(u), b } enum t { a(u), b }
enum u { c, d } enum u { c, d }
fn main() { fn main() {
let x = a(c); let x = a(c);
match x { match x { //~ ERROR non-exhaustive patterns: a(c) not covered
a(d) => { fail!("hello"); } a(d) => { fail!("hello"); }
b => { fail!("goodbye"); } b => { fail!("goodbye"); }
} }

View File

@ -12,21 +12,21 @@ enum t { a, b, }
fn main() { fn main() {
let x = a; let x = a;
match x { b => { } } //~ ERROR non-exhaustive patterns match x { b => { } } //~ ERROR non-exhaustive patterns: a not covered
match true { //~ ERROR non-exhaustive patterns match true { //~ ERROR non-exhaustive patterns: false not covered
true => {} true => {}
} }
match Some(10) { //~ ERROR non-exhaustive patterns match Some(10) { //~ ERROR non-exhaustive patterns: core::option::Some(_) not covered
None => {} None => {}
} }
match (2, 3, 4) { //~ ERROR non-exhaustive patterns match (2, 3, 4) { //~ ERROR non-exhaustive patterns: (_, _, _) not covered
(_, _, 4) => {} (_, _, 4) => {}
} }
match (a, a) { //~ ERROR non-exhaustive patterns match (a, a) { //~ ERROR non-exhaustive patterns: (a, a) not covered
(a, b) => {} (a, b) => {}
(b, a) => {} (b, a) => {}
} }
match a { //~ ERROR b not covered match a { //~ ERROR non-exhaustive patterns: b not covered
a => {} a => {}
} }
// This is exhaustive, though the algorithm got it wrong at one point // This is exhaustive, though the algorithm got it wrong at one point
@ -37,8 +37,7 @@ fn main() {
} }
let vec = vec!(Some(42), None, Some(21)); let vec = vec!(Some(42), None, Some(21));
let vec: &[Option<int>] = vec.as_slice(); let vec: &[Option<int>] = vec.as_slice();
match vec { match vec { //~ ERROR non-exhaustive patterns: [] not covered
//~^ ERROR non-exhaustive patterns: vectors of length 0 not covered
[Some(..), None, ..tail] => {} [Some(..), None, ..tail] => {}
[Some(..), Some(..), ..tail] => {} [Some(..), Some(..), ..tail] => {}
[None] => {} [None] => {}
@ -51,7 +50,7 @@ fn main() {
} }
let vec = vec!(0.5); let vec = vec!(0.5);
let vec: &[f32] = vec.as_slice(); let vec: &[f32] = vec.as_slice();
match vec { //~ ERROR non-exhaustive patterns: vectors of length 4 not covered match vec { //~ ERROR non-exhaustive patterns: [_, _, _, _] not covered
[0.1, 0.2, 0.3] => (), [0.1, 0.2, 0.3] => (),
[0.1, 0.2] => (), [0.1, 0.2] => (),
[0.1] => (), [0.1] => (),

View File

@ -10,9 +10,9 @@
fn func((1, (Some(1), 2..3)): (int, (Option<int>, int))) { } fn func((1, (Some(1), 2..3)): (int, (Option<int>, int))) { }
//~^ ERROR refutable pattern in function argument //~^ ERROR refutable pattern in function argument: (_, _) not covered
fn main() { fn main() {
let (1, (Some(1), 2..3)) = (1, (None, 2)); let (1, (Some(1), 2..3)) = (1, (None, 2));
//~^ ERROR refutable pattern in local binding //~^ ERROR refutable pattern in local binding: (_, _) not covered
} }

View File

@ -9,6 +9,7 @@
// except according to those terms. // except according to those terms.
fn main() { fn main() {
let f = |3: int| println!("hello"); //~ ERROR refutable pattern let f = |3: int| println!("hello");
//~^ ERROR refutable pattern in function argument: _ not covered
f(4); f(4);
} }