Skip to content

Commit 5baf1e1

Browse files
committed
Auto merge of #122459 - Nadrieril:sort-eq, r=oli-obk
match lowering: sort `Eq` candidates in the failure case too This is a slight tweak to MIR gen of matches. Take a match like: ```rust match (s, flag) { ("a", _) if foo() => 1, ("b", true) => 2, ("a", false) => 3, (_, true) => 4, _ => 5, } ``` If we switch on `s == "a"`, the first candidate matches, and we learn almost nothing about the second candidate. So there's a choice: 1. (what we do today) stop sorting candidates, keep the "b" case grouped with everything below. This could allow us to be clever here and test on `flag == true` next. 2. (what this PR does) sort "b" into the failure case. The "b" will be alone (fewer opportunities for picking a good test), but that means the two "a" cases require a single test. Today, we aren't clever in which tests we pick, so this is an unambiguous win. In a future where we pick tests better, idk. Grouping tests as much as possible feels like a generally good strategy. This was proposed in #29623 (9 years ago :D)
2 parents 1aedc96 + 65efa5b commit 5baf1e1

16 files changed

+255
-131
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -650,12 +650,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
650650
}
651651
}
652652

653-
// FIXME(#29623): return `Some(1)` when the values are different.
654-
(TestKind::Eq { value: test_val, .. }, TestCase::Constant { value: case_val })
655-
if test_val == case_val =>
656-
{
657-
fully_matched = true;
658-
Some(TestBranch::Success)
653+
(TestKind::Eq { value: test_val, .. }, TestCase::Constant { value: case_val }) => {
654+
if test_val == case_val {
655+
fully_matched = true;
656+
Some(TestBranch::Success)
657+
} else {
658+
fully_matched = false;
659+
Some(TestBranch::Failure)
660+
}
659661
}
660662

661663
(

0 commit comments

Comments
 (0)