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
use build::{BlockAnd, BlockAndExtension, Builder};
use build::matches::{Binding, MatchPair, Candidate};
use hair::*;
use rustc::mir::*;
use rustc_data_structures::fx::FxHashMap;
use std::mem;
impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
pub fn simplify_candidate<'pat>(&mut self,
block: BasicBlock,
candidate: &mut Candidate<'pat, 'tcx>)
-> BlockAnd<()> {
loop {
let match_pairs = mem::replace(&mut candidate.match_pairs, vec![]);
let mut progress = match_pairs.len();
for match_pair in match_pairs {
match self.simplify_match_pair(match_pair, candidate) {
Ok(()) => {}
Err(match_pair) => {
candidate.match_pairs.push(match_pair);
progress -= 1;
}
}
}
if progress == 0 {
return block.unit();
}
}
}
fn simplify_match_pair<'pat>(&mut self,
match_pair: MatchPair<'pat, 'tcx>,
candidate: &mut Candidate<'pat, 'tcx>)
-> Result<(), MatchPair<'pat, 'tcx>> {
match *match_pair.pattern.kind {
PatternKind::Wild => {
Ok(())
}
PatternKind::Binding { name, mutability, mode, var, ty, ref subpattern } => {
candidate.bindings.push(Binding {
name: name,
mutability: mutability,
span: match_pair.pattern.span,
source: match_pair.lvalue.clone(),
var_id: var,
var_ty: ty,
binding_mode: mode,
});
if let Some(subpattern) = subpattern.as_ref() {
candidate.match_pairs.push(MatchPair::new(match_pair.lvalue, subpattern));
}
Ok(())
}
PatternKind::Constant { .. } => {
Err(match_pair)
}
PatternKind::Range { .. } |
PatternKind::Slice { .. } => {
Err(match_pair)
}
PatternKind::Variant { adt_def, substs, variant_index, ref subpatterns } => {
if self.hir.tcx().sess.features.borrow().never_type {
let irrefutable = adt_def.variants.iter().enumerate().all(|(i, v)| {
i == variant_index || {
let mut visited = FxHashMap::default();
let node_set = v.uninhabited_from(&mut visited,
self.hir.tcx(),
substs,
adt_def.adt_kind());
!node_set.is_empty()
}
});
if irrefutable {
let lvalue = match_pair.lvalue.downcast(adt_def, variant_index);
candidate.match_pairs.extend(self.field_match_pairs(lvalue, subpatterns));
Ok(())
} else {
Err(match_pair)
}
} else {
Err(match_pair)
}
},
PatternKind::Array { ref prefix, ref slice, ref suffix } => {
self.prefix_slice_suffix(&mut candidate.match_pairs,
&match_pair.lvalue,
prefix,
slice.as_ref(),
suffix);
Ok(())
}
PatternKind::Leaf { ref subpatterns } => {
candidate.match_pairs
.extend(self.field_match_pairs(match_pair.lvalue, subpatterns));
Ok(())
}
PatternKind::Deref { ref subpattern } => {
let lvalue = match_pair.lvalue.deref();
candidate.match_pairs.push(MatchPair::new(lvalue, subpattern));
Ok(())
}
}
}
}