@@ -1364,9 +1364,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1364
1364
otherwise_block: BasicBlock,
1365
1365
candidates: &mut [&mut Candidate<'pat, 'tcx>],
1366
1366
) {
1367
- let mut split_or_candidate = false;
1368
- for candidate in &mut *candidates {
1369
- if let [MatchPair { test_case: TestCase::Or { .. }, .. }] = &*candidate.match_pairs {
1367
+ let expand_or_pats = candidates.iter().any(|candidate| {
1368
+ matches!(&*candidate.match_pairs, [MatchPair { test_case: TestCase::Or { .. }, .. }])
1369
+ });
1370
+
1371
+ ensure_sufficient_stack(|| {
1372
+ if expand_or_pats {
1370
1373
// Split a candidate in which the only match-pair is an or-pattern into multiple
1371
1374
// candidates. This is so that
1372
1375
//
@@ -1376,30 +1379,32 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
1376
1379
// }
1377
1380
//
1378
1381
// only generates a single switch.
1379
- let match_pair = candidate.match_pairs.pop().unwrap();
1380
- self.create_or_subcandidates(candidate, match_pair);
1381
- split_or_candidate = true;
1382
- }
1383
- }
1384
-
1385
- ensure_sufficient_stack(|| {
1386
- if split_or_candidate {
1387
- // At least one of the candidates has been split into subcandidates.
1388
- // We need to change the candidate list to include those.
1389
1382
let mut new_candidates = Vec::new();
1390
1383
for candidate in candidates.iter_mut() {
1391
- candidate.visit_leaves(|leaf_candidate| new_candidates.push(leaf_candidate));
1384
+ if let [MatchPair { test_case: TestCase::Or { .. }, .. }] =
1385
+ &*candidate.match_pairs
1386
+ {
1387
+ let match_pair = candidate.match_pairs.pop().unwrap();
1388
+ self.create_or_subcandidates(candidate, match_pair);
1389
+ for subcandidate in candidate.subcandidates.iter_mut() {
1390
+ new_candidates.push(subcandidate);
1391
+ }
1392
+ } else {
1393
+ new_candidates.push(candidate);
1394
+ }
1392
1395
}
1393
1396
self.match_candidates(
1394
1397
span,
1395
1398
scrutinee_span,
1396
1399
start_block,
1397
1400
otherwise_block,
1398
- &mut * new_candidates,
1401
+ new_candidates.as_mut_slice() ,
1399
1402
);
1400
1403
1401
1404
for candidate in candidates {
1402
- self.merge_trivial_subcandidates(candidate);
1405
+ if !candidate.subcandidates.is_empty() {
1406
+ self.merge_trivial_subcandidates(candidate);
1407
+ }
1403
1408
}
1404
1409
} else {
1405
1410
self.match_simplified_candidates(
0 commit comments