From 5e11a99bb662629cc7d0ad3ce7475b7738c9c2cb Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 15 Feb 2024 04:22:56 +0100 Subject: [PATCH 1/2] Remove hacky branch in `sort_candidate` Reusing `self.test` wasn't actually pulling a lot of weight. --- .../rustc_mir_build/src/build/matches/test.rs | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index 1c97de58863bd..a97d0de27e326 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -673,6 +673,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } } } + (TestKind::Len { .. }, _) => { + fully_matched = false; + None + } (TestKind::Range(test), &TestCase::Range(pat)) => { if test.as_ref() == pat { @@ -700,29 +704,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { None } - (&TestKind::Eq { .. } | &TestKind::Len { .. }, _) => { - // The call to `self.test(&match_pair)` below is not actually used to generate any - // MIR. Instead, we just want to compare with `test` (the parameter of the method) - // to see if it is the same. - // - // However, at this point we can still encounter or-patterns that were extracted - // from previous calls to `sort_candidate`, so we need to manually address that - // case to avoid panicking in `self.test()`. - if let TestCase::Or { .. } = &match_pair.test_case { - return None; - } - - // These are all binary tests. - // - // FIXME(#29623) we can be more clever here - let pattern_test = self.test(match_pair); - if pattern_test.kind == test.kind { - fully_matched = true; - Some(0) - } else { - fully_matched = false; - None - } + // FIXME(#29623): return `Some(1)` when the values are different. + (TestKind::Eq { value: test_val, .. }, TestCase::Constant { value: case_val }) + if test_val == case_val => + { + fully_matched = true; + Some(0) + } + (TestKind::Eq { .. }, _) => { + fully_matched = false; + None } }; From f363c1a3fe58f66924955755a6408dd1caedbdac Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 20 Feb 2024 22:59:11 +0100 Subject: [PATCH 2/2] Group default cases in `sort_candidate` --- .../rustc_mir_build/src/build/matches/test.rs | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_mir_build/src/build/matches/test.rs b/compiler/rustc_mir_build/src/build/matches/test.rs index a97d0de27e326..3712b8fb95b1b 100644 --- a/compiler/rustc_mir_build/src/build/matches/test.rs +++ b/compiler/rustc_mir_build/src/build/matches/test.rs @@ -575,10 +575,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { fully_matched = true; Some(variant_index.as_usize()) } - (&TestKind::Switch { .. }, _) => { - fully_matched = false; - None - } // If we are performing a switch over integers, then this informs integer // equality, but nothing else. @@ -603,10 +599,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { options.len() }) } - (&TestKind::SwitchInt { .. }, _) => { - fully_matched = false; - None - } ( &TestKind::Len { len: test_len, op: BinOp::Eq }, @@ -673,10 +665,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } } } - (TestKind::Len { .. }, _) => { - fully_matched = false; - None - } (TestKind::Range(test), &TestCase::Range(pat)) => { if test.as_ref() == pat { @@ -699,10 +687,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { None } } - (&TestKind::Range { .. }, _) => { - fully_matched = false; - None - } // FIXME(#29623): return `Some(1)` when the values are different. (TestKind::Eq { value: test_val, .. }, TestCase::Constant { value: case_val }) @@ -711,7 +695,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { fully_matched = true; Some(0) } - (TestKind::Eq { .. }, _) => { + + ( + TestKind::Switch { .. } + | TestKind::SwitchInt { .. } + | TestKind::Len { .. } + | TestKind::Range { .. } + | TestKind::Eq { .. }, + _, + ) => { fully_matched = false; None }