@@ -1272,9 +1272,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
12721272 ) {
12731273 let mut split_or_candidate = false ;
12741274 for candidate in & mut * candidates {
1275- if let [ MatchPair { test_case : TestCase :: Or { pats, .. } , .. } ] =
1276- & * candidate. match_pairs
1277- {
1275+ if let [ MatchPair { test_case : TestCase :: Or { .. } , .. } ] = & * candidate. match_pairs {
12781276 // Split a candidate in which the only match-pair is an or-pattern into multiple
12791277 // candidates. This is so that
12801278 //
@@ -1284,9 +1282,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
12841282 // }
12851283 //
12861284 // only generates a single switch.
1287- candidate. subcandidates = self . create_or_subcandidates ( pats, candidate. has_guard ) ;
1288- let first_match_pair = candidate. match_pairs . pop ( ) . unwrap ( ) ;
1289- candidate. or_span = Some ( first_match_pair. pattern . span ) ;
1285+ let match_pair = candidate. match_pairs . pop ( ) . unwrap ( ) ;
1286+ self . create_or_subcandidates ( candidate, & match_pair) ;
12901287 split_or_candidate = true ;
12911288 }
12921289 }
@@ -1476,12 +1473,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
14761473
14771474 let match_pairs = mem:: take ( & mut first_candidate. match_pairs ) ;
14781475 let ( first_match_pair, remaining_match_pairs) = match_pairs. split_first ( ) . unwrap ( ) ;
1479- let TestCase :: Or { ref pats } = & first_match_pair. test_case else { unreachable ! ( ) } ;
14801476
14811477 let remainder_start = self . cfg . start_new_block ( ) ;
1482- let or_span = first_match_pair. pattern . span ;
14831478 // Test the alternatives of this or-pattern.
1484- self . test_or_pattern ( first_candidate, start_block, remainder_start, pats , or_span ) ;
1479+ self . test_or_pattern ( first_candidate, start_block, remainder_start, first_match_pair ) ;
14851480
14861481 if !remaining_match_pairs. is_empty ( ) {
14871482 // If more match pairs remain, test them after each subcandidate.
@@ -1516,39 +1511,48 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15161511 ) ;
15171512 }
15181513
1519- #[ instrument(
1520- skip( self , start_block, otherwise_block, or_span, candidate, pats) ,
1521- level = "debug"
1522- ) ]
1514+ #[ instrument( skip( self , start_block, otherwise_block, candidate, match_pair) , level = "debug" ) ]
15231515 fn test_or_pattern < ' pat > (
15241516 & mut self ,
15251517 candidate : & mut Candidate < ' pat , ' tcx > ,
15261518 start_block : BasicBlock ,
15271519 otherwise_block : BasicBlock ,
1528- pats : & [ FlatPat < ' pat , ' tcx > ] ,
1529- or_span : Span ,
1520+ match_pair : & MatchPair < ' pat , ' tcx > ,
15301521 ) {
1531- debug ! ( "candidate={:#?}\n pats={:#?}" , candidate, pats) ;
1532- let mut or_candidates: Vec < _ > = pats
1533- . iter ( )
1534- . cloned ( )
1535- . map ( |flat_pat| Candidate :: from_flat_pat ( flat_pat, candidate. has_guard ) )
1536- . collect ( ) ;
1537- let mut or_candidate_refs: Vec < _ > = or_candidates. iter_mut ( ) . collect ( ) ;
1522+ self . create_or_subcandidates ( candidate, match_pair) ;
1523+ let mut or_candidate_refs: Vec < _ > = candidate. subcandidates . iter_mut ( ) . collect ( ) ;
1524+ let or_span = match_pair. pattern . span ;
15381525 self . match_candidates (
15391526 or_span,
15401527 or_span,
15411528 start_block,
15421529 otherwise_block,
15431530 & mut or_candidate_refs,
15441531 ) ;
1545- candidate. subcandidates = or_candidates;
1546- candidate. or_span = Some ( or_span) ;
15471532 self . merge_trivial_subcandidates ( candidate) ;
15481533 }
15491534
1550- /// Try to merge all of the subcandidates of the given candidate into one.
1551- /// This avoids exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`.
1535+ /// Given a match-pair that corresponds to an or-pattern, expand each subpattern into a new
1536+ /// subcandidate. Any candidate that has been expanded that way should be passed to
1537+ /// `merge_trivial_subcandidates` after its subcandidates have been processed.
1538+ fn create_or_subcandidates < ' pat > (
1539+ & mut self ,
1540+ candidate : & mut Candidate < ' pat , ' tcx > ,
1541+ match_pair : & MatchPair < ' pat , ' tcx > ,
1542+ ) {
1543+ let TestCase :: Or { ref pats } = & match_pair. test_case else { bug ! ( ) } ;
1544+ debug ! ( "expanding or-pattern: candidate={:#?}\n pats={:#?}" , candidate, pats) ;
1545+ candidate. or_span = Some ( match_pair. pattern . span ) ;
1546+ candidate. subcandidates = pats
1547+ . iter ( )
1548+ . cloned ( )
1549+ . map ( |flat_pat| Candidate :: from_flat_pat ( flat_pat, candidate. has_guard ) )
1550+ . collect ( ) ;
1551+ }
1552+
1553+ /// Try to merge all of the subcandidates of the given candidate into one. This avoids
1554+ /// exponentially large CFGs in cases like `(1 | 2, 3 | 4, ...)`. The or-pattern should have
1555+ /// been expanded with `create_or_subcandidates`.
15521556 fn merge_trivial_subcandidates ( & mut self , candidate : & mut Candidate < ' _ , ' tcx > ) {
15531557 if candidate. subcandidates . is_empty ( ) || candidate. has_guard {
15541558 // FIXME(or_patterns; matthewjasper) Don't give up if we have a guard.
0 commit comments