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