]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir/src/const_eval/fn_queries.rs
Auto merge of #86335 - CDirkx:ipv4-in-ipv6, r=dtolnay
[rust.git] / compiler / rustc_mir / src / const_eval / fn_queries.rs
1 use rustc_hir as hir;
2 use rustc_hir::def_id::{DefId, LocalDefId};
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` counts as const fn in your current crate, considering all active
10 /// feature gates
11 pub fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
12     tcx.is_const_fn_raw(def_id)
13         && match is_unstable_const_fn(tcx, def_id) {
14             Some(feature_name) => {
15                 // has a `rustc_const_unstable` attribute, check whether the user enabled the
16                 // corresponding feature gate.
17                 tcx.features().declared_lib_features.iter().any(|&(sym, _)| sym == feature_name)
18             }
19             // functions without const stability are either stable user written
20             // const fn or the user is using feature gates and we thus don't
21             // care what they do
22             None => true,
23         }
24 }
25
26 /// Whether the `def_id` is an unstable const fn and what feature gate is necessary to enable it
27 pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
28     if tcx.is_const_fn_raw(def_id) {
29         let const_stab = tcx.lookup_const_stability(def_id)?;
30         if const_stab.level.is_unstable() { Some(const_stab.feature) } else { None }
31     } else {
32         None
33     }
34 }
35
36 pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, hir_id: hir::HirId) -> bool {
37     let parent_id = tcx.hir().get_parent_did(hir_id);
38     if !parent_id.is_top_level_module() { is_const_impl_raw(tcx, parent_id) } else { false }
39 }
40
41 /// Checks whether the function has a `const` modifier or, in case it is an intrinsic, whether
42 /// said intrinsic has a `rustc_const_{un,}stable` attribute.
43 fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
44     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
45
46     let node = tcx.hir().get(hir_id);
47
48     if let hir::Node::ForeignItem(hir::ForeignItem { kind: hir::ForeignItemKind::Fn(..), .. }) =
49         node
50     {
51         // Intrinsics use `rustc_const_{un,}stable` attributes to indicate constness. All other
52         // foreign items cannot be evaluated at compile-time.
53         if let Abi::RustIntrinsic | Abi::PlatformIntrinsic = tcx.hir().get_foreign_abi(hir_id) {
54             tcx.lookup_const_stability(def_id).is_some()
55         } else {
56             false
57         }
58     } else if let Some(fn_like) = FnLikeNode::from_node(node) {
59         if fn_like.constness() == hir::Constness::Const {
60             return true;
61         }
62
63         // If the function itself is not annotated with `const`, it may still be a `const fn`
64         // if it resides in a const trait impl.
65         is_parent_const_impl_raw(tcx, hir_id)
66     } else if let hir::Node::Ctor(_) = node {
67         true
68     } else {
69         false
70     }
71 }
72
73 /// Checks whether the given item is an `impl` that has a `const` modifier.
74 fn is_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
75     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
76     let node = tcx.hir().get(hir_id);
77     matches!(
78         node,
79         hir::Node::Item(hir::Item {
80             kind: hir::ItemKind::Impl(hir::Impl { constness: hir::Constness::Const, .. }),
81             ..
82         })
83     )
84 }
85
86 fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
87     is_const_fn(tcx, def_id)
88         && match tcx.lookup_const_stability(def_id) {
89             Some(stab) => {
90                 if cfg!(debug_assertions) && stab.promotable {
91                     let sig = tcx.fn_sig(def_id);
92                     assert_eq!(
93                         sig.unsafety(),
94                         hir::Unsafety::Normal,
95                         "don't mark const unsafe fns as promotable",
96                         // https://github.com/rust-lang/rust/pull/53851#issuecomment-418760682
97                     );
98                 }
99                 stab.promotable
100             }
101             None => false,
102         }
103 }
104
105 pub fn provide(providers: &mut Providers) {
106     *providers = Providers {
107         is_const_fn_raw,
108         is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, def_id.expect_local()),
109         is_promotable_const_fn,
110         ..*providers
111     };
112 }