Auto merge of #131244 - clubby789:match-branches-unreachable, r=DianQK

Consider empty-unreachable otherwise branches in MatchBranchSimplification

Fixes #131219
This commit is contained in:
bors
2024-12-28 11:09:28 +00:00
7 changed files with 108 additions and 29 deletions
@@ -25,12 +25,12 @@ fn num_to_digit(_1: char) -> u32 {
bb1: {
StorageLive(_3);
_3 = discriminant(_2);
switchInt(move _3) -> [1: bb2, 0: bb6, otherwise: bb8];
StorageDead(_2);
switchInt(move _3) -> [1: bb2, otherwise: bb7];
}
bb2: {
StorageDead(_3);
StorageDead(_2);
StorageLive(_4);
_4 = char::methods::<impl char>::to_digit(move _1, const 8_u32) -> [return: bb3, unwind unreachable];
}
@@ -38,7 +38,7 @@ fn num_to_digit(_1: char) -> u32 {
bb3: {
StorageLive(_5);
_5 = discriminant(_4);
switchInt(move _5) -> [0: bb4, 1: bb5, otherwise: bb8];
switchInt(move _5) -> [0: bb4, 1: bb5, otherwise: bb6];
}
bb4: {
@@ -49,21 +49,20 @@ fn num_to_digit(_1: char) -> u32 {
_0 = move ((_4 as Some).0: u32);
StorageDead(_5);
StorageDead(_4);
goto -> bb7;
goto -> bb8;
}
bb6: {
StorageDead(_3);
StorageDead(_2);
_0 = const 0_u32;
goto -> bb7;
unreachable;
}
bb7: {
return;
StorageDead(_3);
_0 = const 0_u32;
goto -> bb8;
}
bb8: {
unreachable;
return;
}
}
@@ -25,12 +25,12 @@ fn num_to_digit(_1: char) -> u32 {
bb1: {
StorageLive(_3);
_3 = discriminant(_2);
switchInt(move _3) -> [1: bb2, 0: bb6, otherwise: bb8];
StorageDead(_2);
switchInt(move _3) -> [1: bb2, otherwise: bb7];
}
bb2: {
StorageDead(_3);
StorageDead(_2);
StorageLive(_4);
_4 = char::methods::<impl char>::to_digit(move _1, const 8_u32) -> [return: bb3, unwind continue];
}
@@ -38,7 +38,7 @@ fn num_to_digit(_1: char) -> u32 {
bb3: {
StorageLive(_5);
_5 = discriminant(_4);
switchInt(move _5) -> [0: bb4, 1: bb5, otherwise: bb8];
switchInt(move _5) -> [0: bb4, 1: bb5, otherwise: bb6];
}
bb4: {
@@ -49,21 +49,20 @@ fn num_to_digit(_1: char) -> u32 {
_0 = move ((_4 as Some).0: u32);
StorageDead(_5);
StorageDead(_4);
goto -> bb7;
goto -> bb8;
}
bb6: {
StorageDead(_3);
StorageDead(_2);
_0 = const 0_u32;
goto -> bb7;
unreachable;
}
bb7: {
return;
StorageDead(_3);
_0 = const 0_u32;
goto -> bb8;
}
bb8: {
unreachable;
return;
}
}
@@ -0,0 +1,37 @@
- // MIR for `my_is_some` before MatchBranchSimplification
+ // MIR for `my_is_some` after MatchBranchSimplification
fn my_is_some(_1: Option<()>) -> bool {
debug bar => _1;
let mut _0: bool;
let mut _2: isize;
+ let mut _3: isize;
bb0: {
_2 = discriminant(_1);
- switchInt(move _2) -> [0: bb2, 1: bb3, otherwise: bb1];
- }
-
- bb1: {
- unreachable;
- }
-
- bb2: {
- _0 = const false;
- goto -> bb4;
- }
-
- bb3: {
- _0 = const true;
- goto -> bb4;
- }
-
- bb4: {
+ StorageLive(_3);
+ _3 = move _2;
+ _0 = Ne(copy _3, const 0_isize);
+ StorageDead(_3);
return;
}
}
+14
View File
@@ -19,6 +19,18 @@ fn foo(bar: Option<()>) {
}
}
// EMIT_MIR matches_reduce_branches.my_is_some.MatchBranchSimplification.diff
// Test for #131219.
fn my_is_some(bar: Option<()>) -> bool {
// CHECK-LABEL: fn my_is_some(
// CHECK: = Ne
// CHECK: return
match bar {
Some(_) => true,
None => false,
}
}
// EMIT_MIR matches_reduce_branches.bar.MatchBranchSimplification.diff
fn bar(i: i32) -> (bool, bool, bool, bool) {
// CHECK-LABEL: fn bar(
@@ -651,4 +663,6 @@ fn main() {
let _: u8 = match_trunc_u16_u8_failed(EnumAu16::u0_0x0000);
let _ = match_i128_u128(EnumAi128::A);
let _ = my_is_some(None);
}