@@ -1052,7 +1052,7 @@ struct Ascription<'tcx> {
1052
1052
variance : ty:: Variance ,
1053
1053
}
1054
1054
1055
- #[ derive( Debug ) ]
1055
+ #[ derive( Debug , Clone ) ]
1056
1056
pub ( crate ) struct MatchPair < ' pat , ' tcx > {
1057
1057
// This place...
1058
1058
place : PlaceBuilder < ' tcx > ,
@@ -1413,56 +1413,69 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1413
1413
fake_borrows : & mut Option < FxIndexSet < Place < ' tcx > > > ,
1414
1414
) {
1415
1415
let ( first_candidate, remaining_candidates) = candidates. split_first_mut ( ) . unwrap ( ) ;
1416
-
1417
- // All of the or-patterns have been sorted to the end, so if the first
1418
- // pattern is an or-pattern we only have or-patterns.
1419
- match first_candidate. match_pairs [ 0 ] . pattern . kind {
1420
- PatKind :: Or { .. } => ( ) ,
1421
- _ => {
1422
- self . test_candidates (
1423
- span,
1424
- scrutinee_span,
1425
- candidates,
1426
- start_block,
1427
- otherwise_block,
1428
- fake_borrows,
1429
- ) ;
1430
- return ;
1431
- }
1416
+ assert ! ( first_candidate. subcandidates. is_empty( ) ) ;
1417
+ if !matches ! ( first_candidate. match_pairs[ 0 ] . pattern. kind, PatKind :: Or { .. } ) {
1418
+ self . test_candidates (
1419
+ span,
1420
+ scrutinee_span,
1421
+ candidates,
1422
+ start_block,
1423
+ otherwise_block,
1424
+ fake_borrows,
1425
+ ) ;
1426
+ return ;
1432
1427
}
1433
1428
1434
1429
let match_pairs = mem:: take ( & mut first_candidate. match_pairs ) ;
1430
+ let ( first_match_pair, remaining_match_pairs) = match_pairs. split_first ( ) . unwrap ( ) ;
1431
+ let PatKind :: Or { ref pats } = & first_match_pair. pattern . kind else { unreachable ! ( ) } ;
1435
1432
1436
1433
let remainder_start = self . cfg . start_new_block ( ) ;
1437
- for match_pair in match_pairs {
1438
- let PatKind :: Or { ref pats } = & match_pair. pattern . kind else {
1439
- bug ! ( "Or-patterns should have been sorted to the end" ) ;
1440
- } ;
1441
- let or_span = match_pair. pattern . span ;
1434
+ let or_span = first_match_pair. pattern . span ;
1435
+ // Test the alternatives of this or-pattern.
1436
+ self . test_or_pattern (
1437
+ first_candidate,
1438
+ start_block,
1439
+ remainder_start,
1440
+ pats,
1441
+ or_span,
1442
+ & first_match_pair. place ,
1443
+ fake_borrows,
1444
+ ) ;
1442
1445
1446
+ if !remaining_match_pairs. is_empty ( ) {
1447
+ // If more match pairs remain, test them after each subcandidate.
1448
+ // We could add them to the or-candidates before the call to `test_or_pattern` but this
1449
+ // would make it impossible to detect simplifiable or-patterns. That would guarantee
1450
+ // exponentially large CFGs for cases like `(1 | 2, 3 | 4, ...)`.
1443
1451
first_candidate. visit_leaves ( |leaf_candidate| {
1444
- let or_start = leaf_candidate. pre_binding_block . unwrap_or ( start_block) ;
1452
+ assert ! ( leaf_candidate. match_pairs. is_empty( ) ) ;
1453
+ leaf_candidate. match_pairs . extend ( remaining_match_pairs. iter ( ) . cloned ( ) ) ;
1454
+ let or_start = leaf_candidate. pre_binding_block . unwrap ( ) ;
1455
+ // In a case like `(a | b, c | d)`, if `a` succeeds and `c | d` fails, we know `(b,
1456
+ // c | d)` will fail too. If there is no guard, we skip testing of `b` by branching
1457
+ // directly to `remainder_start`. If there is a guard, we have to try `(b, c | d)`.
1445
1458
let or_otherwise = leaf_candidate. otherwise_block . unwrap_or ( remainder_start) ;
1446
- self . test_or_pattern (
1447
- leaf_candidate,
1459
+ self . test_candidates_with_or (
1460
+ span,
1461
+ scrutinee_span,
1462
+ & mut [ leaf_candidate] ,
1448
1463
or_start,
1449
1464
or_otherwise,
1450
- pats,
1451
- or_span,
1452
- & match_pair. place ,
1453
1465
fake_borrows,
1454
1466
) ;
1455
1467
} ) ;
1456
1468
}
1457
1469
1470
+ // Test the remaining candidates.
1458
1471
self . match_candidates (
1459
1472
span,
1460
1473
scrutinee_span,
1461
1474
remainder_start,
1462
1475
otherwise_block,
1463
1476
remaining_candidates,
1464
1477
fake_borrows,
1465
- )
1478
+ ) ;
1466
1479
}
1467
1480
1468
1481
#[ instrument(
0 commit comments