Skip to content

Commit 8f88a7c

Browse files
committed
Small simplification
1 parent 3811f40 commit 8f88a7c

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

+20-14
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,17 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
11401140
|_| {},
11411141
);
11421142
}
1143+
1144+
/// Visit the leaf candidates in reverse order.
1145+
fn visit_leaves_rev<'a>(&'a mut self, mut visit_leaf: impl FnMut(&'a mut Self)) {
1146+
traverse_candidate(
1147+
self,
1148+
&mut (),
1149+
&mut move |c, _| visit_leaf(c),
1150+
move |c, _| c.subcandidates.iter_mut().rev(),
1151+
|_| {},
1152+
);
1153+
}
11431154
}
11441155

11451156
/// A depth-first traversal of the `Candidate` and all of its recursive
@@ -1333,23 +1344,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13331344
let otherwise_block =
13341345
self.match_candidates(match_start_span, scrutinee_span, block, candidates);
13351346

1336-
// Link each leaf candidate to the `false_edge_start_block` of the next one.
1337-
let mut previous_candidate: Option<&mut Candidate<'_, '_>> = None;
1338-
for candidate in candidates {
1339-
candidate.visit_leaves(|leaf_candidate| {
1340-
if let Some(ref mut prev) = previous_candidate {
1341-
assert!(leaf_candidate.false_edge_start_block.is_some());
1342-
prev.next_candidate_start_block = leaf_candidate.false_edge_start_block;
1343-
}
1344-
previous_candidate = Some(leaf_candidate);
1347+
// Link each leaf candidate to the `false_edge_start_block` of the next one. In the
1348+
// refutable case we also want a false edge to the failure block.
1349+
let mut next_candidate_start_block = if refutable { Some(otherwise_block) } else { None };
1350+
for candidate in candidates.iter_mut().rev() {
1351+
candidate.visit_leaves_rev(|leaf_candidate| {
1352+
leaf_candidate.next_candidate_start_block = next_candidate_start_block;
1353+
assert!(leaf_candidate.false_edge_start_block.is_some());
1354+
next_candidate_start_block = leaf_candidate.false_edge_start_block;
13451355
});
13461356
}
13471357

1348-
if refutable {
1349-
// In refutable cases there's always at least one candidate, and we want a false edge to
1350-
// the failure block.
1351-
previous_candidate.as_mut().unwrap().next_candidate_start_block = Some(otherwise_block)
1352-
} else {
1358+
if !refutable {
13531359
// Match checking ensures `otherwise_block` is actually unreachable in irrefutable
13541360
// cases.
13551361
let source_info = self.source_info(scrutinee_span);

0 commit comments

Comments
 (0)