@@ -1364,61 +1364,104 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13641364 otherwise_block : BasicBlock ,
13651365 candidates : & mut [ & mut Candidate < ' pat , ' tcx > ] ,
13661366 ) {
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) ;
13701401
13711402 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 { .. } , .. } , ..] =
13851417 & * candidate. match_pairs
13861418 {
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.
13891423 for subcandidate in candidate. subcandidates . iter_mut ( ) {
1390- new_candidates . push ( subcandidate) ;
1424+ expanded_candidates . push ( subcandidate) ;
13911425 }
13921426 } else {
1393- new_candidates . push ( candidate) ;
1427+ expanded_candidates . push ( candidate) ;
13941428 }
13951429 }
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.
13961435 self . match_candidates (
13971436 span,
13981437 scrutinee_span,
13991438 start_block,
1400- otherwise_block ,
1401- new_candidates . as_mut_slice ( ) ,
1439+ remainder_start ,
1440+ expanded_candidates . as_mut_slice ( ) ,
14021441 ) ;
14031442
1404- for candidate in candidates {
1443+ // Simplify subcandidates and process any leftover match pairs.
1444+ for candidate in candidates_to_expand {
14051445 if !candidate. subcandidates . is_empty ( ) {
1406- self . merge_trivial_subcandidates ( candidate) ;
1446+ self . finalize_or_candidate ( span , scrutinee_span , candidate) ;
14071447 }
14081448 }
1409- } else {
1410- self . match_simplified_candidates (
1449+
1450+ // Process the remaining candidates.
1451+ self . match_candidates (
14111452 span,
14121453 scrutinee_span,
1413- start_block ,
1454+ remainder_start ,
14141455 otherwise_block,
1415- candidates ,
1456+ remaining_candidates ,
14161457 ) ;
14171458 }
14181459 } ) ;
14191460 }
14201461
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 (
14221465 & mut self ,
14231466 span : Span ,
14241467 scrutinee_span : Span ,
@@ -1443,7 +1486,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
14431486 // The first candidate has satisfied all its match pairs; we link it up and continue
14441487 // with the remaining candidates.
14451488 start_block = self . select_matched_candidate ( first, start_block) ;
1446- self . match_simplified_candidates (
1489+ self . match_expanded_candidates (
14471490 span,
14481491 scrutinee_span,
14491492 start_block,
@@ -1453,7 +1496,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
14531496 }
14541497 candidates => {
14551498 // The first candidate has some unsatisfied match pairs; we proceed to do more tests.
1456- self . test_candidates_with_or (
1499+ self . test_candidates (
14571500 span,
14581501 scrutinee_span,
14591502 candidates,
@@ -1506,8 +1549,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15061549 otherwise_block
15071550 }
15081551
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` .
15111554 ///
15121555 /// Given a pattern `(P | Q, R | S)` we (in principle) generate a CFG like
15131556 /// so:
@@ -1559,45 +1602,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15591602 /// |
15601603 /// ...
15611604 /// ```
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`.
16011605 fn finalize_or_candidate (
16021606 & mut self ,
16031607 span : Span ,
@@ -1634,40 +1638,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16341638 } else {
16351639 last_otherwise. unwrap ( )
16361640 } ;
1637- self . test_candidates_with_or (
1641+ self . match_candidates (
16381642 span,
16391643 scrutinee_span,
1640- & mut [ leaf_candidate] ,
16411644 or_start,
16421645 or_otherwise,
1646+ & mut [ leaf_candidate] ,
16431647 ) ;
16441648 } ) ;
16451649 }
16461650 }
16471651
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-
16711652 /// Given a match-pair that corresponds to an or-pattern, expand each subpattern into a new
16721653 /// subcandidate. Any candidate that has been expanded that way should be passed to
16731654 /// `finalize_or_candidate` after its subcandidates have been processed.
0 commit comments