1
Fork 0

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`. /// Allows basic arithmetic on floating point types in a `const fn`.
(active, const_fn_floating_point_arithmetic, "1.48.0", Some(57241), None), (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 // feature-group-end: actual feature gates
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------

View file

@ -213,11 +213,21 @@ impl NonConstOp for FnPtrCast {
const STOPS_CONST_CHECKING: bool = true; const STOPS_CONST_CHECKING: bool = true;
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status { 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) { 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; const STOPS_CONST_CHECKING: bool = true;
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status { fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
// FIXME: This attribute a hack to allow the specialization of the `futures` API. See if ccx.const_kind() != hir::ConstContext::ConstFn {
// #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) {
Status::Allowed Status::Allowed
} else { } else {
mcf_status_in_item(ccx) Status::Unstable(sym::const_fn_fn_ptr_basics)
} }
} }
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) { 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_extern_fn,
const_fn, const_fn,
const_fn_floating_point_arithmetic, const_fn_floating_point_arithmetic,
const_fn_fn_ptr_basics,
const_fn_transmute, const_fn_transmute,
const_fn_union, const_fn_union,
const_generics, const_generics,

View file

@ -83,6 +83,7 @@
#![feature(const_fn_union)] #![feature(const_fn_union)]
#![feature(const_fn)] #![feature(const_fn)]
#![cfg_attr(not(bootstrap), feature(const_fn_floating_point_arithmetic))] #![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_generics)]
#![feature(const_option)] #![feature(const_option)]
#![feature(const_precise_live_drops)] #![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) // (see https://github.com/rust-rfcs/const-eval/issues/19#issuecomment-472799062)
#[rustc_allow_const_fn_ptr] #[rustc_allow_const_fn_ptr]
#[rustc_const_stable(feature = "futures_api", since = "1.36.0")] #[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( pub const fn new(
clone: unsafe fn(*const ()) -> RawWaker, clone: unsafe fn(*const ()) -> RawWaker,
wake: unsafe fn(*const ()), wake: unsafe fn(*const ()),

View file

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

View file

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