2012-09-04 13:54:36 -05:00
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::ast_util::{variant_def_ids, dummy_sp, unguarded_pat};
|
|
|
|
use const_eval::{eval_const_expr, const_val, const_int, const_bool,
|
2012-04-24 04:13:25 -05:00
|
|
|
compare_const_vals};
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::codemap::span;
|
|
|
|
use syntax::print::pprust::pat_to_str;
|
|
|
|
use util::ppaux::ty_to_str;
|
|
|
|
use pat_util::*;
|
|
|
|
use syntax::visit;
|
|
|
|
use middle::ty;
|
|
|
|
use middle::ty::*;
|
2012-09-10 17:38:28 -05:00
|
|
|
use std::map::HashMap;
|
2011-07-25 06:45:09 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn check_crate(tcx: ty::ctxt, crate: @crate) {
|
2012-02-22 09:57:23 -06:00
|
|
|
visit::visit_crate(*crate, (), visit::mk_vt(@{
|
2012-06-30 18:19:07 -05:00
|
|
|
visit_expr: |a,b,c| check_expr(tcx, a, b, c),
|
2012-09-04 15:29:32 -05:00
|
|
|
visit_local: |a,b,c| check_local(tcx, a, b, c),
|
|
|
|
.. *visit::default_visitor::<()>()
|
2012-02-22 09:57:23 -06:00
|
|
|
}));
|
2011-07-25 06:45:09 -05:00
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
}
|
|
|
|
|
2011-10-06 05:26:12 -05:00
|
|
|
fn check_expr(tcx: ty::ctxt, ex: @expr, &&s: (), v: visit::vt<()>) {
|
2011-07-25 06:45:09 -05:00
|
|
|
visit::visit_expr(ex, s, v);
|
2012-08-06 14:34:08 -05:00
|
|
|
match ex.node {
|
2012-08-24 23:03:51 -05:00
|
|
|
expr_match(scrut, arms) => {
|
2012-02-15 02:35:11 -06:00
|
|
|
check_arms(tcx, arms);
|
|
|
|
/* Check for exhaustiveness */
|
2012-07-27 15:13:03 -05:00
|
|
|
// Check for empty enum, because is_useful only works on inhabited
|
|
|
|
// types.
|
|
|
|
let pat_ty = node_id_to_type(tcx, scrut.id);
|
2012-08-15 16:28:52 -05:00
|
|
|
if arms.is_empty() {
|
|
|
|
if !type_is_empty(tcx, pat_ty) {
|
|
|
|
// We know the type is inhabited, so this must be wrong
|
2012-10-12 14:32:36 -05:00
|
|
|
tcx.sess.span_err(ex.span, fmt!("non-exhaustive patterns: \
|
2012-08-15 16:28:52 -05:00
|
|
|
type %s is non-empty", ty_to_str(tcx, pat_ty)));
|
2012-07-27 15:13:03 -05:00
|
|
|
}
|
2012-08-15 16:28:52 -05:00
|
|
|
// If the type *is* empty, it's vacuously exhaustive
|
|
|
|
return;
|
|
|
|
}
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(pat_ty).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty_enum(did, _) => {
|
2012-07-27 15:13:03 -05:00
|
|
|
if (*enum_variants(tcx, did)).is_empty() && arms.is_empty() {
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return;
|
2012-07-27 15:13:03 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => { /* We assume only enum types can be uninhabited */ }
|
2012-07-27 15:13:03 -05:00
|
|
|
}
|
2012-08-24 23:03:51 -05:00
|
|
|
let arms = vec::concat(vec::filter_map(arms, unguarded_pat));
|
|
|
|
check_exhaustive(tcx, ex.span, arms);
|
|
|
|
}
|
|
|
|
_ => ()
|
2012-01-14 18:05:07 -06:00
|
|
|
}
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
|
2012-04-24 04:13:25 -05:00
|
|
|
// Check for unreachable patterns
|
2012-06-29 18:26:56 -05:00
|
|
|
fn check_arms(tcx: ty::ctxt, arms: ~[arm]) {
|
|
|
|
let mut seen = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for arms.each |arm| {
|
|
|
|
for arm.pats.each |pat| {
|
2012-09-19 18:55:01 -05:00
|
|
|
let v = ~[*pat];
|
2012-08-06 14:34:08 -05:00
|
|
|
match is_useful(tcx, seen, v) {
|
2012-08-03 21:59:04 -05:00
|
|
|
not_useful => {
|
2012-07-14 00:57:48 -05:00
|
|
|
tcx.sess.span_err(pat.span, ~"unreachable pattern");
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
2012-09-26 19:33:34 -05:00
|
|
|
if arm.guard.is_none() { seen.push(v); }
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
|
|
|
|
2012-02-15 02:40:42 -06:00
|
|
|
fn raw_pat(p: @pat) -> @pat {
|
2012-08-06 14:34:08 -05:00
|
|
|
match p.node {
|
2012-08-20 14:23:37 -05:00
|
|
|
pat_ident(_, _, Some(s)) => { raw_pat(s) }
|
2012-07-31 21:25:24 -05:00
|
|
|
_ => { p }
|
2012-02-15 02:40:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn check_exhaustive(tcx: ty::ctxt, sp: span, pats: ~[@pat]) {
|
2012-07-27 15:13:03 -05:00
|
|
|
assert(pats.is_not_empty());
|
2012-09-21 20:43:30 -05:00
|
|
|
let ext = match is_useful(tcx, vec::map(pats, |p| ~[*p]), ~[wild()]) {
|
2012-08-03 21:59:04 -05:00
|
|
|
not_useful => return, // This is good, wildcard pattern isn't reachable
|
2012-08-20 14:23:37 -05:00
|
|
|
useful_ => None,
|
2012-08-03 21:59:04 -05:00
|
|
|
useful(ty, ctor) => {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ty).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_bool => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match ctor {
|
2012-08-20 14:23:37 -05:00
|
|
|
val(const_bool(true)) => Some(~"true"),
|
|
|
|
val(const_bool(false)) => Some(~"false"),
|
|
|
|
_ => None
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_enum(id, _) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
let vid = match ctor { variant(id) => id,
|
|
|
|
_ => fail ~"check_exhaustive: non-variant ctor" };
|
|
|
|
match vec::find(*ty::enum_variants(tcx, id),
|
2012-06-30 18:19:07 -05:00
|
|
|
|v| v.id == vid) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(v) => Some(tcx.sess.str_of(v.name)),
|
|
|
|
None => fail ~"check_exhaustive: bad variant in ctor"
|
2012-02-15 02:40:42 -06:00
|
|
|
}
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => None
|
2012-02-15 02:40:42 -06:00
|
|
|
}
|
|
|
|
}
|
2012-04-24 04:13:25 -05:00
|
|
|
};
|
2012-08-06 14:34:08 -05:00
|
|
|
let msg = ~"non-exhaustive patterns" + match ext {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(s) => ~": " + s + ~" not covered",
|
|
|
|
None => ~""
|
2012-04-24 04:13:25 -05:00
|
|
|
};
|
|
|
|
tcx.sess.span_err(sp, msg);
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
type matrix = ~[~[@pat]];
|
2012-04-24 04:13:25 -05:00
|
|
|
|
|
|
|
enum useful { useful(ty::t, ctor), useful_, not_useful }
|
|
|
|
|
|
|
|
enum ctor {
|
|
|
|
single,
|
|
|
|
variant(def_id),
|
|
|
|
val(const_val),
|
|
|
|
range(const_val, const_val),
|
2012-02-15 02:40:42 -06:00
|
|
|
}
|
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl ctor : cmp::Eq {
|
|
|
|
pure fn eq(other: &ctor) -> bool {
|
|
|
|
match (self, (*other)) {
|
|
|
|
(single, single) => true,
|
|
|
|
(variant(did_self), variant(did_other)) => did_self == did_other,
|
|
|
|
(val(cv_self), val(cv_other)) => cv_self == cv_other,
|
|
|
|
(range(cv0_self, cv1_self), range(cv0_other, cv1_other)) => {
|
|
|
|
cv0_self == cv0_other && cv1_self == cv1_other
|
|
|
|
}
|
|
|
|
(single, _) | (variant(_), _) | (val(_), _) | (range(*), _) => {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pure fn ne(other: &ctor) -> bool { !self.eq(other) }
|
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-04-24 04:13:25 -05:00
|
|
|
// 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
|
|
|
|
// vectors `m` is defined as there being a set of inputs that will match `v`
|
|
|
|
// but not any of the sets in `m`.
|
|
|
|
//
|
|
|
|
// This is used both for reachability checking (if a pattern isn't useful in
|
|
|
|
// relation to preceding patterns, it is not reachable) and exhaustiveness
|
|
|
|
// checking (if a wildcard pattern is useful in relation to a matrix, the
|
|
|
|
// matrix isn't exhaustive).
|
|
|
|
|
2012-07-27 15:13:03 -05:00
|
|
|
// Note: is_useful doesn't work on empty types, as the paper notes.
|
|
|
|
// So it assumes that v is non-empty.
|
2012-06-29 18:26:56 -05:00
|
|
|
fn is_useful(tcx: ty::ctxt, m: matrix, v: ~[@pat]) -> useful {
|
2012-08-01 19:30:05 -05:00
|
|
|
if m.len() == 0u { return useful_; }
|
|
|
|
if m[0].len() == 0u { return not_useful; }
|
2012-08-06 14:34:08 -05:00
|
|
|
let real_pat = match vec::find(m, |r| r[0].id != 0) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(r) => r[0], None => v[0]
|
2012-04-24 04:13:25 -05:00
|
|
|
};
|
|
|
|
let left_ty = if real_pat.id == 0 { ty::mk_nil(tcx) }
|
|
|
|
else { ty::node_id_to_type(tcx, real_pat.id) };
|
2012-02-15 02:40:42 -06:00
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match pat_ctor_id(tcx, v[0]) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match missing_ctor(tcx, m, left_ty) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(left_ty).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_bool => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match is_useful_specialized(tcx, m, v, val(const_bool(true)),
|
2012-04-24 04:13:25 -05:00
|
|
|
0u, left_ty){
|
2012-08-03 21:59:04 -05:00
|
|
|
not_useful => {
|
2012-08-23 17:51:23 -05:00
|
|
|
is_useful_specialized(tcx, m, v, val(const_bool(false)),
|
2012-04-24 04:13:25 -05:00
|
|
|
0u, left_ty)
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
u => u
|
2012-02-15 02:40:42 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_enum(eid, _) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
for (*ty::enum_variants(tcx, eid)).each |va| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match is_useful_specialized(tcx, m, v, variant(va.id),
|
2012-04-24 04:13:25 -05:00
|
|
|
va.args.len(), left_ty) {
|
2012-08-03 21:59:04 -05:00
|
|
|
not_useful => (),
|
|
|
|
u => return u
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
not_useful
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-04-24 04:13:25 -05:00
|
|
|
let arity = ctor_arity(tcx, single, left_ty);
|
|
|
|
is_useful_specialized(tcx, m, v, single, arity, left_ty)
|
2012-04-20 02:54:42 -05:00
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
2012-05-03 10:35:12 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(ctor) => {
|
2012-09-28 00:20:47 -05:00
|
|
|
match is_useful(tcx, vec::filter_map(m, |r| default(tcx, *r) ),
|
2012-05-03 10:35:12 -05:00
|
|
|
vec::tail(v)) {
|
2012-08-03 21:59:04 -05:00
|
|
|
useful_ => useful(left_ty, ctor),
|
|
|
|
u => u
|
2012-05-03 10:35:12 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(v0_ctor) => {
|
2012-04-24 04:13:25 -05:00
|
|
|
let arity = ctor_arity(tcx, v0_ctor, left_ty);
|
|
|
|
is_useful_specialized(tcx, m, v, v0_ctor, arity, left_ty)
|
|
|
|
}
|
2012-01-30 23:00:57 -06:00
|
|
|
}
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn is_useful_specialized(tcx: ty::ctxt, m: matrix, v: ~[@pat], ctor: ctor,
|
2012-04-24 04:13:25 -05:00
|
|
|
arity: uint, lty: ty::t) -> useful {
|
2012-09-28 00:20:47 -05:00
|
|
|
let ms = vec::filter_map(m, |r| specialize(tcx, *r, ctor, arity, lty) );
|
2012-08-06 14:34:08 -05:00
|
|
|
let could_be_useful = is_useful(
|
2012-09-21 21:37:57 -05:00
|
|
|
tcx, ms, specialize(tcx, v, ctor, arity, lty).get());
|
2012-08-06 14:34:08 -05:00
|
|
|
match could_be_useful {
|
2012-08-03 21:59:04 -05:00
|
|
|
useful_ => useful(lty, ctor),
|
|
|
|
u => u
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn pat_ctor_id(tcx: ty::ctxt, p: @pat) -> Option<ctor> {
|
2012-04-24 04:13:25 -05:00
|
|
|
let pat = raw_pat(p);
|
2012-08-06 14:34:08 -05:00
|
|
|
match pat.node {
|
2012-08-20 14:23:37 -05:00
|
|
|
pat_wild => { None }
|
2012-07-31 21:25:24 -05:00
|
|
|
pat_ident(_, _, _) | pat_enum(_, _) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match tcx.def_map.find(pat.id) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(def_variant(_, id)) => Some(variant(id)),
|
|
|
|
_ => None
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
pat_lit(expr) => { Some(val(eval_const_expr(tcx, expr))) }
|
2012-07-31 21:25:24 -05:00
|
|
|
pat_range(lo, hi) => {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(range(eval_const_expr(tcx, lo), eval_const_expr(tcx, hi)))
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2012-09-07 19:07:32 -05:00
|
|
|
pat_box(_) | pat_uniq(_) | pat_rec(_, _) | pat_tup(_) | pat_region(*) |
|
2012-08-06 19:01:14 -05:00
|
|
|
pat_struct(*) => {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(single)
|
2012-07-31 21:25:24 -05:00
|
|
|
}
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2011-07-25 06:45:09 -05:00
|
|
|
|
2012-04-24 04:13:25 -05:00
|
|
|
fn is_wild(tcx: ty::ctxt, p: @pat) -> bool {
|
|
|
|
let pat = raw_pat(p);
|
2012-08-06 14:34:08 -05:00
|
|
|
match pat.node {
|
2012-07-31 21:25:24 -05:00
|
|
|
pat_wild => { true }
|
|
|
|
pat_ident(_, _, _) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match tcx.def_map.find(pat.id) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(def_variant(_, _)) => { false }
|
2012-07-31 21:25:24 -05:00
|
|
|
_ => { true }
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-31 21:25:24 -05:00
|
|
|
_ => { false }
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn missing_ctor(tcx: ty::ctxt, m: matrix, left_ty: ty::t) -> Option<ctor> {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(left_ty).sty {
|
2012-09-07 19:07:32 -05:00
|
|
|
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_rptr(*) | ty::ty_tup(_) |
|
|
|
|
ty::ty_rec(_) | ty::ty_class(*) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
for m.each |r| {
|
2012-08-20 14:23:37 -05:00
|
|
|
if !is_wild(tcx, r[0]) { return None; }
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(single);
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_enum(eid, _) => {
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut found = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for m.each |r| {
|
2012-09-21 21:37:57 -05:00
|
|
|
do option::iter(&pat_ctor_id(tcx, r[0])) |id| {
|
2012-09-28 00:20:47 -05:00
|
|
|
if !vec::contains(found, id) {
|
|
|
|
found.push(*id);
|
|
|
|
}
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
2012-05-03 10:35:12 -05:00
|
|
|
let variants = ty::enum_variants(tcx, eid);
|
|
|
|
if found.len() != (*variants).len() {
|
2012-09-18 23:41:13 -05:00
|
|
|
for vec::each(*variants) |v| {
|
2012-09-25 19:39:22 -05:00
|
|
|
if !found.contains(&(variant(v.id))) {
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(variant(v.id));
|
2012-05-03 10:35:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fail;
|
2012-08-20 14:23:37 -05:00
|
|
|
} else { None }
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
ty::ty_nil => None,
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_bool => {
|
2012-04-24 04:13:25 -05:00
|
|
|
let mut true_found = false, false_found = false;
|
2012-06-30 18:19:07 -05:00
|
|
|
for m.each |r| {
|
2012-08-23 17:51:23 -05:00
|
|
|
match pat_ctor_id(tcx, r[0]) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => (),
|
|
|
|
Some(val(const_bool(true))) => true_found = true,
|
|
|
|
Some(val(const_bool(false))) => false_found = true,
|
2012-08-23 17:51:23 -05:00
|
|
|
_ => fail ~"impossible case"
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
if true_found && false_found { None }
|
|
|
|
else if true_found { Some(val(const_bool(false))) }
|
|
|
|
else { Some(val(const_bool(true))) }
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => Some(single)
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ctor_arity(tcx: ty::ctxt, ctor: ctor, ty: ty::t) -> uint {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ty).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_tup(fs) => fs.len(),
|
|
|
|
ty::ty_rec(fs) => fs.len(),
|
2012-09-07 19:07:32 -05:00
|
|
|
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_rptr(*) => 1u,
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_enum(eid, _) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
let id = match ctor { variant(id) => id,
|
|
|
|
_ => fail ~"impossible case" };
|
|
|
|
match vec::find(*ty::enum_variants(tcx, eid), |v| v.id == id ) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(v) => v.args.len(),
|
|
|
|
None => fail ~"impossible case"
|
2011-09-23 13:15:17 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-06 19:01:14 -05:00
|
|
|
ty::ty_class(cid, _) => ty::lookup_class_fields(tcx, cid).len(),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => 0u
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wild() -> @pat {
|
|
|
|
@{id: 0, node: pat_wild, span: syntax::ast_util::dummy_sp()}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn specialize(tcx: ty::ctxt, r: ~[@pat], ctor_id: ctor, arity: uint,
|
2012-08-20 14:23:37 -05:00
|
|
|
left_ty: ty::t) -> Option<~[@pat]> {
|
2012-04-24 04:13:25 -05:00
|
|
|
let r0 = raw_pat(r[0]);
|
2012-08-06 14:34:08 -05:00
|
|
|
match r0.node {
|
2012-08-20 14:23:37 -05:00
|
|
|
pat_wild => Some(vec::append(vec::from_elem(arity, wild()),
|
2012-08-03 21:59:04 -05:00
|
|
|
vec::tail(r))),
|
|
|
|
pat_ident(_, _, _) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match tcx.def_map.find(r0.id) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(def_variant(_, id)) => {
|
|
|
|
if variant(id) == ctor_id { Some(vec::tail(r)) }
|
|
|
|
else { None }
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => Some(vec::append(vec::from_elem(arity, wild()), vec::tail(r)))
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
pat_enum(_, args) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
match tcx.def_map.get(r0.id) {
|
2012-08-03 21:59:04 -05:00
|
|
|
def_variant(_, id) if variant(id) == ctor_id => {
|
2012-08-06 14:34:08 -05:00
|
|
|
let args = match args {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(args) => args,
|
|
|
|
None => vec::from_elem(arity, wild())
|
2012-04-24 04:13:25 -05:00
|
|
|
};
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(vec::append(args, vec::tail(r)))
|
2011-09-28 14:07:33 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
def_variant(_, _) => None,
|
|
|
|
_ => None
|
2011-09-28 14:07:33 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
pat_rec(flds, _) => {
|
2012-09-11 18:20:31 -05:00
|
|
|
let ty_flds = match ty::get(left_ty).sty {
|
2012-08-23 17:51:23 -05:00
|
|
|
ty::ty_rec(flds) => flds,
|
|
|
|
_ => fail ~"bad type for pat_rec"
|
2012-04-24 04:13:25 -05:00
|
|
|
};
|
2012-06-30 18:19:07 -05:00
|
|
|
let args = vec::map(ty_flds, |ty_f| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match vec::find(flds, |f| f.ident == ty_f.ident ) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(f) => f.pat,
|
2012-08-06 19:01:14 -05:00
|
|
|
_ => wild()
|
|
|
|
}
|
|
|
|
});
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(vec::append(args, vec::tail(r)))
|
2012-08-06 19:01:14 -05:00
|
|
|
}
|
|
|
|
pat_struct(_, flds, _) => {
|
|
|
|
// Grab the class data that we care about.
|
|
|
|
let class_fields, class_id;
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(left_ty).sty {
|
2012-08-26 14:12:05 -05:00
|
|
|
ty::ty_class(cid, _) => {
|
2012-08-06 19:01:14 -05:00
|
|
|
class_id = cid;
|
|
|
|
class_fields = ty::lookup_class_fields(tcx, class_id);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
tcx.sess.span_bug(r0.span, ~"struct pattern didn't resolve \
|
|
|
|
to a struct");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let args = vec::map(class_fields, |class_field| {
|
|
|
|
match vec::find(flds, |f| f.ident == class_field.ident ) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(f) => f.pat,
|
2012-08-06 19:01:14 -05:00
|
|
|
_ => wild()
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
|
|
|
});
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(vec::append(args, vec::tail(r)))
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
pat_tup(args) => Some(vec::append(args, vec::tail(r))),
|
2012-09-07 19:07:32 -05:00
|
|
|
pat_box(a) | pat_uniq(a) | pat_region(a) =>
|
|
|
|
Some(vec::append(~[a], vec::tail(r))),
|
2012-08-03 21:59:04 -05:00
|
|
|
pat_lit(expr) => {
|
2012-04-24 04:13:25 -05:00
|
|
|
let e_v = eval_const_expr(tcx, expr);
|
2012-08-23 17:51:23 -05:00
|
|
|
let match_ = match ctor_id {
|
2012-08-03 21:59:04 -05:00
|
|
|
val(v) => compare_const_vals(e_v, v) == 0,
|
|
|
|
range(c_lo, c_hi) => {
|
|
|
|
compare_const_vals(c_lo, e_v) >= 0 &&
|
|
|
|
compare_const_vals(c_hi, e_v) <= 0
|
|
|
|
}
|
2012-08-23 17:51:23 -05:00
|
|
|
single => true,
|
|
|
|
_ => fail ~"type error"
|
2012-04-24 04:13:25 -05:00
|
|
|
};
|
2012-08-20 14:23:37 -05:00
|
|
|
if match_ { Some(vec::tail(r)) } else { None }
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
pat_range(lo, hi) => {
|
2012-08-23 17:51:23 -05:00
|
|
|
let (c_lo, c_hi) = match ctor_id {
|
2012-08-03 21:59:04 -05:00
|
|
|
val(v) => (v, v),
|
|
|
|
range(lo, hi) => (lo, hi),
|
2012-08-20 14:23:37 -05:00
|
|
|
single => return Some(vec::tail(r)),
|
2012-08-23 17:51:23 -05:00
|
|
|
_ => fail ~"type error"
|
2012-04-24 04:13:25 -05:00
|
|
|
};
|
|
|
|
let v_lo = eval_const_expr(tcx, lo),
|
|
|
|
v_hi = eval_const_expr(tcx, hi);
|
2012-07-31 18:38:41 -05:00
|
|
|
let match_ = compare_const_vals(c_lo, v_lo) >= 0 &&
|
2012-04-24 04:13:25 -05:00
|
|
|
compare_const_vals(c_hi, v_hi) <= 0;
|
2012-08-20 14:23:37 -05:00
|
|
|
if match_ { Some(vec::tail(r)) } else { None }
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn default(tcx: ty::ctxt, r: ~[@pat]) -> Option<~[@pat]> {
|
|
|
|
if is_wild(tcx, r[0]) { Some(vec::tail(r)) }
|
|
|
|
else { None }
|
2012-04-24 04:13:25 -05:00
|
|
|
}
|
|
|
|
|
2011-10-06 05:26:12 -05:00
|
|
|
fn check_local(tcx: ty::ctxt, loc: @local, &&s: (), v: visit::vt<()>) {
|
2011-08-01 08:26:48 -05:00
|
|
|
visit::visit_local(loc, s, v);
|
|
|
|
if is_refutable(tcx, loc.node.pat) {
|
|
|
|
tcx.sess.span_err(loc.node.pat.span,
|
2012-07-14 00:57:48 -05:00
|
|
|
~"refutable pattern in local binding");
|
2011-08-01 08:26:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-28 19:04:39 -05:00
|
|
|
fn is_refutable(tcx: ty::ctxt, pat: &pat) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match tcx.def_map.find(pat.id) {
|
2012-08-26 14:12:05 -05:00
|
|
|
Some(def_variant(enum_id, _)) => {
|
2012-08-06 19:01:14 -05:00
|
|
|
if vec::len(*ty::enum_variants(tcx, enum_id)) != 1u {
|
|
|
|
return true;
|
|
|
|
}
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2012-02-22 09:57:23 -06:00
|
|
|
}
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
match pat.node {
|
2012-09-07 19:07:32 -05:00
|
|
|
pat_box(sub) | pat_uniq(sub) | pat_region(sub) |
|
|
|
|
pat_ident(_, _, Some(sub)) => {
|
2011-12-08 04:56:16 -06:00
|
|
|
is_refutable(tcx, sub)
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
pat_wild | pat_ident(_, _, None) => { false }
|
2012-08-21 20:03:47 -05:00
|
|
|
pat_lit(@{node: expr_lit(@{node: lit_nil, _}), _}) => { false } // "()"
|
2012-07-31 21:25:24 -05:00
|
|
|
pat_lit(_) | pat_range(_, _) => { true }
|
|
|
|
pat_rec(fields, _) => {
|
2012-09-19 18:55:01 -05:00
|
|
|
fields.any(|f| is_refutable(tcx, f.pat))
|
2011-08-01 08:26:48 -05:00
|
|
|
}
|
2012-08-06 19:01:14 -05:00
|
|
|
pat_struct(_, fields, _) => {
|
2012-09-19 18:55:01 -05:00
|
|
|
fields.any(|f| is_refutable(tcx, f.pat))
|
2012-08-06 19:01:14 -05:00
|
|
|
}
|
2012-07-31 21:25:24 -05:00
|
|
|
pat_tup(elts) => {
|
2012-09-28 19:04:39 -05:00
|
|
|
elts.any(|elt| is_refutable(tcx, *elt))
|
2011-08-15 06:15:19 -05:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
pat_enum(_, Some(args)) => {
|
2012-09-28 19:04:39 -05:00
|
|
|
args.any(|a| is_refutable(tcx, *a))
|
2011-08-01 08:26:48 -05:00
|
|
|
}
|
2012-07-31 21:25:24 -05:00
|
|
|
pat_enum(_,_) => { false }
|
2011-08-01 08:26:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-25 06:45:09 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|