Skip to content

Commit a49d9a0

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

7 files changed

+54
-11
lines changed

compiler/rustc_const_eval/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ const_eval_unstable_const_fn = `{$def_path}` is not yet stable as a const fn
419419
const_eval_unstable_in_stable =
420420
const-stable function cannot use `#[feature({$gate})]`
421421
.unstable_sugg = if it is not part of the public API, make this function unstably const
422-
.bypass_sugg = otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
422+
.bypass_sugg = otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks (but requires team approval)
423423
424424
const_eval_unterminated_c_string =
425425
reading a null-terminated string starting at {$pointer} with no null found before end of allocation

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

tests/ui/consts/min_const_fn/allow_const_fn_ptr_run_pass.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
//@ run-pass
2-
#![feature(rustc_allow_const_fn_unstable)]
3-
42
#![feature(rustc_attrs, staged_api)]
53
#![stable(feature = "rust1", since = "1.0.0")]
64

tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ const fn bar2() -> u32 { foo2() } //~ ERROR not yet stable as a const fn
2828
// conformity is required
2929
const fn bar3() -> u32 {
3030
let x = std::cell::Cell::new(0u32);
31-
x.get()
31+
x.get();
3232
//~^ ERROR const-stable function cannot use `#[feature(const_refs_to_cell)]`
3333
//~| ERROR cannot call non-const fn
34+
foo()
35+
//~^ ERROR is not yet stable as a const fn
3436
}
3537

3638
// check whether this function cannot be called even with the feature gate active

tests/ui/consts/min_const_fn/min_const_fn_libstd_stability.stderr

+13-5
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ LL | const fn bar2() -> u32 { foo2() }
1717
error: const-stable function cannot use `#[feature(const_refs_to_cell)]`
1818
--> $DIR/min_const_fn_libstd_stability.rs:31:5
1919
|
20-
LL | x.get()
20+
LL | x.get();
2121
| ^
2222
|
2323
help: if it is not part of the public API, make this function unstably const
2424
|
2525
LL + #[rustc_const_unstable(feature = "...", issue = "...")]
2626
LL | const fn bar3() -> u32 {
2727
|
28-
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
28+
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks (but requires team approval)
2929
|
3030
LL + #[rustc_allow_const_fn_unstable(const_refs_to_cell)]
3131
LL | const fn bar3() -> u32 {
@@ -34,19 +34,27 @@ LL | const fn bar3() -> u32 {
3434
error[E0015]: cannot call non-const fn `Cell::<u32>::get` in constant functions
3535
--> $DIR/min_const_fn_libstd_stability.rs:31:7
3636
|
37-
LL | x.get()
37+
LL | x.get();
3838
| ^^^^^
3939
|
4040
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
4141

42+
error: `foo` is not yet stable as a const fn
43+
--> $DIR/min_const_fn_libstd_stability.rs:34:5
44+
|
45+
LL | foo()
46+
| ^^^^^
47+
|
48+
= help: const-stable functions can only call other const-stable functions
49+
4250
error: `foo2_gated` is not yet stable as a const fn
43-
--> $DIR/min_const_fn_libstd_stability.rs:43:32
51+
--> $DIR/min_const_fn_libstd_stability.rs:45:32
4452
|
4553
LL | const fn bar2_gated() -> u32 { foo2_gated() }
4654
| ^^^^^^^^^^^^
4755
|
4856
= help: const-stable functions can only call other const-stable functions
4957

50-
error: aborting due to 5 previous errors
58+
error: aborting due to 6 previous errors
5159

5260
For more information about this error, try `rustc --explain E0015`.
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)