Skip to content

Commit b56b175

Browse files
committed
Auto merge of #84310 - RalfJung:const-fn-feature-flags, r=oli-obk
further split up const_fn feature flag This continues the work on splitting up `const_fn` into separate feature flags: * `const_fn_trait_bound` for `const fn` with trait bounds * `const_fn_unsize` for unsizing coercions in `const fn` (looks like only `dyn` unsizing is still guarded here) I don't know if there are even any things left that `const_fn` guards... at least libcore and liballoc do not need it any more. `@oli-obk` are you currently able to do reviews?
2 parents 42816d6 + 49054c3 commit b56b175

File tree

37 files changed

+176
-114
lines changed

37 files changed

+176
-114
lines changed

compiler/rustc_ast/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
)]
1111
#![feature(box_syntax)]
1212
#![feature(box_patterns)]
13-
#![feature(const_fn)] // For the `transmute` in `P::new`
13+
#![cfg_attr(bootstrap, feature(const_fn))] // For the `transmute` in `P::new`
14+
#![cfg_attr(not(bootstrap), feature(const_fn_unsize))] // For the `transmute` in `P::new`
1415
#![feature(const_fn_transmute)]
1516
#![feature(const_panic)]
1617
#![feature(crate_visibility_modifier)]

compiler/rustc_error_codes/src/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,6 @@ E0716: include_str!("./error_codes/E0716.md"),
416416
E0718: include_str!("./error_codes/E0718.md"),
417417
E0719: include_str!("./error_codes/E0719.md"),
418418
E0720: include_str!("./error_codes/E0720.md"),
419-
E0723: include_str!("./error_codes/E0723.md"),
420419
E0724: include_str!("./error_codes/E0724.md"),
421420
E0725: include_str!("./error_codes/E0725.md"),
422421
E0727: include_str!("./error_codes/E0727.md"),
@@ -636,6 +635,7 @@ E0781: include_str!("./error_codes/E0781.md"),
636635
E0717, // rustc_promotable without stability attribute
637636
// E0721, // `await` keyword
638637
E0722, // Malformed `#[optimize]` attribute
638+
// E0723, unstable feature in `const` context
639639
E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
640640
// E0738, // Removed; errored on `#[track_caller] fn`s in `extern "Rust" { ... }`.
641641
E0757, // `#[ffi_const]` functions cannot be `#[ffi_pure]`

compiler/rustc_error_codes/src/error_codes/E0723.md

-20
This file was deleted.

compiler/rustc_feature/src/active.rs

+6
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,12 @@ declare_features! (
645645
/// Allows `extern "wasm" fn`
646646
(active, wasm_abi, "1.53.0", Some(83788), None),
647647

648+
/// Allows trait bounds in `const fn`.
649+
(active, const_fn_trait_bound, "1.53.0", Some(57563), None),
650+
651+
/// Allows unsizing coercions in `const fn`.
652+
(active, const_fn_unsize, "1.53.0", Some(64992), None),
653+
648654
// -------------------------------------------------------------------------
649655
// feature-group-end: actual feature gates
650656
// -------------------------------------------------------------------------

compiler/rustc_hir/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
44
55
#![feature(crate_visibility_modifier)]
6-
#![feature(const_fn)] // For the unsizing cast on `&[]`
76
#![feature(const_panic)]
87
#![feature(extended_key_value_attributes)]
98
#![feature(in_band_lifetimes)]

compiler/rustc_index/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(allow_internal_unstable)]
2-
#![feature(const_fn)]
32
#![feature(const_panic)]
43
#![feature(extend_one)]
54
#![feature(iter_zip)]

compiler/rustc_infer/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#![feature(bool_to_option)]
1717
#![feature(box_patterns)]
1818
#![feature(box_syntax)]
19-
#![feature(const_fn)]
2019
#![feature(const_panic)]
2120
#![feature(extend_one)]
2221
#![feature(iter_zip)]

compiler/rustc_middle/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#![feature(bool_to_option)]
3030
#![feature(box_patterns)]
3131
#![feature(box_syntax)]
32-
#![feature(const_fn)]
3332
#![feature(const_panic)]
3433
#![feature(core_intrinsics)]
3534
#![feature(discriminant_kind)]

compiler/rustc_mir/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Rust MIR: a lowered representation of Rust.
1212
#![feature(bool_to_option)]
1313
#![feature(box_patterns)]
1414
#![feature(box_syntax)]
15-
#![feature(const_fn)]
1615
#![feature(const_panic)]
1716
#![feature(crate_visibility_modifier)]
1817
#![feature(decl_macro)]

compiler/rustc_mir/src/transform/check_consts/ops.rs

+16-24
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,17 @@ impl NonConstOp for UnionAccess {
540540
pub struct UnsizingCast;
541541
impl NonConstOp for UnsizingCast {
542542
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
543-
mcf_status_in_item(ccx)
543+
if ccx.const_kind() != hir::ConstContext::ConstFn {
544+
Status::Allowed
545+
} else {
546+
Status::Unstable(sym::const_fn_unsize)
547+
}
544548
}
545549

546550
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
547-
mcf_build_error(
548-
ccx,
551+
feature_err(
552+
&ccx.tcx.sess.parse_sess,
553+
sym::const_fn_unsize,
549554
span,
550555
"unsizing casts to types besides slices are not allowed in const fn",
551556
)
@@ -642,12 +647,17 @@ pub mod ty {
642647
}
643648

644649
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
645-
mcf_status_in_item(ccx)
650+
if ccx.const_kind() != hir::ConstContext::ConstFn {
651+
Status::Allowed
652+
} else {
653+
Status::Unstable(sym::const_fn_trait_bound)
654+
}
646655
}
647656

648657
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
649-
mcf_build_error(
650-
ccx,
658+
feature_err(
659+
&ccx.tcx.sess.parse_sess,
660+
sym::const_fn_trait_bound,
651661
span,
652662
"trait bounds other than `Sized` on const fn parameters are unstable",
653663
)
@@ -672,21 +682,3 @@ pub mod ty {
672682
}
673683
}
674684
}
675-
676-
fn mcf_status_in_item(ccx: &ConstCx<'_, '_>) -> Status {
677-
if ccx.const_kind() != hir::ConstContext::ConstFn {
678-
Status::Allowed
679-
} else {
680-
Status::Unstable(sym::const_fn)
681-
}
682-
}
683-
684-
fn mcf_build_error(ccx: &ConstCx<'_, 'tcx>, span: Span, msg: &str) -> DiagnosticBuilder<'tcx> {
685-
let mut err = struct_span_err!(ccx.tcx.sess, span, E0723, "{}", msg);
686-
err.note(
687-
"see issue #57563 <https://github.com/rust-lang/rust/issues/57563> \
688-
for more information",
689-
);
690-
err.help("add `#![feature(const_fn)]` to the crate attributes to enable");
691-
err
692-
}

compiler/rustc_mir/src/transform/check_consts/validation.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
22
3-
use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorReported};
3+
use rustc_errors::{Applicability, Diagnostic, ErrorReported};
44
use rustc_hir::def_id::DefId;
55
use rustc_hir::{self as hir, HirId, LangItem};
66
use rustc_index::bit_set::BitSet;
@@ -234,13 +234,11 @@ impl Validator<'mir, 'tcx> {
234234
if self.is_const_stable_const_fn() {
235235
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
236236
if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) {
237-
struct_span_err!(
238-
self.ccx.tcx.sess,
239-
self.span,
240-
E0723,
241-
"trait methods cannot be stable const fn"
242-
)
243-
.emit();
237+
self.ccx
238+
.tcx
239+
.sess
240+
.struct_span_err(self.span, "trait methods cannot be stable const fn")
241+
.emit();
244242
}
245243
}
246244

compiler/rustc_mir_build/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#![feature(array_windows)]
55
#![feature(box_patterns)]
66
#![feature(box_syntax)]
7-
#![feature(const_fn)]
87
#![feature(const_panic)]
98
#![feature(control_flow_enum)]
109
#![feature(crate_visibility_modifier)]

compiler/rustc_passes/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//! This API is completely unstable and subject to change.
66
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
8-
#![feature(const_fn)]
98
#![feature(const_panic)]
109
#![feature(crate_visibility_modifier)]
1110
#![feature(in_band_lifetimes)]

compiler/rustc_query_system/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(bool_to_option)]
2-
#![feature(const_fn)]
32
#![feature(const_panic)]
43
#![feature(core_intrinsics)]
54
#![feature(drain_filter)]

compiler/rustc_span/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1717
#![feature(array_windows)]
1818
#![feature(crate_visibility_modifier)]
19-
#![feature(const_fn)]
2019
#![feature(const_panic)]
2120
#![feature(negative_impls)]
2221
#![feature(nll)]

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,10 @@ symbols! {
382382
const_fn,
383383
const_fn_floating_point_arithmetic,
384384
const_fn_fn_ptr_basics,
385+
const_fn_trait_bound,
385386
const_fn_transmute,
386387
const_fn_union,
388+
const_fn_unsize,
387389
const_generic_defaults,
388390
const_generics,
389391
const_generics_defaults,

compiler/rustc_target/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
1010
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
1111
#![feature(bool_to_option)]
12-
#![feature(const_fn)]
1312
#![feature(const_panic)]
1413
#![feature(nll)]
1514
#![feature(never_type)]

library/alloc/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@
8989
#![feature(cfg_target_has_atomic)]
9090
#![feature(coerce_unsized)]
9191
#![feature(const_btree_new)]
92-
#![feature(const_fn)]
92+
#![cfg_attr(bootstrap, feature(const_fn))]
93+
#![cfg_attr(not(bootstrap), feature(const_fn_trait_bound))]
9394
#![feature(cow_is_borrowed)]
9495
#![feature(const_cow_is_borrowed)]
9596
#![feature(destructuring_assignment)]

library/core/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@
8181
#![feature(const_refs_to_cell)]
8282
#![feature(const_panic)]
8383
#![feature(const_pin)]
84-
#![feature(const_fn)]
84+
#![cfg_attr(bootstrap, feature(const_fn))]
8585
#![feature(const_fn_union)]
8686
#![feature(const_impl_trait)]
8787
#![feature(const_fn_floating_point_arithmetic)]
8888
#![feature(const_fn_fn_ptr_basics)]
89+
#![cfg_attr(not(bootstrap), feature(const_fn_trait_bound))]
8990
#![feature(const_option)]
9091
#![feature(const_precise_live_drops)]
9192
#![feature(const_ptr_offset)]

src/ci/pgo.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ RUSTC_BOOTSTRAP=1 ./build/$PGO_HOST/stage2/bin/rustc --edition=2018 \
1212

1313
# Download and build a single-file stress test benchmark on perf.rust-lang.org.
1414
function pgo_perf_benchmark {
15-
local PERF=e095f5021bf01cf3800f50b3a9f14a9683eb3e4e
15+
local PERF=9442def56a39d742bf27ebcc3e0614cf117e1bc2
1616
local github_prefix=https://raw.githubusercontent.com/rust-lang/rustc-perf/$PERF
1717
local name=$1
1818
curl -o /tmp/$name.rs $github_prefix/collector/benchmarks/$name/src/lib.rs

src/test/ui/consts/const-eval/issue-49296.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// issue-49296: Unsafe shenigans in constants can result in missing errors
22

3-
#![feature(const_fn)]
43
#![feature(const_fn_union)]
4+
#![feature(const_fn_trait_bound)]
55

66
const unsafe fn transmute<T: Copy, U: Copy>(t: T) -> U {
77
#[repr(C)]

src/test/ui/consts/const-fn.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
// A very basic test of const fn functionality.
55

6-
#![feature(const_fn, const_indexing)]
6+
#![feature(const_indexing)]
7+
#![feature(const_fn_trait_bound)]
78

89
const fn add(x: u32, y: u32) -> u32 {
910
x + y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: fatal error triggered by #[rustc_error]
2+
--> $DIR/const_fn_trait_bound.rs:17:1
3+
|
4+
LL | fn main() {}
5+
| ^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// gate-test-const_fn_trait_bound
2+
3+
// revisions: stock gated
4+
5+
#![feature(rustc_attrs)]
6+
#![cfg_attr(gated, feature(const_fn_trait_bound))]
7+
8+
const fn test1<T: std::ops::Add>() {}
9+
//[stock]~^ trait bounds
10+
const fn test2(_x: &dyn Send) {}
11+
//[stock]~^ trait bounds
12+
const fn test3() -> &'static dyn Send { loop {} }
13+
//[stock]~^ trait bounds
14+
15+
16+
#[rustc_error]
17+
fn main() {} //[gated]~ fatal error triggered by #[rustc_error]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
2+
--> $DIR/const_fn_trait_bound.rs:8:16
3+
|
4+
LL | const fn test1<T: std::ops::Add>() {}
5+
| ^
6+
|
7+
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
8+
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
9+
10+
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
11+
--> $DIR/const_fn_trait_bound.rs:10:16
12+
|
13+
LL | const fn test2(_x: &dyn Send) {}
14+
| ^^
15+
|
16+
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
17+
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
18+
19+
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
20+
--> $DIR/const_fn_trait_bound.rs:12:21
21+
|
22+
LL | const fn test3() -> &'static dyn Send { loop {} }
23+
| ^^^^^^^^^^^^^^^^^
24+
|
25+
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
26+
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable
27+
28+
error: aborting due to 3 previous errors
29+
30+
For more information about this error, try `rustc --explain E0658`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: fatal error triggered by #[rustc_error]
2+
--> $DIR/const_fn_unsize.rs:16:1
3+
|
4+
LL | fn main() {}
5+
| ^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

src/test/ui/consts/const_fn_unsize.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// gate-test-const_fn_unsize
2+
3+
// revisions: stock gated
4+
5+
#![feature(rustc_attrs)]
6+
#![cfg_attr(gated, feature(const_fn_unsize))]
7+
8+
use std::ptr::NonNull;
9+
10+
const fn test() {
11+
let _x = NonNull::<[i32; 0]>::dangling() as NonNull<[i32]>;
12+
//[stock]~^ unsizing cast
13+
}
14+
15+
#[rustc_error]
16+
fn main() {} //[gated]~ fatal error triggered by #[rustc_error]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
2+
--> $DIR/const_fn_unsize.rs:11:14
3+
|
4+
LL | let _x = NonNull::<[i32; 0]>::dangling() as NonNull<[i32]>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #64992 <https://github.com/rust-lang/rust/issues/64992> for more information
8+
= help: add `#![feature(const_fn_unsize)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)