Skip to content

Commit

Permalink
Auto merge of #84310 - RalfJung:const-fn-feature-flags, r=oli-obk
Browse files Browse the repository at this point in the history
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?
  • Loading branch information
bors committed Apr 24, 2021
2 parents 42816d6 + 49054c3 commit b56b175
Show file tree
Hide file tree
Showing 37 changed files with 176 additions and 114 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
)]
#![feature(box_syntax)]
#![feature(box_patterns)]
#![feature(const_fn)] // For the `transmute` in `P::new`
#![cfg_attr(bootstrap, feature(const_fn))] // For the `transmute` in `P::new`
#![cfg_attr(not(bootstrap), feature(const_fn_unsize))] // For the `transmute` in `P::new`
#![feature(const_fn_transmute)]
#![feature(const_panic)]
#![feature(crate_visibility_modifier)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ E0716: include_str!("./error_codes/E0716.md"),
E0718: include_str!("./error_codes/E0718.md"),
E0719: include_str!("./error_codes/E0719.md"),
E0720: include_str!("./error_codes/E0720.md"),
E0723: include_str!("./error_codes/E0723.md"),
E0724: include_str!("./error_codes/E0724.md"),
E0725: include_str!("./error_codes/E0725.md"),
E0727: include_str!("./error_codes/E0727.md"),
Expand Down Expand Up @@ -636,6 +635,7 @@ E0781: include_str!("./error_codes/E0781.md"),
E0717, // rustc_promotable without stability attribute
// E0721, // `await` keyword
E0722, // Malformed `#[optimize]` attribute
// E0723, unstable feature in `const` context
E0726, // non-explicit (not `'_`) elided lifetime in unsupported position
// E0738, // Removed; errored on `#[track_caller] fn`s in `extern "Rust" { ... }`.
E0757, // `#[ffi_const]` functions cannot be `#[ffi_pure]`
Expand Down
20 changes: 0 additions & 20 deletions compiler/rustc_error_codes/src/error_codes/E0723.md

This file was deleted.

6 changes: 6 additions & 0 deletions compiler/rustc_feature/src/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,12 @@ declare_features! (
/// Allows `extern "wasm" fn`
(active, wasm_abi, "1.53.0", Some(83788), None),

/// Allows trait bounds in `const fn`.
(active, const_fn_trait_bound, "1.53.0", Some(57563), None),

/// Allows unsizing coercions in `const fn`.
(active, const_fn_unsize, "1.53.0", Some(64992), None),

// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
#![feature(crate_visibility_modifier)]
#![feature(const_fn)] // For the unsizing cast on `&[]`
#![feature(const_panic)]
#![feature(extended_key_value_attributes)]
#![feature(in_band_lifetimes)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_index/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(allow_internal_unstable)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(extend_one)]
#![feature(iter_zip)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_infer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(extend_one)]
#![feature(iter_zip)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(core_intrinsics)]
#![feature(discriminant_kind)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_mir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Rust MIR: a lowered representation of Rust.
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(crate_visibility_modifier)]
#![feature(decl_macro)]
Expand Down
40 changes: 16 additions & 24 deletions compiler/rustc_mir/src/transform/check_consts/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,17 @@ impl NonConstOp for UnionAccess {
pub struct UnsizingCast;
impl NonConstOp for UnsizingCast {
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
mcf_status_in_item(ccx)
if ccx.const_kind() != hir::ConstContext::ConstFn {
Status::Allowed
} else {
Status::Unstable(sym::const_fn_unsize)
}
}

fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
mcf_build_error(
ccx,
feature_err(
&ccx.tcx.sess.parse_sess,
sym::const_fn_unsize,
span,
"unsizing casts to types besides slices are not allowed in const fn",
)
Expand Down Expand Up @@ -642,12 +647,17 @@ pub mod ty {
}

fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
mcf_status_in_item(ccx)
if ccx.const_kind() != hir::ConstContext::ConstFn {
Status::Allowed
} else {
Status::Unstable(sym::const_fn_trait_bound)
}
}

fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
mcf_build_error(
ccx,
feature_err(
&ccx.tcx.sess.parse_sess,
sym::const_fn_trait_bound,
span,
"trait bounds other than `Sized` on const fn parameters are unstable",
)
Expand All @@ -672,21 +682,3 @@ pub mod ty {
}
}
}

fn mcf_status_in_item(ccx: &ConstCx<'_, '_>) -> Status {
if ccx.const_kind() != hir::ConstContext::ConstFn {
Status::Allowed
} else {
Status::Unstable(sym::const_fn)
}
}

fn mcf_build_error(ccx: &ConstCx<'_, 'tcx>, span: Span, msg: &str) -> DiagnosticBuilder<'tcx> {
let mut err = struct_span_err!(ccx.tcx.sess, span, E0723, "{}", msg);
err.note(
"see issue #57563 <https://github.com/rust-lang/rust/issues/57563> \
for more information",
);
err.help("add `#![feature(const_fn)]` to the crate attributes to enable");
err
}
14 changes: 6 additions & 8 deletions compiler/rustc_mir/src/transform/check_consts/validation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorReported};
use rustc_errors::{Applicability, Diagnostic, ErrorReported};
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, HirId, LangItem};
use rustc_index::bit_set::BitSet;
Expand Down Expand Up @@ -234,13 +234,11 @@ impl Validator<'mir, 'tcx> {
if self.is_const_stable_const_fn() {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) {
struct_span_err!(
self.ccx.tcx.sess,
self.span,
E0723,
"trait methods cannot be stable const fn"
)
.emit();
self.ccx
.tcx
.sess
.struct_span_err(self.span, "trait methods cannot be stable const fn")
.emit();
}
}

Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_mir_build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![feature(array_windows)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(control_flow_enum)]
#![feature(crate_visibility_modifier)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_passes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! This API is completely unstable and subject to change.
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(crate_visibility_modifier)]
#![feature(in_band_lifetimes)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_query_system/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![feature(bool_to_option)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(core_intrinsics)]
#![feature(drain_filter)]
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(array_windows)]
#![feature(crate_visibility_modifier)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(negative_impls)]
#![feature(nll)]
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,10 @@ symbols! {
const_fn,
const_fn_floating_point_arithmetic,
const_fn_fn_ptr_basics,
const_fn_trait_bound,
const_fn_transmute,
const_fn_union,
const_fn_unsize,
const_generic_defaults,
const_generics,
const_generics_defaults,
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_target/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(bool_to_option)]
#![feature(const_fn)]
#![feature(const_panic)]
#![feature(nll)]
#![feature(never_type)]
Expand Down
3 changes: 2 additions & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
#![feature(cfg_target_has_atomic)]
#![feature(coerce_unsized)]
#![feature(const_btree_new)]
#![feature(const_fn)]
#![cfg_attr(bootstrap, feature(const_fn))]
#![cfg_attr(not(bootstrap), feature(const_fn_trait_bound))]
#![feature(cow_is_borrowed)]
#![feature(const_cow_is_borrowed)]
#![feature(destructuring_assignment)]
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@
#![feature(const_refs_to_cell)]
#![feature(const_panic)]
#![feature(const_pin)]
#![feature(const_fn)]
#![cfg_attr(bootstrap, feature(const_fn))]
#![feature(const_fn_union)]
#![feature(const_impl_trait)]
#![feature(const_fn_floating_point_arithmetic)]
#![feature(const_fn_fn_ptr_basics)]
#![cfg_attr(not(bootstrap), feature(const_fn_trait_bound))]
#![feature(const_option)]
#![feature(const_precise_live_drops)]
#![feature(const_ptr_offset)]
Expand Down
2 changes: 1 addition & 1 deletion src/ci/pgo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUSTC_BOOTSTRAP=1 ./build/$PGO_HOST/stage2/bin/rustc --edition=2018 \

# Download and build a single-file stress test benchmark on perf.rust-lang.org.
function pgo_perf_benchmark {
local PERF=e095f5021bf01cf3800f50b3a9f14a9683eb3e4e
local PERF=9442def56a39d742bf27ebcc3e0614cf117e1bc2
local github_prefix=https://raw.githubusercontent.com/rust-lang/rustc-perf/$PERF
local name=$1
curl -o /tmp/$name.rs $github_prefix/collector/benchmarks/$name/src/lib.rs
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/consts/const-eval/issue-49296.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// issue-49296: Unsafe shenigans in constants can result in missing errors

#![feature(const_fn)]
#![feature(const_fn_union)]
#![feature(const_fn_trait_bound)]

const unsafe fn transmute<T: Copy, U: Copy>(t: T) -> U {
#[repr(C)]
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/consts/const-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

// A very basic test of const fn functionality.

#![feature(const_fn, const_indexing)]
#![feature(const_indexing)]
#![feature(const_fn_trait_bound)]

const fn add(x: u32, y: u32) -> u32 {
x + y
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/consts/const_fn_trait_bound.gated.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: fatal error triggered by #[rustc_error]
--> $DIR/const_fn_trait_bound.rs:17:1
|
LL | fn main() {}
| ^^^^^^^^^

error: aborting due to previous error

17 changes: 17 additions & 0 deletions src/test/ui/consts/const_fn_trait_bound.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// gate-test-const_fn_trait_bound

// revisions: stock gated

#![feature(rustc_attrs)]
#![cfg_attr(gated, feature(const_fn_trait_bound))]

const fn test1<T: std::ops::Add>() {}
//[stock]~^ trait bounds
const fn test2(_x: &dyn Send) {}
//[stock]~^ trait bounds
const fn test3() -> &'static dyn Send { loop {} }
//[stock]~^ trait bounds


#[rustc_error]
fn main() {} //[gated]~ fatal error triggered by #[rustc_error]
30 changes: 30 additions & 0 deletions src/test/ui/consts/const_fn_trait_bound.stock.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
--> $DIR/const_fn_trait_bound.rs:8:16
|
LL | const fn test1<T: std::ops::Add>() {}
| ^
|
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable

error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
--> $DIR/const_fn_trait_bound.rs:10:16
|
LL | const fn test2(_x: &dyn Send) {}
| ^^
|
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable

error[E0658]: trait bounds other than `Sized` on const fn parameters are unstable
--> $DIR/const_fn_trait_bound.rs:12:21
|
LL | const fn test3() -> &'static dyn Send { loop {} }
| ^^^^^^^^^^^^^^^^^
|
= note: see issue #57563 <https://github.com/rust-lang/rust/issues/57563> for more information
= help: add `#![feature(const_fn_trait_bound)]` to the crate attributes to enable

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0658`.
8 changes: 8 additions & 0 deletions src/test/ui/consts/const_fn_unsize.gated.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: fatal error triggered by #[rustc_error]
--> $DIR/const_fn_unsize.rs:16:1
|
LL | fn main() {}
| ^^^^^^^^^

error: aborting due to previous error

16 changes: 16 additions & 0 deletions src/test/ui/consts/const_fn_unsize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// gate-test-const_fn_unsize

// revisions: stock gated

#![feature(rustc_attrs)]
#![cfg_attr(gated, feature(const_fn_unsize))]

use std::ptr::NonNull;

const fn test() {
let _x = NonNull::<[i32; 0]>::dangling() as NonNull<[i32]>;
//[stock]~^ unsizing cast
}

#[rustc_error]
fn main() {} //[gated]~ fatal error triggered by #[rustc_error]
12 changes: 12 additions & 0 deletions src/test/ui/consts/const_fn_unsize.stock.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0658]: unsizing casts to types besides slices are not allowed in const fn
--> $DIR/const_fn_unsize.rs:11:14
|
LL | let _x = NonNull::<[i32; 0]>::dangling() as NonNull<[i32]>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #64992 <https://github.com/rust-lang/rust/issues/64992> for more information
= help: add `#![feature(const_fn_unsize)]` to the crate attributes to enable

error: aborting due to previous error

For more information about this error, try `rustc --explain E0658`.
Loading

0 comments on commit b56b175

Please sign in to comment.