Rename UninhabitedEnumBranching to UnreachableEnumBranching

This commit is contained in:
DianQK 2024-03-08 20:56:09 +08:00
parent 1b427b3bf7
commit 8ddd966223
No known key found for this signature in database
GPG Key ID: 46BDB1AC96C48912
29 changed files with 89 additions and 88 deletions

View File

@ -109,7 +109,7 @@
mod simplify_branches;
mod simplify_comparison_integral;
mod sroa;
mod uninhabited_enum_branching;
mod unreachable_enum_branching;
mod unreachable_prop;
use rustc_const_eval::transform::check_consts::{self, ConstCx};
@ -579,9 +579,10 @@ fn o1<T>(x: T) -> WithMinOptLevel<T> {
&remove_zsts::RemoveZsts,
&remove_unneeded_drops::RemoveUnneededDrops,
// Type instantiation may create uninhabited enums.
&uninhabited_enum_branching::UninhabitedEnumBranching,
// Also eliminates some unreachable branches based on variants of enums.
&unreachable_enum_branching::UnreachableEnumBranching,
&unreachable_prop::UnreachablePropagation,
&o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
&o1(simplify::SimplifyCfg::AfterUnreachableEnumBranching),
// Inlining may have introduced a lot of redundant code and a large move pattern.
// Now, we need to shrink the generated MIR.

View File

@ -41,7 +41,7 @@ pub enum SimplifyCfg {
ElaborateDrops,
Final,
MakeShim,
AfterUninhabitedEnumBranching,
AfterUnreachableEnumBranching,
}
impl SimplifyCfg {
@ -54,8 +54,8 @@ pub fn name(&self) -> &'static str {
SimplifyCfg::ElaborateDrops => "SimplifyCfg-elaborate-drops",
SimplifyCfg::Final => "SimplifyCfg-final",
SimplifyCfg::MakeShim => "SimplifyCfg-make_shim",
SimplifyCfg::AfterUninhabitedEnumBranching => {
"SimplifyCfg-after-uninhabited-enum-branching"
SimplifyCfg::AfterUnreachableEnumBranching => {
"SimplifyCfg-after-unreachable-enum-branching"
}
}
}

View File

@ -1,4 +1,4 @@
//! A pass that eliminates branches on uninhabited enum variants.
//! A pass that eliminates branches on uninhabited or unreachable enum variants.
use crate::MirPass;
use rustc_data_structures::fx::FxHashSet;
@ -11,7 +11,7 @@
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_target::abi::{Abi, Variants};
pub struct UninhabitedEnumBranching;
pub struct UnreachableEnumBranching;
fn get_discriminant_local(terminator: &TerminatorKind<'_>) -> Option<Local> {
if let TerminatorKind::SwitchInt { discr: Operand::Move(p), .. } = terminator {
@ -71,13 +71,13 @@ fn variant_discriminants<'tcx>(
}
}
impl<'tcx> MirPass<'tcx> for UninhabitedEnumBranching {
impl<'tcx> MirPass<'tcx> for UnreachableEnumBranching {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
trace!("UninhabitedEnumBranching starting for {:?}", body.source);
trace!("UnreachableEnumBranching starting for {:?}", body.source);
let mut unreachable_targets = Vec::new();
let mut patch = MirPatch::new(body);
@ -121,9 +121,9 @@ fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
}
let otherwise_is_empty_unreachable =
body.basic_blocks[targets.otherwise()].is_empty_unreachable();
// After resolving https://github.com/llvm/llvm-project/issues/78578,
// we can remove the limit on the number of successors.
fn check_successors(basic_blocks: &BasicBlocks<'_>, bb: BasicBlock) -> bool {
// After resolving https://github.com/llvm/llvm-project/issues/78578,
// We can remove this check.
let mut successors = basic_blocks[bb].terminator().successors();
let Some(first_successor) = successors.next() else { return true };
if successors.next().is_some() {

View File

@ -1,5 +1,5 @@
- // MIR for `assert_nonzero_nonmax` before SimplifyCfg-after-uninhabited-enum-branching
+ // MIR for `assert_nonzero_nonmax` after SimplifyCfg-after-uninhabited-enum-branching
- // MIR for `assert_nonzero_nonmax` before SimplifyCfg-after-unreachable-enum-branching
+ // MIR for `assert_nonzero_nonmax` after SimplifyCfg-after-unreachable-enum-branching
fn assert_nonzero_nonmax(_1: u8) -> u8 {
let mut _0: u8;

View File

@ -4,9 +4,9 @@
use std::intrinsics::mir::*;
//@ unit-test: SimplifyCfg-after-uninhabited-enum-branching
//@ unit-test: SimplifyCfg-after-unreachable-enum-branching
// EMIT_MIR simplify_duplicate_unreachable_blocks.assert_nonzero_nonmax.SimplifyCfg-after-uninhabited-enum-branching.diff
// EMIT_MIR simplify_duplicate_unreachable_blocks.assert_nonzero_nonmax.SimplifyCfg-after-unreachable-enum-branching.diff
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
pub unsafe fn assert_nonzero_nonmax(x: u8) -> u8 {
mir!(

View File

@ -1,5 +1,5 @@
- // MIR for `eliminate_fallthrough` before UninhabitedEnumBranching
+ // MIR for `eliminate_fallthrough` after UninhabitedEnumBranching
- // MIR for `eliminate_fallthrough` before UnreachableEnumBranching
+ // MIR for `eliminate_fallthrough` after UnreachableEnumBranching
fn eliminate_fallthrough(_1: S) -> u32 {
debug s => _1;

View File

@ -1,5 +1,5 @@
- // MIR for `keep_fallthrough` before UninhabitedEnumBranching
+ // MIR for `keep_fallthrough` after UninhabitedEnumBranching
- // MIR for `keep_fallthrough` before UnreachableEnumBranching
+ // MIR for `keep_fallthrough` after UnreachableEnumBranching
fn keep_fallthrough(_1: S) -> u32 {
debug s => _1;

View File

@ -9,7 +9,7 @@ enum S {
use S::*;
// EMIT_MIR uninhabited_fallthrough_elimination.keep_fallthrough.UninhabitedEnumBranching.diff
// EMIT_MIR uninhabited_fallthrough_elimination.keep_fallthrough.UnreachableEnumBranching.diff
fn keep_fallthrough(s: S) -> u32 {
match s {
A(_) => 1,
@ -18,7 +18,7 @@ fn keep_fallthrough(s: S) -> u32 {
}
}
// EMIT_MIR uninhabited_fallthrough_elimination.eliminate_fallthrough.UninhabitedEnumBranching.diff
// EMIT_MIR uninhabited_fallthrough_elimination.eliminate_fallthrough.UnreachableEnumBranching.diff
fn eliminate_fallthrough(s: S) -> u32 {
match s {
C => 1,

View File

@ -1,5 +1,5 @@
- // MIR for `byref` before UninhabitedEnumBranching
+ // MIR for `byref` after UninhabitedEnumBranching
- // MIR for `byref` before UnreachableEnumBranching
+ // MIR for `byref` after UnreachableEnumBranching
fn byref() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `byref` before UninhabitedEnumBranching
+ // MIR for `byref` after UninhabitedEnumBranching
- // MIR for `byref` before UnreachableEnumBranching
+ // MIR for `byref` after UnreachableEnumBranching
fn byref() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `custom_discriminant` before UninhabitedEnumBranching
+ // MIR for `custom_discriminant` after UninhabitedEnumBranching
- // MIR for `custom_discriminant` before UnreachableEnumBranching
+ // MIR for `custom_discriminant` after UnreachableEnumBranching
fn custom_discriminant() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `custom_discriminant` before UninhabitedEnumBranching
+ // MIR for `custom_discriminant` after UninhabitedEnumBranching
- // MIR for `custom_discriminant` before UnreachableEnumBranching
+ // MIR for `custom_discriminant` after UnreachableEnumBranching
fn custom_discriminant() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `otherwise_t1` before UninhabitedEnumBranching
+ // MIR for `otherwise_t1` after UninhabitedEnumBranching
- // MIR for `otherwise_t1` before UnreachableEnumBranching
+ // MIR for `otherwise_t1` after UnreachableEnumBranching
fn otherwise_t1() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `otherwise_t1` before UninhabitedEnumBranching
+ // MIR for `otherwise_t1` after UninhabitedEnumBranching
- // MIR for `otherwise_t1` before UnreachableEnumBranching
+ // MIR for `otherwise_t1` after UnreachableEnumBranching
fn otherwise_t1() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `otherwise_t2` before UninhabitedEnumBranching
+ // MIR for `otherwise_t2` after UninhabitedEnumBranching
- // MIR for `otherwise_t2` before UnreachableEnumBranching
+ // MIR for `otherwise_t2` after UnreachableEnumBranching
fn otherwise_t2() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `otherwise_t2` before UninhabitedEnumBranching
+ // MIR for `otherwise_t2` after UninhabitedEnumBranching
- // MIR for `otherwise_t2` before UnreachableEnumBranching
+ // MIR for `otherwise_t2` after UnreachableEnumBranching
fn otherwise_t2() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `otherwise_t3` before UninhabitedEnumBranching
+ // MIR for `otherwise_t3` after UninhabitedEnumBranching
- // MIR for `otherwise_t3` before UnreachableEnumBranching
+ // MIR for `otherwise_t3` after UnreachableEnumBranching
fn otherwise_t3() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `otherwise_t3` before UninhabitedEnumBranching
+ // MIR for `otherwise_t3` after UninhabitedEnumBranching
- // MIR for `otherwise_t3` before UnreachableEnumBranching
+ // MIR for `otherwise_t3` after UnreachableEnumBranching
fn otherwise_t3() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `otherwise_t4` before UninhabitedEnumBranching
+ // MIR for `otherwise_t4` after UninhabitedEnumBranching
- // MIR for `otherwise_t4` before UnreachableEnumBranching
+ // MIR for `otherwise_t4` after UnreachableEnumBranching
fn otherwise_t4() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `otherwise_t4` before UninhabitedEnumBranching
+ // MIR for `otherwise_t4` after UninhabitedEnumBranching
- // MIR for `otherwise_t4` before UnreachableEnumBranching
+ // MIR for `otherwise_t4` after UnreachableEnumBranching
fn otherwise_t4() -> () {
let mut _0: ();

View File

@ -1,7 +1,7 @@
- // MIR for `otherwise_t4_uninhabited_default` before UninhabitedEnumBranching
+ // MIR for `otherwise_t4_uninhabited_default` after UninhabitedEnumBranching
- // MIR for `otherwise_t4_unreachable_default` before UnreachableEnumBranching
+ // MIR for `otherwise_t4_unreachable_default` after UnreachableEnumBranching
fn otherwise_t4_uninhabited_default() -> () {
fn otherwise_t4_unreachable_default() -> () {
let mut _0: ();
let _1: &str;
let mut _2: Test4;

View File

@ -1,7 +1,7 @@
- // MIR for `otherwise_t4_uninhabited_default` before UninhabitedEnumBranching
+ // MIR for `otherwise_t4_uninhabited_default` after UninhabitedEnumBranching
- // MIR for `otherwise_t4_unreachable_default` before UnreachableEnumBranching
+ // MIR for `otherwise_t4_unreachable_default` after UnreachableEnumBranching
fn otherwise_t4_uninhabited_default() -> () {
fn otherwise_t4_unreachable_default() -> () {
let mut _0: ();
let _1: &str;
let mut _2: Test4;

View File

@ -1,7 +1,7 @@
- // MIR for `otherwise_t4_uninhabited_default_2` before UninhabitedEnumBranching
+ // MIR for `otherwise_t4_uninhabited_default_2` after UninhabitedEnumBranching
- // MIR for `otherwise_t4_unreachable_default_2` before UnreachableEnumBranching
+ // MIR for `otherwise_t4_unreachable_default_2` after UnreachableEnumBranching
fn otherwise_t4_uninhabited_default_2() -> () {
fn otherwise_t4_unreachable_default_2() -> () {
let mut _0: ();
let _1: &str;
let mut _2: Test4;

View File

@ -1,7 +1,7 @@
- // MIR for `otherwise_t4_uninhabited_default_2` before UninhabitedEnumBranching
+ // MIR for `otherwise_t4_uninhabited_default_2` after UninhabitedEnumBranching
- // MIR for `otherwise_t4_unreachable_default_2` before UnreachableEnumBranching
+ // MIR for `otherwise_t4_unreachable_default_2` after UnreachableEnumBranching
fn otherwise_t4_uninhabited_default_2() -> () {
fn otherwise_t4_unreachable_default_2() -> () {
let mut _0: ();
let _1: &str;
let mut _2: Test4;

View File

@ -1,7 +1,7 @@
- // MIR for `otherwise_t5_uninhabited_default` before UninhabitedEnumBranching
+ // MIR for `otherwise_t5_uninhabited_default` after UninhabitedEnumBranching
- // MIR for `otherwise_t5_unreachable_default` before UnreachableEnumBranching
+ // MIR for `otherwise_t5_unreachable_default` after UnreachableEnumBranching
fn otherwise_t5_uninhabited_default() -> () {
fn otherwise_t5_unreachable_default() -> () {
let mut _0: ();
let _1: &str;
let mut _2: Test5<T>;

View File

@ -1,7 +1,7 @@
- // MIR for `otherwise_t5_uninhabited_default` before UninhabitedEnumBranching
+ // MIR for `otherwise_t5_uninhabited_default` after UninhabitedEnumBranching
- // MIR for `otherwise_t5_unreachable_default` before UnreachableEnumBranching
+ // MIR for `otherwise_t5_unreachable_default` after UnreachableEnumBranching
fn otherwise_t5_uninhabited_default() -> () {
fn otherwise_t5_unreachable_default() -> () {
let mut _0: ();
let _1: &str;
let mut _2: Test5<T>;

View File

@ -1,4 +1,4 @@
//@ unit-test: UninhabitedEnumBranching
//@ unit-test: UnreachableEnumBranching
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
enum Empty {}
@ -45,7 +45,7 @@ struct Plop {
test3: Test3,
}
// EMIT_MIR uninhabited_enum_branching.simple.UninhabitedEnumBranching.diff
// EMIT_MIR unreachable_enum_branching.simple.UnreachableEnumBranching.diff
fn simple() {
// CHECK-LABEL: fn simple(
// CHECK: [[discr:_.*]] = discriminant(
@ -59,7 +59,7 @@ fn simple() {
};
}
// EMIT_MIR uninhabited_enum_branching.custom_discriminant.UninhabitedEnumBranching.diff
// EMIT_MIR unreachable_enum_branching.custom_discriminant.UnreachableEnumBranching.diff
fn custom_discriminant() {
// CHECK-LABEL: fn custom_discriminant(
// CHECK: [[discr:_.*]] = discriminant(
@ -72,7 +72,7 @@ fn custom_discriminant() {
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t1.UninhabitedEnumBranching.diff
// EMIT_MIR unreachable_enum_branching.otherwise_t1.UnreachableEnumBranching.diff
fn otherwise_t1() {
// CHECK-LABEL: fn otherwise_t1(
// CHECK: [[discr:_.*]] = discriminant(
@ -86,7 +86,7 @@ fn otherwise_t1() {
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t2.UninhabitedEnumBranching.diff
// EMIT_MIR unreachable_enum_branching.otherwise_t2.UnreachableEnumBranching.diff
fn otherwise_t2() {
// CHECK-LABEL: fn otherwise_t2(
// CHECK: [[discr:_.*]] = discriminant(
@ -99,7 +99,7 @@ fn otherwise_t2() {
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t3.UninhabitedEnumBranching.diff
// EMIT_MIR unreachable_enum_branching.otherwise_t3.UnreachableEnumBranching.diff
fn otherwise_t3() {
// CHECK-LABEL: fn otherwise_t3(
// CHECK: [[discr:_.*]] = discriminant(
@ -116,9 +116,9 @@ fn otherwise_t3() {
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t4_uninhabited_default.UninhabitedEnumBranching.diff
fn otherwise_t4_uninhabited_default() {
// CHECK-LABEL: fn otherwise_t4_uninhabited_default(
// EMIT_MIR unreachable_enum_branching.otherwise_t4_unreachable_default.UnreachableEnumBranching.diff
fn otherwise_t4_unreachable_default() {
// CHECK-LABEL: fn otherwise_t4_unreachable_default(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [0: bb2, 1: bb3, 2: bb4, 3: bb1, otherwise: [[unreachable:bb.*]]];
// CHECK: [[unreachable]]: {
@ -131,9 +131,9 @@ fn otherwise_t4_uninhabited_default() {
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t4_uninhabited_default_2.UninhabitedEnumBranching.diff
fn otherwise_t4_uninhabited_default_2() {
// CHECK-LABEL: fn otherwise_t4_uninhabited_default_2(
// EMIT_MIR unreachable_enum_branching.otherwise_t4_unreachable_default_2.UnreachableEnumBranching.diff
fn otherwise_t4_unreachable_default_2() {
// CHECK-LABEL: fn otherwise_t4_unreachable_default_2(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [0: bb2, 1: bb5, 2: bb6, 3: bb1, otherwise: [[unreachable:bb.*]]];
// CHECK: [[unreachable]]: {
@ -147,7 +147,7 @@ fn otherwise_t4_uninhabited_default_2() {
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t4.UninhabitedEnumBranching.diff
// EMIT_MIR unreachable_enum_branching.otherwise_t4.UnreachableEnumBranching.diff
fn otherwise_t4() {
// CHECK-LABEL: fn otherwise_t4(
// CHECK: [[discr:_.*]] = discriminant(
@ -162,9 +162,9 @@ fn otherwise_t4() {
};
}
// EMIT_MIR uninhabited_enum_branching.otherwise_t5_uninhabited_default.UninhabitedEnumBranching.diff
fn otherwise_t5_uninhabited_default<T>() {
// CHECK-LABEL: fn otherwise_t5_uninhabited_default(
// EMIT_MIR unreachable_enum_branching.otherwise_t5_unreachable_default.UnreachableEnumBranching.diff
fn otherwise_t5_unreachable_default<T>() {
// CHECK-LABEL: fn otherwise_t5_unreachable_default(
// CHECK: [[discr:_.*]] = discriminant(
// CHECK: switchInt(move [[discr]]) -> [255: bb2, 0: bb3, 5: bb4, 3: bb1, otherwise: [[unreachable:bb.*]]];
// CHECK: [[unreachable]]: {
@ -177,7 +177,7 @@ fn otherwise_t5_uninhabited_default<T>() {
};
}
// EMIT_MIR uninhabited_enum_branching.byref.UninhabitedEnumBranching.diff
// EMIT_MIR unreachable_enum_branching.byref.UnreachableEnumBranching.diff
fn byref() {
// CHECK-LABEL: fn byref(
let plop = Plop { xx: 51, test3: Test3::C };
@ -210,9 +210,9 @@ fn main() {
otherwise_t1();
otherwise_t2();
otherwise_t3();
otherwise_t4_uninhabited_default();
otherwise_t4_uninhabited_default_2();
otherwise_t4_unreachable_default();
otherwise_t4_unreachable_default_2();
otherwise_t4();
otherwise_t5_uninhabited_default::<i32>();
otherwise_t5_unreachable_default::<i32>();
byref();
}

View File

@ -1,5 +1,5 @@
- // MIR for `simple` before UninhabitedEnumBranching
+ // MIR for `simple` after UninhabitedEnumBranching
- // MIR for `simple` before UnreachableEnumBranching
+ // MIR for `simple` after UnreachableEnumBranching
fn simple() -> () {
let mut _0: ();

View File

@ -1,5 +1,5 @@
- // MIR for `simple` before UninhabitedEnumBranching
+ // MIR for `simple` after UninhabitedEnumBranching
- // MIR for `simple` before UnreachableEnumBranching
+ // MIR for `simple` after UnreachableEnumBranching
fn simple() -> () {
let mut _0: ();