@@ -1364,61 +1364,104 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1364
1364
otherwise_block : BasicBlock ,
1365
1365
candidates : & mut [ & mut Candidate < ' pat , ' tcx > ] ,
1366
1366
) {
1367
- let expand_or_pats = candidates. iter ( ) . any ( |candidate| {
1368
- matches ! ( & * candidate. match_pairs, [ MatchPair { test_case: TestCase :: Or { .. } , .. } ] )
1369
- } ) ;
1367
+ // We process or-patterns here. If any candidate starts with an or-pattern, we have to
1368
+ // expand the or-pattern before we can proceed further.
1369
+ //
1370
+ // We can't expand them freely however. The rule is: if the candidate has an or-pattern as
1371
+ // its only remaining match pair, we can expand it freely. If it has other match pairs, we
1372
+ // can expand it but we can't process more candidates after it.
1373
+ //
1374
+ // If we didn't stop, the `otherwise` cases could get mixed up. E.g. in the following, the
1375
+ // `otherwise` of the first `2` would be set to the second arm, so when simplifying the
1376
+ // or-pattern (in `merge_trivial_subcandidates`) the `otherwise` cases would get mixed up
1377
+ // and the `(1, true)` case would execute the second arm.
1378
+ // ```ignore(illustrative)
1379
+ // match (1, true) {
1380
+ // (1 | 2, false) => {},
1381
+ // (2, _) => {},
1382
+ // _ => {}
1383
+ // }
1384
+ // ```
1385
+ //
1386
+ // We therefore split the `candidates` slice in two, and expand or-patterns in the first
1387
+ // half.
1388
+ let mut expand_until = 0 ;
1389
+ for ( i, candidate) in candidates. iter ( ) . enumerate ( ) {
1390
+ if matches ! (
1391
+ & * candidate. match_pairs,
1392
+ [ MatchPair { test_case: TestCase :: Or { .. } , .. } , ..]
1393
+ ) {
1394
+ expand_until = i + 1 ;
1395
+ if candidate. match_pairs . len ( ) > 1 {
1396
+ break ;
1397
+ }
1398
+ }
1399
+ }
1400
+ let ( candidates_to_expand, remaining_candidates) = candidates. split_at_mut ( expand_until) ;
1370
1401
1371
1402
ensure_sufficient_stack ( || {
1372
- if expand_or_pats {
1373
- // Split a candidate in which the only match-pair is an or-pattern into multiple
1374
- // candidates. This is so that
1375
- //
1376
- // match x {
1377
- // 0 | 1 => { ... },
1378
- // 2 | 3 => { ... },
1379
- // }
1380
- //
1381
- // only generates a single switch.
1382
- let mut new_candidates = Vec :: new ( ) ;
1383
- for candidate in candidates. iter_mut ( ) {
1384
- if let [ MatchPair { test_case : TestCase :: Or { .. } , .. } ] =
1403
+ if candidates_to_expand. is_empty ( ) {
1404
+ // No candidates start with an or-pattern, we can continue.
1405
+ self . match_expanded_candidates (
1406
+ span,
1407
+ scrutinee_span,
1408
+ start_block,
1409
+ otherwise_block,
1410
+ remaining_candidates,
1411
+ ) ;
1412
+ } else {
1413
+ // Expand one level of or-patterns for each candidate in `candidates_to_expand`.
1414
+ let mut expanded_candidates = Vec :: new ( ) ;
1415
+ for candidate in candidates_to_expand. iter_mut ( ) {
1416
+ if let [ MatchPair { test_case : TestCase :: Or { .. } , .. } , ..] =
1385
1417
& * candidate. match_pairs
1386
1418
{
1387
- let match_pair = candidate. match_pairs . pop ( ) . unwrap ( ) ;
1388
- self . create_or_subcandidates ( candidate, match_pair) ;
1419
+ let or_match_pair = candidate. match_pairs . remove ( 0 ) ;
1420
+ // Expand the or-pattern into subcandidates.
1421
+ self . create_or_subcandidates ( candidate, or_match_pair) ;
1422
+ // Collect the newly created subcandidates.
1389
1423
for subcandidate in candidate. subcandidates . iter_mut ( ) {
1390
- new_candidates . push ( subcandidate) ;
1424
+ expanded_candidates . push ( subcandidate) ;
1391
1425
}
1392
1426
} else {
1393
- new_candidates . push ( candidate) ;
1427
+ expanded_candidates . push ( candidate) ;
1394
1428
}
1395
1429
}
1430
+
1431
+ // Process the expanded candidates.
1432
+ let remainder_start = self . cfg . start_new_block ( ) ;
1433
+ // There might be new or-patterns obtained from expanding the old ones, so we call
1434
+ // `match_candidates` again.
1396
1435
self . match_candidates (
1397
1436
span,
1398
1437
scrutinee_span,
1399
1438
start_block,
1400
- otherwise_block ,
1401
- new_candidates . as_mut_slice ( ) ,
1439
+ remainder_start ,
1440
+ expanded_candidates . as_mut_slice ( ) ,
1402
1441
) ;
1403
1442
1404
- for candidate in candidates {
1443
+ // Simplify subcandidates and process any leftover match pairs.
1444
+ for candidate in candidates_to_expand {
1405
1445
if !candidate. subcandidates . is_empty ( ) {
1406
- self . merge_trivial_subcandidates ( candidate) ;
1446
+ self . finalize_or_candidate ( span , scrutinee_span , candidate) ;
1407
1447
}
1408
1448
}
1409
- } else {
1410
- self . match_simplified_candidates (
1449
+
1450
+ // Process the remaining candidates.
1451
+ self . match_candidates (
1411
1452
span,
1412
1453
scrutinee_span,
1413
- start_block ,
1454
+ remainder_start ,
1414
1455
otherwise_block,
1415
- candidates ,
1456
+ remaining_candidates ,
1416
1457
) ;
1417
1458
}
1418
1459
} ) ;
1419
1460
}
1420
1461
1421
- fn match_simplified_candidates (
1462
+ /// Construct the decision tree for `candidates`. Caller must ensure that no candidate in
1463
+ /// `candidates` starts with an or-pattern.
1464
+ fn match_expanded_candidates (
1422
1465
& mut self ,
1423
1466
span : Span ,
1424
1467
scrutinee_span : Span ,
@@ -1443,7 +1486,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1443
1486
// The first candidate has satisfied all its match pairs; we link it up and continue
1444
1487
// with the remaining candidates.
1445
1488
start_block = self . select_matched_candidate ( first, start_block) ;
1446
- self . match_simplified_candidates (
1489
+ self . match_expanded_candidates (
1447
1490
span,
1448
1491
scrutinee_span,
1449
1492
start_block,
@@ -1453,7 +1496,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1453
1496
}
1454
1497
candidates => {
1455
1498
// The first candidate has some unsatisfied match pairs; we proceed to do more tests.
1456
- self . test_candidates_with_or (
1499
+ self . test_candidates (
1457
1500
span,
1458
1501
scrutinee_span,
1459
1502
candidates,
@@ -1506,8 +1549,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1506
1549
otherwise_block
1507
1550
}
1508
1551
1509
- /// Tests a candidate where there are only or-patterns left to test, or
1510
- /// forwards to [Builder::test_candidates] .
1552
+ /// Simplify subcandidates and process any leftover match pairs. The candidate should have been
1553
+ /// expanded with `create_or_subcandidates` .
1511
1554
///
1512
1555
/// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like
1513
1556
/// so:
@@ -1559,45 +1602,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1559
1602
/// |
1560
1603
/// ...
1561
1604
/// ```
1562
- fn test_candidates_with_or (
1563
- & mut self ,
1564
- span : Span ,
1565
- scrutinee_span : Span ,
1566
- candidates : & mut [ & mut Candidate < ' _ , ' tcx > ] ,
1567
- start_block : BasicBlock ,
1568
- otherwise_block : BasicBlock ,
1569
- ) {
1570
- let ( first_candidate, remaining_candidates) = candidates. split_first_mut ( ) . unwrap ( ) ;
1571
- assert ! ( first_candidate. subcandidates. is_empty( ) ) ;
1572
- if !matches ! ( first_candidate. match_pairs[ 0 ] . test_case, TestCase :: Or { .. } ) {
1573
- self . test_candidates ( span, scrutinee_span, candidates, start_block, otherwise_block) ;
1574
- return ;
1575
- }
1576
-
1577
- let first_match_pair = first_candidate. match_pairs . remove ( 0 ) ;
1578
- let remainder_start = self . cfg . start_new_block ( ) ;
1579
- // Test the alternatives of this or-pattern.
1580
- self . test_or_pattern (
1581
- span,
1582
- scrutinee_span,
1583
- first_candidate,
1584
- start_block,
1585
- remainder_start,
1586
- first_match_pair,
1587
- ) ;
1588
-
1589
- // Test the remaining candidates.
1590
- self . match_candidates (
1591
- span,
1592
- scrutinee_span,
1593
- remainder_start,
1594
- otherwise_block,
1595
- remaining_candidates,
1596
- ) ;
1597
- }
1598
-
1599
- /// Simplify subcandidates and process any leftover match pairs. The candidate should have been
1600
- /// expanded with `create_or_subcandidates`.
1601
1605
fn finalize_or_candidate (
1602
1606
& mut self ,
1603
1607
span : Span ,
@@ -1634,40 +1638,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1634
1638
} else {
1635
1639
last_otherwise. unwrap ( )
1636
1640
} ;
1637
- self . test_candidates_with_or (
1641
+ self . match_candidates (
1638
1642
span,
1639
1643
scrutinee_span,
1640
- & mut [ leaf_candidate] ,
1641
1644
or_start,
1642
1645
or_otherwise,
1646
+ & mut [ leaf_candidate] ,
1643
1647
) ;
1644
1648
} ) ;
1645
1649
}
1646
1650
}
1647
1651
1648
- #[ instrument( skip( self , start_block, otherwise_block, candidate, match_pair) , level = "debug" ) ]
1649
- fn test_or_pattern < ' pat > (
1650
- & mut self ,
1651
- span : Span ,
1652
- scrutinee_span : Span ,
1653
- candidate : & mut Candidate < ' pat , ' tcx > ,
1654
- start_block : BasicBlock ,
1655
- otherwise_block : BasicBlock ,
1656
- match_pair : MatchPair < ' pat , ' tcx > ,
1657
- ) {
1658
- let or_span = match_pair. pattern . span ;
1659
- self . create_or_subcandidates ( candidate, match_pair) ;
1660
- let mut or_candidate_refs: Vec < _ > = candidate. subcandidates . iter_mut ( ) . collect ( ) ;
1661
- self . match_candidates (
1662
- or_span,
1663
- or_span,
1664
- start_block,
1665
- otherwise_block,
1666
- & mut or_candidate_refs,
1667
- ) ;
1668
- self . finalize_or_candidate ( span, scrutinee_span, candidate) ;
1669
- }
1670
-
1671
1652
/// Given a match-pair that corresponds to an or-pattern, expand each subpattern into a new
1672
1653
/// subcandidate. Any candidate that has been expanded that way should be passed to
1673
1654
/// `finalize_or_candidate` after its subcandidates have been processed.
0 commit comments