Avoid recursion in creating and merging or-patterns

By calling back into `match_candidates`, we only need to expand one
layer at a time. Conversely, since we always try to simplify a layer
that we just expanded, we only have to merge one layer at a time.
This commit is contained in:
Nadrieril 2024-03-04 01:57:39 +01:00
parent 10a7aa14fe
commit 23c9f698c0
2 changed files with 3 additions and 21 deletions

View File

@ -1299,7 +1299,7 @@ fn match_candidates<'pat>(
for candidate in candidates.iter_mut() {
candidate.visit_leaves(|leaf_candidate| new_candidates.push(leaf_candidate));
}
self.match_simplified_candidates(
self.match_candidates(
span,
scrutinee_span,
start_block,
@ -1556,11 +1556,7 @@ fn merge_trivial_subcandidates(&mut self, candidate: &mut Candidate<'_, 'tcx>) {
}
let mut can_merge = true;
// Not `Iterator::all` because we don't want to short-circuit.
for subcandidate in &mut candidate.subcandidates {
self.merge_trivial_subcandidates(subcandidate);
// FIXME(or_patterns; matthewjasper) Try to be more aggressive here.
can_merge &=
subcandidate.subcandidates.is_empty() && subcandidate.extra_data.is_empty();

View File

@ -67,26 +67,12 @@ pub(super) fn simplify_match_pairs<'pat>(
debug!(simplified = ?match_pairs, "simplify_match_pairs");
}
/// Create a new candidate for each pattern in `pats`, and recursively simplify tje
/// single-or-pattern case.
/// Create a new candidate for each pattern in `pats`.
pub(super) fn create_or_subcandidates<'pat>(
&mut self,
pats: &[FlatPat<'pat, 'tcx>],
has_guard: bool,
) -> Vec<Candidate<'pat, 'tcx>> {
pats.iter()
.cloned()
.map(|flat_pat| {
let mut candidate = Candidate::from_flat_pat(flat_pat, has_guard);
if let [MatchPair { test_case: TestCase::Or { pats, .. }, .. }] =
&*candidate.match_pairs
{
candidate.subcandidates = self.create_or_subcandidates(pats, has_guard);
let first_match_pair = candidate.match_pairs.pop().unwrap();
candidate.or_span = Some(first_match_pair.pattern.span);
}
candidate
})
.collect()
pats.iter().cloned().map(|flat_pat| Candidate::from_flat_pat(flat_pat, has_guard)).collect()
}
}