about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-10-09 06:00:23 +0000
committerbors <bors@rust-lang.org>2023-10-09 06:00:23 +0000
commit093b9d5b295d85e144d0ee7da65ea03987214c06 (patch)
tree6ef09fdeb0126ffa561738dd1b37a03f9a8e6463
parent1f48cbc3f8dbd393a7e713a0f90d7c6ec72d58ee (diff)
parent005ec2e51ce99c6dd5e88c273b3f9b273fdf3f41 (diff)
downloadrust-093b9d5b295d85e144d0ee7da65ea03987214c06.tar.gz
rust-093b9d5b295d85e144d0ee7da65ea03987214c06.zip
Auto merge of #116533 - cjgillot:skip-trivial-mir, r=oli-obk
Do not run optimizations on trivial MIR.

Fixes https://github.com/rust-lang/rust/issues/116513

The bug was introduced in https://github.com/rust-lang/rust/pull/110728, which put the check too early in the query chain.

cc `@oli-obk` `@ouz-a`
-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