about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-08-27 18:59:27 +0200
committerGitHub <noreply@github.com>2024-08-27 18:59:27 +0200
commit849c240c1e93337800c2447947cda7fc1e8f0ca1 (patch)
tree943de4c04f807c5836030e3059dc3848a671185f
parent600edc948ab5de7a92538bcc2f49cb8d47925e2d (diff)
parent5412499ad54b333b03a6aa798f19dc8552fb2936 (diff)
downloadrust-849c240c1e93337800c2447947cda7fc1e8f0ca1.tar.gz
rust-849c240c1e93337800c2447947cda7fc1e8f0ca1.zip
Rollup merge of #129507 - RalfJung:per-fn-const_precise_live_drops, r=wesleywiser
make it possible to enable const_precise_live_drops per-function

This makes const_precise_live_drops work with rustc_allow_const_fn_unstable so that we can stabilize individual functions that rely on const_precise_live_drops.

The goal is that we can use that to stabilize some of https://github.com/rust-lang/rust/issues/67441 without having to stabilize const_precise_live_drops.
-rw-r--r--compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs10
-rw-r--r--tests/ui/consts/precise-drop-allow-const-fn-unstable.not_allow.stderr12
-rw-r--r--tests/ui/consts/precise-drop-allow-const-fn-unstable.rs17
3 files changed, 37 insertions, 2 deletions
diff --git a/compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs b/compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs
index c4f06e5af0b..f0998300dc8 100644
--- a/compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs
+++ b/compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs
@@ -9,13 +9,19 @@ use super::check::Qualifs;
 use super::ops::{self, NonConstOp};
 use super::qualifs::{NeedsNonConstDrop, Qualif};
 use super::ConstCx;
+use crate::check_consts::rustc_allow_const_fn_unstable;
 
 /// Returns `true` if we should use the more precise live drop checker that runs after drop
 /// elaboration.
 pub fn checking_enabled(ccx: &ConstCx<'_, '_>) -> bool {
-    // Const-stable functions must always use the stable live drop checker.
+    // Const-stable functions must always use the stable live drop checker...
     if ccx.is_const_stable_const_fn() {
-        return false;
+        // ...except if they have the feature flag set via `rustc_allow_const_fn_unstable`.
+        return rustc_allow_const_fn_unstable(
+            ccx.tcx,
+            ccx.body.source.def_id().expect_local(),
+            sym::const_precise_live_drops,
+        );
     }
 
     ccx.tcx.features().const_precise_live_drops
diff --git a/tests/ui/consts/precise-drop-allow-const-fn-unstable.not_allow.stderr b/tests/ui/consts/precise-drop-allow-const-fn-unstable.not_allow.stderr
new file mode 100644
index 00000000000..6038c6d332f
--- /dev/null
+++ b/tests/ui/consts/precise-drop-allow-const-fn-unstable.not_allow.stderr
@@ -0,0 +1,12 @@
+error[E0493]: destructor of `Option<T>` cannot be evaluated at compile-time
+  --> $DIR/precise-drop-allow-const-fn-unstable.rs:11:24
+   |
+LL | pub const fn unwrap<T>(this: Option<T>) -> T {
+   |                        ^^^^ the destructor for this type cannot be evaluated in constant functions
+...
+LL | }
+   | - value is dropped here
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0493`.
diff --git a/tests/ui/consts/precise-drop-allow-const-fn-unstable.rs b/tests/ui/consts/precise-drop-allow-const-fn-unstable.rs
new file mode 100644
index 00000000000..56155e519dc
--- /dev/null
+++ b/tests/ui/consts/precise-drop-allow-const-fn-unstable.rs
@@ -0,0 +1,17 @@
+//@ revisions: allow not_allow
+//@ compile-flags: --crate-type=lib -Cinstrument-coverage  -Zno-profiler-runtime
+//@[allow] check-pass
+
+#![feature(staged_api, rustc_allow_const_fn_unstable)]
+#![stable(feature = "rust_test", since = "1.0.0")]
+
+#[stable(feature = "rust_test", since = "1.0.0")]
+#[rustc_const_stable(feature = "rust_test", since = "1.0.0")]
+#[cfg_attr(allow, rustc_allow_const_fn_unstable(const_precise_live_drops))]
+pub const fn unwrap<T>(this: Option<T>) -> T {
+//[not_allow]~^ ERROR: cannot be evaluated
+    match this {
+        Some(x) => x,
+        None => panic!(),
+    }
+}