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.
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 //! Simplifying Candidates
13 //! *Simplifying* a match pair `place @ pattern` means breaking it down
14 //! into bindings or other, simpler match pairs. For example:
16 //! - `place @ (P1, P2)` can be simplified to `[place.0 @ P1, place.1 @ P2]`
17 //! - `place @ x` can be simplified to `[]` by binding `x` to `place`
19 //! The `simplify_candidate` routine just repeatedly applies these
20 //! sort of simplifications until there is nothing left to
21 //! simplify. Match pairs cannot be simplified if they require some
22 //! sort of test: for example, testing which variant an enum is, or
23 //! testing a value against a constant.
25 use build::{BlockAnd, BlockAndExtension, Builder};
26 use build::matches::{Ascription, Binding, MatchPair, Candidate};
32 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
33 pub fn simplify_candidate<'pat>(&mut self,
35 candidate: &mut Candidate<'pat, 'tcx>)
37 // repeatedly simplify match pairs until fixed point is reached
39 let match_pairs = mem::replace(&mut candidate.match_pairs, vec![]);
40 let mut progress = match_pairs.len(); // count how many were simplified
41 for match_pair in match_pairs {
42 match self.simplify_match_pair(match_pair, candidate) {
45 candidate.match_pairs.push(match_pair);
46 progress -= 1; // this one was not simplified
51 return block.unit(); // if we were not able to simplify any, done.
56 /// Tries to simplify `match_pair`, returning true if
57 /// successful. If successful, new match pairs and bindings will
58 /// have been pushed into the candidate. If no simplification is
59 /// possible, Err is returned and no changes are made to
61 fn simplify_match_pair<'pat>(&mut self,
62 match_pair: MatchPair<'pat, 'tcx>,
63 candidate: &mut Candidate<'pat, 'tcx>)
64 -> Result<(), MatchPair<'pat, 'tcx>> {
65 match *match_pair.pattern.kind {
66 PatternKind::AscribeUserType { ref subpattern, user_ty } => {
67 candidate.ascriptions.push(Ascription {
68 span: match_pair.pattern.span,
70 source: match_pair.place.clone(),
73 candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern));
78 PatternKind::Wild => {
83 PatternKind::Binding { name, mutability, mode, var, ty, ref subpattern } => {
84 candidate.bindings.push(Binding {
87 span: match_pair.pattern.span,
88 source: match_pair.place.clone(),
94 if let Some(subpattern) = subpattern.as_ref() {
95 // this is the `x @ P` case; have to keep matching against `P` now
96 candidate.match_pairs.push(MatchPair::new(match_pair.place, subpattern));
102 PatternKind::Constant { .. } => {
103 // FIXME normalize patterns when possible
107 PatternKind::Range { .. } => {
111 PatternKind::Slice { ref prefix, ref slice, ref suffix } => {
112 if prefix.is_empty() && slice.is_some() && suffix.is_empty() {
114 self.prefix_slice_suffix(&mut candidate.match_pairs,
125 PatternKind::Variant { adt_def, substs, variant_index, ref subpatterns } => {
126 let irrefutable = adt_def.variants.iter().enumerate().all(|(i, v)| {
127 i == variant_index || {
128 self.hir.tcx().features().never_type &&
129 self.hir.tcx().features().exhaustive_patterns &&
130 self.hir.tcx().is_variant_uninhabited_from_all_modules(v, substs)
134 let place = match_pair.place.downcast(adt_def, variant_index);
135 candidate.match_pairs.extend(self.field_match_pairs(place, subpatterns));
142 PatternKind::Array { ref prefix, ref slice, ref suffix } => {
143 self.prefix_slice_suffix(&mut candidate.match_pairs,
151 PatternKind::Leaf { ref subpatterns } => {
152 // tuple struct, match subpats (if any)
153 candidate.match_pairs
154 .extend(self.field_match_pairs(match_pair.place, subpatterns));
158 PatternKind::Deref { ref subpattern } => {
159 let place = match_pair.place.deref();
160 candidate.match_pairs.push(MatchPair::new(place, subpattern));