Skip to content

Commit 25cb909

Browse files
committed
Small simplification
1 parent 716752e commit 25cb909

File tree

1 file changed

+20
-14
lines changed
  • compiler/rustc_mir_build/src/build/matches

1 file changed

+20
-14
lines changed

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,17 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
10671067
|_| {},
10681068
);
10691069
}
1070+
1071+
/// Visit the leaf candidates in reverse order.
1072+
fn visit_leaves_rev<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
1073+
traverse_candidate(
1074+
self,
1075+
&mut (),
1076+
&mut move |c, _| visit_leaf(c),
1077+
move |c, _| c.subcandidates.iter_mut().rev(),
1078+
|_| {},
1079+
);
1080+
}
10701081
}
10711082

10721083
/// A depth-first traversal of the `Candidate` and all of its recursive
@@ -1251,23 +1262,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
12511262
// This will generate code to test scrutinee_place and branch to the appropriate arm block
12521263
self.match_candidates(match_start_span, scrutinee_span, block, otherwise_block, candidates);
12531264

1254-
// Link each leaf candidate to the `false_edge_start_block` of the next one.
1255-
let mut previous_candidate: Option<&mut Candidate<'_, '_>> = None;
1256-
for candidate in candidates {
1257-
candidate.visit_leaves(|leaf_candidate| {
1258-
if let Some(ref mut prev) = previous_candidate {
1259-
assert!(leaf_candidate.false_edge_start_block.is_some());
1260-
prev.next_candidate_start_block = leaf_candidate.false_edge_start_block;
1261-
}
1262-
previous_candidate = Some(leaf_candidate);
1265+
// Link each leaf candidate to the `false_edge_start_block` of the next one. In the
1266+
// refutable case we also want a false edge to the failure block.
1267+
let mut next_candidate_start_block = if refutable { Some(otherwise_block) } else { None };
1268+
for candidate in candidates.iter_mut().rev() {
1269+
candidate.visit_leaves_rev(|leaf_candidate| {
1270+
leaf_candidate.next_candidate_start_block = next_candidate_start_block;
1271+
assert!(leaf_candidate.false_edge_start_block.is_some());
1272+
next_candidate_start_block = leaf_candidate.false_edge_start_block;
12631273
});
12641274
}
12651275

1266-
if refutable {
1267-
// In refutable cases there's always at least one candidate, and we want a false edge to
1268-
// the failure block.
1269-
previous_candidate.as_mut().unwrap().next_candidate_start_block = Some(otherwise_block)
1270-
} else {
1276+
if !refutable {
12711277
// Match checking ensures `otherwise_block` is actually unreachable in irrefutable
12721278
// cases.
12731279
let source_info = self.source_info(scrutinee_span);

0 commit comments

Comments
 (0)