Skip to content

Commit 96ff1a4

Browse files
committed
Move Or test out of the loop
1 parent 3ea464f commit 96ff1a4

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6262
let mut existing_bindings = mem::take(&mut candidate.bindings);
6363
let mut new_bindings = Vec::new();
6464
loop {
65-
let match_pairs = mem::take(&mut candidate.match_pairs);
66-
67-
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place }] =
68-
&*match_pairs
69-
{
70-
existing_bindings.extend_from_slice(&new_bindings);
71-
mem::swap(&mut candidate.bindings, &mut existing_bindings);
72-
candidate.subcandidates = self.create_or_subcandidates(candidate, place, pats);
73-
return true;
74-
}
75-
7665
let mut changed = false;
77-
for match_pair in match_pairs {
66+
for match_pair in mem::take(&mut candidate.match_pairs) {
7867
match self.simplify_match_pair(match_pair, candidate) {
7968
Ok(()) => {
8069
changed = true;
@@ -84,6 +73,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8473
}
8574
}
8675
}
76+
8777
// Avoid issue #69971: the binding order should be right to left if there are more
8878
// bindings after `@` to please the borrow checker
8979
// Ex
@@ -102,18 +92,32 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10292
candidate.bindings.clear();
10393

10494
if !changed {
105-
existing_bindings.extend_from_slice(&new_bindings);
106-
mem::swap(&mut candidate.bindings, &mut existing_bindings);
107-
// Move or-patterns to the end, because they can result in us
108-
// creating additional candidates, so we want to test them as
109-
// late as possible.
110-
candidate
111-
.match_pairs
112-
.sort_by_key(|pair| matches!(pair.pattern.kind, PatKind::Or { .. }));
113-
debug!(simplified = ?candidate, "simplify_candidate");
114-
return false; // if we were not able to simplify any, done.
95+
// If we were not able to simplify anymore, done.
96+
break;
11597
}
11698
}
99+
100+
existing_bindings.extend_from_slice(&new_bindings);
101+
mem::swap(&mut candidate.bindings, &mut existing_bindings);
102+
103+
let did_expand_or =
104+
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place }] =
105+
&*candidate.match_pairs
106+
{
107+
candidate.subcandidates = self.create_or_subcandidates(candidate, place, pats);
108+
candidate.match_pairs.clear();
109+
true
110+
} else {
111+
false
112+
};
113+
114+
// Move or-patterns to the end, because they can result in us
115+
// creating additional candidates, so we want to test them as
116+
// late as possible.
117+
candidate.match_pairs.sort_by_key(|pair| matches!(pair.pattern.kind, PatKind::Or { .. }));
118+
debug!(simplified = ?candidate, "simplify_candidate");
119+
120+
did_expand_or
117121
}
118122

119123
/// Given `candidate` that has a single or-pattern for its match-pairs,

0 commit comments

Comments
 (0)