2 use crate::hir::def_id::DefId;
3 use crate::hir::map::blocks::FnLikeNode;
4 use crate::ty::query::Providers;
6 use rustc_target::spec::abi::Abi;
8 use syntax_pos::symbol::Symbol;
10 impl<'tcx> TyCtxt<'tcx> {
11 /// Whether the `def_id` counts as const fn in your current crate, considering all active
13 pub fn is_const_fn(self, def_id: DefId) -> bool {
14 self.is_const_fn_raw(def_id)
15 && match self.is_unstable_const_fn(def_id) {
16 Some(feature_name) => {
17 // has a `rustc_const_unstable` attribute, check whether the user enabled the
18 // corresponding feature gate.
20 .declared_lib_features
22 .any(|&(sym, _)| sym == feature_name)
24 // functions without const stability are either stable user written
25 // const fn or the user is using feature gates and we thus don't
31 /// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
32 pub fn is_unstable_const_fn(self, def_id: DefId) -> Option<Symbol> {
33 if self.is_const_fn_raw(def_id) {
34 let const_stab = self.lookup_const_stability(def_id)?;
35 if const_stab.level.is_unstable() { Some(const_stab.feature) } else { None }
41 /// Returns `true` if this function must conform to `min_const_fn`
42 pub fn is_min_const_fn(self, def_id: DefId) -> bool {
43 // Bail out if the signature doesn't contain `const`
44 if !self.is_const_fn_raw(def_id) {
48 if self.features().staged_api {
49 // In order for a libstd function to be considered min_const_fn
50 // it needs to be stable and have no `rustc_const_unstable` attribute.
51 match self.lookup_const_stability(def_id) {
52 // `rustc_const_unstable` functions don't need to conform.
53 Some(&attr::ConstStability { ref level, .. }) if level.is_unstable() => false,
55 if let Some(stab) = self.lookup_stability(def_id) {
56 if stab.level.is_stable() {
58 self.def_span(def_id),
59 "stable const functions must have either `rustc_const_stable` or \
60 `rustc_const_unstable` attribute",
62 // While we errored above, because we don't know if we need to conform, we
63 // err on the "safe" side and require min_const_fn.
66 // Unstable functions need not conform to min_const_fn.
70 // Internal functions are forced to conform to min_const_fn.
71 // Annotate the internal function with a const stability attribute if
72 // you need to use unstable features.
73 // Note: this is an arbitrary choice that does not affect stability or const
74 // safety or anything, it just changes whether we need to annotate some
75 // internal functions with `rustc_const_stable` or with `rustc_const_unstable`
79 // Everything else needs to conform, because it would be callable from
80 // other `min_const_fn` functions.
84 // users enabling the `const_fn` feature gate can do what they want
85 !self.features().const_fn
90 pub fn provide(providers: &mut Providers<'_>) {
91 /// Const evaluability whitelist is here to check evaluability at the
92 /// top level beforehand.
93 fn is_const_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> Option<bool> {
94 match tcx.fn_sig(def_id).abi() {
95 Abi::RustIntrinsic | Abi::PlatformIntrinsic => {
96 Some(tcx.lookup_const_stability(def_id).is_some())
102 /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
103 /// said intrinsic is on the whitelist for being const callable.
104 fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
107 .as_local_hir_id(def_id)
108 .expect("Non-local call to local provider is_const_fn");
110 let node = tcx.hir().get(hir_id);
112 if let Some(whitelisted) = is_const_intrinsic(tcx, def_id) {
114 } else if let Some(fn_like) = FnLikeNode::from_node(node) {
115 fn_like.constness() == hir::Constness::Const
116 } else if let hir::Node::Ctor(_) = node {
123 fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
124 tcx.is_const_fn(def_id)
125 && match tcx.lookup_const_stability(def_id) {
127 if cfg!(debug_assertions) && stab.promotable {
128 let sig = tcx.fn_sig(def_id);
131 hir::Unsafety::Normal,
132 "don't mark const unsafe fns as promotable",
133 // https://github.com/rust-lang/rust/pull/53851#issuecomment-418760682
142 fn const_fn_is_allowed_fn_ptr(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
143 tcx.is_const_fn(def_id)
145 .lookup_const_stability(def_id)
146 .map(|stab| stab.allow_const_fn_ptr)
150 *providers = Providers {
152 is_promotable_const_fn,
153 const_fn_is_allowed_fn_ptr,