1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use middle::const_eval::{compare_const_vals, const_bool, const_float, const_nil, const_val};
12 use middle::const_eval::{eval_const_expr, lookup_const_by_id};
14 use middle::pat_util::*;
18 use std::gc::{Gc, GC};
19 use std::iter::AdditiveIterator;
20 use std::iter::range_inclusive;
22 use syntax::ast_util::{is_unguarded, walk_pat};
23 use syntax::codemap::{Span, Spanned, DUMMY_SP};
24 use syntax::owned_slice::OwnedSlice;
25 use syntax::print::pprust::pat_to_str;
27 use syntax::visit::{Visitor, FnKind};
28 use util::ppaux::ty_to_str;
30 struct Matrix(Vec<Vec<Gc<Pat>>>);
32 /// Pretty-printer for matrices of patterns, example:
33 /// ++++++++++++++++++++++++++
35 /// ++++++++++++++++++++++++++
36 /// + true + [First] +
37 /// ++++++++++++++++++++++++++
38 /// + true + [Second(true)] +
39 /// ++++++++++++++++++++++++++
41 /// ++++++++++++++++++++++++++
42 /// + _ + [_, _, ..tail] +
43 /// ++++++++++++++++++++++++++
44 impl fmt::Show for Matrix {
45 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46 try!(write!(f, "\n"));
48 let &Matrix(ref m) = self;
49 let pretty_printed_matrix: Vec<Vec<String>> = m.iter().map(|row| {
50 row.iter().map(|&pat| pat_to_str(pat)).collect::<Vec<String>>()
53 let column_count = m.iter().map(|row| row.len()).max().unwrap_or(0u);
54 assert!(m.iter().all(|row| row.len() == column_count));
55 let column_widths: Vec<uint> = range(0, column_count).map(|col| {
56 pretty_printed_matrix.iter().map(|row| row.get(col).len()).max().unwrap_or(0u)
59 let total_width = column_widths.iter().map(|n| *n).sum() + column_count * 3 + 1;
60 let br = String::from_char(total_width, '+');
61 try!(write!(f, "{}\n", br));
62 for row in pretty_printed_matrix.move_iter() {
64 for (column, pat_str) in row.move_iter().enumerate() {
66 f.width = Some(*column_widths.get(column));
67 try!(f.pad(pat_str.as_slice()));
68 try!(write!(f, " +"));
70 try!(write!(f, "\n"));
71 try!(write!(f, "{}\n", br));
77 struct MatchCheckCtxt<'a> {
81 #[deriving(Clone, PartialEq)]
83 /// The constructor of all patterns that don't vary by constructor,
84 /// e.g. struct patterns and fixed-length arrays.
89 ConstantValue(const_val),
90 /// Ranges of literal values (2..5).
91 ConstantRange(const_val, const_val),
92 /// Array patterns of length n.
102 enum WitnessPreference {
108 fn useful(self) -> Option<Vec<Gc<Pat>>> {
110 Useful(pats) => Some(pats),
116 impl<'a> Visitor<()> for MatchCheckCtxt<'a> {
117 fn visit_expr(&mut self, ex: &Expr, _: ()) {
118 check_expr(self, ex);
120 fn visit_local(&mut self, l: &Local, _: ()) {
121 check_local(self, l);
123 fn visit_fn(&mut self, fk: &FnKind, fd: &FnDecl, b: &Block, s: Span, _: NodeId, _: ()) {
124 check_fn(self, fk, fd, b, s);
128 pub fn check_crate(tcx: &ty::ctxt, krate: &Crate) {
129 let mut cx = MatchCheckCtxt { tcx: tcx, };
131 visit::walk_crate(&mut cx, krate, ());
133 tcx.sess.abort_if_errors();
136 fn check_expr(cx: &mut MatchCheckCtxt, ex: &Expr) {
137 visit::walk_expr(cx, ex, ());
139 ExprMatch(scrut, ref arms) => {
140 // First, check legality of move bindings.
141 for arm in arms.iter() {
142 check_legality_of_move_bindings(cx,
144 arm.pats.as_slice());
147 // Second, check for unreachable arms.
148 check_arms(cx, arms.as_slice());
150 // Finally, check if the whole match expression is exhaustive.
151 // Check for empty enum, because is_useful only works on inhabited types.
152 let pat_ty = node_id_to_type(cx.tcx, scrut.id);
153 if (*arms).is_empty() {
154 if !type_is_empty(cx.tcx, pat_ty) {
155 // We know the type is inhabited, so this must be wrong
156 cx.tcx.sess.span_err(ex.span, format!("non-exhaustive patterns: \
157 type {} is non-empty",
158 ty_to_str(cx.tcx, pat_ty)).as_slice());
160 // If the type *is* empty, it's vacuously exhaustive
163 let m: Matrix = Matrix(arms
165 .filter(|&arm| is_unguarded(arm))
166 .flat_map(|arm| arm.pats.iter())
167 .map(|pat| vec!(pat.clone()))
169 check_exhaustive(cx, ex.span, &m);
175 // Check for unreachable patterns
176 fn check_arms(cx: &MatchCheckCtxt, arms: &[Arm]) {
177 let mut seen = Matrix(vec!());
178 for arm in arms.iter() {
179 for pat in arm.pats.iter() {
180 // Check that we do not match against a static NaN (#6804)
181 let pat_matches_nan: |&Pat| -> bool = |p| {
182 let opt_def = cx.tcx.def_map.borrow().find_copy(&p.id);
184 Some(DefStatic(did, false)) => {
185 let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
186 match eval_const_expr(cx.tcx, &*const_expr) {
187 const_float(f) if f.is_nan() => true,
195 walk_pat(&**pat, |p| {
196 if pat_matches_nan(p) {
197 cx.tcx.sess.span_warn(p.span, "unmatchable NaN in pattern, \
198 use the is_nan method in a guard instead");
204 match is_useful(cx, &seen, v.as_slice(), LeaveOutWitness) {
205 NotUseful => cx.tcx.sess.span_err(pat.span, "unreachable pattern"),
208 if arm.guard.is_none() {
209 let Matrix(mut rows) = seen;
217 fn raw_pat(p: Gc<Pat>) -> Gc<Pat> {
219 PatIdent(_, _, Some(s)) => { raw_pat(s) }
224 fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, m: &Matrix) {
225 match is_useful(cx, m, [wild()], ConstructWitness) {
227 let witness = match pats.as_slice() {
228 [witness] => witness,
232 let msg = format!("non-exhaustive patterns: `{0}` not covered", pat_to_str(&*witness));
233 cx.tcx.sess.span_err(sp, msg.as_slice());
236 // This is good, wildcard pattern isn't reachable
241 fn const_val_to_expr(value: &const_val) -> Gc<Expr> {
242 let node = match value {
243 &const_bool(b) => LitBool(b),
244 &const_nil => LitNil,
249 node: ExprLit(box(GC) Spanned { node: node, span: DUMMY_SP }),
254 fn def_to_path(tcx: &ty::ctxt, id: DefId) -> Path {
255 ty::with_path(tcx, id, |mut path| Path {
257 segments: path.last().map(|elem| PathSegment {
258 identifier: Ident::new(elem.name()),
260 types: OwnedSlice::empty()
261 }).move_iter().collect(),
266 /// Constructs a partial witness for a pattern given a list of
267 /// patterns expanded by the specialization step.
269 /// When a pattern P is discovered to be useful, this function is used bottom-up
270 /// to reconstruct a complete witness, e.g. a pattern P' that covers a subset
271 /// of values, V, where each value in that set is not covered by any previously
272 /// used patterns and is covered by the pattern P'. Examples:
274 /// left_ty: tuple of 3 elements
275 /// pats: [10, 20, _] => (10, 20, _)
277 /// left_ty: struct X { a: (bool, &'static str), b: uint}
278 /// pats: [(false, "foo"), 42] => X { a: (false, "foo"), b: 42 }
279 fn construct_witness(cx: &MatchCheckCtxt, ctor: &Constructor,
280 pats: Vec<Gc<Pat>>, left_ty: ty::t) -> Gc<Pat> {
281 let pat = match ty::get(left_ty).sty {
282 ty::ty_tup(_) => PatTup(pats),
284 ty::ty_enum(cid, _) | ty::ty_struct(cid, _) => {
285 let (vid, is_structure) = match ctor {
286 &Variant(vid) => (vid,
287 ty::enum_variant_with_id(cx.tcx, cid, vid).arg_names.is_some()),
291 let fields = ty::lookup_struct_fields(cx.tcx, vid);
292 let field_pats = fields.move_iter()
294 .map(|(field, pat)| FieldPat {
295 ident: Ident::new(field.name),
298 PatStruct(def_to_path(cx.tcx, vid), field_pats, false)
300 PatEnum(def_to_path(cx.tcx, vid), Some(pats))
304 ty::ty_rptr(_, ty::mt { ty: ty, .. }) => {
305 match ty::get(ty).sty {
306 ty::ty_vec(_, Some(n)) => match ctor {
308 assert_eq!(pats.len(), n);
309 PatVec(pats, None, vec!())
313 ty::ty_vec(_, None) => match ctor {
315 assert_eq!(pats.len(), n);
316 PatVec(pats, None, vec!())
320 ty::ty_str => PatWild,
323 assert_eq!(pats.len(), 1);
324 PatRegion(pats.get(0).clone())
330 assert_eq!(pats.len(), 1);
331 PatBox(pats.get(0).clone())
334 ty::ty_vec(_, Some(len)) => {
335 assert_eq!(pats.len(), len);
336 PatVec(pats, None, vec!())
341 ConstantValue(ref v) => PatLit(const_val_to_expr(v)),
354 fn missing_constructor(cx: &MatchCheckCtxt, &Matrix(ref rows): &Matrix,
355 left_ty: ty::t, max_slice_length: uint) -> Option<Constructor> {
356 let used_constructors: Vec<Constructor> = rows.iter()
357 .flat_map(|row| pat_constructors(cx, *row.get(0), left_ty, max_slice_length).move_iter())
359 all_constructors(cx, left_ty, max_slice_length)
361 .find(|c| !used_constructors.contains(c))
364 /// This determines the set of all possible constructors of a pattern matching
365 /// values of type `left_ty`. For vectors, this would normally be an infinite set
366 /// but is instead bounded by the maximum fixed length of slice patterns in
367 /// the column of patterns being analyzed.
368 fn all_constructors(cx: &MatchCheckCtxt, left_ty: ty::t,
369 max_slice_length: uint) -> Vec<Constructor> {
370 match ty::get(left_ty).sty {
372 [true, false].iter().map(|b| ConstantValue(const_bool(*b))).collect(),
375 vec!(ConstantValue(const_nil)),
377 ty::ty_rptr(_, ty::mt { ty: ty, .. }) => match ty::get(ty).sty {
378 ty::ty_vec(_, None) =>
379 range_inclusive(0, max_slice_length).map(|length| Slice(length)).collect(),
383 ty::ty_enum(eid, _) =>
384 ty::enum_variants(cx.tcx, eid)
386 .map(|va| Variant(va.id))
394 // Algorithm from http://moscova.inria.fr/~maranget/papers/warn/index.html
396 // Whether a vector `v` of patterns is 'useful' in relation to a set of such
397 // vectors `m` is defined as there being a set of inputs that will match `v`
398 // but not any of the sets in `m`.
400 // This is used both for reachability checking (if a pattern isn't useful in
401 // relation to preceding patterns, it is not reachable) and exhaustiveness
402 // checking (if a wildcard pattern is useful in relation to a matrix, the
403 // matrix isn't exhaustive).
405 // Note: is_useful doesn't work on empty types, as the paper notes.
406 // So it assumes that v is non-empty.
407 fn is_useful(cx: &MatchCheckCtxt, m @ &Matrix(ref rows): &Matrix,
408 v: &[Gc<Pat>], witness: WitnessPreference) -> Usefulness {
410 if rows.len() == 0u {
411 return Useful(vec!());
413 if rows.get(0).len() == 0u {
416 let real_pat = match rows.iter().find(|r| r.get(0).id != 0) {
418 match r.get(0).node {
419 // An arm of the form `ref x @ sub_pat` has type
420 // `sub_pat`, not `&sub_pat` as `x` itself does.
421 PatIdent(BindByRef(_), _, Some(sub)) => sub,
425 None if v.len() == 0 => return NotUseful,
428 let left_ty = if real_pat.id == 0 {
431 ty::pat_ty(cx.tcx, &*real_pat)
434 let max_slice_length = rows.iter().filter_map(|row| match row.get(0).node {
435 PatVec(ref before, _, ref after) => Some(before.len() + after.len()),
437 }).max().map_or(0, |v| v + 1);
439 let constructors = pat_constructors(cx, v[0], left_ty, max_slice_length);
440 if constructors.is_empty() {
441 match missing_constructor(cx, m, left_ty, max_slice_length) {
443 all_constructors(cx, left_ty, max_slice_length).move_iter().filter_map(|c| {
444 is_useful_specialized(cx, m, v, c.clone(),
445 left_ty, witness).useful().map(|pats| {
446 Useful(match witness {
447 ConstructWitness => {
448 let arity = constructor_arity(cx, &c, left_ty);
450 let pat_slice = pats.as_slice();
451 Vec::from_fn(arity, |i| {
452 pat_slice.get(i).map(|p| p.clone())
453 .unwrap_or_else(|| wild())
456 let mut result = vec!(construct_witness(cx, &c, subpats, left_ty));
457 result.extend(pats.move_iter().skip(arity));
460 LeaveOutWitness => vec!()
463 }).nth(0).unwrap_or(NotUseful)
466 Some(constructor) => {
467 let matrix = Matrix(rows.iter().filter_map(|r|
468 default(cx, r.as_slice())).collect());
469 match is_useful(cx, &matrix, v.tail(), witness) {
470 Useful(pats) => Useful(match witness {
471 ConstructWitness => {
472 let arity = constructor_arity(cx, &constructor, left_ty);
473 let wild_pats = Vec::from_elem(arity, wild());
474 let enum_pat = construct_witness(cx, &constructor, wild_pats, left_ty);
475 (vec!(enum_pat)).append(pats.as_slice())
477 LeaveOutWitness => vec!()
484 constructors.move_iter().filter_map(|c| {
485 is_useful_specialized(cx, m, v, c.clone(), left_ty, witness)
486 .useful().map(|pats| Useful(pats))
487 }).nth(0).unwrap_or(NotUseful)
491 fn is_useful_specialized(cx: &MatchCheckCtxt, &Matrix(ref m): &Matrix, v: &[Gc<Pat>],
492 ctor: Constructor, lty: ty::t, witness: WitnessPreference) -> Usefulness {
493 let arity = constructor_arity(cx, &ctor, lty);
494 let matrix = Matrix(m.iter().filter_map(|r| {
495 specialize(cx, r.as_slice(), &ctor, arity)
497 match specialize(cx, v, &ctor, arity) {
498 Some(v) => is_useful(cx, &matrix, v.as_slice(), witness),
503 /// Determines the constructors that the given pattern can be specialized to.
505 /// In most cases, there's only one constructor that a specific pattern
506 /// represents, such as a specific enum variant or a specific literal value.
507 /// Slice patterns, however, can match slices of different lengths. For instance,
508 /// `[a, b, ..tail]` can match a slice of length 2, 3, 4 and so on.
510 /// On the other hand, a wild pattern and an identifier pattern cannot be
511 /// specialized in any way.
512 fn pat_constructors(cx: &MatchCheckCtxt, p: Gc<Pat>,
513 left_ty: ty::t, max_slice_length: uint) -> Vec<Constructor> {
514 let pat = raw_pat(p);
517 match cx.tcx.def_map.borrow().find(&pat.id) {
518 Some(&DefStatic(did, false)) => {
519 let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
520 vec!(ConstantValue(eval_const_expr(cx.tcx, &*const_expr)))
522 Some(&DefVariant(_, id, _)) => vec!(Variant(id)),
526 match cx.tcx.def_map.borrow().find(&pat.id) {
527 Some(&DefStatic(did, false)) => {
528 let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
529 vec!(ConstantValue(eval_const_expr(cx.tcx, &*const_expr)))
531 Some(&DefVariant(_, id, _)) => vec!(Variant(id)),
535 match cx.tcx.def_map.borrow().find(&pat.id) {
536 Some(&DefVariant(_, id, _)) => vec!(Variant(id)),
540 vec!(ConstantValue(eval_const_expr(cx.tcx, &*expr))),
542 vec!(ConstantRange(eval_const_expr(cx.tcx, &*lo), eval_const_expr(cx.tcx, &*hi))),
543 PatVec(ref before, ref slice, ref after) =>
544 match ty::get(left_ty).sty {
545 ty::ty_vec(_, Some(_)) => vec!(Single),
546 _ => if slice.is_some() {
547 range_inclusive(before.len() + after.len(), max_slice_length)
548 .map(|length| Slice(length))
551 vec!(Slice(before.len() + after.len()))
554 PatBox(_) | PatTup(_) | PatRegion(..) =>
556 PatWild | PatWildMulti =>
559 cx.tcx.sess.bug("unexpanded macro")
563 fn is_wild(cx: &MatchCheckCtxt, p: Gc<Pat>) -> bool {
564 let pat = raw_pat(p);
566 PatWild | PatWildMulti => true,
568 match cx.tcx.def_map.borrow().find(&pat.id) {
569 Some(&DefVariant(_, _, _)) | Some(&DefStatic(..)) => false,
572 PatVec(ref before, Some(_), ref after) =>
573 before.is_empty() && after.is_empty(),
578 /// This computes the arity of a constructor. The arity of a constructor
579 /// is how many subpattern patterns of that constructor should be expanded to.
581 /// For instance, a tuple pattern (_, 42u, Some([])) has the arity of 3.
582 /// A struct pattern's arity is the number of fields it contains, etc.
583 fn constructor_arity(cx: &MatchCheckCtxt, ctor: &Constructor, ty: ty::t) -> uint {
584 match ty::get(ty).sty {
585 ty::ty_tup(ref fs) => fs.len(),
586 ty::ty_box(_) | ty::ty_uniq(_) => 1u,
587 ty::ty_rptr(_, ty::mt { ty: ty, .. }) => match ty::get(ty).sty {
588 ty::ty_vec(_, None) => match *ctor {
589 Slice(length) => length,
595 ty::ty_enum(eid, _) => {
597 Variant(id) => enum_variant_with_id(cx.tcx, eid, id).args.len(),
601 ty::ty_struct(cid, _) => ty::lookup_struct_fields(cx.tcx, cid).len(),
602 ty::ty_vec(_, Some(n)) => n,
607 fn range_covered_by_constructor(ctor: &Constructor,
608 from: &const_val,to: &const_val) -> Option<bool> {
609 let (c_from, c_to) = match *ctor {
610 ConstantValue(ref value) => (value, value),
611 ConstantRange(ref from, ref to) => (from, to),
612 Single => return Some(true),
615 let cmp_from = compare_const_vals(c_from, from);
616 let cmp_to = compare_const_vals(c_to, to);
617 match (cmp_from, cmp_to) {
618 (Some(val1), Some(val2)) => Some(val1 >= 0 && val2 <= 0),
623 /// This is the main specialization step. It expands the first pattern in the given row
624 /// into `arity` patterns based on the constructor. For most patterns, the step is trivial,
625 /// for instance tuple patterns are flattened and box patterns expand into their inner pattern.
627 /// OTOH, slice patterns with a subslice pattern (..tail) can be expanded into multiple
628 /// different patterns.
629 /// Structure patterns with a partial wild pattern (Foo { a: 42, .. }) have their missing
630 /// fields filled with wild patterns.
631 fn specialize(cx: &MatchCheckCtxt, r: &[Gc<Pat>],
632 constructor: &Constructor, arity: uint) -> Option<Vec<Gc<Pat>>> {
634 id: pat_id, node: ref node, span: pat_span
635 } = &(*raw_pat(r[0]));
636 let head: Option<Vec<Gc<Pat>>> = match node {
638 Some(Vec::from_elem(arity, wild())),
641 Some(Vec::from_elem(arity, wild())),
643 &PatIdent(_, _, _) => {
644 let opt_def = cx.tcx.def_map.borrow().find_copy(&pat_id);
646 Some(DefVariant(_, id, _)) => if *constructor == Variant(id) {
651 Some(DefStatic(did, _)) => {
652 let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
653 let e_v = eval_const_expr(cx.tcx, &*const_expr);
654 match range_covered_by_constructor(constructor, &e_v, &e_v) {
655 Some(true) => Some(vec!()),
658 cx.tcx.sess.span_err(pat_span, "mismatched types between arms");
664 Some(Vec::from_elem(arity, wild()))
669 &PatEnum(_, ref args) => {
670 let def = cx.tcx.def_map.borrow().get_copy(&pat_id);
672 DefStatic(did, _) => {
673 let const_expr = lookup_const_by_id(cx.tcx, did).unwrap();
674 let e_v = eval_const_expr(cx.tcx, &*const_expr);
675 match range_covered_by_constructor(constructor, &e_v, &e_v) {
676 Some(true) => Some(vec!()),
679 cx.tcx.sess.span_err(pat_span, "mismatched types between arms");
684 DefVariant(_, id, _) if *constructor != Variant(id) => None,
685 DefVariant(..) | DefFn(..) | DefStruct(..) => {
687 &Some(ref args) => args.clone(),
688 &None => Vec::from_elem(arity, wild())
695 &PatStruct(_, ref pattern_fields, _) => {
696 // Is this a struct or an enum variant?
697 let def = cx.tcx.def_map.borrow().get_copy(&pat_id);
698 let class_id = match def {
699 DefVariant(_, variant_id, _) => if *constructor == Variant(variant_id) {
704 DefStruct(struct_id) => Some(struct_id),
707 class_id.map(|variant_id| {
708 let struct_fields = ty::lookup_struct_fields(cx.tcx, variant_id);
709 let args = struct_fields.iter().map(|sf| {
710 match pattern_fields.iter().find(|f| f.ident.name == sf.name) {
722 &PatBox(ref inner) | &PatRegion(ref inner) =>
723 Some(vec!(inner.clone())),
725 &PatLit(ref expr) => {
726 let expr_value = eval_const_expr(cx.tcx, &**expr);
727 match range_covered_by_constructor(constructor, &expr_value, &expr_value) {
728 Some(true) => Some(vec!()),
731 cx.tcx.sess.span_err(pat_span, "mismatched types between arms");
737 &PatRange(ref from, ref to) => {
738 let from_value = eval_const_expr(cx.tcx, &**from);
739 let to_value = eval_const_expr(cx.tcx, &**to);
740 match range_covered_by_constructor(constructor, &from_value, &to_value) {
741 Some(true) => Some(vec!()),
744 cx.tcx.sess.span_err(pat_span, "mismatched types between arms");
750 &PatVec(ref before, ref slice, ref after) => {
752 // Fixed-length vectors.
754 let mut pats = before.clone();
755 pats.grow_fn(arity - before.len() - after.len(), |_| wild());
756 pats.push_all(after.as_slice());
759 Slice(length) if before.len() + after.len() <= length && slice.is_some() => {
760 let mut pats = before.clone();
761 pats.grow_fn(arity - before.len() - after.len(), |_| wild());
762 pats.push_all(after.as_slice());
765 Slice(length) if before.len() + after.len() == length => {
766 let mut pats = before.clone();
767 pats.push_all(after.as_slice());
775 cx.tcx.sess.span_err(pat_span, "unexpanded macro");
779 head.map(|head| head.append(r.tail()))
782 fn default(cx: &MatchCheckCtxt, r: &[Gc<Pat>]) -> Option<Vec<Gc<Pat>>> {
783 if is_wild(cx, r[0]) {
784 Some(Vec::from_slice(r.tail()))
790 fn check_local(cx: &mut MatchCheckCtxt, loc: &Local) {
791 visit::walk_local(cx, loc, ());
793 let name = match loc.source {
795 LocalFor => "`for` loop"
798 match is_refutable(cx, loc.pat) {
801 "refutable pattern in {} binding: `{}` not covered",
802 name, pat_to_str(&*pat)
804 cx.tcx.sess.span_err(loc.pat.span, msg.as_slice());
809 // Check legality of move bindings.
810 check_legality_of_move_bindings(cx, false, [ loc.pat ]);
813 fn check_fn(cx: &mut MatchCheckCtxt,
818 visit::walk_fn(cx, kind, decl, body, sp, ());
819 for input in decl.inputs.iter() {
820 match is_refutable(cx, input.pat) {
823 "refutable pattern in function argument: `{}` not covered",
826 cx.tcx.sess.span_err(input.pat.span, msg.as_slice());
830 check_legality_of_move_bindings(cx, false, [input.pat]);
834 fn is_refutable(cx: &MatchCheckCtxt, pat: Gc<Pat>) -> Option<Gc<Pat>> {
835 let pats = Matrix(vec!(vec!(pat)));
836 is_useful(cx, &pats, [wild()], ConstructWitness)
839 assert_eq!(pats.len(), 1);
844 // Legality of move bindings checking
846 fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,
850 let def_map = &tcx.def_map;
851 let mut by_ref_span = None;
852 for pat in pats.iter() {
853 pat_bindings(def_map, &**pat, |bm, _, span, _path| {
856 by_ref_span = Some(span);
864 let check_move: |&Pat, Option<Gc<Pat>>| = |p, sub| {
865 // check legality of moving out of the enum
867 // x @ Foo(..) is legal, but x @ Foo(y) isn't.
868 if sub.map_or(false, |p| pat_contains_bindings(def_map, &*p)) {
871 "cannot bind by-move with sub-bindings");
872 } else if has_guard {
875 "cannot bind by-move into a pattern guard");
876 } else if by_ref_span.is_some() {
879 "cannot bind by-move and by-ref \
880 in the same pattern");
882 by_ref_span.unwrap(),
883 "by-ref binding occurs here");
887 for pat in pats.iter() {
888 walk_pat(&**pat, |p| {
889 if pat_is_binding(def_map, &*p) {
891 PatIdent(BindByValue(_), _, sub) => {
892 let pat_ty = ty::node_id_to_type(tcx, p.id);
893 if ty::type_moves_by_default(tcx, pat_ty) {
897 PatIdent(BindByRef(_), _, _) => {
900 cx.tcx.sess.span_bug(
902 format!("binding pattern {} is not an \