Skip to content

Commit 31177a2

Browse files
committed
Don't repeatedly simplify already-simplified match pairs
1 parent 573b28c commit 31177a2

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5959
//
6060
// final bindings: [6, 7, 4, 5, 1, 2, 3]
6161
let mut accumulated_bindings = mem::take(candidate_bindings);
62-
// Repeatedly simplify match pairs until fixed point is reached
62+
let mut simplified_match_pairs = Vec::new();
63+
// Repeatedly simplify match pairs until we're left with only unsimplifiable ones.
6364
loop {
64-
let mut changed = false;
6565
for match_pair in mem::take(match_pairs) {
66-
match self.simplify_match_pair(
66+
if let Err(match_pair) = self.simplify_match_pair(
6767
match_pair,
6868
candidate_bindings,
6969
candidate_ascriptions,
7070
match_pairs,
7171
) {
72-
Ok(()) => {
73-
changed = true;
74-
}
75-
Err(match_pair) => {
76-
match_pairs.push(match_pair);
77-
}
72+
simplified_match_pairs.push(match_pair);
7873
}
7974
}
8075

@@ -83,14 +78,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8378
mem::swap(candidate_bindings, &mut accumulated_bindings);
8479
candidate_bindings.clear();
8580

86-
if !changed {
87-
// If we were not able to simplify anymore, done.
81+
if match_pairs.is_empty() {
8882
break;
8983
}
9084
}
9185

9286
// Store computed bindings back in `candidate_bindings`.
9387
mem::swap(candidate_bindings, &mut accumulated_bindings);
88+
// Store simplified match pairs back in `match_pairs`.
89+
mem::swap(match_pairs, &mut simplified_match_pairs);
9490

9591
// Move or-patterns to the end, because they can result in us
9692
// creating additional candidates, so we want to test them as

tests/mir-opt/exponential_or.match_tuple.SimplifyCfg-initial.after.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ fn match_tuple(_1: (u32, bool, Option<i32>, u32)) -> u32 {
1919

2020
bb0: {
2121
PlaceMention(_1);
22-
switchInt((_1.0: u32)) -> [1: bb2, 4: bb2, otherwise: bb1];
22+
_2 = discriminant((_1.2: std::option::Option<i32>));
23+
switchInt(move _2) -> [0: bb3, 1: bb2, otherwise: bb1];
2324
}
2425

2526
bb1: {
@@ -28,12 +29,11 @@ fn match_tuple(_1: (u32, bool, Option<i32>, u32)) -> u32 {
2829
}
2930

3031
bb2: {
31-
_2 = discriminant((_1.2: std::option::Option<i32>));
32-
switchInt(move _2) -> [0: bb4, 1: bb3, otherwise: bb1];
32+
switchInt((((_1.2: std::option::Option<i32>) as Some).0: i32)) -> [1: bb3, 8: bb3, otherwise: bb1];
3333
}
3434

3535
bb3: {
36-
switchInt((((_1.2: std::option::Option<i32>) as Some).0: i32)) -> [1: bb4, 8: bb4, otherwise: bb1];
36+
switchInt((_1.0: u32)) -> [1: bb4, 4: bb4, otherwise: bb1];
3737
}
3838

3939
bb4: {

0 commit comments

Comments
 (0)