mirror of
https://github.com/rust-lang/rust.git
synced 2025-12-01 19:38:04 +00:00
Fundamentally, we have *three* disjoint categories of functions: 1. const-stable functions 2. private/unstable functions that are meant to be callable from const-stable functions 3. functions that can make use of unstable const features This PR implements the following system: - `#[rustc_const_stable]` puts functions in the first category. It may only be applied to `#[stable]` functions. - `#[rustc_const_unstable]` by default puts functions in the third category. The new attribute `#[rustc_const_stable_indirect]` can be added to such a function to move it into the second category. - `const fn` without a const stability marker are in the second category if they are still unstable. They automatically inherit the feature gate for regular calls, it can now also be used for const-calls. Also, several holes in recursive const stability checking are being closed. There's still one potential hole that is hard to avoid, which is when MIR building automatically inserts calls to a particular function in stable functions -- which happens in the panic machinery. Those need to *not* be `rustc_const_unstable` (or manually get a `rustc_const_stable_indirect`) to be sure they follow recursive const stability. But that's a fairly rare and special case so IMO it's fine. The net effect of this is that a `#[unstable]` or unmarked function can be constified simply by marking it as `const fn`, and it will then be const-callable from stable `const fn` and subject to recursive const stability requirements. If it is publicly reachable (which implies it cannot be unmarked), it will be const-unstable under the same feature gate. Only if the function ever becomes `#[stable]` does it need a `#[rustc_const_unstable]` or `#[rustc_const_stable]` marker to decide if this should also imply const-stability. Adding `#[rustc_const_unstable]` is only needed for (a) functions that need to use unstable const lang features (including intrinsics), or (b) `#[stable]` functions that are not yet intended to be const-stable. Adding `#[rustc_const_stable]` is only needed for functions that are actually meant to be directly callable from stable const code. `#[rustc_const_stable_indirect]` is used to mark intrinsics as const-callable and for `#[rustc_const_unstable]` functions that are actually called from other, exposed-on-stable `const fn`. No other attributes are required.
78 lines
3.2 KiB
Rust
78 lines
3.2 KiB
Rust
use rustc_hir as hir;
|
|
use rustc_hir::def::DefKind;
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
|
use rustc_middle::query::Providers;
|
|
use rustc_middle::ty::TyCtxt;
|
|
|
|
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
|
|
let parent_id = tcx.local_parent(def_id);
|
|
matches!(tcx.def_kind(parent_id), DefKind::Impl { .. })
|
|
&& tcx.constness(parent_id) == hir::Constness::Const
|
|
}
|
|
|
|
/// Checks whether an item is considered to be `const`. If it is a constructor, anonymous const,
|
|
/// const block, const item or associated const, it is const. If it is a trait impl/function,
|
|
/// return if it has a `const` modifier. If it is an intrinsic, report whether said intrinsic
|
|
/// has a `rustc_const_{un,}stable` attribute. Otherwise, return `Constness::NotConst`.
|
|
fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
|
|
let node = tcx.hir_node_by_def_id(def_id);
|
|
|
|
match node {
|
|
hir::Node::Ctor(_)
|
|
| hir::Node::AnonConst(_)
|
|
| hir::Node::ConstBlock(_)
|
|
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. }) => {
|
|
hir::Constness::Const
|
|
}
|
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
|
|
hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
|
|
// Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
|
|
// foreign items cannot be evaluated at compile-time.
|
|
let is_const = if tcx.intrinsic(def_id).is_some() {
|
|
tcx.lookup_const_stability(def_id).is_some()
|
|
} else {
|
|
false
|
|
};
|
|
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
|
|
}
|
|
hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness,
|
|
_ => {
|
|
if let Some(fn_kind) = node.fn_kind() {
|
|
if fn_kind.constness() == hir::Constness::Const {
|
|
return hir::Constness::Const;
|
|
}
|
|
|
|
// If the function itself is not annotated with `const`, it may still be a `const fn`
|
|
// if it resides in a const trait impl.
|
|
let is_const = is_parent_const_impl_raw(tcx, def_id);
|
|
if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
|
|
} else {
|
|
hir::Constness::NotConst
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
|
tcx.is_const_fn(def_id)
|
|
&& match tcx.lookup_const_stability(def_id) {
|
|
Some(stab) => {
|
|
if cfg!(debug_assertions) && stab.promotable {
|
|
let sig = tcx.fn_sig(def_id);
|
|
assert_eq!(
|
|
sig.skip_binder().safety(),
|
|
hir::Safety::Safe,
|
|
"don't mark const unsafe fns as promotable",
|
|
// https://github.com/rust-lang/rust/pull/53851#issuecomment-418760682
|
|
);
|
|
}
|
|
stab.promotable
|
|
}
|
|
None => false,
|
|
}
|
|
}
|
|
|
|
pub fn provide(providers: &mut Providers) {
|
|
*providers = Providers { constness, is_promotable_const_fn, ..*providers };
|
|
}
|