Skip to content

Commit cd0fc78

Browse files
committed
Replace the loop with recursive calls for clarity
1 parent 998d0e9 commit cd0fc78

File tree

1 file changed

+31
-28
lines changed
  • compiler/rustc_mir_build/src/build/matches

1 file changed

+31
-28
lines changed

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

+31-28
Original file line numberDiff line numberDiff line change
@@ -1212,35 +1212,38 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
12121212
candidates: &mut [&mut Candidate<'_, 'tcx>],
12131213
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
12141214
) {
1215-
// The candidates are sorted by priority. Check to see whether the
1216-
// higher priority candidates (and hence at the front of the slice)
1217-
// have satisfied all their match pairs.
1218-
let fully_matched = candidates.iter().take_while(|c| c.match_pairs.is_empty()).count();
1219-
debug!("match_candidates: {:?} candidates fully matched", fully_matched);
1220-
let (matched_candidates, unmatched_candidates) = candidates.split_at_mut(fully_matched);
1221-
1222-
for candidate in matched_candidates.iter_mut() {
1223-
start_block = self.select_matched_candidate(candidate, start_block, fake_borrows);
1224-
}
1225-
1226-
// If there are no candidates that still need testing, we're
1227-
// done. Since all matches are exhaustive, execution should
1228-
// never reach this point.
1229-
if unmatched_candidates.is_empty() {
1230-
let source_info = self.source_info(span);
1231-
self.cfg.goto(start_block, source_info, otherwise_block);
1232-
return;
1215+
match candidates {
1216+
[] => {
1217+
// If there are no candidates that still need testing, we're done. Since all matches are
1218+
// exhaustive, execution should never reach this point.
1219+
let source_info = self.source_info(span);
1220+
self.cfg.goto(start_block, source_info, otherwise_block);
1221+
}
1222+
[first, remaining @ ..] if first.match_pairs.is_empty() => {
1223+
// The first candidate has satisfied all its match pairs; we link it up and continue
1224+
// with the remaining candidates.
1225+
start_block = self.select_matched_candidate(first, start_block, fake_borrows);
1226+
self.match_simplified_candidates(
1227+
span,
1228+
scrutinee_span,
1229+
start_block,
1230+
otherwise_block,
1231+
remaining,
1232+
fake_borrows,
1233+
)
1234+
}
1235+
candidates => {
1236+
// The first candidate has some unsatisfied match pairs; we proceed to do more tests.
1237+
self.test_candidates_with_or(
1238+
span,
1239+
scrutinee_span,
1240+
candidates,
1241+
start_block,
1242+
otherwise_block,
1243+
fake_borrows,
1244+
);
1245+
}
12331246
}
1234-
1235-
// Test for the remaining candidates.
1236-
self.test_candidates_with_or(
1237-
span,
1238-
scrutinee_span,
1239-
unmatched_candidates,
1240-
start_block,
1241-
otherwise_block,
1242-
fake_borrows,
1243-
);
12441247
}
12451248

12461249
/// Link up matched candidates.

0 commit comments

Comments
 (0)