Rollup merge of #77170 - ecstatic-morse:const-fn-ptr, r=oli-obk
Remove `#[rustc_allow_const_fn_ptr]` and add `#![feature(const_fn_fn_ptr_basics)]` `rustc_allow_const_fn_ptr` was a hack to work around the lack of an escape hatch for the "min `const fn`" checks in const-stable functions. Now that we have co-opted `allow_internal_unstable` for this purpose, we no longer need a bespoke attribute. Now this functionality is gated under `const_fn_fn_ptr_basics` (how concise!), and `#[allow_internal_unstable(const_fn_fn_ptr_basics)]` replaces `#[rustc_allow_const_fn_ptr]`. `const_fn_fn_ptr_basics` allows function pointer types to appear in the arguments and locals of a `const fn` as well as function pointer casts to be performed inside a `const fn`. Both of these were allowed in constants and statics already. Notably, this does **not** allow users to invoke function pointers in a const context. Presumably, we will use a nicer name for that (`const_fn_ptr`?). r? @oli-obk
This commit is contained in:
commit
85a59d40f1
39 changed files with 146 additions and 135 deletions
|
@ -145,8 +145,6 @@ pub struct ConstStability {
|
|||
pub feature: Symbol,
|
||||
/// whether the function has a `#[rustc_promotable]` attribute
|
||||
pub promotable: bool,
|
||||
/// whether the function has a `#[rustc_allow_const_fn_ptr]` attribute
|
||||
pub allow_const_fn_ptr: bool,
|
||||
}
|
||||
|
||||
/// The available stability levels.
|
||||
|
@ -190,7 +188,6 @@ where
|
|||
let mut stab: Option<Stability> = None;
|
||||
let mut const_stab: Option<ConstStability> = None;
|
||||
let mut promotable = false;
|
||||
let mut allow_const_fn_ptr = false;
|
||||
let diagnostic = &sess.parse_sess.span_diagnostic;
|
||||
|
||||
'outer: for attr in attrs_iter {
|
||||
|
@ -200,7 +197,6 @@ where
|
|||
sym::unstable,
|
||||
sym::stable,
|
||||
sym::rustc_promotable,
|
||||
sym::rustc_allow_const_fn_ptr,
|
||||
]
|
||||
.iter()
|
||||
.any(|&s| attr.has_name(s))
|
||||
|
@ -215,9 +211,6 @@ where
|
|||
if attr.has_name(sym::rustc_promotable) {
|
||||
promotable = true;
|
||||
}
|
||||
if attr.has_name(sym::rustc_allow_const_fn_ptr) {
|
||||
allow_const_fn_ptr = true;
|
||||
}
|
||||
// attributes with data
|
||||
else if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta {
|
||||
let meta = meta.as_ref().unwrap();
|
||||
|
@ -360,12 +353,8 @@ where
|
|||
if sym::unstable == meta_name {
|
||||
stab = Some(Stability { level, feature });
|
||||
} else {
|
||||
const_stab = Some(ConstStability {
|
||||
level,
|
||||
feature,
|
||||
promotable: false,
|
||||
allow_const_fn_ptr: false,
|
||||
});
|
||||
const_stab =
|
||||
Some(ConstStability { level, feature, promotable: false });
|
||||
}
|
||||
}
|
||||
(None, _, _) => {
|
||||
|
@ -440,12 +429,8 @@ where
|
|||
if sym::stable == meta_name {
|
||||
stab = Some(Stability { level, feature });
|
||||
} else {
|
||||
const_stab = Some(ConstStability {
|
||||
level,
|
||||
feature,
|
||||
promotable: false,
|
||||
allow_const_fn_ptr: false,
|
||||
});
|
||||
const_stab =
|
||||
Some(ConstStability { level, feature, promotable: false });
|
||||
}
|
||||
}
|
||||
(None, _) => {
|
||||
|
@ -464,18 +449,16 @@ where
|
|||
}
|
||||
|
||||
// Merge the const-unstable info into the stability info
|
||||
if promotable || allow_const_fn_ptr {
|
||||
if promotable {
|
||||
if let Some(ref mut stab) = const_stab {
|
||||
stab.promotable = promotable;
|
||||
stab.allow_const_fn_ptr = allow_const_fn_ptr;
|
||||
} else {
|
||||
struct_span_err!(
|
||||
diagnostic,
|
||||
item_sp,
|
||||
E0717,
|
||||
"rustc_promotable and rustc_allow_const_fn_ptr attributes \
|
||||
must be paired with either a rustc_const_unstable or a rustc_const_stable \
|
||||
attribute"
|
||||
"`rustc_promotable` attribute must be paired with either a `rustc_const_unstable` \
|
||||
or a `rustc_const_stable` attribute"
|
||||
)
|
||||
.emit();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
// -------------------------------------------------------------------------
|
||||
|
|
|
@ -464,7 +464,6 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
|
|||
// ==========================================================================
|
||||
|
||||
rustc_attr!(rustc_promotable, AssumedUsed, template!(Word), IMPL_DETAIL),
|
||||
rustc_attr!(rustc_allow_const_fn_ptr, AssumedUsed, template!(Word), IMPL_DETAIL),
|
||||
rustc_attr!(rustc_args_required_const, AssumedUsed, template!(List: "N"), INTERNAL_UNSTABLE),
|
||||
|
||||
// ==========================================================================
|
||||
|
|
|
@ -457,10 +457,6 @@ rustc_queries! {
|
|||
desc { |tcx| "checking if item is promotable: `{}`", tcx.def_path_str(key) }
|
||||
}
|
||||
|
||||
query const_fn_is_allowed_fn_ptr(key: DefId) -> bool {
|
||||
desc { |tcx| "checking if const fn allows `fn()` types: `{}`", tcx.def_path_str(key) }
|
||||
}
|
||||
|
||||
/// Returns `true` if this is a foreign item (i.e., linked via `extern { ... }`).
|
||||
query is_foreign_item(key: DefId) -> bool {
|
||||
desc { |tcx| "checking if `{}` is a foreign item", tcx.def_path_str(key) }
|
||||
|
|
|
@ -151,17 +151,11 @@ fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
fn const_fn_is_allowed_fn_ptr(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
||||
is_const_fn(tcx, def_id)
|
||||
&& tcx.lookup_const_stability(def_id).map(|stab| stab.allow_const_fn_ptr).unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn provide(providers: &mut Providers) {
|
||||
*providers = Providers {
|
||||
is_const_fn_raw,
|
||||
is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, def_id.expect_local()),
|
||||
is_promotable_const_fn,
|
||||
const_fn_is_allowed_fn_ptr,
|
||||
..*providers
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
@ -884,7 +885,6 @@ symbols! {
|
|||
rustc,
|
||||
rustc_allocator,
|
||||
rustc_allocator_nounwind,
|
||||
rustc_allow_const_fn_ptr,
|
||||
rustc_args_required_const,
|
||||
rustc_attrs,
|
||||
rustc_builtin_macro,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue