]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/matches/test.rs
Rollup merge of #51099 - Crazycolorz5:expectedcloseparen, r=estebank
[rust.git] / src / librustc_mir / build / matches / test.rs
1 // Copyright 2015 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.
4 //
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.
10
11 // Testing candidates
12 //
13 // After candidates have been simplified, the only match pairs that
14 // remain are those that require some sort of test. The functions here
15 // identify what tests are needed, perform the tests, and then filter
16 // the candidates based on the result.
17
18 use build::Builder;
19 use build::matches::{Candidate, MatchPair, Test, TestKind};
20 use hair::*;
21 use rustc_data_structures::fx::FxHashMap;
22 use rustc_data_structures::bitvec::BitVector;
23 use rustc::ty::{self, Ty};
24 use rustc::ty::util::IntTypeExt;
25 use rustc::mir::*;
26 use rustc::hir::{RangeEnd, Mutability};
27 use syntax_pos::Span;
28 use std::cmp::Ordering;
29
30 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
31     /// Identifies what test is needed to decide if `match_pair` is applicable.
32     ///
33     /// It is a bug to call this with a simplifyable pattern.
34     pub fn test<'pat>(&mut self, match_pair: &MatchPair<'pat, 'tcx>) -> Test<'tcx> {
35         match *match_pair.pattern.kind {
36             PatternKind::Variant { ref adt_def, substs: _, variant_index: _, subpatterns: _ } => {
37                 Test {
38                     span: match_pair.pattern.span,
39                     kind: TestKind::Switch {
40                         adt_def: adt_def.clone(),
41                         variants: BitVector::new(adt_def.variants.len()),
42                     },
43                 }
44             }
45
46             PatternKind::Constant { .. }
47             if is_switch_ty(match_pair.pattern.ty) => {
48                 // for integers, we use a SwitchInt match, which allows
49                 // us to handle more cases
50                 Test {
51                     span: match_pair.pattern.span,
52                     kind: TestKind::SwitchInt {
53                         switch_ty: match_pair.pattern.ty,
54
55                         // these maps are empty to start; cases are
56                         // added below in add_cases_to_switch
57                         options: vec![],
58                         indices: FxHashMap(),
59                     }
60                 }
61             }
62
63             PatternKind::Constant { value } => {
64                 Test {
65                     span: match_pair.pattern.span,
66                     kind: TestKind::Eq {
67                         value,
68                         ty: match_pair.pattern.ty.clone()
69                     }
70                 }
71             }
72
73             PatternKind::Range { lo, hi, end } => {
74                 Test {
75                     span: match_pair.pattern.span,
76                     kind: TestKind::Range {
77                         lo: Literal::Value { value: lo },
78                         hi: Literal::Value { value: hi },
79                         ty: match_pair.pattern.ty.clone(),
80                         end,
81                     },
82                 }
83             }
84
85             PatternKind::Slice { ref prefix, ref slice, ref suffix }
86                     if !match_pair.slice_len_checked => {
87                 let len = prefix.len() + suffix.len();
88                 let op = if slice.is_some() {
89                     BinOp::Ge
90                 } else {
91                     BinOp::Eq
92                 };
93                 Test {
94                     span: match_pair.pattern.span,
95                     kind: TestKind::Len { len: len as u64, op: op },
96                 }
97             }
98
99             PatternKind::Array { .. } |
100             PatternKind::Slice { .. } |
101             PatternKind::Wild |
102             PatternKind::Binding { .. } |
103             PatternKind::Leaf { .. } |
104             PatternKind::Deref { .. } => {
105                 self.error_simplifyable(match_pair)
106             }
107         }
108     }
109
110     pub fn add_cases_to_switch<'pat>(&mut self,
111                                      test_place: &Place<'tcx>,
112                                      candidate: &Candidate<'pat, 'tcx>,
113                                      switch_ty: Ty<'tcx>,
114                                      options: &mut Vec<u128>,
115                                      indices: &mut FxHashMap<&'tcx ty::Const<'tcx>, usize>)
116                                      -> bool
117     {
118         let match_pair = match candidate.match_pairs.iter().find(|mp| mp.place == *test_place) {
119             Some(match_pair) => match_pair,
120             _ => { return false; }
121         };
122
123         match *match_pair.pattern.kind {
124             PatternKind::Constant { value } => {
125                 let switch_ty = ty::ParamEnv::empty().and(switch_ty);
126                 indices.entry(value)
127                        .or_insert_with(|| {
128                            options.push(value.unwrap_bits(self.hir.tcx(), switch_ty));
129                            options.len() - 1
130                        });
131                 true
132             }
133             PatternKind::Variant { .. } => {
134                 panic!("you should have called add_variants_to_switch instead!");
135             }
136             PatternKind::Range { .. } |
137             PatternKind::Slice { .. } |
138             PatternKind::Array { .. } |
139             PatternKind::Wild |
140             PatternKind::Binding { .. } |
141             PatternKind::Leaf { .. } |
142             PatternKind::Deref { .. } => {
143                 // don't know how to add these patterns to a switch
144                 false
145             }
146         }
147     }
148
149     pub fn add_variants_to_switch<'pat>(&mut self,
150                                         test_place: &Place<'tcx>,
151                                         candidate: &Candidate<'pat, 'tcx>,
152                                         variants: &mut BitVector)
153                                         -> bool
154     {
155         let match_pair = match candidate.match_pairs.iter().find(|mp| mp.place == *test_place) {
156             Some(match_pair) => match_pair,
157             _ => { return false; }
158         };
159
160         match *match_pair.pattern.kind {
161             PatternKind::Variant { adt_def: _ , variant_index,  .. } => {
162                 // We have a pattern testing for variant `variant_index`
163                 // set the corresponding index to true
164                 variants.insert(variant_index);
165                 true
166             }
167             _ => {
168                 // don't know how to add these patterns to a switch
169                 false
170             }
171         }
172     }
173
174     /// Generates the code to perform a test.
175     pub fn perform_test(&mut self,
176                         block: BasicBlock,
177                         place: &Place<'tcx>,
178                         test: &Test<'tcx>)
179                         -> Vec<BasicBlock> {
180         debug!("perform_test({:?}, {:?}: {:?}, {:?})",
181                block,
182                place,
183                place.ty(&self.local_decls, self.hir.tcx()),
184                test);
185         let source_info = self.source_info(test.span);
186         match test.kind {
187             TestKind::Switch { adt_def, ref variants } => {
188                 // Variants is a BitVec of indexes into adt_def.variants.
189                 let num_enum_variants = adt_def.variants.len();
190                 let used_variants = variants.count();
191                 let mut otherwise_block = None;
192                 let mut target_blocks = Vec::with_capacity(num_enum_variants);
193                 let mut targets = Vec::with_capacity(used_variants + 1);
194                 let mut values = Vec::with_capacity(used_variants);
195                 let tcx = self.hir.tcx();
196                 for (idx, discr) in adt_def.discriminants(tcx).enumerate() {
197                     target_blocks.push(if variants.contains(idx) {
198                         values.push(discr.val);
199                         targets.push(self.cfg.start_new_block());
200                         *targets.last().unwrap()
201                     } else {
202                         if otherwise_block.is_none() {
203                             otherwise_block = Some(self.cfg.start_new_block());
204                         }
205                         otherwise_block.unwrap()
206                     });
207                 }
208                 if let Some(otherwise_block) = otherwise_block {
209                     targets.push(otherwise_block);
210                 } else {
211                     targets.push(self.unreachable_block());
212                 }
213                 debug!("num_enum_variants: {}, tested variants: {:?}, variants: {:?}",
214                        num_enum_variants, values, variants);
215                 let discr_ty = adt_def.repr.discr_type().to_ty(tcx);
216                 let discr = self.temp(discr_ty, test.span);
217                 self.cfg.push_assign(block, source_info, &discr,
218                                      Rvalue::Discriminant(place.clone()));
219                 assert_eq!(values.len() + 1, targets.len());
220                 self.cfg.terminate(block, source_info, TerminatorKind::SwitchInt {
221                     discr: Operand::Move(discr),
222                     switch_ty: discr_ty,
223                     values: From::from(values),
224                     targets,
225                 });
226                 target_blocks
227             }
228
229             TestKind::SwitchInt { switch_ty, ref options, indices: _ } => {
230                 let (ret, terminator) = if switch_ty.sty == ty::TyBool {
231                     assert!(options.len() > 0 && options.len() <= 2);
232                     let (true_bb, false_bb) = (self.cfg.start_new_block(),
233                                                self.cfg.start_new_block());
234                     let ret = match options[0] {
235                         1 => vec![true_bb, false_bb],
236                         0 => vec![false_bb, true_bb],
237                         v => span_bug!(test.span, "expected boolean value but got {:?}", v)
238                     };
239                     (ret, TerminatorKind::if_(self.hir.tcx(), Operand::Copy(place.clone()),
240                                               true_bb, false_bb))
241                 } else {
242                     // The switch may be inexhaustive so we
243                     // add a catch all block
244                     let otherwise = self.cfg.start_new_block();
245                     let targets: Vec<_> =
246                         options.iter()
247                                .map(|_| self.cfg.start_new_block())
248                                .chain(Some(otherwise))
249                                .collect();
250                     (targets.clone(), TerminatorKind::SwitchInt {
251                         discr: Operand::Copy(place.clone()),
252                         switch_ty,
253                         values: options.clone().into(),
254                         targets,
255                     })
256                 };
257                 self.cfg.terminate(block, source_info, terminator);
258                 ret
259             }
260
261             TestKind::Eq { value, mut ty } => {
262                 let mut val = Operand::Copy(place.clone());
263                 let mut expect = self.literal_operand(test.span, ty, Literal::Value {
264                     value
265                 });
266                 // Use PartialEq::eq instead of BinOp::Eq
267                 // (the binop can only handle primitives)
268                 let fail = self.cfg.start_new_block();
269                 if !ty.is_scalar() {
270                     // If we're using b"..." as a pattern, we need to insert an
271                     // unsizing coercion, as the byte string has the type &[u8; N].
272                     //
273                     // We want to do this even when the scrutinee is a reference to an
274                     // array, so we can call `<[u8]>::eq` rather than having to find an
275                     // `<[u8; N]>::eq`.
276                     let unsize = |ty: Ty<'tcx>| match ty.sty {
277                         ty::TyRef(region, rty, _) => match rty.sty {
278                             ty::TyArray(inner_ty, n) => Some((region, inner_ty, n)),
279                             _ => None,
280                         },
281                         _ => None,
282                     };
283                     let opt_ref_ty = unsize(ty);
284                     let opt_ref_test_ty = unsize(value.ty);
285                     let mut place = place.clone();
286                     match (opt_ref_ty, opt_ref_test_ty) {
287                         // nothing to do, neither is an array
288                         (None, None) => {},
289                         (Some((region, elem_ty, _)), _) |
290                         (None, Some((region, elem_ty, _))) => {
291                             let tcx = self.hir.tcx();
292                             // make both a slice
293                             ty = tcx.mk_imm_ref(region, tcx.mk_slice(elem_ty));
294                             if opt_ref_ty.is_some() {
295                                 place = self.temp(ty, test.span);
296                                 self.cfg.push_assign(block, source_info, &place,
297                                                     Rvalue::Cast(CastKind::Unsize, val, ty));
298                             }
299                             if opt_ref_test_ty.is_some() {
300                                 let array = self.literal_operand(
301                                     test.span,
302                                     value.ty,
303                                     Literal::Value {
304                                         value
305                                     },
306                                 );
307
308                                 let slice = self.temp(ty, test.span);
309                                 self.cfg.push_assign(block, source_info, &slice,
310                                                     Rvalue::Cast(CastKind::Unsize, array, ty));
311                                 expect = Operand::Move(slice);
312                             }
313                         },
314                     }
315                     let eq_def_id = self.hir.tcx().lang_items().eq_trait().unwrap();
316                     let (mty, method) = self.hir.trait_method(eq_def_id, "eq", ty, &[ty.into()]);
317
318                     // take the argument by reference
319                     let region_scope = self.topmost_scope();
320                     let region = self.hir.tcx().mk_region(ty::ReScope(region_scope));
321                     let tam = ty::TypeAndMut {
322                         ty,
323                         mutbl: Mutability::MutImmutable,
324                     };
325                     let ref_ty = self.hir.tcx().mk_ref(region, tam);
326
327                     // let lhs_ref_place = &lhs;
328                     let ref_rvalue = Rvalue::Ref(region, BorrowKind::Shared, place.clone());
329                     let lhs_ref_place = self.temp(ref_ty, test.span);
330                     self.cfg.push_assign(block, source_info, &lhs_ref_place, ref_rvalue);
331                     let val = Operand::Move(lhs_ref_place);
332
333                     // let rhs_place = rhs;
334                     let rhs_place = self.temp(ty, test.span);
335                     self.cfg.push_assign(block, source_info, &rhs_place, Rvalue::Use(expect));
336
337                     // let rhs_ref_place = &rhs_place;
338                     let ref_rvalue = Rvalue::Ref(region, BorrowKind::Shared, rhs_place);
339                     let rhs_ref_place = self.temp(ref_ty, test.span);
340                     self.cfg.push_assign(block, source_info, &rhs_ref_place, ref_rvalue);
341                     let expect = Operand::Move(rhs_ref_place);
342
343                     let bool_ty = self.hir.bool_ty();
344                     let eq_result = self.temp(bool_ty, test.span);
345                     let eq_block = self.cfg.start_new_block();
346                     let cleanup = self.diverge_cleanup();
347                     self.cfg.terminate(block, source_info, TerminatorKind::Call {
348                         func: Operand::Constant(box Constant {
349                             span: test.span,
350                             ty: mty,
351                             literal: method
352                         }),
353                         args: vec![val, expect],
354                         destination: Some((eq_result.clone(), eq_block)),
355                         cleanup: Some(cleanup),
356                     });
357
358                     // check the result
359                     let block = self.cfg.start_new_block();
360                     self.cfg.terminate(eq_block, source_info,
361                                        TerminatorKind::if_(self.hir.tcx(),
362                                                            Operand::Move(eq_result),
363                                                            block, fail));
364                     vec![block, fail]
365                 } else {
366                     let block = self.compare(block, fail, test.span, BinOp::Eq, expect, val);
367                     vec![block, fail]
368                 }
369             }
370
371             TestKind::Range { ref lo, ref hi, ty, ref end } => {
372                 // Test `val` by computing `lo <= val && val <= hi`, using primitive comparisons.
373                 let lo = self.literal_operand(test.span, ty.clone(), lo.clone());
374                 let hi = self.literal_operand(test.span, ty.clone(), hi.clone());
375                 let val = Operand::Copy(place.clone());
376
377                 let fail = self.cfg.start_new_block();
378                 let block = self.compare(block, fail, test.span, BinOp::Le, lo, val.clone());
379                 let block = match *end {
380                     RangeEnd::Included => self.compare(block, fail, test.span, BinOp::Le, val, hi),
381                     RangeEnd::Excluded => self.compare(block, fail, test.span, BinOp::Lt, val, hi),
382                 };
383
384                 vec![block, fail]
385             }
386
387             TestKind::Len { len, op } => {
388                 let (usize_ty, bool_ty) = (self.hir.usize_ty(), self.hir.bool_ty());
389                 let (actual, result) = (self.temp(usize_ty, test.span),
390                                         self.temp(bool_ty, test.span));
391
392                 // actual = len(place)
393                 self.cfg.push_assign(block, source_info,
394                                      &actual, Rvalue::Len(place.clone()));
395
396                 // expected = <N>
397                 let expected = self.push_usize(block, source_info, len);
398
399                 // result = actual == expected OR result = actual < expected
400                 self.cfg.push_assign(block, source_info, &result,
401                                      Rvalue::BinaryOp(op,
402                                                       Operand::Move(actual),
403                                                       Operand::Move(expected)));
404
405                 // branch based on result
406                 let (false_bb, true_bb) = (self.cfg.start_new_block(),
407                                            self.cfg.start_new_block());
408                 self.cfg.terminate(block, source_info,
409                                    TerminatorKind::if_(self.hir.tcx(), Operand::Move(result),
410                                                        true_bb, false_bb));
411                 vec![true_bb, false_bb]
412             }
413         }
414     }
415
416     fn compare(&mut self,
417                block: BasicBlock,
418                fail_block: BasicBlock,
419                span: Span,
420                op: BinOp,
421                left: Operand<'tcx>,
422                right: Operand<'tcx>) -> BasicBlock {
423         let bool_ty = self.hir.bool_ty();
424         let result = self.temp(bool_ty, span);
425
426         // result = op(left, right)
427         let source_info = self.source_info(span);
428         self.cfg.push_assign(block, source_info, &result,
429                              Rvalue::BinaryOp(op, left, right));
430
431         // branch based on result
432         let target_block = self.cfg.start_new_block();
433         self.cfg.terminate(block, source_info,
434                            TerminatorKind::if_(self.hir.tcx(), Operand::Move(result),
435                                                target_block, fail_block));
436         target_block
437     }
438
439     /// Given that we are performing `test` against `test_place`,
440     /// this job sorts out what the status of `candidate` will be
441     /// after the test. The `resulting_candidates` vector stores, for
442     /// each possible outcome of `test`, a vector of the candidates
443     /// that will result. This fn should add a (possibly modified)
444     /// clone of candidate into `resulting_candidates` wherever
445     /// appropriate.
446     ///
447     /// So, for example, if this candidate is `x @ Some(P0)` and the
448     /// test is a variant test, then we would add `(x as Option).0 @
449     /// P0` to the `resulting_candidates` entry corresponding to the
450     /// variant `Some`.
451     ///
452     /// However, in some cases, the test may just not be relevant to
453     /// candidate. For example, suppose we are testing whether `foo.x == 22`,
454     /// but in one match arm we have `Foo { x: _, ... }`... in that case,
455     /// the test for what value `x` has has no particular relevance
456     /// to this candidate. In such cases, this function just returns false
457     /// without doing anything. This is used by the overall `match_candidates`
458     /// algorithm to structure the match as a whole. See `match_candidates` for
459     /// more details.
460     ///
461     /// FIXME(#29623). In some cases, we have some tricky choices to
462     /// make.  for example, if we are testing that `x == 22`, but the
463     /// candidate is `x @ 13..55`, what should we do? In the event
464     /// that the test is true, we know that the candidate applies, but
465     /// in the event of false, we don't know that it *doesn't*
466     /// apply. For now, we return false, indicate that the test does
467     /// not apply to this candidate, but it might be we can get
468     /// tighter match code if we do something a bit different.
469     pub fn sort_candidate<'pat>(&mut self,
470                                 test_place: &Place<'tcx>,
471                                 test: &Test<'tcx>,
472                                 candidate: &Candidate<'pat, 'tcx>,
473                                 resulting_candidates: &mut [Vec<Candidate<'pat, 'tcx>>])
474                                 -> bool {
475         // Find the match_pair for this place (if any). At present,
476         // afaik, there can be at most one. (In the future, if we
477         // adopted a more general `@` operator, there might be more
478         // than one, but it'd be very unusual to have two sides that
479         // both require tests; you'd expect one side to be simplified
480         // away.)
481         let tested_match_pair = candidate.match_pairs.iter()
482                                                      .enumerate()
483                                                      .filter(|&(_, mp)| mp.place == *test_place)
484                                                      .next();
485         let (match_pair_index, match_pair) = match tested_match_pair {
486             Some(pair) => pair,
487             None => {
488                 // We are not testing this place. Therefore, this
489                 // candidate applies to ALL outcomes.
490                 return false;
491             }
492         };
493
494         match (&test.kind, &*match_pair.pattern.kind) {
495             // If we are performing a variant switch, then this
496             // informs variant patterns, but nothing else.
497             (&TestKind::Switch { adt_def: tested_adt_def, .. },
498              &PatternKind::Variant { adt_def, variant_index, ref subpatterns, .. }) => {
499                 assert_eq!(adt_def, tested_adt_def);
500                 let new_candidate =
501                     self.candidate_after_variant_switch(match_pair_index,
502                                                         adt_def,
503                                                         variant_index,
504                                                         subpatterns,
505                                                         candidate);
506                 resulting_candidates[variant_index].push(new_candidate);
507                 true
508             }
509             (&TestKind::Switch { .. }, _) => false,
510
511             // If we are performing a switch over integers, then this informs integer
512             // equality, but nothing else.
513             //
514             // FIXME(#29623) we could use PatternKind::Range to rule
515             // things out here, in some cases.
516             (&TestKind::SwitchInt { switch_ty: _, options: _, ref indices },
517              &PatternKind::Constant { ref value })
518             if is_switch_ty(match_pair.pattern.ty) => {
519                 let index = indices[value];
520                 let new_candidate = self.candidate_without_match_pair(match_pair_index,
521                                                                       candidate);
522                 resulting_candidates[index].push(new_candidate);
523                 true
524             }
525             (&TestKind::SwitchInt { .. }, _) => false,
526
527
528             (&TestKind::Len { len: test_len, op: BinOp::Eq },
529              &PatternKind::Slice { ref prefix, ref slice, ref suffix }) => {
530                 let pat_len = (prefix.len() + suffix.len()) as u64;
531                 match (test_len.cmp(&pat_len), slice) {
532                     (Ordering::Equal, &None) => {
533                         // on true, min_len = len = $actual_length,
534                         // on false, len != $actual_length
535                         resulting_candidates[0].push(
536                             self.candidate_after_slice_test(match_pair_index,
537                                                             candidate,
538                                                             prefix,
539                                                             slice.as_ref(),
540                                                             suffix)
541                         );
542                         true
543                     }
544                     (Ordering::Less, _) => {
545                         // test_len < pat_len. If $actual_len = test_len,
546                         // then $actual_len < pat_len and we don't have
547                         // enough elements.
548                         resulting_candidates[1].push(candidate.clone());
549                         true
550                     }
551                     (Ordering::Equal, &Some(_)) | (Ordering::Greater, &Some(_)) => {
552                         // This can match both if $actual_len = test_len >= pat_len,
553                         // and if $actual_len > test_len. We can't advance.
554                         false
555                     }
556                     (Ordering::Greater, &None) => {
557                         // test_len != pat_len, so if $actual_len = test_len, then
558                         // $actual_len != pat_len.
559                         resulting_candidates[1].push(candidate.clone());
560                         true
561                     }
562                 }
563             }
564
565             (&TestKind::Len { len: test_len, op: BinOp::Ge },
566              &PatternKind::Slice { ref prefix, ref slice, ref suffix }) => {
567                 // the test is `$actual_len >= test_len`
568                 let pat_len = (prefix.len() + suffix.len()) as u64;
569                 match (test_len.cmp(&pat_len), slice) {
570                     (Ordering::Equal, &Some(_))  => {
571                         // $actual_len >= test_len = pat_len,
572                         // so we can match.
573                         resulting_candidates[0].push(
574                             self.candidate_after_slice_test(match_pair_index,
575                                                             candidate,
576                                                             prefix,
577                                                             slice.as_ref(),
578                                                             suffix)
579                         );
580                         true
581                     }
582                     (Ordering::Less, _) | (Ordering::Equal, &None) => {
583                         // test_len <= pat_len. If $actual_len < test_len,
584                         // then it is also < pat_len, so the test passing is
585                         // necessary (but insufficient).
586                         resulting_candidates[0].push(candidate.clone());
587                         true
588                     }
589                     (Ordering::Greater, &None) => {
590                         // test_len > pat_len. If $actual_len >= test_len > pat_len,
591                         // then we know we won't have a match.
592                         resulting_candidates[1].push(candidate.clone());
593                         true
594                     }
595                     (Ordering::Greater, &Some(_)) => {
596                         // test_len < pat_len, and is therefore less
597                         // strict. This can still go both ways.
598                         false
599                     }
600                 }
601             }
602
603             (&TestKind::Eq { .. }, _) |
604             (&TestKind::Range { .. }, _) |
605             (&TestKind::Len { .. }, _) => {
606                 // These are all binary tests.
607                 //
608                 // FIXME(#29623) we can be more clever here
609                 let pattern_test = self.test(&match_pair);
610                 if pattern_test.kind == test.kind {
611                     let new_candidate = self.candidate_without_match_pair(match_pair_index,
612                                                                           candidate);
613                     resulting_candidates[0].push(new_candidate);
614                     true
615                 } else {
616                     false
617                 }
618             }
619         }
620     }
621
622     fn candidate_without_match_pair<'pat>(&mut self,
623                                           match_pair_index: usize,
624                                           candidate: &Candidate<'pat, 'tcx>)
625                                           -> Candidate<'pat, 'tcx> {
626         let other_match_pairs =
627             candidate.match_pairs.iter()
628                                  .enumerate()
629                                  .filter(|&(index, _)| index != match_pair_index)
630                                  .map(|(_, mp)| mp.clone())
631                                  .collect();
632         Candidate {
633             span: candidate.span,
634             match_pairs: other_match_pairs,
635             bindings: candidate.bindings.clone(),
636             guard: candidate.guard.clone(),
637             arm_index: candidate.arm_index,
638             pre_binding_block: candidate.pre_binding_block,
639             next_candidate_pre_binding_block: candidate.next_candidate_pre_binding_block,
640         }
641     }
642
643     fn candidate_after_slice_test<'pat>(&mut self,
644                                         match_pair_index: usize,
645                                         candidate: &Candidate<'pat, 'tcx>,
646                                         prefix: &'pat [Pattern<'tcx>],
647                                         opt_slice: Option<&'pat Pattern<'tcx>>,
648                                         suffix: &'pat [Pattern<'tcx>])
649                                         -> Candidate<'pat, 'tcx> {
650         let mut new_candidate =
651             self.candidate_without_match_pair(match_pair_index, candidate);
652         self.prefix_slice_suffix(
653             &mut new_candidate.match_pairs,
654             &candidate.match_pairs[match_pair_index].place,
655             prefix,
656             opt_slice,
657             suffix);
658
659         new_candidate
660     }
661
662     fn candidate_after_variant_switch<'pat>(&mut self,
663                                             match_pair_index: usize,
664                                             adt_def: &'tcx ty::AdtDef,
665                                             variant_index: usize,
666                                             subpatterns: &'pat [FieldPattern<'tcx>],
667                                             candidate: &Candidate<'pat, 'tcx>)
668                                             -> Candidate<'pat, 'tcx> {
669         let match_pair = &candidate.match_pairs[match_pair_index];
670
671         // So, if we have a match-pattern like `x @ Enum::Variant(P1, P2)`,
672         // we want to create a set of derived match-patterns like
673         // `(x as Variant).0 @ P1` and `(x as Variant).1 @ P1`.
674         let elem = ProjectionElem::Downcast(adt_def, variant_index);
675         let downcast_place = match_pair.place.clone().elem(elem); // `(x as Variant)`
676         let consequent_match_pairs =
677             subpatterns.iter()
678                        .map(|subpattern| {
679                            // e.g., `(x as Variant).0`
680                            let place = downcast_place.clone().field(subpattern.field,
681                                                                       subpattern.pattern.ty);
682                            // e.g., `(x as Variant).0 @ P1`
683                            MatchPair::new(place, &subpattern.pattern)
684                        });
685
686         // In addition, we need all the other match pairs from the old candidate.
687         let other_match_pairs =
688             candidate.match_pairs.iter()
689                                  .enumerate()
690                                  .filter(|&(index, _)| index != match_pair_index)
691                                  .map(|(_, mp)| mp.clone());
692
693         let all_match_pairs = consequent_match_pairs.chain(other_match_pairs).collect();
694
695         Candidate {
696             span: candidate.span,
697             match_pairs: all_match_pairs,
698             bindings: candidate.bindings.clone(),
699             guard: candidate.guard.clone(),
700             arm_index: candidate.arm_index,
701             pre_binding_block: candidate.pre_binding_block,
702             next_candidate_pre_binding_block: candidate.next_candidate_pre_binding_block,
703         }
704     }
705
706     fn error_simplifyable<'pat>(&mut self, match_pair: &MatchPair<'pat, 'tcx>) -> ! {
707         span_bug!(match_pair.pattern.span,
708                   "simplifyable pattern found: {:?}",
709                   match_pair.pattern)
710     }
711 }
712
713 fn is_switch_ty<'tcx>(ty: Ty<'tcx>) -> bool {
714     ty.is_integral() || ty.is_char() || ty.is_bool()
715 }