Tweak simple or-pattern expansion

This commit is contained in:
Nadrieril 2024-03-20 15:53:25 +01:00
parent 6b84d7566e
commit e74b30e3a9

View File

@ -1364,9 +1364,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
otherwise_block: BasicBlock, otherwise_block: BasicBlock,
candidates: &mut [&mut Candidate<'pat, 'tcx>], candidates: &mut [&mut Candidate<'pat, 'tcx>],
) { ) {
let mut split_or_candidate = false; let expand_or_pats = candidates.iter().any(|candidate| {
for candidate in &mut *candidates { matches!(&*candidate.match_pairs, [MatchPair { test_case: TestCase::Or { .. }, .. }])
if let [MatchPair { test_case: TestCase::Or { .. }, .. }] = &*candidate.match_pairs { });
ensure_sufficient_stack(|| {
if expand_or_pats {
// Split a candidate in which the only match-pair is an or-pattern into multiple // Split a candidate in which the only match-pair is an or-pattern into multiple
// candidates. This is so that // candidates. This is so that
// //
@ -1376,30 +1379,32 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// } // }
// //
// only generates a single switch. // only generates a single switch.
let match_pair = candidate.match_pairs.pop().unwrap();
self.create_or_subcandidates(candidate, match_pair);
split_or_candidate = true;
}
}
ensure_sufficient_stack(|| {
if split_or_candidate {
// At least one of the candidates has been split into subcandidates.
// We need to change the candidate list to include those.
let mut new_candidates = Vec::new(); let mut new_candidates = Vec::new();
for candidate in candidates.iter_mut() { for candidate in candidates.iter_mut() {
candidate.visit_leaves(|leaf_candidate| new_candidates.push(leaf_candidate)); if let [MatchPair { test_case: TestCase::Or { .. }, .. }] =
&*candidate.match_pairs
{
let match_pair = candidate.match_pairs.pop().unwrap();
self.create_or_subcandidates(candidate, match_pair);
for subcandidate in candidate.subcandidates.iter_mut() {
new_candidates.push(subcandidate);
}
} else {
new_candidates.push(candidate);
}
} }
self.match_candidates( self.match_candidates(
span, span,
scrutinee_span, scrutinee_span,
start_block, start_block,
otherwise_block, otherwise_block,
&mut *new_candidates, new_candidates.as_mut_slice(),
); );
for candidate in candidates { for candidate in candidates {
self.merge_trivial_subcandidates(candidate); if !candidate.subcandidates.is_empty() {
self.merge_trivial_subcandidates(candidate);
}
} }
} else { } else {
self.match_simplified_candidates( self.match_simplified_candidates(