@@ -1282,9 +1282,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1282
1282
) {
1283
1283
let mut split_or_candidate = false ;
1284
1284
for candidate in & mut * candidates {
1285
- if let [ MatchPair { test_case : TestCase :: Or { pats, .. } , .. } ] =
1286
- & * candidate. match_pairs
1287
- {
1285
+ if let [ MatchPair { test_case : TestCase :: Or { .. } , .. } ] = & * candidate. match_pairs {
1288
1286
// Split a candidate in which the only match-pair is an or-pattern into multiple
1289
1287
// candidates. This is so that
1290
1288
//
@@ -1294,9 +1292,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1294
1292
// }
1295
1293
//
1296
1294
// only generates a single switch.
1297
- candidate. subcandidates = self . create_or_subcandidates ( pats, candidate. has_guard ) ;
1298
- let first_match_pair = candidate. match_pairs . pop ( ) . unwrap ( ) ;
1299
- candidate. or_span = Some ( first_match_pair. pattern . span ) ;
1295
+ let match_pair = candidate. match_pairs . pop ( ) . unwrap ( ) ;
1296
+ self . create_or_subcandidates ( candidate, & match_pair) ;
1300
1297
split_or_candidate = true ;
1301
1298
}
1302
1299
}
@@ -1486,12 +1483,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1486
1483
1487
1484
let match_pairs = mem:: take ( & mut first_candidate. match_pairs ) ;
1488
1485
let ( first_match_pair, remaining_match_pairs) = match_pairs. split_first ( ) . unwrap ( ) ;
1489
- let TestCase :: Or { ref pats } = & first_match_pair. test_case else { unreachable ! ( ) } ;
1490
1486
1491
1487
let remainder_start = self . cfg . start_new_block ( ) ;
1492
- let or_span = first_match_pair. pattern . span ;
1493
1488
// Test the alternatives of this or-pattern.
1494
- self . test_or_pattern ( first_candidate, start_block, remainder_start, pats , or_span ) ;
1489
+ self . test_or_pattern ( first_candidate, start_block, remainder_start, first_match_pair ) ;
1495
1490
1496
1491
if !remaining_match_pairs. is_empty ( ) {
1497
1492
// If more match pairs remain, test them after each subcandidate.
@@ -1526,39 +1521,48 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1526
1521
) ;
1527
1522
}
1528
1523
1529
- #[ instrument(
1530
- skip( self , start_block, otherwise_block, or_span, candidate, pats) ,
1531
- level = "debug"
1532
- ) ]
1524
+ #[ instrument( skip( self , start_block, otherwise_block, candidate, match_pair) , level = "debug" ) ]
1533
1525
fn test_or_pattern < ' pat > (
1534
1526
& mut self ,
1535
1527
candidate : & mut Candidate < ' pat , ' tcx > ,
1536
1528
start_block : BasicBlock ,
1537
1529
otherwise_block : BasicBlock ,
1538
- pats : & [ FlatPat < ' pat , ' tcx > ] ,
1539
- or_span : Span ,
1530
+ match_pair : & MatchPair < ' pat , ' tcx > ,
1540
1531
) {
1541
- debug ! ( "candidate={:#?}\n pats={:#?}" , candidate, pats) ;
1542
- let mut or_candidates: Vec < _ > = pats
1543
- . iter ( )
1544
- . cloned ( )
1545
- . map ( |flat_pat| Candidate :: from_flat_pat ( flat_pat, candidate. has_guard ) )
1546
- . collect ( ) ;
1547
- let mut or_candidate_refs: Vec < _ > = or_candidates. iter_mut ( ) . collect ( ) ;
1532
+ self . create_or_subcandidates ( candidate, match_pair) ;
1533
+ let mut or_candidate_refs: Vec < _ > = candidate. subcandidates . iter_mut ( ) . collect ( ) ;
1534
+ let or_span = match_pair. pattern . span ;
1548
1535
self . match_candidates (
1549
1536
or_span,
1550
1537
or_span,
1551
1538
start_block,
1552
1539
otherwise_block,
1553
1540
& mut or_candidate_refs,
1554
1541
) ;
1555
- candidate. subcandidates = or_candidates;
1556
- candidate. or_span = Some ( or_span) ;
1557
1542
self . merge_trivial_subcandidates ( candidate) ;
1558
1543
}
1559
1544
1560
- /// Try to merge all of the subcandidates of the given candidate into one.
1561
- /// This avoids exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`.
1545
+ /// Given a match-pair that corresponds to an or-pattern, expand each subpattern into a new
1546
+ /// subcandidate. Any candidate that has been expanded that way should be passed to
1547
+ /// `merge_trivial_subcandidates` after its subcandidates have been processed.
1548
+ fn create_or_subcandidates < ' pat > (
1549
+ & mut self ,
1550
+ candidate : & mut Candidate < ' pat , ' tcx > ,
1551
+ match_pair : & MatchPair < ' pat , ' tcx > ,
1552
+ ) {
1553
+ let TestCase :: Or { ref pats } = & match_pair. test_case else { bug ! ( ) } ;
1554
+ debug ! ( "expanding or-pattern: candidate={:#?}\n pats={:#?}" , candidate, pats) ;
1555
+ candidate. or_span = Some ( match_pair. pattern . span ) ;
1556
+ candidate. subcandidates = pats
1557
+ . iter ( )
1558
+ . cloned ( )
1559
+ . map ( |flat_pat| Candidate :: from_flat_pat ( flat_pat, candidate. has_guard ) )
1560
+ . collect ( ) ;
1561
+ }
1562
+
1563
+ /// Try to merge all of the subcandidates of the given candidate into one. This avoids
1564
+ /// exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`. The or-pattern should have
1565
+ /// been expanded with `create_or_subcandidates`.
1562
1566
fn merge_trivial_subcandidates ( & mut self , candidate : & mut Candidate < ' _ , ' tcx > ) {
1563
1567
if candidate. subcandidates . is_empty ( ) || candidate. has_guard {
1564
1568
// FIXME(or_patterns; matthewjasper) Don't give up if we have a guard.
0 commit comments