rustc_const_eval/check_consts/
mod.rs1use rustc_errors::DiagCtxtHandle;
8use rustc_hir::def_id::{DefId, LocalDefId};
9use rustc_middle::ty::{self, PolyFnSig, TyCtxt};
10use rustc_middle::{bug, mir};
11use rustc_span::Symbol;
12use {rustc_attr_data_structures as attrs, rustc_hir as hir};
13
14pub use self::qualifs::Qualif;
15
16pub mod check;
17mod ops;
18pub mod post_drop_elaboration;
19pub mod qualifs;
20mod resolver;
21
22pub struct ConstCx<'mir, 'tcx> {
25    pub body: &'mir mir::Body<'tcx>,
26    pub tcx: TyCtxt<'tcx>,
27    pub typing_env: ty::TypingEnv<'tcx>,
28    pub const_kind: Option<hir::ConstContext>,
29}
30
31impl<'mir, 'tcx> ConstCx<'mir, 'tcx> {
32    pub fn new(tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>) -> Self {
33        let typing_env = body.typing_env(tcx);
34        let const_kind = tcx.hir_body_const_context(body.source.def_id().expect_local());
35        ConstCx { body, tcx, typing_env, const_kind }
36    }
37
38    pub(crate) fn dcx(&self) -> DiagCtxtHandle<'tcx> {
39        self.tcx.dcx()
40    }
41
42    pub fn def_id(&self) -> LocalDefId {
43        self.body.source.def_id().expect_local()
44    }
45
46    pub fn const_kind(&self) -> hir::ConstContext {
50        self.const_kind.expect("`const_kind` must not be called on a non-const fn")
51    }
52
53    pub fn enforce_recursive_const_stability(&self) -> bool {
54        self.const_kind == Some(hir::ConstContext::ConstFn)
57            && (self.tcx.features().staged_api()
58                || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked)
59            && is_fn_or_trait_safe_to_expose_on_stable(self.tcx, self.def_id().to_def_id())
60    }
61
62    fn is_async(&self) -> bool {
63        self.tcx.asyncness(self.def_id()).is_async()
64    }
65
66    pub fn fn_sig(&self) -> PolyFnSig<'tcx> {
67        let did = self.def_id().to_def_id();
68        if self.tcx.is_closure_like(did) {
69            let ty = self.tcx.type_of(did).instantiate_identity();
70            let ty::Closure(_, args) = ty.kind() else { bug!("type_of closure not ty::Closure") };
71            args.as_closure().sig()
72        } else {
73            self.tcx.fn_sig(did).instantiate_identity()
74        }
75    }
76}
77
78pub fn rustc_allow_const_fn_unstable(
79    tcx: TyCtxt<'_>,
80    def_id: LocalDefId,
81    feature_gate: Symbol,
82) -> bool {
83    let attrs = tcx.hir_attrs(tcx.local_def_id_to_hir_id(def_id));
84
85    attrs::find_attr!(attrs, attrs::AttributeKind::AllowConstFnUnstable(syms, _) if syms.contains(&feature_gate))
86}
87
88pub fn is_fn_or_trait_safe_to_expose_on_stable(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
96    if tcx.is_const_default_method(def_id) {
98        return is_fn_or_trait_safe_to_expose_on_stable(tcx, tcx.parent(def_id));
99    }
100
101    match tcx.lookup_const_stability(def_id) {
102        None => {
103            def_id.is_local() && tcx.features().staged_api()
107        }
108        Some(stab) => {
109            stab.is_const_stable() || stab.const_stable_indirect
112        }
113    }
114}