Skip to content

Commit c1514a6

Browse files
committed
Test one or pattern at a time
1 parent 780beda commit c1514a6

File tree

1 file changed

+42
-29
lines changed
  • compiler/rustc_mir_build/src/build/matches

1 file changed

+42
-29
lines changed

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

+42-29
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ struct Ascription<'tcx> {
10521052
variance: ty::Variance,
10531053
}
10541054

1055-
#[derive(Debug)]
1055+
#[derive(Debug, Clone)]
10561056
pub(crate) struct MatchPair<'pat, 'tcx> {
10571057
// This place...
10581058
place: PlaceBuilder<'tcx>,
@@ -1413,56 +1413,69 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
14131413
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
14141414
) {
14151415
let (first_candidate, remaining_candidates) = candidates.split_first_mut().unwrap();
1416-
1417-
// All of the or-patterns have been sorted to the end, so if the first
1418-
// pattern is an or-pattern we only have or-patterns.
1419-
match first_candidate.match_pairs[0].pattern.kind {
1420-
PatKind::Or { .. } => (),
1421-
_ => {
1422-
self.test_candidates(
1423-
span,
1424-
scrutinee_span,
1425-
candidates,
1426-
start_block,
1427-
otherwise_block,
1428-
fake_borrows,
1429-
);
1430-
return;
1431-
}
1416+
assert!(first_candidate.subcandidates.is_empty());
1417+
if !matches!(first_candidate.match_pairs[0].pattern.kind, PatKind::Or { .. }) {
1418+
self.test_candidates(
1419+
span,
1420+
scrutinee_span,
1421+
candidates,
1422+
start_block,
1423+
otherwise_block,
1424+
fake_borrows,
1425+
);
1426+
return;
14321427
}
14331428

14341429
let match_pairs = mem::take(&mut first_candidate.match_pairs);
1430+
let (first_match_pair, remaining_match_pairs) = match_pairs.split_first().unwrap();
1431+
let PatKind::Or { ref pats } = &first_match_pair.pattern.kind else { unreachable!() };
14351432

14361433
let remainder_start = self.cfg.start_new_block();
1437-
for match_pair in match_pairs {
1438-
let PatKind::Or { ref pats } = &match_pair.pattern.kind else {
1439-
bug!("Or-patterns should have been sorted to the end");
1440-
};
1441-
let or_span = match_pair.pattern.span;
1434+
let or_span = first_match_pair.pattern.span;
1435+
// Test the alternatives of this or-pattern.
1436+
self.test_or_pattern(
1437+
first_candidate,
1438+
start_block,
1439+
remainder_start,
1440+
pats,
1441+
or_span,
1442+
&first_match_pair.place,
1443+
fake_borrows,
1444+
);
14421445

1446+
if !remaining_match_pairs.is_empty() {
1447+
// If more match pairs remain, test them after each subcandidate.
1448+
// We could add them to the or-candidates before the call to `test_or_pattern` but this
1449+
// would make it impossible to detect simplifiable or-patterns. That would guarantee
1450+
// exponentially large CFGs for cases like `(1 | 2, 3 | 4, ...)`.
14431451
first_candidate.visit_leaves(|leaf_candidate| {
1444-
let or_start = leaf_candidate.pre_binding_block.unwrap_or(start_block);
1452+
assert!(leaf_candidate.match_pairs.is_empty());
1453+
leaf_candidate.match_pairs.extend(remaining_match_pairs.iter().cloned());
1454+
let or_start = leaf_candidate.pre_binding_block.unwrap();
1455+
// In a case like `(a | b, c | d)`, if `a` succeeds and `c | d` fails, we know `(b,
1456+
// c | d)` will fail too. If there is no guard, we skip testing of `b` by branching
1457+
// directly to `remainder_start`. If there is a guard, we have to try `(b, c | d)`.
14451458
let or_otherwise = leaf_candidate.otherwise_block.unwrap_or(remainder_start);
1446-
self.test_or_pattern(
1447-
leaf_candidate,
1459+
self.test_candidates_with_or(
1460+
span,
1461+
scrutinee_span,
1462+
&mut [leaf_candidate],
14481463
or_start,
14491464
or_otherwise,
1450-
pats,
1451-
or_span,
1452-
&match_pair.place,
14531465
fake_borrows,
14541466
);
14551467
});
14561468
}
14571469

1470+
// Test the remaining candidates.
14581471
self.match_candidates(
14591472
span,
14601473
scrutinee_span,
14611474
remainder_start,
14621475
otherwise_block,
14631476
remaining_candidates,
14641477
fake_borrows,
1465-
)
1478+
);
14661479
}
14671480

14681481
#[instrument(

0 commit comments

Comments
 (0)