@@ -44,7 +44,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
44
44
// be unreachable or reachable multiple times.
45
45
let var_extent = self . extent_of_innermost_scope ( ) . unwrap ( ) ;
46
46
for arm in & arms {
47
- self . declare_bindings ( var_extent, arm. patterns [ 0 ] . clone ( ) ) ;
47
+ self . declare_bindings ( var_extent, & arm. patterns [ 0 ] ) ;
48
48
}
49
49
50
50
let mut arm_blocks = ArmBlocks {
@@ -64,18 +64,18 @@ impl<'a,'tcx> Builder<'a,'tcx> {
64
64
// highest priority candidate comes last in the list. This the
65
65
// reverse of the order in which candidates are written in the
66
66
// source.
67
- let candidates: Vec < Candidate < ' tcx > > =
67
+ let candidates: Vec < _ > =
68
68
arms. iter ( )
69
69
. enumerate ( )
70
70
. rev ( ) // highest priority comes last
71
71
. flat_map ( |( arm_index, arm) | {
72
72
arm. patterns . iter ( )
73
73
. rev ( )
74
- . map ( move |pat| ( arm_index, pat. clone ( ) , arm. guard . clone ( ) ) )
74
+ . map ( move |pat| ( arm_index, pat, arm. guard . clone ( ) ) )
75
75
} )
76
76
. map ( |( arm_index, pattern, guard) | {
77
77
Candidate {
78
- match_pairs : vec ! [ self . match_pair ( discriminant_lvalue. clone( ) , pattern) ] ,
78
+ match_pairs : vec ! [ MatchPair :: new ( discriminant_lvalue. clone( ) , pattern) ] ,
79
79
bindings : vec ! [ ] ,
80
80
guard : guard,
81
81
arm_index : arm_index,
@@ -102,12 +102,11 @@ impl<'a,'tcx> Builder<'a,'tcx> {
102
102
pub fn expr_into_pattern ( & mut self ,
103
103
mut block : BasicBlock ,
104
104
var_extent : CodeExtent , // lifetime of vars
105
- irrefutable_pat : PatternRef < ' tcx > ,
105
+ irrefutable_pat : Pattern < ' tcx > ,
106
106
initializer : ExprRef < ' tcx > )
107
107
-> BlockAnd < ( ) > {
108
108
// optimize the case of `let x = ...`
109
- let irrefutable_pat = self . hir . mirror ( irrefutable_pat) ;
110
- match irrefutable_pat. kind {
109
+ match * irrefutable_pat. kind {
111
110
PatternKind :: Binding { mutability,
112
111
name,
113
112
mode : BindingMode :: ByValue ,
@@ -128,22 +127,22 @@ impl<'a,'tcx> Builder<'a,'tcx> {
128
127
let lvalue = unpack ! ( block = self . as_lvalue( block, initializer) ) ;
129
128
self . lvalue_into_pattern ( block,
130
129
var_extent,
131
- PatternRef :: Mirror ( Box :: new ( irrefutable_pat) ) ,
130
+ irrefutable_pat,
132
131
& lvalue)
133
132
}
134
133
135
134
pub fn lvalue_into_pattern ( & mut self ,
136
135
mut block : BasicBlock ,
137
136
var_extent : CodeExtent ,
138
- irrefutable_pat : PatternRef < ' tcx > ,
137
+ irrefutable_pat : Pattern < ' tcx > ,
139
138
initializer : & Lvalue < ' tcx > )
140
139
-> BlockAnd < ( ) > {
141
140
// first, creating the bindings
142
- self . declare_bindings ( var_extent, irrefutable_pat. clone ( ) ) ;
141
+ self . declare_bindings ( var_extent, & irrefutable_pat) ;
143
142
144
143
// create a dummy candidate
145
- let mut candidate = Candidate :: < ' tcx > {
146
- match_pairs : vec ! [ self . match_pair ( initializer. clone( ) , irrefutable_pat. clone ( ) ) ] ,
144
+ let mut candidate = Candidate {
145
+ match_pairs : vec ! [ MatchPair :: new ( initializer. clone( ) , & irrefutable_pat) ] ,
147
146
bindings : vec ! [ ] ,
148
147
guard : None ,
149
148
arm_index : 0 , // since we don't call `match_candidates`, this field is unused
@@ -166,29 +165,29 @@ impl<'a,'tcx> Builder<'a,'tcx> {
166
165
block. unit ( )
167
166
}
168
167
169
- pub fn declare_bindings ( & mut self , var_extent : CodeExtent , pattern : PatternRef < ' tcx > ) {
170
- let pattern = self . hir . mirror ( pattern) ;
171
- match pattern. kind {
172
- PatternKind :: Binding { mutability, name, mode : _, var, ty, subpattern } => {
168
+ pub fn declare_bindings ( & mut self , var_extent : CodeExtent , pattern : & Pattern < ' tcx > ) {
169
+ match * pattern. kind {
170
+ PatternKind :: Binding { mutability, name, mode : _, var, ty, ref subpattern } => {
173
171
self . declare_binding ( var_extent, mutability, name, var, ty, pattern. span ) ;
174
- if let Some ( subpattern) = subpattern {
172
+ if let Some ( subpattern) = subpattern. as_ref ( ) {
175
173
self . declare_bindings ( var_extent, subpattern) ;
176
174
}
177
175
}
178
- PatternKind :: Array { prefix, slice, suffix } |
179
- PatternKind :: Slice { prefix, slice, suffix } => {
180
- for subpattern in prefix. into_iter ( ) . chain ( slice) . chain ( suffix) {
176
+ PatternKind :: Array { ref prefix, ref slice, ref suffix } |
177
+ PatternKind :: Slice { ref prefix, ref slice, ref suffix } => {
178
+ for subpattern in prefix. iter ( ) . chain ( slice) . chain ( suffix) {
181
179
self . declare_bindings ( var_extent, subpattern) ;
182
180
}
183
181
}
184
- PatternKind :: Constant { .. } | PatternKind :: Range { .. } | PatternKind :: Wild => { }
185
- PatternKind :: Deref { subpattern } => {
182
+ PatternKind :: Constant { .. } | PatternKind :: Range { .. } | PatternKind :: Wild => {
183
+ }
184
+ PatternKind :: Deref { ref subpattern } => {
186
185
self . declare_bindings ( var_extent, subpattern) ;
187
186
}
188
- PatternKind :: Leaf { subpatterns } |
189
- PatternKind :: Variant { subpatterns, .. } => {
187
+ PatternKind :: Leaf { ref subpatterns } |
188
+ PatternKind :: Variant { ref subpatterns, .. } => {
190
189
for subpattern in subpatterns {
191
- self . declare_bindings ( var_extent, subpattern. pattern ) ;
190
+ self . declare_bindings ( var_extent, & subpattern. pattern ) ;
192
191
}
193
192
}
194
193
}
@@ -202,9 +201,9 @@ struct ArmBlocks {
202
201
}
203
202
204
203
#[ derive( Clone , Debug ) ]
205
- struct Candidate < ' tcx > {
204
+ struct Candidate < ' pat , ' tcx : ' pat > {
206
205
// all of these must be satisfied...
207
- match_pairs : Vec < MatchPair < ' tcx > > ,
206
+ match_pairs : Vec < MatchPair < ' pat , ' tcx > > ,
208
207
209
208
// ...these bindings established...
210
209
bindings : Vec < Binding < ' tcx > > ,
@@ -228,12 +227,12 @@ struct Binding<'tcx> {
228
227
}
229
228
230
229
#[ derive( Clone , Debug ) ]
231
- struct MatchPair < ' tcx > {
230
+ struct MatchPair < ' pat , ' tcx : ' pat > {
232
231
// this lvalue...
233
232
lvalue : Lvalue < ' tcx > ,
234
233
235
234
// ... must match this pattern.
236
- pattern : Pattern < ' tcx > ,
235
+ pattern : & ' pat Pattern < ' tcx > ,
237
236
}
238
237
239
238
#[ derive( Clone , Debug , PartialEq ) ]
@@ -280,11 +279,11 @@ struct Test<'tcx> {
280
279
// Main matching algorithm
281
280
282
281
impl < ' a , ' tcx > Builder < ' a , ' tcx > {
283
- fn match_candidates ( & mut self ,
284
- span : Span ,
285
- arm_blocks : & mut ArmBlocks ,
286
- mut candidates : Vec < Candidate < ' tcx > > ,
287
- mut block : BasicBlock )
282
+ fn match_candidates < ' pat > ( & mut self ,
283
+ span : Span ,
284
+ arm_blocks : & mut ArmBlocks ,
285
+ mut candidates : Vec < Candidate < ' pat , ' tcx > > ,
286
+ mut block : BasicBlock )
288
287
{
289
288
debug ! ( "matched_candidate(span={:?}, block={:?}, candidates={:?})" ,
290
289
span, block, candidates) ;
@@ -346,17 +345,20 @@ impl<'a,'tcx> Builder<'a,'tcx> {
346
345
debug ! ( "match_candidates: test={:?} match_pair={:?}" , test, match_pair) ;
347
346
let target_blocks = self . perform_test ( block, & match_pair. lvalue , & test) ;
348
347
349
- for ( outcome, target_block) in target_blocks. into_iter ( ) . enumerate ( ) {
350
- let applicable_candidates: Vec < Candidate < ' tcx > > =
351
- candidates. iter ( )
352
- . filter_map ( |candidate| {
353
- self . candidate_under_assumption ( & match_pair. lvalue ,
354
- & test. kind ,
355
- outcome,
356
- candidate)
357
- } )
358
- . collect ( ) ;
359
- self . match_candidates ( span, arm_blocks, applicable_candidates, target_block) ;
348
+ let mut target_candidates: Vec < _ > = ( 0 ..target_blocks. len ( ) ) . map ( |_| vec ! [ ] ) . collect ( ) ;
349
+
350
+ for candidate in & candidates {
351
+ self . sort_candidate ( & match_pair. lvalue ,
352
+ & test,
353
+ candidate,
354
+ & mut target_candidates) ;
355
+ }
356
+
357
+ for ( target_block, target_candidates) in
358
+ target_blocks. into_iter ( )
359
+ . zip ( target_candidates. into_iter ( ) )
360
+ {
361
+ self . match_candidates ( span, arm_blocks, target_candidates, target_block) ;
360
362
}
361
363
}
362
364
@@ -372,11 +374,11 @@ impl<'a,'tcx> Builder<'a,'tcx> {
372
374
/// bindings, further tests would be a use-after-move (which would
373
375
/// in turn be detected by the borrowck code that runs on the
374
376
/// MIR).
375
- fn bind_and_guard_matched_candidate ( & mut self ,
376
- mut block : BasicBlock ,
377
- arm_blocks : & mut ArmBlocks ,
378
- candidate : Candidate < ' tcx > )
379
- -> Option < BasicBlock > {
377
+ fn bind_and_guard_matched_candidate < ' pat > ( & mut self ,
378
+ mut block : BasicBlock ,
379
+ arm_blocks : & mut ArmBlocks ,
380
+ candidate : Candidate < ' pat , ' tcx > )
381
+ -> Option < BasicBlock > {
380
382
debug ! ( "bind_and_guard_matched_candidate(block={:?}, candidate={:?})" ,
381
383
block, candidate) ;
382
384
0 commit comments