Skip to content

Commit 8c1b720

Browse files
committed
Unroll
1 parent 4689da3 commit 8c1b720

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,13 +1170,33 @@ 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(
1173+
self.simplify_candidate(
11741174
&mut candidate.bindings,
11751175
&mut candidate.ascriptions,
11761176
&mut candidate.match_pairs,
1177-
&mut candidate.subcandidates,
1178-
candidate.has_guard,
11791177
);
1178+
1179+
split_or_candidate |= {
1180+
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place, .. }] =
1181+
&*candidate.match_pairs
1182+
{
1183+
// Split a candidate in which the only match-pair is an or-pattern into multiple
1184+
// candidates. This is so that
1185+
//
1186+
// match x {
1187+
// 0 | 1 => { ... },
1188+
// 2 | 3 => { ... },
1189+
// }
1190+
//
1191+
// only generates a single switch.
1192+
candidate.subcandidates =
1193+
self.create_or_subcandidates(place, pats, candidate.has_guard);
1194+
candidate.match_pairs.pop();
1195+
true
1196+
} else {
1197+
false
1198+
}
1199+
};
11801200
}
11811201

11821202
ensure_sufficient_stack(|| {

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

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,8 @@ use std::mem;
2020

2121
impl<'a, 'tcx> Builder<'a, 'tcx> {
2222
/// Simplify a candidate so that all match pairs require a test.
23-
///
24-
/// This method will also split a candidate, in which the only
25-
/// match-pair is an or-pattern, into multiple candidates.
26-
/// This is so that
27-
///
28-
/// match x {
29-
/// 0 | 1 => { ... },
30-
/// 2 | 3 => { ... },
31-
/// }
32-
///
33-
/// only generates a single switch. If this happens this method returns
34-
/// `true`.
3523
#[instrument(skip(self), level = "debug")]
3624
pub(super) fn simplify_candidate<'pat>(
37-
&mut self,
38-
bindings: &mut Vec<Binding<'tcx>>,
39-
ascriptions: &mut Vec<Ascription<'tcx>>,
40-
match_pairs: &mut Vec<MatchPair<'pat, 'tcx>>,
41-
subcandidates: &mut Vec<Candidate<'pat, 'tcx>>,
42-
has_guard: bool,
43-
) -> bool {
44-
self.simplify_candidate_inner(bindings, ascriptions, match_pairs);
45-
46-
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place, .. }] =
47-
&**match_pairs
48-
{
49-
*subcandidates = self.create_or_subcandidates(place, pats, has_guard);
50-
match_pairs.pop();
51-
return true;
52-
}
53-
54-
false
55-
}
56-
57-
#[instrument(skip(self), level = "debug")]
58-
pub(super) fn simplify_candidate_inner<'pat>(
5925
&mut self,
6026
candidate_bindings: &mut Vec<Binding<'tcx>>,
6127
candidate_ascriptions: &mut Vec<Ascription<'tcx>>,
@@ -144,7 +110,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
144110
/// Given `candidate` that has a single or-pattern for its match-pairs,
145111
/// creates a fresh candidate for each of its input subpatterns passed via
146112
/// `pats`.
147-
fn create_or_subcandidates<'pat>(
113+
pub(super) fn create_or_subcandidates<'pat>(
148114
&mut self,
149115
place: &PlaceBuilder<'tcx>,
150116
pats: &'pat [Box<Pat<'tcx>>],
@@ -157,9 +123,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
157123
&mut candidate.bindings,
158124
&mut candidate.ascriptions,
159125
&mut candidate.match_pairs,
160-
&mut candidate.subcandidates,
161-
candidate.has_guard,
162126
);
127+
128+
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place, .. }] =
129+
&*candidate.match_pairs
130+
{
131+
candidate.subcandidates =
132+
self.create_or_subcandidates(place, pats, candidate.has_guard);
133+
candidate.match_pairs.pop();
134+
}
163135
candidate
164136
})
165137
.collect()

0 commit comments

Comments
 (0)