Skip to content

Commit 3f729a0

Browse files
committed
Small simplification
1 parent f92a6c4 commit 3f729a0

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
@@ -1130,6 +1130,17 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
11301130
|_| {},
11311131
);
11321132
}
1133+
1134+
/// Visit the leaf candidates in reverse order.
1135+
fn visit_leaves_rev<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
1136+
traverse_candidate(
1137+
self,
1138+
&mut (),
1139+
&mut move |c, _| visit_leaf(c),
1140+
move |c, _| c.subcandidates.iter_mut().rev(),
1141+
|_| {},
1142+
);
1143+
}
11331144
}
11341145

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

1317-
// Link each leaf candidate to the `false_edge_start_block` of the next one.
1318-
let mut previous_candidate: Option<&mut Candidate<'_, '_>> = None;
1319-
for candidate in candidates {
1320-
candidate.visit_leaves(|leaf_candidate| {
1321-
if let Some(ref mut prev) = previous_candidate {
1322-
assert!(leaf_candidate.false_edge_start_block.is_some());
1323-
prev.next_candidate_start_block = leaf_candidate.false_edge_start_block;
1324-
}
1325-
previous_candidate = Some(leaf_candidate);
1328+
// Link each leaf candidate to the `false_edge_start_block` of the next one. In the
1329+
// refutable case we also want a false edge to the failure block.
1330+
let mut next_candidate_start_block = if refutable { Some(otherwise_block) } else { None };
1331+
for candidate in candidates.iter_mut().rev() {
1332+
candidate.visit_leaves_rev(|leaf_candidate| {
1333+
leaf_candidate.next_candidate_start_block = next_candidate_start_block;
1334+
assert!(leaf_candidate.false_edge_start_block.is_some());
1335+
next_candidate_start_block = leaf_candidate.false_edge_start_block;
13261336
});
13271337
}
13281338

1329-
if refutable {
1330-
// In refutable cases there's always at least one candidate, and we want a false edge to
1331-
// the failure block.
1332-
previous_candidate.as_mut().unwrap().next_candidate_start_block = Some(otherwise_block)
1333-
} else {
1339+
if !refutable {
13341340
// Match checking ensures `otherwise_block` is actually unreachable in irrefutable
13351341
// cases.
13361342
let source_info = self.source_info(scrutinee_span);

0 commit comments

Comments
 (0)