Skip to content

Commit 5412499

Browse files
committed
make it possible to enable const_precise_live_drops per-function
1 parent 717aec0 commit 5412499

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

compiler/rustc_const_eval/src/check_consts/post_drop_elaboration.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ use super::check::Qualifs;
99
use super::ops::{self, NonConstOp};
1010
use super::qualifs::{NeedsNonConstDrop, Qualif};
1111
use super::ConstCx;
12+
use crate::check_consts::rustc_allow_const_fn_unstable;
1213

1314
/// Returns `true` if we should use the more precise live drop checker that runs after drop
1415
/// elaboration.
1516
pub fn checking_enabled(ccx: &ConstCx<'_, '_>) -> bool {
16-
// Const-stable functions must always use the stable live drop checker.
17+
// Const-stable functions must always use the stable live drop checker...
1718
if ccx.is_const_stable_const_fn() {
18-
return false;
19+
// ...except if they have the feature flag set via `rustc_allow_const_fn_unstable`.
20+
return rustc_allow_const_fn_unstable(
21+
ccx.tcx,
22+
ccx.body.source.def_id().expect_local(),
23+
sym::const_precise_live_drops,
24+
);
1925
}
2026

2127
ccx.tcx.features().const_precise_live_drops
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0493]: destructor of `Option<T>` cannot be evaluated at compile-time
2+
--> $DIR/precise-drop-allow-const-fn-unstable.rs:11:24
3+
|
4+
LL | pub const fn unwrap<T>(this: Option<T>) -> T {
5+
| ^^^^ the destructor for this type cannot be evaluated in constant functions
6+
...
7+
LL | }
8+
| - value is dropped here
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0493`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ revisions: allow not_allow
2+
//@ compile-flags: --crate-type=lib -Cinstrument-coverage -Zno-profiler-runtime
3+
//@[allow] check-pass
4+
5+
#![feature(staged_api, rustc_allow_const_fn_unstable)]
6+
#![stable(feature = "rust_test", since = "1.0.0")]
7+
8+
#[stable(feature = "rust_test", since = "1.0.0")]
9+
#[rustc_const_stable(feature = "rust_test", since = "1.0.0")]
10+
#[cfg_attr(allow, rustc_allow_const_fn_unstable(const_precise_live_drops))]
11+
pub const fn unwrap<T>(this: Option<T>) -> T {
12+
//[not_allow]~^ ERROR: cannot be evaluated
13+
match this {
14+
Some(x) => x,
15+
None => panic!(),
16+
}
17+
}

0 commit comments

Comments
 (0)