Skip to content

Commit 4d8ee0d

Browse files
committed
Move Or test outside of simplify_candidate
1 parent ece0971 commit 4d8ee0d

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11701170
// be a switch or pattern comparison.
11711171
let mut split_or_candidate = false;
11721172
for candidate in &mut *candidates {
1173-
split_or_candidate |= self.simplify_candidate(candidate);
1173+
self.simplify_candidate(candidate);
1174+
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place, .. }] =
1175+
&*candidate.match_pairs
1176+
{
1177+
// Split a candidate in which the only match-pair is an or-pattern into multiple
1178+
// candidates. This is so that
1179+
//
1180+
// match x {
1181+
// 0 | 1 => { ... },
1182+
// 2 | 3 => { ... },
1183+
// }
1184+
//
1185+
// only generates a single switch.
1186+
candidate.subcandidates = self.create_or_subcandidates(candidate, place, pats);
1187+
candidate.match_pairs.pop();
1188+
split_or_candidate = true;
1189+
}
11741190
}
11751191

11761192
ensure_sufficient_stack(|| {

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

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,8 @@ use std::mem;
2323

2424
impl<'a, 'tcx> Builder<'a, 'tcx> {
2525
/// Simplify a candidate so that all match pairs require a test.
26-
///
27-
/// This method will also split a candidate, in which the only
28-
/// match-pair is an or-pattern, into multiple candidates.
29-
/// This is so that
30-
///
31-
/// match x {
32-
/// 0 | 1 => { ... },
33-
/// 2 | 3 => { ... },
34-
/// }
35-
///
36-
/// only generates a single switch. If this happens this method returns
37-
/// `true`.
3826
#[instrument(skip(self, candidate), level = "debug")]
39-
pub(super) fn simplify_candidate<'pat>(
40-
&mut self,
41-
candidate: &mut Candidate<'pat, 'tcx>,
42-
) -> bool {
27+
pub(super) fn simplify_candidate<'pat>(&mut self, candidate: &mut Candidate<'pat, 'tcx>) {
4328
debug!("{candidate:#?}");
4429
// In order to please the borrow checker, in a pattern like `x @ pat` we must lower the
4530
// bindings in `pat` before `x`. E.g. (#69971):
@@ -97,30 +82,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
9782
// Store computed bindings back in `candidate`.
9883
mem::swap(&mut candidate.bindings, &mut accumulated_bindings);
9984

100-
let did_expand_or =
101-
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place }] =
102-
&*candidate.match_pairs
103-
{
104-
candidate.subcandidates = self.create_or_subcandidates(candidate, place, pats);
105-
candidate.match_pairs.clear();
106-
true
107-
} else {
108-
false
109-
};
110-
11185
// Move or-patterns to the end, because they can result in us
11286
// creating additional candidates, so we want to test them as
11387
// late as possible.
11488
candidate.match_pairs.sort_by_key(|pair| matches!(pair.pattern.kind, PatKind::Or { .. }));
11589
debug!(simplified = ?candidate, "simplify_candidate");
116-
117-
did_expand_or
11890
}
11991

12092
/// Given `candidate` that has a single or-pattern for its match-pairs,
12193
/// creates a fresh candidate for each of its input subpatterns passed via
12294
/// `pats`.
123-
fn create_or_subcandidates<'pat>(
95+
pub(super) fn create_or_subcandidates<'pat>(
12496
&mut self,
12597
candidate: &Candidate<'pat, 'tcx>,
12698
place: &PlaceBuilder<'tcx>,
@@ -130,6 +102,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
130102
.map(|box pat| {
131103
let mut candidate = Candidate::new(place.clone(), pat, candidate.has_guard, self);
132104
self.simplify_candidate(&mut candidate);
105+
106+
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place, .. }] =
107+
&*candidate.match_pairs
108+
{
109+
candidate.subcandidates = self.create_or_subcandidates(&candidate, place, pats);
110+
candidate.match_pairs.pop();
111+
}
133112
candidate
134113
})
135114
.collect()

0 commit comments

Comments
 (0)