]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/const_eval/fn_queries.rs
Add and use stability helper methods
[rust.git] / compiler / rustc_const_eval / src / const_eval / fn_queries.rs
1 use rustc_hir as hir;
2 use rustc_hir::def::DefKind;
3 use rustc_hir::def_id::{DefId, LocalDefId};
4 use rustc_middle::ty::query::Providers;
5 use rustc_middle::ty::{DefIdTree, TyCtxt};
6 use rustc_span::symbol::Symbol;
7
8 /// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
9 pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
10     if tcx.is_const_fn_raw(def_id) {
11         let const_stab = tcx.lookup_const_stability(def_id)?;
12         if const_stab.is_const_unstable() { Some(const_stab.feature) } else { None }
13     } else {
14         None
15     }
16 }
17
18 pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
19     let parent_id = tcx.local_parent(def_id);
20     tcx.def_kind(parent_id) == DefKind::Impl
21         && tcx.impl_constness(parent_id) == hir::Constness::Const
22 }
23
24 /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
25 /// said intrinsic has a `rustc_const_{un,}stable` attribute.
26 fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
27     let def_id = def_id.expect_local();
28     let node = tcx.hir().get_by_def_id(def_id);
29
30     match node {
31         hir::Node::Ctor(_) => hir::Constness::Const,
32         hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.constness,
33         hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) => {
34             // Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
35             // foreign items cannot be evaluated at compile-time.
36             let is_const = if tcx.is_intrinsic(def_id) {
37                 tcx.lookup_const_stability(def_id).is_some()
38             } else {
39                 false
40             };
41             if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
42         }
43         _ => {
44             if let Some(fn_kind) = node.fn_kind() {
45                 if fn_kind.constness() == hir::Constness::Const {
46                     return hir::Constness::Const;
47                 }
48
49                 // If the function itself is not annotated with `const`, it may still be a `const fn`
50                 // if it resides in a const trait impl.
51                 let is_const = is_parent_const_impl_raw(tcx, def_id);
52                 if is_const { hir::Constness::Const } else { hir::Constness::NotConst }
53             } else {
54                 hir::Constness::NotConst
55             }
56         }
57     }
58 }
59
60 fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
61     tcx.is_const_fn(def_id)
62         && match tcx.lookup_const_stability(def_id) {
63             Some(stab) => {
64                 if cfg!(debug_assertions) && stab.promotable {
65                     let sig = tcx.fn_sig(def_id);
66                     assert_eq!(
67                         sig.unsafety(),
68                         hir::Unsafety::Normal,
69                         "don't mark const unsafe fns as promotable",
70                         // https://github.com/rust-lang/rust/pull/53851#issuecomment-418760682
71                     );
72                 }
73                 stab.promotable
74             }
75             None => false,
76         }
77 }
78
79 pub fn provide(providers: &mut Providers) {
80     *providers = Providers { impl_constness, is_promotable_const_fn, ..*providers };
81 }