rustc_const_eval/const_eval/
fn_queries.rs1use rustc_hir::def_id::{DefId, LocalDefId};
2use rustc_hir::{
3 Constness, ExprKind, ForeignItemKind, ImplItem, ImplItemImplKind, ImplItemKind, Item, ItemKind,
4 Node, TraitItem, TraitItemKind, VariantData,
5};
6use rustc_middle::query::Providers;
7use rustc_middle::ty::TyCtxt;
8
9fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Constness {
11 let node = tcx.hir_node_by_def_id(def_id);
12
13 match node {
14 Node::Ctor(VariantData::Tuple(..)) => Constness::Const,
15 Node::ForeignItem(item) if let ForeignItemKind::Fn(..) = item.kind => {
16 Constness::NotConst
18 }
19 Node::Expr(e) if let ExprKind::Closure(c) = e.kind => c.constness,
20 Node::Item(i) if let ItemKind::Impl(impl_) = i.kind => impl_.constness,
22 Node::Item(Item { kind: ItemKind::Fn { sig, .. }, .. }) => sig.header.constness,
23 Node::ImplItem(ImplItem {
24 impl_kind: ImplItemImplKind::Trait { .. },
25 kind: ImplItemKind::Fn(..),
26 ..
27 }) => tcx.impl_trait_header(tcx.local_parent(def_id)).constness,
28 Node::ImplItem(ImplItem {
29 impl_kind: ImplItemImplKind::Inherent { .. },
30 kind: ImplItemKind::Fn(sig, _),
31 ..
32 }) => {
33 match sig.header.constness {
34 Constness::Const => Constness::Const,
35 Constness::NotConst => tcx.constness(tcx.local_parent(def_id)),
37 }
38 }
39 Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. }) => tcx.trait_def(tcx.local_parent(def_id)).constness,
40 _ => {
41 tcx.dcx().span_bug(
42 tcx.def_span(def_id),
43 format!("should not be requesting the constness of items that can't be const: {node:#?}: {:?}", tcx.def_kind(def_id))
44 )
45 }
46 }
47}
48
49fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
50 tcx.is_const_fn(def_id)
51 && match tcx.lookup_const_stability(def_id) {
52 Some(stab) => {
53 if cfg!(debug_assertions) && stab.promotable {
54 let sig = tcx.fn_sig(def_id);
55 assert!(
56 sig.skip_binder().safety().is_safe(),
57 "don't mark const unsafe fns as promotable",
58 );
60 }
61 stab.promotable
62 }
63 None => false,
64 }
65}
66
67pub fn provide(providers: &mut Providers) {
68 *providers = Providers { constness, is_promotable_const_fn, ..*providers };
69}