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