Add a feature gate for basic function pointer use in `const fn`

This commit is contained in:
Dylan MacKenzie 2020-09-24 16:17:03 -07:00
parent 1d216fef3e
commit 1ff143191c
7 changed files with 29 additions and 7 deletions

View File

@ -587,6 +587,9 @@ declare_features! (
/// Allows basic arithmetic on floating point types in a `const fn`.
(active, const_fn_floating_point_arithmetic, "1.48.0", Some(57241), None),
/// Allows using and casting function pointers in a `const fn`.
(active, const_fn_fn_ptr_basics, "1.48.0", Some(57563), None),
// -------------------------------------------------------------------------
// feature-group-end: actual feature gates
// -------------------------------------------------------------------------

View File

@ -213,11 +213,21 @@ impl NonConstOp for FnPtrCast {
const STOPS_CONST_CHECKING: bool = true;
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_fn_ptr_basics)
}
}
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
mcf_emit_error(ccx, span, "function pointer casts are not allowed in const fn");
feature_err(
&ccx.tcx.sess.parse_sess,
sym::const_fn_fn_ptr_basics,
span,
&format!("function pointer casts are not allowed in {}s", ccx.const_kind()),
)
.emit()
}
}
@ -596,17 +606,21 @@ pub mod ty {
const STOPS_CONST_CHECKING: bool = true;
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
// FIXME: This attribute a hack to allow the specialization of the `futures` API. See
// #59739. We should have a proper feature gate for this.
if ccx.tcx.has_attr(ccx.def_id.to_def_id(), sym::rustc_allow_const_fn_ptr) {
if ccx.const_kind() != hir::ConstContext::ConstFn {
Status::Allowed
} else {
mcf_status_in_item(ccx)
Status::Unstable(sym::const_fn_fn_ptr_basics)
}
}
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
mcf_emit_error(ccx, span, "function pointers in const fn are unstable");
feature_err(
&ccx.tcx.sess.parse_sess,
sym::const_fn_fn_ptr_basics,
span,
&format!("function pointers cannot appear in {}s", ccx.const_kind()),
)
.emit()
}
}

View File

@ -353,6 +353,7 @@ symbols! {
const_extern_fn,
const_fn,
const_fn_floating_point_arithmetic,
const_fn_fn_ptr_basics,
const_fn_transmute,
const_fn_union,
const_generics,

View File

@ -83,6 +83,7 @@
#![feature(const_fn_union)]
#![feature(const_fn)]
#![cfg_attr(not(bootstrap), feature(const_fn_floating_point_arithmetic))]
#![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))]
#![feature(const_generics)]
#![feature(const_option)]
#![feature(const_precise_live_drops)]

View File

@ -136,6 +136,7 @@ impl RawWakerVTable {
// (see https://github.com/rust-rfcs/const-eval/issues/19#issuecomment-472799062)
#[rustc_allow_const_fn_ptr]
#[rustc_const_stable(feature = "futures_api", since = "1.36.0")]
#[cfg_attr(not(bootstrap), allow_internal_unstable(const_fn_fn_ptr_basics))]
pub const fn new(
clone: unsafe fn(*const ()) -> RawWaker,
wake: unsafe fn(*const ()),

View File

@ -21,6 +21,7 @@
#![feature(nll)]
#![feature(staged_api)]
#![feature(const_fn)]
#![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))]
#![feature(allow_internal_unstable)]
#![feature(decl_macro)]
#![feature(extern_types)]

View File

@ -239,6 +239,7 @@
#![cfg_attr(not(bootstrap), feature(const_fn_floating_point_arithmetic))]
#![feature(const_fn_transmute)]
#![feature(const_fn)]
#![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))]
#![feature(const_ip)]
#![feature(const_ipv6)]
#![feature(const_raw_ptr_deref)]