2011-07-25 06:45:09 -05:00
|
|
|
import syntax::ast::*;
|
2011-09-12 18:13:28 -05:00
|
|
|
import syntax::ast_util::{variant_def_ids, dummy_sp};
|
2011-07-25 06:45:09 -05:00
|
|
|
import syntax::visit;
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn check_crate(tcx: ty::ctxt, crate: @crate) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let v =
|
2011-08-01 08:26:48 -05:00
|
|
|
@{visit_expr: bind check_expr(tcx, _, _, _),
|
|
|
|
visit_local: bind check_local(tcx, _, _, _)
|
2011-08-19 17:16:48 -05:00
|
|
|
with *visit::default_visitor::<()>()};
|
2011-07-25 06:45:09 -05:00
|
|
|
visit::visit_crate(*crate, (), visit::mk_vt(v));
|
|
|
|
tcx.sess.abort_if_errors();
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -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);
|
2011-07-27 07:19:39 -05:00
|
|
|
alt ex.node { expr_alt(_, arms) { check_arms(tcx, arms); } _ { } }
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn check_arms(tcx: ty::ctxt, arms: [arm]) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0;
|
2011-08-15 23:54:52 -05:00
|
|
|
for arm: arm in arms {
|
|
|
|
for arm_pat: @pat in arm.pats {
|
2011-07-27 07:19:39 -05:00
|
|
|
let reachable = true;
|
|
|
|
let j = 0;
|
2011-07-25 06:45:09 -05:00
|
|
|
while j < i {
|
2011-08-22 07:38:48 -05:00
|
|
|
if std::option::is_none(arms[j].guard) {
|
|
|
|
for prev_pat: @pat in arms[j].pats {
|
|
|
|
if pattern_supersedes(tcx, prev_pat, arm_pat) {
|
|
|
|
reachable = false;
|
|
|
|
}
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
if !reachable {
|
2011-09-02 17:34:58 -05:00
|
|
|
tcx.sess.span_err(arm_pat.span, "unreachable pattern");
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn pattern_supersedes(tcx: ty::ctxt, a: @pat, b: @pat) -> bool {
|
|
|
|
fn patterns_supersede(tcx: ty::ctxt, as: [@pat], bs: [@pat]) -> bool {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0;
|
2011-08-15 23:54:52 -05:00
|
|
|
for a: @pat in as {
|
2011-08-19 17:16:48 -05:00
|
|
|
if !pattern_supersedes(tcx, a, bs[i]) { ret false; }
|
2011-07-25 06:45:09 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
ret true;
|
|
|
|
}
|
2011-09-12 04:27:30 -05:00
|
|
|
fn field_patterns_supersede(tcx: ty::ctxt, fas: [field_pat],
|
|
|
|
fbs: [field_pat]) -> bool {
|
2011-08-15 15:33:12 -05:00
|
|
|
let wild = @{id: 0, node: pat_wild, span: dummy_sp()};
|
2011-08-15 23:54:52 -05:00
|
|
|
for fa: field_pat in fas {
|
2011-07-27 07:19:39 -05:00
|
|
|
let pb = wild;
|
2011-08-15 23:54:52 -05:00
|
|
|
for fb: field_pat in fbs {
|
2011-07-25 06:45:09 -05:00
|
|
|
if fa.ident == fb.ident { pb = fb.pat; }
|
|
|
|
}
|
|
|
|
if !pattern_supersedes(tcx, fa.pat, pb) { ret false; }
|
|
|
|
}
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-09-21 10:31:43 -05:00
|
|
|
|
2011-07-25 06:45:09 -05:00
|
|
|
alt a.node {
|
2011-07-27 07:19:39 -05:00
|
|
|
pat_wild. | pat_bind(_) { ret true; }
|
|
|
|
pat_lit(la) {
|
2011-07-25 06:45:09 -05:00
|
|
|
alt b.node {
|
2011-07-27 07:19:39 -05:00
|
|
|
pat_lit(lb) { ret util::common::lit_eq(la, lb); }
|
2011-07-25 06:45:09 -05:00
|
|
|
_ { ret false; }
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
pat_tag(va, suba) {
|
2011-07-25 06:45:09 -05:00
|
|
|
alt b.node {
|
2011-07-27 07:19:39 -05:00
|
|
|
pat_tag(vb, subb) {
|
2011-07-25 06:45:09 -05:00
|
|
|
ret tcx.def_map.get(a.id) == tcx.def_map.get(b.id) &&
|
2011-07-27 07:19:39 -05:00
|
|
|
patterns_supersede(tcx, suba, subb);
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
_ { ret false; }
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
pat_rec(suba, _) {
|
2011-07-25 06:45:09 -05:00
|
|
|
alt b.node {
|
2011-07-27 07:19:39 -05:00
|
|
|
pat_rec(subb, _) { ret field_patterns_supersede(tcx, suba, subb); }
|
2011-07-25 06:45:09 -05:00
|
|
|
_ { ret false; }
|
|
|
|
}
|
|
|
|
}
|
2011-08-15 06:15:19 -05:00
|
|
|
pat_tup(suba) {
|
|
|
|
alt b.node {
|
|
|
|
pat_tup(subb) { ret patterns_supersede(tcx, suba, subb); }
|
|
|
|
_ { ret false; }
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
pat_box(suba) {
|
2011-07-25 06:45:09 -05:00
|
|
|
alt b.node {
|
2011-07-27 07:19:39 -05:00
|
|
|
pat_box(subb) { ret pattern_supersedes(tcx, suba, subb); }
|
2011-07-25 06:45:09 -05:00
|
|
|
_ { ret pattern_supersedes(tcx, suba, b); }
|
|
|
|
}
|
|
|
|
}
|
2011-09-23 13:15:17 -05:00
|
|
|
pat_uniq(suba) {
|
|
|
|
alt b.node {
|
|
|
|
pat_uniq(subb) { ret pattern_supersedes(tcx, suba, subb); }
|
|
|
|
_ { ret pattern_supersedes(tcx, suba, b); }
|
|
|
|
}
|
|
|
|
}
|
2011-07-25 06:45:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -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,
|
2011-09-02 17:34:58 -05:00
|
|
|
"refutable pattern in local binding");
|
2011-08-01 08:26:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn is_refutable(tcx: ty::ctxt, pat: @pat) -> bool {
|
2011-08-01 08:26:48 -05:00
|
|
|
alt pat.node {
|
|
|
|
pat_wild. | pat_bind(_) { ret false; }
|
|
|
|
pat_lit(_) { ret true; }
|
|
|
|
pat_box(sub) { ret is_refutable(tcx, sub); }
|
2011-09-23 18:08:30 -05:00
|
|
|
pat_uniq(sub) { ret is_refutable(tcx, sub); }
|
2011-08-01 08:26:48 -05:00
|
|
|
pat_rec(fields, _) {
|
|
|
|
for field: field_pat in fields {
|
|
|
|
if is_refutable(tcx, field.pat) { ret true; }
|
|
|
|
}
|
|
|
|
ret false;
|
|
|
|
}
|
2011-08-15 06:15:19 -05:00
|
|
|
pat_tup(elts) {
|
2011-08-19 17:16:48 -05:00
|
|
|
for elt in elts { if is_refutable(tcx, elt) { ret true; } }
|
2011-08-15 06:15:19 -05:00
|
|
|
ret false;
|
|
|
|
}
|
2011-08-01 08:26:48 -05:00
|
|
|
pat_tag(_, args) {
|
|
|
|
let vdef = variant_def_ids(tcx.def_map.get(pat.id));
|
2011-08-19 17:16:48 -05:00
|
|
|
if std::vec::len(ty::tag_variants(tcx, vdef.tg)) != 1u { ret true; }
|
|
|
|
for p: @pat in args { if is_refutable(tcx, p) { ret true; } }
|
2011-08-01 08:26:48 -05:00
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
|
|
|
// End:
|