Skip to content

Commit 23c9f69

Browse files
committed
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.
1 parent 10a7aa1 commit 23c9f69

File tree

2 files changed

+3
-21
lines changed

2 files changed

+3
-21
lines changed

compiler/rustc_mir_build/src/build/matches/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
12991299
for candidate in candidates.iter_mut() {
13001300
candidate.visit_leaves(|leaf_candidate| new_candidates.push(leaf_candidate));
13011301
}
1302-
self.match_simplified_candidates(
1302+
self.match_candidates(
13031303
span,
13041304
scrutinee_span,
13051305
start_block,
@@ -1556,11 +1556,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15561556
}
15571557

15581558
let mut can_merge = true;
1559-
1560-
// Not `Iterator::all` because we don't want to short-circuit.
15611559
for subcandidate in &mut candidate.subcandidates {
1562-
self.merge_trivial_subcandidates(subcandidate);
1563-
15641560
// FIXME(or_patterns; matthewjasper) Try to be more aggressive here.
15651561
can_merge &=
15661562
subcandidate.subcandidates.is_empty() && subcandidate.extra_data.is_empty();

compiler/rustc_mir_build/src/build/matches/simplify.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6767
debug!(simplified = ?match_pairs, "simplify_match_pairs");
6868
}
6969

70-
/// Create a new candidate for each pattern in `pats`, and recursively simplify tje
71-
/// single-or-pattern case.
70+
/// Create a new candidate for each pattern in `pats`.
7271
pub(super) fn create_or_subcandidates<'pat>(
7372
&mut self,
7473
pats: &[FlatPat<'pat, 'tcx>],
7574
has_guard: bool,
7675
) -> Vec<Candidate<'pat, 'tcx>> {
77-
pats.iter()
78-
.cloned()
79-
.map(|flat_pat| {
80-
let mut candidate = Candidate::from_flat_pat(flat_pat, has_guard);
81-
if let [MatchPair { test_case: TestCase::Or { pats, .. }, .. }] =
82-
&*candidate.match_pairs
83-
{
84-
candidate.subcandidates = self.create_or_subcandidates(pats, has_guard);
85-
let first_match_pair = candidate.match_pairs.pop().unwrap();
86-
candidate.or_span = Some(first_match_pair.pattern.span);
87-
}
88-
candidate
89-
})
90-
.collect()
76+
pats.iter().cloned().map(|flat_pat| Candidate::from_flat_pat(flat_pat, has_guard)).collect()
9177
}
9278
}

0 commit comments

Comments
 (0)