Skip to content

Commit

Permalink
Avoid recursion in creating and merging or-patterns
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Nadrieril committed Mar 27, 2024
1 parent 10a7aa1 commit 23c9f69
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 21 deletions.
6 changes: 1 addition & 5 deletions compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
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,
Expand Down Expand Up @@ -1556,11 +1556,7 @@ impl<'a, 'tcx> Builder<'a, '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();
Expand Down
18 changes: 2 additions & 16 deletions compiler/rustc_mir_build/src/build/matches/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
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()
}
}

0 comments on commit 23c9f69

Please sign in to comment.