about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_mir_transform/src/lib.rs9
-rw-r--r--tests/ui/consts/issue-67696-const-prop-ice.rs2
2 files changed, 10 insertions, 1 deletions
diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs
index c0a09b7a761..c42888d8791 100644
--- a/compiler/rustc_mir_transform/src/lib.rs
+++ b/compiler/rustc_mir_transform/src/lib.rs
@@ -613,6 +613,15 @@ fn inner_optimized_mir(tcx: TyCtxt<'_>, did: LocalDefId) -> Body<'_> {
         return body;
     }
 
+    // If `mir_drops_elaborated_and_const_checked` found that the current body has unsatisfiable
+    // predicates, it will shrink the MIR to a single `unreachable` terminator.
+    // More generally, if MIR is a lone `unreachable`, there is nothing to optimize.
+    if let TerminatorKind::Unreachable = body.basic_blocks[START_BLOCK].terminator().kind
+        && body.basic_blocks[START_BLOCK].statements.is_empty()
+    {
+        return body;
+    }
+
     run_optimization_passes(tcx, &mut body);
 
     body
diff --git a/tests/ui/consts/issue-67696-const-prop-ice.rs b/tests/ui/consts/issue-67696-const-prop-ice.rs
index ad52608b3f4..858035190ca 100644
--- a/tests/ui/consts/issue-67696-const-prop-ice.rs
+++ b/tests/ui/consts/issue-67696-const-prop-ice.rs
@@ -1,5 +1,5 @@
 // check-pass
-// compile-flags: --emit=mir,link
+// compile-flags: --emit=mir,link -Zmir-opt-level=4
 // Checks that we don't ICE due to attempting to run const prop
 // on a function with unsatisifable 'where' clauses