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