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 `lvalue @ pattern` means breaking it down
14 //! into bindings or other, simpler match pairs. For example:
16 //! - `lvalue @ (P1, P2)` can be simplified to `[lvalue.0 @ P1, lvalue.1 @ P2]`
17 //! - `lvalue @ x` can be simplified to `[]` by binding `x` to `lvalue`
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::{Binding, MatchPair, Candidate};
29 use rustc_data_structures::fx::FxHashMap;
33 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
34 pub fn simplify_candidate<'pat>(&mut self,
36 candidate: &mut Candidate<'pat, 'tcx>)
38 // repeatedly simplify match pairs until fixed point is reached
40 let match_pairs = mem::replace(&mut candidate.match_pairs, vec![]);
41 let mut progress = match_pairs.len(); // count how many were simplified
42 for match_pair in match_pairs {
43 match self.simplify_match_pair(match_pair, candidate) {
46 candidate.match_pairs.push(match_pair);
47 progress -= 1; // this one was not simplified
52 return block.unit(); // if we were not able to simplify any, done.
57 /// Tries to simplify `match_pair`, returning true if
58 /// successful. If successful, new match pairs and bindings will
59 /// have been pushed into the candidate. If no simplification is
60 /// possible, Err is returned and no changes are made to
62 fn simplify_match_pair<'pat>(&mut self,
63 match_pair: MatchPair<'pat, 'tcx>,
64 candidate: &mut Candidate<'pat, 'tcx>)
65 -> Result<(), MatchPair<'pat, 'tcx>> {
66 match *match_pair.pattern.kind {
67 PatternKind::Wild => {
72 PatternKind::Binding { name, mutability, mode, var, ty, ref subpattern } => {
73 candidate.bindings.push(Binding {
76 span: match_pair.pattern.span,
77 source: match_pair.lvalue.clone(),
83 if let Some(subpattern) = subpattern.as_ref() {
84 // this is the `x @ P` case; have to keep matching against `P` now
85 candidate.match_pairs.push(MatchPair::new(match_pair.lvalue, subpattern));
91 PatternKind::Constant { .. } => {
92 // FIXME normalize patterns when possible
96 PatternKind::Range { .. } |
97 PatternKind::Slice { .. } => {
101 PatternKind::Variant { adt_def, substs, variant_index, ref subpatterns } => {
102 if self.hir.tcx().sess.features.borrow().never_type {
103 let irrefutable = adt_def.variants.iter().enumerate().all(|(i, v)| {
104 i == variant_index || {
105 let mut visited = FxHashMap::default();
106 let node_set = v.uninhabited_from(&mut visited,
114 let lvalue = match_pair.lvalue.downcast(adt_def, variant_index);
115 candidate.match_pairs.extend(self.field_match_pairs(lvalue, subpatterns));
125 PatternKind::Array { ref prefix, ref slice, ref suffix } => {
126 self.prefix_slice_suffix(&mut candidate.match_pairs,
134 PatternKind::Leaf { ref subpatterns } => {
135 // tuple struct, match subpats (if any)
136 candidate.match_pairs
137 .extend(self.field_match_pairs(match_pair.lvalue, subpatterns));
141 PatternKind::Deref { ref subpattern } => {
142 let lvalue = match_pair.lvalue.deref();
143 candidate.match_pairs.push(MatchPair::new(lvalue, subpattern));