/
check_match.rs
851 lines (804 loc) · 29.4 KB
/
check_match.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
// Copyright 2012 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.
use core::prelude::*;
use middle::const_eval::{compare_const_vals, lookup_const_by_id};
use middle::const_eval::{eval_const_expr, const_val, const_int, const_bool};
use middle::pat_util::*;
use middle::ty::*;
use middle::ty;
use middle::typeck::method_map;
use middle::moves;
use util::ppaux::ty_to_str;
use core::cmp;
use core::option;
use core::uint;
use core::vec;
use std::oldmap::HashMap;
use std::sort;
use syntax::ast::*;
use syntax::ast_util::{variant_def_ids, unguarded_pat, walk_pat};
use syntax::codemap::{span, dummy_sp, spanned};
use syntax::print::pprust::pat_to_str;
use syntax::visit;
pub struct MatchCheckCtxt {
tcx: ty::ctxt,
method_map: method_map,
moves_map: moves::MovesMap
}
pub fn check_crate(tcx: ty::ctxt,
method_map: method_map,
moves_map: moves::MovesMap,
crate: @crate) {
let cx = @MatchCheckCtxt {tcx: tcx,
method_map: method_map,
moves_map: moves_map};
visit::visit_crate(*crate, (), visit::mk_vt(@visit::Visitor {
visit_expr: |a,b,c| check_expr(cx, a, b, c),
visit_local: |a,b,c| check_local(cx, a, b, c),
visit_fn: |kind, decl, body, sp, id, e, v|
check_fn(cx, kind, decl, body, sp, id, e, v),
.. *visit::default_visitor::<()>()
}));
tcx.sess.abort_if_errors();
}
pub fn expr_is_non_moving_lvalue(cx: @MatchCheckCtxt, expr: @expr) -> bool {
if !ty::expr_is_lval(cx.tcx, cx.method_map, expr) {
return false;
}
!cx.moves_map.contains_key(&expr.id)
}
pub fn check_expr(cx: @MatchCheckCtxt, ex: @expr, &&s: (), v: visit::vt<()>) {
visit::visit_expr(ex, s, v);
match ex.node {
expr_match(scrut, ref arms) => {
// First, check legality of move bindings.
let is_non_moving_lvalue = expr_is_non_moving_lvalue(cx, ex);
for arms.each |arm| {
check_legality_of_move_bindings(cx,
is_non_moving_lvalue,
arm.guard.is_some(),
arm.pats);
}
check_arms(cx, (/*bad*/copy *arms));
/* Check for exhaustiveness */
// Check for empty enum, because is_useful only works on inhabited
// types.
let pat_ty = node_id_to_type(cx.tcx, scrut.id);
if (*arms).is_empty() {
if !type_is_empty(cx.tcx, pat_ty) {
// We know the type is inhabited, so this must be wrong
cx.tcx.sess.span_err(ex.span, fmt!("non-exhaustive patterns: \
type %s is non-empty",
ty_to_str(cx.tcx, pat_ty)));
}
// If the type *is* empty, it's vacuously exhaustive
return;
}
match ty::get(pat_ty).sty {
ty_enum(did, _) => {
if (*enum_variants(cx.tcx, did)).is_empty() &&
(*arms).is_empty() {
return;
}
}
_ => { /* We assume only enum types can be uninhabited */ }
}
let arms = vec::concat(arms.filter_mapped(unguarded_pat));
if arms.is_empty() {
cx.tcx.sess.span_err(ex.span, ~"non-exhaustive patterns");
} else {
check_exhaustive(cx, ex.span, arms);
}
}
_ => ()
}
}
// Check for unreachable patterns
pub fn check_arms(cx: @MatchCheckCtxt, arms: ~[arm]) {
let mut seen = ~[];
for arms.each |arm| {
for arm.pats.each |pat| {
let v = ~[*pat];
match is_useful(cx, copy seen, v) {
not_useful => {
cx.tcx.sess.span_err(pat.span, ~"unreachable pattern");
}
_ => ()
}
if arm.guard.is_none() { seen.push(v); }
}
}
}
pub fn raw_pat(p: @pat) -> @pat {
match p.node {
pat_ident(_, _, Some(s)) => { raw_pat(s) }
_ => { p }
}
}
pub fn check_exhaustive(cx: @MatchCheckCtxt, sp: span, pats: ~[@pat]) {
assert(!pats.is_empty());
let ext = match is_useful(cx, vec::map(pats, |p| ~[*p]), ~[wild()]) {
not_useful => {
// This is good, wildcard pattern isn't reachable
return;
}
useful_ => None,
useful(ty, ref ctor) => {
match ty::get(ty).sty {
ty::ty_bool => {
match (*ctor) {
val(const_bool(true)) => Some(@~"true"),
val(const_bool(false)) => Some(@~"false"),
_ => 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);
match variants.find(|v| v.id == vid) {
Some(v) => Some(cx.tcx.sess.str_of(v.name)),
None => {
fail!(~"check_exhaustive: bad variant in ctor")
}
}
}
ty::ty_unboxed_vec(*) | ty::ty_evec(*) => {
match *ctor {
vec(n) => Some(@fmt!("vectors of length %u", n)),
_ => None
}
}
_ => None
}
}
};
let msg = ~"non-exhaustive patterns" + match ext {
Some(ref s) => ~": " + **s + ~" not covered",
None => ~""
};
cx.tcx.sess.span_err(sp, msg);
}
pub type matrix = ~[~[@pat]];
pub enum useful { useful(ty::t, ctor), useful_, not_useful }
#[deriving_eq]
pub enum ctor {
single,
variant(def_id),
val(const_val),
range(const_val, const_val),
vec(uint)
}
// 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).
// Note: is_useful doesn't work on empty types, as the paper notes.
// So it assumes that v is non-empty.
pub fn is_useful(cx: @MatchCheckCtxt, +m: matrix, +v: &[@pat]) -> useful {
if m.len() == 0u { return useful_; }
if m[0].len() == 0u { return not_useful; }
let real_pat = match vec::find(m, |r| r[0].id != 0) {
Some(r) => r[0], None => v[0]
};
let left_ty = if real_pat.id == 0 { ty::mk_nil(cx.tcx) }
else { ty::node_id_to_type(cx.tcx, real_pat.id) };
match pat_ctor_id(cx, v[0]) {
None => {
match missing_ctor(cx, m, left_ty) {
None => {
match ty::get(left_ty).sty {
ty::ty_bool => {
match is_useful_specialized(cx, m, v,
val(const_bool(true)),
0u, left_ty){
not_useful => {
is_useful_specialized(cx, m, v,
val(const_bool(false)),
0u, left_ty)
}
ref u => (/*bad*/copy *u)
}
}
ty::ty_enum(eid, _) => {
for (*ty::enum_variants(cx.tcx, eid)).each |va| {
match is_useful_specialized(cx, m, v, variant(va.id),
va.args.len(), left_ty) {
not_useful => (),
ref u => return (/*bad*/copy *u)
}
}
not_useful
}
ty::ty_unboxed_vec(*) | ty::ty_evec(*) => {
let max_len = do m.foldr(0) |r, max_len| {
match /*bad*/copy r[0].node {
pat_vec(elems, _) => uint::max(elems.len(), max_len),
_ => max_len
}
};
for uint::range(0, max_len + 1) |n| {
match is_useful_specialized(cx, m, v, vec(n), n, left_ty) {
not_useful => (),
ref u => return (/*bad*/copy *u)
}
}
not_useful
}
_ => {
let arity = ctor_arity(cx, single, left_ty);
is_useful_specialized(cx, m, v, single, arity, left_ty)
}
}
}
Some(ref ctor) => {
match is_useful(cx,
vec::filter_map(m, |r| default(cx, r)),
vec::tail(v)) {
useful_ => useful(left_ty, (/*bad*/copy *ctor)),
ref u => (/*bad*/copy *u)
}
}
}
}
Some(ref v0_ctor) => {
let arity = ctor_arity(cx, (*v0_ctor), left_ty);
is_useful_specialized(cx, m, v, /*bad*/copy *v0_ctor, arity, left_ty)
}
}
}
pub fn is_useful_specialized(cx: @MatchCheckCtxt,
m: matrix,
+v: &[@pat],
+ctor: ctor,
arity: uint,
lty: ty::t)
-> useful {
let ms = m.filter_mapped(|r| specialize(cx, *r, ctor, arity, lty));
let could_be_useful = is_useful(
cx, ms, specialize(cx, v, ctor, arity, lty).get());
match could_be_useful {
useful_ => useful(lty, ctor),
ref u => (/*bad*/copy *u)
}
}
pub fn pat_ctor_id(cx: @MatchCheckCtxt, p: @pat) -> Option<ctor> {
let pat = raw_pat(p);
match /*bad*/copy pat.node {
pat_wild => { None }
pat_ident(_, _, _) | pat_enum(_, _) => {
match cx.tcx.def_map.find(&pat.id) {
Some(def_variant(_, id)) => Some(variant(id)),
Some(def_const(did)) => {
let const_expr = lookup_const_by_id(cx.tcx, did).get();
Some(val(eval_const_expr(cx.tcx, const_expr)))
}
_ => None
}
}
pat_lit(expr) => { Some(val(eval_const_expr(cx.tcx, expr))) }
pat_range(lo, hi) => {
Some(range(eval_const_expr(cx.tcx, lo), eval_const_expr(cx.tcx, hi)))
}
pat_struct(*) => {
match cx.tcx.def_map.find(&pat.id) {
Some(def_variant(_, id)) => Some(variant(id)),
_ => Some(single)
}
}
pat_box(_) | pat_uniq(_) | pat_rec(_, _) | pat_tup(_) |
pat_region(*) => {
Some(single)
}
pat_vec(elems, tail) => {
match tail {
Some(_) => None,
None => Some(vec(elems.len()))
}
}
}
}
pub fn is_wild(cx: @MatchCheckCtxt, p: @pat) -> bool {
let pat = raw_pat(p);
match pat.node {
pat_wild => { true }
pat_ident(_, _, _) => {
match cx.tcx.def_map.find(&pat.id) {
Some(def_variant(_, _)) | Some(def_const(*)) => { false }
_ => { true }
}
}
_ => { false }
}
}
pub fn missing_ctor(cx: @MatchCheckCtxt,
m: matrix,
left_ty: ty::t)
-> Option<ctor> {
match ty::get(left_ty).sty {
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_rptr(*) | ty::ty_tup(_) |
ty::ty_rec(_) | ty::ty_struct(*) => {
for m.each |r| {
if !is_wild(cx, r[0]) { return None; }
}
return Some(single);
}
ty::ty_enum(eid, _) => {
let mut found = ~[];
for m.each |r| {
do option::iter(&pat_ctor_id(cx, r[0])) |id| {
if !vec::contains(found, id) {
found.push(/*bad*/copy *id);
}
}
}
let variants = ty::enum_variants(cx.tcx, eid);
if found.len() != (*variants).len() {
for vec::each(*variants) |v| {
if !found.contains(&(variant(v.id))) {
return Some(variant(v.id));
}
}
fail!();
} else { None }
}
ty::ty_nil => None,
ty::ty_bool => {
let mut true_found = false, false_found = false;
for m.each |r| {
match pat_ctor_id(cx, r[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_unboxed_vec(*) | ty::ty_evec(*) => {
// Find the lengths and tails of all vector patterns.
let vec_pat_lens = do m.filter_mapped |r| {
match r[0].node {
pat_vec(ref elems, ref tail) => {
Some((elems.len(), tail.is_some()))
}
_ => None
}
};
// Sort them by length such that for patterns of the same length,
// those with a destructured tail come first.
let mut sorted_vec_lens = sort::merge_sort(vec_pat_lens,
|&(len1, tail1), &(len2, tail2)| {
if len1 == len2 {
tail1 > tail2
} else {
len1 <= len2
}
}
);
vec::dedup(&mut sorted_vec_lens);
let mut found_tail = false;
let mut next = 0;
let mut missing = None;
for sorted_vec_lens.each |&(length, tail)| {
if length != next {
missing = Some(next);
break;
}
if tail {
found_tail = true;
break;
}
next += 1;
}
// We found patterns of all lengths within <0, next), yet there was no
// pattern with a tail - therefore, we report vec(next) as missing.
if !found_tail {
missing = Some(next);
}
match missing {
Some(k) => Some(vec(k)),
None => None
}
}
_ => Some(single)
}
}
pub fn ctor_arity(cx: @MatchCheckCtxt, ctor: ctor, ty: ty::t) -> uint {
match /*bad*/copy ty::get(ty).sty {
ty::ty_tup(fs) => fs.len(),
ty::ty_rec(fs) => fs.len(),
ty::ty_box(_) | ty::ty_uniq(_) | ty::ty_rptr(*) => 1u,
ty::ty_enum(eid, _) => {
let id = match ctor { variant(id) => id,
_ => fail!(~"impossible case") };
match vec::find(*ty::enum_variants(cx.tcx, eid), |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_unboxed_vec(*) | ty::ty_evec(*) => {
match ctor {
vec(n) => n,
_ => 0u
}
}
_ => 0u
}
}
pub fn wild() -> @pat {
@pat {id: 0, node: pat_wild, span: dummy_sp()}
}
pub fn specialize(cx: @MatchCheckCtxt,
+r: &[@pat],
ctor_id: ctor,
arity: uint,
left_ty: ty::t)
-> Option<~[@pat]> {
// Sad, but I can't get rid of this easily
let mut r0 = copy *raw_pat(r[0]);
match r0 {
pat{id: pat_id, node: n, span: pat_span} =>
match n {
pat_wild => Some(vec::append(vec::from_elem(arity, wild()),
vec::tail(r))),
pat_ident(_, _, _) => {
match cx.tcx.def_map.find(&pat_id) {
Some(def_variant(_, id)) => {
if variant(id) == ctor_id { Some(vec::tail(r)) }
else { None }
}
Some(def_const(did)) => {
let const_expr =
lookup_const_by_id(cx.tcx, did).get();
let e_v = eval_const_expr(cx.tcx, const_expr);
let match_ = match ctor_id {
val(ref v) => compare_const_vals(e_v, (*v)) == 0,
range(ref c_lo, ref c_hi) => {
compare_const_vals((*c_lo), e_v) >= 0 &&
compare_const_vals((*c_hi), e_v) <= 0
}
single => true,
_ => fail!(~"type error")
};
if match_ { Some(vec::tail(r)) } else { None }
}
_ => Some(vec::append(vec::from_elem(arity, wild()),
vec::tail(r)))
}
}
pat_enum(_, args) => {
match cx.tcx.def_map.get(&pat_id) {
def_variant(_, id) if variant(id) == ctor_id => {
let args = match args {
Some(args) => args,
None => vec::from_elem(arity, wild())
};
Some(vec::append(args, vec::tail(r)))
}
def_variant(_, _) => None,
def_struct(*) => {
// FIXME #4731: Is this right? --pcw
let new_args;
match args {
Some(args) => new_args = args,
None => new_args = vec::from_elem(arity, wild())
}
Some(vec::append(new_args, vec::tail(r)))
}
_ => None
}
}
pat_rec(ref flds, _) => {
let ty_flds = match /*bad*/copy ty::get(left_ty).sty {
ty::ty_rec(flds) => flds,
_ => fail!(~"bad type for pat_rec")
};
let args = vec::map(ty_flds, |ty_fld| {
match flds.find(|f| f.ident == ty_fld.ident) {
Some(f) => f.pat,
_ => wild()
}
});
Some(vec::append(args, vec::tail(r)))
}
pat_struct(_, ref flds, _) => {
// Is this a struct or an enum variant?
match cx.tcx.def_map.get(&pat_id) {
def_variant(_, variant_id) => {
if variant(variant_id) == ctor_id {
// FIXME #4731: Is this right? --pcw
let args = flds.map(|ty_field| {
match flds.find(|f|
f.ident == ty_field.ident) {
Some(f) => f.pat,
_ => wild()
}
});
Some(vec::append(args, vec::tail(r)))
} else {
None
}
}
_ => {
// Grab the class data that we care about.
let class_fields, class_id;
match ty::get(left_ty).sty {
ty::ty_struct(cid, _) => {
class_id = cid;
class_fields =
ty::lookup_struct_fields(cx.tcx,
class_id);
}
_ => {
cx.tcx.sess.span_bug(pat_span,
~"struct pattern didn't resolve to a struct");
}
}
let args = vec::map(class_fields, |class_field| {
match flds.find(|f|
f.ident == class_field.ident) {
Some(f) => f.pat,
_ => wild()
}
});
Some(vec::append(args, vec::tail(r)))
}
}
}
pat_tup(args) => Some(vec::append(args, vec::tail(r))),
pat_box(a) | pat_uniq(a) | pat_region(a) =>
Some(vec::append(~[a], vec::tail(r))),
pat_lit(expr) => {
let e_v = eval_const_expr(cx.tcx, expr);
let match_ = match ctor_id {
val(ref v) => compare_const_vals(e_v, (*v)) == 0,
range(ref c_lo, ref c_hi) => {
compare_const_vals((*c_lo), e_v) >= 0 &&
compare_const_vals((*c_hi), e_v) <= 0
}
single => true,
_ => fail!(~"type error")
};
if match_ { Some(vec::tail(r)) } else { None }
}
pat_range(lo, hi) => {
let (c_lo, c_hi) = match ctor_id {
val(ref v) => ((/*bad*/copy *v), (/*bad*/copy *v)),
range(ref lo, ref hi) =>
((/*bad*/copy *lo), (/*bad*/copy *hi)),
single => return Some(vec::tail(r)),
_ => fail!(~"type error")
};
let v_lo = eval_const_expr(cx.tcx, lo),
v_hi = eval_const_expr(cx.tcx, hi);
let match_ = compare_const_vals(c_lo, v_lo) >= 0 &&
compare_const_vals(c_hi, v_hi) <= 0;
if match_ { Some(vec::tail(r)) } else { None }
}
pat_vec(elems, tail) => {
match ctor_id {
vec(_) => {
let num_elements = elems.len();
if num_elements < arity && tail.is_some() {
Some(vec::append(
vec::append(elems, vec::from_elem(
arity - num_elements, wild()
)),
vec::tail(r)
))
} else if num_elements == arity {
Some(vec::append(elems, vec::tail(r)))
} else {
None
}
}
_ => None
}
}
}
}
}
pub fn default(cx: @MatchCheckCtxt, r: ~[@pat]) -> Option<~[@pat]> {
if is_wild(cx, r[0]) { Some(vec::tail(r)) }
else { None }
}
pub fn check_local(cx: @MatchCheckCtxt,
loc: @local,
&&s: (),
v: visit::vt<()>) {
visit::visit_local(loc, s, v);
if is_refutable(cx, loc.node.pat) {
cx.tcx.sess.span_err(loc.node.pat.span,
~"refutable pattern in local binding");
}
// Check legality of move bindings.
let is_lvalue = match loc.node.init {
Some(init) => expr_is_non_moving_lvalue(cx, init),
None => true
};
check_legality_of_move_bindings(cx, is_lvalue, false, [ loc.node.pat ]);
}
pub fn check_fn(cx: @MatchCheckCtxt,
kind: visit::fn_kind,
decl: fn_decl,
body: blk,
sp: span,
id: node_id,
&&s: (),
v: visit::vt<()>) {
visit::visit_fn(kind, decl, body, sp, id, s, v);
for decl.inputs.each |input| {
if is_refutable(cx, input.pat) {
cx.tcx.sess.span_err(input.pat.span,
~"refutable pattern in function argument");
}
}
}
pub fn is_refutable(cx: @MatchCheckCtxt, pat: &pat) -> bool {
match cx.tcx.def_map.find(&pat.id) {
Some(def_variant(enum_id, _)) => {
if vec::len(*ty::enum_variants(cx.tcx, enum_id)) != 1u {
return true;
}
}
Some(def_const(*)) => return true,
_ => ()
}
match /*bad*/copy pat.node {
pat_box(sub) | pat_uniq(sub) | pat_region(sub) |
pat_ident(_, _, Some(sub)) => {
is_refutable(cx, sub)
}
pat_wild | pat_ident(_, _, None) => { false }
pat_lit(@expr {node: expr_lit(@spanned { node: lit_nil, _}), _}) => {
// "()"
false
}
pat_lit(_) | pat_range(_, _) => { true }
pat_rec(fields, _) => {
fields.any(|f| is_refutable(cx, f.pat))
}
pat_struct(_, fields, _) => {
fields.any(|f| is_refutable(cx, f.pat))
}
pat_tup(elts) => {
elts.any(|elt| is_refutable(cx, *elt))
}
pat_enum(_, Some(args)) => {
args.any(|a| is_refutable(cx, *a))
}
pat_enum(_,_) => { false }
pat_vec(*) => { true }
}
}
// Legality of move bindings checking
pub fn check_legality_of_move_bindings(cx: @MatchCheckCtxt,
is_lvalue: bool,
has_guard: bool,
pats: &[@pat]) {
let tcx = cx.tcx;
let def_map = tcx.def_map;
let mut by_ref_span = None;
let mut any_by_move = false;
for pats.each |pat| {
do pat_bindings(def_map, *pat) |bm, id, span, _path| {
match bm {
bind_by_copy => {}
bind_by_ref(_) => {
by_ref_span = Some(span);
}
bind_infer => {
if cx.moves_map.contains_key(&id) {
any_by_move = true;
}
}
}
}
}
let check_move: &fn(@pat, Option<@pat>) = |p, sub| {
// check legality of moving out of the enum
if sub.is_some() {
tcx.sess.span_err(
p.span,
~"cannot bind by-move with sub-bindings");
} else if has_guard {
tcx.sess.span_err(
p.span,
~"cannot bind by-move into a pattern guard");
} else if by_ref_span.is_some() {
tcx.sess.span_err(
p.span,
~"cannot bind by-move and by-ref \
in the same pattern");
tcx.sess.span_note(
by_ref_span.get(),
~"by-ref binding occurs here");
} else if is_lvalue {
tcx.sess.span_err(
p.span,
~"cannot bind by-move when \
matching an lvalue");
}
};
if !any_by_move { return; } // pointless micro-optimization
for pats.each |pat| {
do walk_pat(*pat) |p| {
if pat_is_binding(def_map, p) {
match p.node {
pat_ident(_, _, sub) => {
if cx.moves_map.contains_key(&p.id) {
check_move(p, sub);
}
}
_ => {
cx.tcx.sess.span_bug(
p.span,
fmt!("Binding pattern %d is \
not an identifier: %?",
p.id, p.node));
}
}
}
}
// Now check to ensure that any move binding is not behind an @ or &.
// This is always illegal.
let vt = visit::mk_vt(@visit::Visitor {
visit_pat: |pat, behind_bad_pointer: bool, v| {
match pat.node {
pat_ident(_, _, sub) => {
debug!("(check legality of move) checking pat \
ident with behind_bad_pointer %?",
behind_bad_pointer);
if behind_bad_pointer &&
cx.moves_map.contains_key(&pat.id)
{
cx.tcx.sess.span_err(
pat.span,
~"by-move pattern \
bindings may not occur \
behind @ or & bindings");
}
match sub {
None => {}
Some(subpat) => {
(v.visit_pat)(subpat, behind_bad_pointer, v);
}
}
}
pat_box(subpat) | pat_region(subpat) => {
(v.visit_pat)(subpat, true, v);
}
_ => visit::visit_pat(pat, behind_bad_pointer, v)
}
},
.. *visit::default_visitor::<bool>()
});
(vt.visit_pat)(*pat, false, vt);
}
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End: