Skip to content

Commit 26ae53d

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 24caefd commit 26ae53d

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
12791279
for candidate in candidates.iter_mut() {
12801280
candidate.visit_leaves(|leaf_candidate| new_candidates.push(leaf_candidate));
12811281
}
1282-
self.match_simplified_candidates(
1282+
self.match_candidates(
12831283
span,
12841284
scrutinee_span,
12851285
start_block,
@@ -1536,11 +1536,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
15361536
}
15371537

15381538
let mut can_merge = true;
1539-
1540-
// Not `Iterator::all` because we don't want to short-circuit.
15411539
for subcandidate in &mut candidate.subcandidates {
1542-
self.merge_trivial_subcandidates(subcandidate);
1543-
15441540
// FIXME(or_patterns; matthewjasper) Try to be more aggressive here.
15451541
can_merge &= subcandidate.subcandidates.is_empty()
15461542
&& subcandidate.bindings.is_empty()

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7272
debug!(simplified = ?match_pairs, "simplify_match_pairs");
7373
}
7474

75-
/// Create a new candidate for each pattern in `pats`, and recursively simplify tje
76-
/// single-or-pattern case.
75+
/// Create a new candidate for each pattern in `pats`.
7776
pub(super) fn create_or_subcandidates<'pat>(
7877
&mut self,
7978
pats: &[FlatPat<'pat, 'tcx>],
8079
has_guard: bool,
8180
) -> Vec<Candidate<'pat, 'tcx>> {
82-
pats.iter()
83-
.cloned()
84-
.map(|flat_pat| {
85-
let mut candidate = Candidate::from_flat_pat(flat_pat, has_guard);
86-
if let [MatchPair { test_case: TestCase::Or { pats, .. }, .. }] =
87-
&*candidate.match_pairs
88-
{
89-
candidate.subcandidates = self.create_or_subcandidates(pats, has_guard);
90-
let first_match_pair = candidate.match_pairs.pop().unwrap();
91-
candidate.or_span = Some(first_match_pair.pattern.span);
92-
}
93-
candidate
94-
})
95-
.collect()
81+
pats.iter().cloned().map(|flat_pat| Candidate::from_flat_pat(flat_pat, has_guard)).collect()
9682
}
9783
}

0 commit comments

Comments
 (0)