1 use rustc
::hir
::map
::blocks
::FnLikeNode
;
2 use rustc
::ty
::query
::Providers
;
5 use rustc_hir
::def_id
::DefId
;
6 use rustc_span
::symbol
::Symbol
;
7 use rustc_target
::spec
::abi
::Abi
;
10 /// Whether the `def_id` counts as const fn in your current crate, considering all active
12 pub fn is_const_fn(tcx
: TyCtxt
<'_
>, def_id
: DefId
) -> bool
{
13 tcx
.is_const_fn_raw(def_id
)
14 && match is_unstable_const_fn(tcx
, def_id
) {
15 Some(feature_name
) => {
16 // has a `rustc_const_unstable` attribute, check whether the user enabled the
17 // corresponding feature gate.
18 tcx
.features().declared_lib_features
.iter().any(|&(sym
, _
)| sym
== feature_name
)
20 // functions without const stability are either stable user written
21 // const fn or the user is using feature gates and we thus don't
27 /// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
28 pub fn is_unstable_const_fn(tcx
: TyCtxt
<'_
>, def_id
: DefId
) -> Option
<Symbol
> {
29 if tcx
.is_const_fn_raw(def_id
) {
30 let const_stab
= tcx
.lookup_const_stability(def_id
)?
;
31 if const_stab
.level
.is_unstable() { Some(const_stab.feature) }
else { None }
37 /// Returns `true` if this function must conform to `min_const_fn`
38 pub fn is_min_const_fn(tcx
: TyCtxt
<'_
>, def_id
: DefId
) -> bool
{
39 // Bail out if the signature doesn't contain `const`
40 if !tcx
.is_const_fn_raw(def_id
) {
44 if tcx
.features().staged_api
{
45 // In order for a libstd function to be considered min_const_fn
46 // it needs to be stable and have no `rustc_const_unstable` attribute.
47 match tcx
.lookup_const_stability(def_id
) {
48 // `rustc_const_unstable` functions don't need to conform.
49 Some(&attr
::ConstStability { ref level, .. }
) if level
.is_unstable() => false,
51 if let Some(stab
) = tcx
.lookup_stability(def_id
) {
52 if stab
.level
.is_stable() {
55 "stable const functions must have either `rustc_const_stable` or \
56 `rustc_const_unstable` attribute",
58 // While we errored above, because we don't know if we need to conform, we
59 // err on the "safe" side and require min_const_fn.
62 // Unstable functions need not conform to min_const_fn.
66 // Internal functions are forced to conform to min_const_fn.
67 // Annotate the internal function with a const stability attribute if
68 // you need to use unstable features.
69 // Note: this is an arbitrary choice that does not affect stability or const
70 // safety or anything, it just changes whether we need to annotate some
71 // internal functions with `rustc_const_stable` or with `rustc_const_unstable`
75 // Everything else needs to conform, because it would be callable from
76 // other `min_const_fn` functions.
80 // users enabling the `const_fn` feature gate can do what they want
81 !tcx
.features().const_fn
85 pub fn provide(providers
: &mut Providers
<'_
>) {
86 /// Const evaluability whitelist is here to check evaluability at the
87 /// top level beforehand.
88 fn is_const_intrinsic(tcx
: TyCtxt
<'_
>, def_id
: DefId
) -> Option
<bool
> {
89 if tcx
.is_closure(def_id
) {
93 match tcx
.fn_sig(def_id
).abi() {
94 Abi
::RustIntrinsic
| Abi
::PlatformIntrinsic
=> {
95 Some(tcx
.lookup_const_stability(def_id
).is_some())
101 /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
102 /// said intrinsic is on the whitelist for being const callable.
103 fn is_const_fn_raw(tcx
: TyCtxt
<'_
>, def_id
: DefId
) -> bool
{
106 .as_local_hir_id(def_id
)
107 .expect("Non-local call to local provider is_const_fn");
109 let node
= tcx
.hir().get(hir_id
);
111 if let Some(whitelisted
) = is_const_intrinsic(tcx
, def_id
) {
113 } else if let Some(fn_like
) = FnLikeNode
::from_node(node
) {
114 fn_like
.constness() == hir
::Constness
::Const
115 } else if let hir
::Node
::Ctor(_
) = node
{
122 fn is_promotable_const_fn(tcx
: TyCtxt
<'_
>, def_id
: DefId
) -> bool
{
123 is_const_fn(tcx
, def_id
)
124 && match tcx
.lookup_const_stability(def_id
) {
126 if cfg
!(debug_assertions
) && stab
.promotable
{
127 let sig
= tcx
.fn_sig(def_id
);
130 hir
::Unsafety
::Normal
,
131 "don't mark const unsafe fns as promotable",
132 // https://github.com/rust-lang/rust/pull/53851#issuecomment-418760682
141 fn const_fn_is_allowed_fn_ptr(tcx
: TyCtxt
<'_
>, def_id
: DefId
) -> bool
{
142 is_const_fn(tcx
, def_id
)
144 .lookup_const_stability(def_id
)
145 .map(|stab
| stab
.allow_const_fn_ptr
)
149 *providers
= Providers
{
151 is_promotable_const_fn
,
152 const_fn_is_allowed_fn_ptr
,