From 3d7f8b4e5b650efc628de9bf4bd5246d3493d261 Mon Sep 17 00:00:00 2001 From: DianQK Date: Wed, 14 Feb 2024 12:09:28 +0800 Subject: [PATCH] Get all variants to eliminate the default branching if we cannot get the layout of type --- .../src/uninhabited_enum_branching.rs | 6 +++++ ..._to_digit.PreCodegen.after.panic-abort.mir | 22 ++++++++++--------- ...to_digit.PreCodegen.after.panic-unwind.mir | 22 ++++++++++--------- ....foo.SimplifyLocals-final.panic-abort.diff | 2 +- ...foo.SimplifyLocals-final.panic-unwind.diff | 2 +- ....UninhabitedEnumBranching.panic-abort.diff | 12 +++++----- ....UninhabitedEnumBranching.panic-abort.diff | 9 ++++++-- ...UninhabitedEnumBranching.panic-unwind.diff | 9 ++++++-- tests/mir-opt/uninhabited_enum_branching.rs | 16 +++++++------- 9 files changed, 60 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs b/compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs index d3408d4882f..0ab60259732 100644 --- a/compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs +++ b/compiler/rustc_mir_transform/src/uninhabited_enum_branching.rs @@ -95,6 +95,12 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { let mut allowed_variants = if let Ok(layout) = layout { variant_discriminants(&layout, discriminant_ty, tcx) + } else if let Some(variant_range) = discriminant_ty.variant_range(tcx) { + variant_range + .map(|variant| { + discriminant_ty.discriminant_for_variant(tcx, variant).unwrap().val + }) + .collect() } else { continue; }; diff --git a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir index f7be8b75db7..bc12002490a 100644 --- a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir +++ b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-abort.mir @@ -33,13 +33,13 @@ fn num_to_digit(_1: char) -> u32 { _3 = &_2; StorageLive(_4); _4 = discriminant(_2); - StorageDead(_3); - StorageDead(_2); - switchInt(move _4) -> [1: bb2, otherwise: bb7]; + switchInt(move _4) -> [1: bb2, 0: bb6, otherwise: bb8]; } bb2: { StorageDead(_4); + StorageDead(_3); + StorageDead(_2); StorageLive(_5); _5 = char::methods::::to_digit(move _1, const 8_u32) -> [return: bb3, unwind unreachable]; } @@ -47,7 +47,7 @@ fn num_to_digit(_1: char) -> u32 { bb3: { StorageLive(_6); _6 = discriminant(_5); - switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb6]; + switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb8]; } bb4: { @@ -58,20 +58,22 @@ fn num_to_digit(_1: char) -> u32 { _0 = move ((_5 as Some).0: u32); StorageDead(_6); StorageDead(_5); - goto -> bb8; + goto -> bb7; } bb6: { - unreachable; + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + _0 = const 0_u32; + goto -> bb7; } bb7: { - StorageDead(_4); - _0 = const 0_u32; - goto -> bb8; + return; } bb8: { - return; + unreachable; } } diff --git a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir index e76fe992ac7..6c7e10a4525 100644 --- a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir +++ b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.panic-unwind.mir @@ -33,13 +33,13 @@ fn num_to_digit(_1: char) -> u32 { _3 = &_2; StorageLive(_4); _4 = discriminant(_2); - StorageDead(_3); - StorageDead(_2); - switchInt(move _4) -> [1: bb2, otherwise: bb7]; + switchInt(move _4) -> [1: bb2, 0: bb6, otherwise: bb8]; } bb2: { StorageDead(_4); + StorageDead(_3); + StorageDead(_2); StorageLive(_5); _5 = char::methods::::to_digit(move _1, const 8_u32) -> [return: bb3, unwind continue]; } @@ -47,7 +47,7 @@ fn num_to_digit(_1: char) -> u32 { bb3: { StorageLive(_6); _6 = discriminant(_5); - switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb6]; + switchInt(move _6) -> [0: bb4, 1: bb5, otherwise: bb8]; } bb4: { @@ -58,20 +58,22 @@ fn num_to_digit(_1: char) -> u32 { _0 = move ((_5 as Some).0: u32); StorageDead(_6); StorageDead(_5); - goto -> bb8; + goto -> bb7; } bb6: { - unreachable; + StorageDead(_4); + StorageDead(_3); + StorageDead(_2); + _0 = const 0_u32; + goto -> bb7; } bb7: { - StorageDead(_4); - _0 = const 0_u32; - goto -> bb8; + return; } bb8: { - return; + unreachable; } } diff --git a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff index 3108b7d3e13..c520a159f47 100644 --- a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff +++ b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-abort.diff @@ -28,7 +28,7 @@ bb1: { _4 = discriminant((_1.1: std::option::Option)); - switchInt(move _4) -> [0: bb2, otherwise: bb3]; + switchInt(move _4) -> [0: bb2, 1: bb3, otherwise: bb5]; } bb2: { diff --git a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff index 3ef3be198ed..686581591fc 100644 --- a/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff +++ b/tests/mir-opt/simplify_locals_fixedpoint.foo.SimplifyLocals-final.panic-unwind.diff @@ -28,7 +28,7 @@ bb1: { _4 = discriminant((_1.1: std::option::Option)); - switchInt(move _4) -> [0: bb2, otherwise: bb3]; + switchInt(move _4) -> [0: bb2, 1: bb3, otherwise: bb5]; } bb2: { diff --git a/tests/mir-opt/uninhabited_enum_branching.otherwise_t4_uninhabited_default_2.UninhabitedEnumBranching.panic-abort.diff b/tests/mir-opt/uninhabited_enum_branching.otherwise_t4_uninhabited_default_2.UninhabitedEnumBranching.panic-abort.diff index de6944ee423..ac39f6be6c6 100644 --- a/tests/mir-opt/uninhabited_enum_branching.otherwise_t4_uninhabited_default_2.UninhabitedEnumBranching.panic-abort.diff +++ b/tests/mir-opt/uninhabited_enum_branching.otherwise_t4_uninhabited_default_2.UninhabitedEnumBranching.panic-abort.diff @@ -16,15 +16,11 @@ StorageLive(_2); _2 = Test4::C; _3 = discriminant(_2); -- switchInt(move _3) -> [0: bb1, 1: bb5, 2: bb6, otherwise: bb2]; -+ switchInt(move _3) -> [0: bb1, 1: bb5, 2: bb6, 3: bb2, otherwise: bb8]; +- switchInt(move _3) -> [0: bb2, 1: bb5, 2: bb6, otherwise: bb1]; ++ switchInt(move _3) -> [0: bb2, 1: bb5, 2: bb6, 3: bb1, otherwise: bb8]; } bb1: { - switchInt(((_2 as A).0: i32)) -> [1: bb3, 2: bb4, otherwise: bb2]; - } - - bb2: { StorageLive(_7); _7 = const "A(other)D"; _1 = &(*_7); @@ -32,6 +28,10 @@ goto -> bb7; } + bb2: { + switchInt(((_2 as A).0: i32)) -> [1: bb3, 2: bb4, otherwise: bb1]; + } + bb3: { _1 = const "A(1)"; goto -> bb7; diff --git a/tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-abort.diff b/tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-abort.diff index aaede2dd924..8180428a6f4 100644 --- a/tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-abort.diff +++ b/tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-abort.diff @@ -5,7 +5,7 @@ let mut _0: (); let _1: &str; let mut _2: Test5; - let mut _3: isize; + let mut _3: i8; let _4: &str; let _5: &str; let _6: &str; @@ -15,7 +15,8 @@ StorageLive(_2); _2 = Test5::::C; _3 = discriminant(_2); - switchInt(move _3) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1]; +- switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, otherwise: bb1]; ++ switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: bb7]; } bb1: { @@ -56,6 +57,10 @@ StorageDead(_1); _0 = const (); return; ++ } ++ ++ bb7: { ++ unreachable; } } diff --git a/tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-unwind.diff b/tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-unwind.diff index edaaa0db588..b13d5816aed 100644 --- a/tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-unwind.diff +++ b/tests/mir-opt/uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.panic-unwind.diff @@ -5,7 +5,7 @@ let mut _0: (); let _1: &str; let mut _2: Test5; - let mut _3: isize; + let mut _3: i8; let _4: &str; let _5: &str; let _6: &str; @@ -15,7 +15,8 @@ StorageLive(_2); _2 = Test5::::C; _3 = discriminant(_2); - switchInt(move _3) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1]; +- switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, otherwise: bb1]; ++ switchInt(move _3) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: bb8]; } bb1: { @@ -60,6 +61,10 @@ bb7 (cleanup): { resume; ++ } ++ ++ bb8: { ++ unreachable; } } diff --git a/tests/mir-opt/uninhabited_enum_branching.rs b/tests/mir-opt/uninhabited_enum_branching.rs index a60ca0288e8..e31bf0227d5 100644 --- a/tests/mir-opt/uninhabited_enum_branching.rs +++ b/tests/mir-opt/uninhabited_enum_branching.rs @@ -32,11 +32,12 @@ enum Test4 { D, } +#[repr(i8)] enum Test5 { - A(T), - B(T), - C, - D, + A(T) = -1, + B(T) = 0, + C = 5, + D = 3, } struct Plop { @@ -165,10 +166,9 @@ fn otherwise_t4() { fn otherwise_t5_uninhabited_default() { // CHECK-LABEL: fn otherwise_t5_uninhabited_default( // CHECK: [[discr:_.*]] = discriminant( - // CHECK: switchInt(move [[discr]]) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb1]; - // CHECK: bb1: { - // CHECK-NOT: unreachable; - // CHECK: } + // CHECK: switchInt(move [[discr]]) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: [[unreachable:bb.*]]]; + // CHECK: [[unreachable]]: { + // CHECK-NEXT: unreachable; match Test5::::C { Test5::A(_) => "A(T)", Test5::B(_) => "B(T)",