@@ -61,16 +61,13 @@ impl<'a,'tcx> Builder<'a,'tcx> {
61
61
// assemble a list of candidates: there is one candidate per
62
62
// pattern, which means there may be more than one candidate
63
63
// *per arm*. These candidates are kept sorted such that the
64
- // highest priority candidate comes last in the list. This the
65
- // reverse of the order in which candidates are written in the
66
- // source.
64
+ // highest priority candidate comes first in the list.
65
+ // (i.e. same order as in source)
67
66
let candidates: Vec < _ > =
68
67
arms. iter ( )
69
68
. enumerate ( )
70
- . rev ( ) // highest priority comes last
71
69
. flat_map ( |( arm_index, arm) | {
72
70
arm. patterns . iter ( )
73
- . rev ( )
74
71
. map ( move |pat| ( arm_index, pat, arm. guard . clone ( ) ) )
75
72
} )
76
73
. map ( |( arm_index, pattern, guard) | {
@@ -290,9 +287,9 @@ impl<'a,'tcx> Builder<'a,'tcx> {
290
287
/// The main match algorithm. It begins with a set of candidates
291
288
/// `candidates` and has the job of generating code to determine
292
289
/// which of these candidates, if any, is the correct one. The
293
- /// candidates are sorted in inverse priority -- so the last item
294
- /// in the list has highest priority. When a candidate is found to
295
- /// match the value, we will generate a branch to the appropriate
290
+ /// candidates are sorted such that the first item in the list
291
+ /// has the highest priority. When a candidate is found to match
292
+ /// the value, we will generate a branch to the appropriate
296
293
/// block found in `arm_blocks`.
297
294
///
298
295
/// The return value is a list of "otherwise" blocks. These are
@@ -324,17 +321,17 @@ impl<'a,'tcx> Builder<'a,'tcx> {
324
321
unpack ! ( block = self . simplify_candidate( block, candidate) ) ;
325
322
}
326
323
327
- // The candidates are inversely sorted by priority. Check to
328
- // see whether the candidates in the front of the queue (and
329
- // hence back of the vec) have satisfied all their match
324
+ // The candidates are sorted by priority. Check to see
325
+ // whether the higher priority candidates (and hence at
326
+ // the front of the vec) have satisfied all their match
330
327
// pairs.
331
328
let fully_matched =
332
- candidates. iter ( ) . rev ( ) . take_while ( |c| c. match_pairs . is_empty ( ) ) . count ( ) ;
329
+ candidates. iter ( ) . take_while ( |c| c. match_pairs . is_empty ( ) ) . count ( ) ;
333
330
debug ! ( "match_candidates: {:?} candidates fully matched" , fully_matched) ;
334
- for _ in 0 ..fully_matched {
331
+ let mut unmatched_candidates = candidates. split_off ( fully_matched) ;
332
+ for candidate in candidates {
335
333
// If so, apply any bindings, test the guard (if any), and
336
334
// branch to the arm.
337
- let candidate = candidates. pop ( ) . unwrap ( ) ;
338
335
if let Some ( b) = self . bind_and_guard_matched_candidate ( block, arm_blocks, candidate) {
339
336
block = b;
340
337
} else {
@@ -346,13 +343,13 @@ impl<'a,'tcx> Builder<'a,'tcx> {
346
343
347
344
// If there are no candidates that still need testing, we're done.
348
345
// Since all matches are exhaustive, execution should never reach this point.
349
- if candidates . is_empty ( ) {
346
+ if unmatched_candidates . is_empty ( ) {
350
347
return vec ! [ block] ;
351
348
}
352
349
353
350
// Test candidates where possible.
354
351
let ( otherwise, tested_candidates) =
355
- self . test_candidates ( span, arm_blocks, & candidates , block) ;
352
+ self . test_candidates ( span, arm_blocks, & unmatched_candidates , block) ;
356
353
357
354
// If the target candidates were exhaustive, then we are done.
358
355
if otherwise. is_empty ( ) {
@@ -361,15 +358,14 @@ impl<'a,'tcx> Builder<'a,'tcx> {
361
358
362
359
// If all candidates were sorted into `target_candidates` somewhere, then
363
360
// the initial set was inexhaustive.
364
- let untested_candidates = candidates . len ( ) - tested_candidates;
365
- if untested_candidates == 0 {
361
+ let untested_candidates = unmatched_candidates . split_off ( tested_candidates) ;
362
+ if untested_candidates. len ( ) == 0 {
366
363
return otherwise;
367
364
}
368
365
369
366
// Otherwise, let's process those remaining candidates.
370
367
let join_block = self . join_otherwise_blocks ( otherwise) ;
371
- candidates. truncate ( untested_candidates) ;
372
- self . match_candidates ( span, arm_blocks, candidates, join_block)
368
+ self . match_candidates ( span, arm_blocks, untested_candidates, join_block)
373
369
}
374
370
375
371
fn join_otherwise_blocks ( & mut self ,
@@ -461,7 +457,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
461
457
-> ( Vec < BasicBlock > , usize )
462
458
{
463
459
// extract the match-pair from the highest priority candidate
464
- let match_pair = & candidates. last ( ) . unwrap ( ) . match_pairs [ 0 ] ;
460
+ let match_pair = & candidates. first ( ) . unwrap ( ) . match_pairs [ 0 ] ;
465
461
let mut test = self . test ( match_pair) ;
466
462
467
463
// most of the time, the test to perform is simply a function
@@ -470,7 +466,7 @@ impl<'a,'tcx> Builder<'a,'tcx> {
470
466
// available
471
467
match test. kind {
472
468
TestKind :: SwitchInt { switch_ty, ref mut options, ref mut indices } => {
473
- for candidate in candidates. iter ( ) . rev ( ) {
469
+ for candidate in candidates. iter ( ) {
474
470
if !self . add_cases_to_switch ( & match_pair. lvalue ,
475
471
candidate,
476
472
switch_ty,
@@ -497,7 +493,6 @@ impl<'a,'tcx> Builder<'a,'tcx> {
497
493
// that point, we stop sorting.
498
494
let tested_candidates =
499
495
candidates. iter ( )
500
- . rev ( )
501
496
. take_while ( |c| self . sort_candidate ( & match_pair. lvalue ,
502
497
& test,
503
498
c,
0 commit comments