@@ -1252,9 +1252,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1252
1252
) {
1253
1253
let mut split_or_candidate = false ;
1254
1254
for candidate in & mut * candidates {
1255
- if let [ MatchPair { test_case : TestCase :: Or { pats, .. } , .. } ] =
1256
- & * candidate. match_pairs
1257
- {
1255
+ if let [ MatchPair { test_case : TestCase :: Or { .. } , .. } ] = & * candidate. match_pairs {
1258
1256
// Split a candidate in which the only match-pair is an or-pattern into multiple
1259
1257
// candidates. This is so that
1260
1258
//
@@ -1264,9 +1262,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1264
1262
// }
1265
1263
//
1266
1264
// only generates a single switch.
1267
- candidate. subcandidates = self . create_or_subcandidates ( pats, candidate. has_guard ) ;
1268
- let first_match_pair = candidate. match_pairs . pop ( ) . unwrap ( ) ;
1269
- candidate. or_span = Some ( first_match_pair. pattern . span ) ;
1265
+ let match_pair = candidate. match_pairs . pop ( ) . unwrap ( ) ;
1266
+ self . create_or_subcandidates ( candidate, & match_pair) ;
1270
1267
split_or_candidate = true ;
1271
1268
}
1272
1269
}
@@ -1456,12 +1453,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1456
1453
1457
1454
let match_pairs = mem:: take ( & mut first_candidate. match_pairs ) ;
1458
1455
let ( first_match_pair, remaining_match_pairs) = match_pairs. split_first ( ) . unwrap ( ) ;
1459
- let TestCase :: Or { ref pats } = & first_match_pair. test_case else { unreachable ! ( ) } ;
1460
1456
1461
1457
let remainder_start = self . cfg . start_new_block ( ) ;
1462
- let or_span = first_match_pair. pattern . span ;
1463
1458
// Test the alternatives of this or-pattern.
1464
- self . test_or_pattern ( first_candidate, start_block, remainder_start, pats , or_span ) ;
1459
+ self . test_or_pattern ( first_candidate, start_block, remainder_start, first_match_pair ) ;
1465
1460
1466
1461
if !remaining_match_pairs. is_empty ( ) {
1467
1462
// If more match pairs remain, test them after each subcandidate.
@@ -1496,39 +1491,48 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1496
1491
) ;
1497
1492
}
1498
1493
1499
- #[ instrument(
1500
- skip( self , start_block, otherwise_block, or_span, candidate, pats) ,
1501
- level = "debug"
1502
- ) ]
1494
+ #[ instrument( skip( self , start_block, otherwise_block, candidate, match_pair) , level = "debug" ) ]
1503
1495
fn test_or_pattern < ' pat > (
1504
1496
& mut self ,
1505
1497
candidate : & mut Candidate < ' pat , ' tcx > ,
1506
1498
start_block : BasicBlock ,
1507
1499
otherwise_block : BasicBlock ,
1508
- pats : & [ FlatPat < ' pat , ' tcx > ] ,
1509
- or_span : Span ,
1500
+ match_pair : & MatchPair < ' pat , ' tcx > ,
1510
1501
) {
1511
- debug ! ( "candidate={:#?}\n pats={:#?}" , candidate, pats) ;
1512
- let mut or_candidates: Vec < _ > = pats
1513
- . iter ( )
1514
- . cloned ( )
1515
- . map ( |flat_pat| Candidate :: from_flat_pat ( flat_pat, candidate. has_guard ) )
1516
- . collect ( ) ;
1517
- let mut or_candidate_refs: Vec < _ > = or_candidates. iter_mut ( ) . collect ( ) ;
1502
+ self . create_or_subcandidates ( candidate, match_pair) ;
1503
+ let mut or_candidate_refs: Vec < _ > = candidate. subcandidates . iter_mut ( ) . collect ( ) ;
1504
+ let or_span = match_pair. pattern . span ;
1518
1505
self . match_candidates (
1519
1506
or_span,
1520
1507
or_span,
1521
1508
start_block,
1522
1509
otherwise_block,
1523
1510
& mut or_candidate_refs,
1524
1511
) ;
1525
- candidate. subcandidates = or_candidates;
1526
- candidate. or_span = Some ( or_span) ;
1527
1512
self . merge_trivial_subcandidates ( candidate) ;
1528
1513
}
1529
1514
1530
- /// Try to merge all of the subcandidates of the given candidate into one.
1531
- /// This avoids exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`.
1515
+ /// Given a match-pair that corresponds to an or-pattern, expand each subpattern into a new
1516
+ /// subcandidate. Any candidate that has been expanded that way should be passed to
1517
+ /// `merge_trivial_subcandidates` after its subcandidates have been processed.
1518
+ fn create_or_subcandidates < ' pat > (
1519
+ & mut self ,
1520
+ candidate : & mut Candidate < ' pat , ' tcx > ,
1521
+ match_pair : & MatchPair < ' pat , ' tcx > ,
1522
+ ) {
1523
+ let TestCase :: Or { ref pats } = & match_pair. test_case else { bug ! ( ) } ;
1524
+ debug ! ( "expanding or-pattern: candidate={:#?}\n pats={:#?}" , candidate, pats) ;
1525
+ candidate. or_span = Some ( match_pair. pattern . span ) ;
1526
+ candidate. subcandidates = pats
1527
+ . iter ( )
1528
+ . cloned ( )
1529
+ . map ( |flat_pat| Candidate :: from_flat_pat ( flat_pat, candidate. has_guard ) )
1530
+ . collect ( ) ;
1531
+ }
1532
+
1533
+ /// Try to merge all of the subcandidates of the given candidate into one. This avoids
1534
+ /// exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`. The or-pattern should have
1535
+ /// been expanded with `create_or_subcandidates`.
1532
1536
fn merge_trivial_subcandidates ( & mut self , candidate : & mut Candidate < ' _ , ' tcx > ) {
1533
1537
if candidate. subcandidates . is_empty ( ) || candidate. has_guard {
1534
1538
// FIXME(or_patterns; matthewjasper) Don't give up if we have a guard.
0 commit comments