Skip to content

Commit 895c37a

Browse files
Add a feature gate for basic function pointer use in const fn
1 parent 1ec980d commit 895c37a

File tree

7 files changed

+29
-7
lines changed

7 files changed

+29
-7
lines changed

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,9 @@ declare_features! (
587587
/// Allows basic arithmetic on floating point types in a `const fn`.
588588
(active, const_fn_floating_point_arithmetic, "1.48.0", Some(57241), None),
589589

590+
/// Allows using and casting function pointers in a `const fn`.
591+
(active, const_fn_fn_ptr_basics, "1.48.0", Some(57563), None),
592+
590593
// -------------------------------------------------------------------------
591594
// feature-group-end: actual feature gates
592595
// -------------------------------------------------------------------------

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,21 @@ impl NonConstOp for FnPtrCast {
213213
const STOPS_CONST_CHECKING: bool = true;
214214

215215
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
216-
mcf_status_in_item(ccx)
216+
if ccx.const_kind() != hir::ConstContext::ConstFn {
217+
Status::Allowed
218+
} else {
219+
Status::Unstable(sym::const_fn_fn_ptr_basics)
220+
}
217221
}
218222

219223
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
220-
mcf_emit_error(ccx, span, "function pointer casts are not allowed in const fn");
224+
feature_err(
225+
&ccx.tcx.sess.parse_sess,
226+
sym::const_fn_fn_ptr_basics,
227+
span,
228+
&format!("function pointer casts are not allowed in {}s", ccx.const_kind()),
229+
)
230+
.emit()
221231
}
222232
}
223233

@@ -596,17 +606,21 @@ pub mod ty {
596606
const STOPS_CONST_CHECKING: bool = true;
597607

598608
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
599-
// FIXME: This attribute a hack to allow the specialization of the `futures` API. See
600-
// #59739. We should have a proper feature gate for this.
601-
if ccx.tcx.has_attr(ccx.def_id.to_def_id(), sym::rustc_allow_const_fn_ptr) {
609+
if ccx.const_kind() != hir::ConstContext::ConstFn {
602610
Status::Allowed
603611
} else {
604-
mcf_status_in_item(ccx)
612+
Status::Unstable(sym::const_fn_fn_ptr_basics)
605613
}
606614
}
607615

608616
fn emit_error(&self, ccx: &ConstCx<'_, '_>, span: Span) {
609-
mcf_emit_error(ccx, span, "function pointers in const fn are unstable");
617+
feature_err(
618+
&ccx.tcx.sess.parse_sess,
619+
sym::const_fn_fn_ptr_basics,
620+
span,
621+
&format!("function pointers cannot appear in {}s", ccx.const_kind()),
622+
)
623+
.emit()
610624
}
611625
}
612626

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ symbols! {
353353
const_extern_fn,
354354
const_fn,
355355
const_fn_floating_point_arithmetic,
356+
const_fn_fn_ptr_basics,
356357
const_fn_transmute,
357358
const_fn_union,
358359
const_generics,

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#![feature(const_fn_union)]
8484
#![feature(const_fn)]
8585
#![cfg_attr(not(bootstrap), feature(const_fn_floating_point_arithmetic))]
86+
#![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))]
8687
#![feature(const_generics)]
8788
#![feature(const_option)]
8889
#![feature(const_precise_live_drops)]

library/core/src/task/wake.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl RawWakerVTable {
136136
// (see https://github.com/rust-rfcs/const-eval/issues/19#issuecomment-472799062)
137137
#[rustc_allow_const_fn_ptr]
138138
#[rustc_const_stable(feature = "futures_api", since = "1.36.0")]
139+
#[cfg_attr(not(bootstrap), allow_internal_unstable(const_fn_fn_ptr_basics))]
139140
pub const fn new(
140141
clone: unsafe fn(*const ()) -> RawWaker,
141142
wake: unsafe fn(*const ()),

library/proc_macro/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#![feature(nll)]
2222
#![feature(staged_api)]
2323
#![feature(const_fn)]
24+
#![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))]
2425
#![feature(allow_internal_unstable)]
2526
#![feature(decl_macro)]
2627
#![feature(extern_types)]

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@
239239
#![cfg_attr(not(bootstrap), feature(const_fn_floating_point_arithmetic))]
240240
#![feature(const_fn_transmute)]
241241
#![feature(const_fn)]
242+
#![cfg_attr(not(bootstrap), feature(const_fn_fn_ptr_basics))]
242243
#![feature(const_ip)]
243244
#![feature(const_ipv6)]
244245
#![feature(const_raw_ptr_deref)]

0 commit comments

Comments
 (0)