]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir/src/const_eval/fn_queries.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / compiler / rustc_mir / src / const_eval / fn_queries.rs
CommitLineData
74b04a01 1use rustc_attr as attr;
dfeec247 2use rustc_hir as hir;
74b04a01 3use rustc_hir::def_id::{DefId, LocalDefId};
ba9703b0
XL
4use rustc_middle::hir::map::blocks::FnLikeNode;
5use rustc_middle::ty::query::Providers;
6use rustc_middle::ty::TyCtxt;
dfeec247
XL
7use rustc_span::symbol::Symbol;
8use rustc_target::spec::abi::Abi;
dfeec247
XL
9
10/// Whether the `def_id` counts as const fn in your current crate, considering all active
11/// feature gates
12pub 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)
19 }
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
22 // care what they do
23 None => true,
24 }
25}
26
27/// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
28pub 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 }
32 } else {
33 None
34 }
35}
36
37/// Returns `true` if this function must conform to `min_const_fn`
38pub 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) {
41 return false;
42 }
43
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,
50 None => {
51 if let Some(stab) = tcx.lookup_stability(def_id) {
52 if stab.level.is_stable() {
1b1a35ee 53 tcx.sess.delay_span_bug(
dfeec247
XL
54 tcx.def_span(def_id),
55 "stable const functions must have either `rustc_const_stable` or \
56 `rustc_const_unstable` attribute",
57 );
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.
60 true
61 } else {
62 // Unstable functions need not conform to min_const_fn.
63 false
64 }
65 } else {
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`
72 true
73 }
74 }
75 // Everything else needs to conform, because it would be callable from
76 // other `min_const_fn` functions.
77 _ => true,
78 }
79 } else {
80 // users enabling the `const_fn` feature gate can do what they want
81 !tcx.features().const_fn
82 }
83}
84
74b04a01
XL
85pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
86 let parent_id = tcx.hir().get_parent_did(hir_id);
ba9703b0 87 if !parent_id.is_top_level_module() { is_const_impl_raw(tcx, parent_id) } else { false }
74b04a01 88}
dfeec247 89
74b04a01 90/// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
f035d41b 91/// said intrinsic has a `rustc_const_{un,}stable` attribute.
74b04a01 92fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
3dfed10e 93 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
74b04a01
XL
94
95 let node = tcx.hir().get(hir_id);
96
ba9703b0
XL
97 if let hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) =
98 node
99 {
100 // Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
101 // foreign items cannot be evaluated at compile-time.
102 if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = tcx.hir().get_foreign_abi(hir_id) {
103 tcx.lookup_const_stability(def_id).is_some()
104 } else {
105 false
106 }
74b04a01
XL
107 } else if let Some(fn_like) = FnLikeNode::from_node(node) {
108 if fn_like.constness() == hir::Constness::Const {
109 return true;
dfeec247 110 }
dfeec247 111
74b04a01
XL
112 // If the function itself is not annotated with `const`, it may still be a `const fn`
113 // if it resides in a const trait impl.
114 is_parent_const_impl_raw(tcx, hir_id)
115 } else if let hir::Node::Ctor(_) = node {
116 true
117 } else {
118 false
119 }
120}
dfeec247 121
74b04a01
XL
122/// Checks whether the given item is an `impl` that has a `const` modifier.
123fn is_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
124 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
125 let node = tcx.hir().get(hir_id);
126 matches!(
127 node,
128 hir::Node::Item(hir::Item {
5869c6ff 129 kind: hir::ItemKind::Impl(hir::Impl { constness: hir::Constness::Const, .. }),
74b04a01
XL
130 ..
131 })
132 )
133}
134
135fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
136 is_const_fn(tcx, def_id)
137 && match tcx.lookup_const_stability(def_id) {
138 Some(stab) => {
139 if cfg!(debug_assertions) && stab.promotable {
140 let sig = tcx.fn_sig(def_id);
141 assert_eq!(
142 sig.unsafety(),
143 hir::Unsafety::Normal,
144 "don't mark const unsafe fns as promotable",
145 // https://github.com/rust-lang/rust/pull/53851#issuecomment-418760682
146 );
dfeec247 147 }
74b04a01 148 stab.promotable
dfeec247 149 }
74b04a01
XL
150 None => false,
151 }
152}
dfeec247 153
f035d41b 154pub fn provide(providers: &mut Providers) {
dfeec247
XL
155 *providers = Providers {
156 is_const_fn_raw,
ba9703b0 157 is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, def_id.expect_local()),
dfeec247 158 is_promotable_const_fn,
dfeec247
XL
159 ..*providers
160 };
161}