]> git.proxmox.com Git - rustc.git/blame - src/librustc_mir/transform/check_consts/mod.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_mir / transform / check_consts / mod.rs
CommitLineData
e74abb32
XL
1//! Check the bodies of `const`s, `static`s and `const fn`s for illegal operations.
2//!
3//! This module will eventually replace the parts of `qualify_consts.rs` that check whether a local
4//! has interior mutability or needs to be dropped, as well as the visitor that emits errors when
5//! it finds operations that are invalid in a certain context.
6
dfeec247 7use rustc_hir as hir;
f9f354fc 8use rustc_hir::def_id::{DefId, LocalDefId};
ba9703b0
XL
9use rustc_middle::mir;
10use rustc_middle::ty::{self, TyCtxt};
e74abb32 11
e74abb32
XL
12pub use self::qualifs::Qualif;
13
ba9703b0 14mod ops;
f035d41b 15pub mod post_drop_elaboration;
e74abb32
XL
16pub mod qualifs;
17mod resolver;
18pub mod validation;
19
20/// Information about the item currently being const-checked, as well as a reference to the global
21/// context.
f9f354fc
XL
22pub struct ConstCx<'mir, 'tcx> {
23 pub body: &'mir mir::Body<'tcx>,
e74abb32 24 pub tcx: TyCtxt<'tcx>,
f035d41b 25 pub def_id: LocalDefId,
e74abb32 26 pub param_env: ty::ParamEnv<'tcx>,
f9f354fc 27 pub const_kind: Option<hir::ConstContext>,
e74abb32
XL
28}
29
f9f354fc
XL
30impl ConstCx<'mir, 'tcx> {
31 pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'mir mir::Body<'tcx>) -> Self {
e74abb32 32 let param_env = tcx.param_env(def_id);
f9f354fc
XL
33 Self::new_with_param_env(tcx, def_id, body, param_env)
34 }
e74abb32 35
f9f354fc
XL
36 pub fn new_with_param_env(
37 tcx: TyCtxt<'tcx>,
38 def_id: LocalDefId,
39 body: &'mir mir::Body<'tcx>,
40 param_env: ty::ParamEnv<'tcx>,
41 ) -> Self {
42 let const_kind = tcx.hir().body_const_context(def_id);
f035d41b 43 ConstCx { body, tcx, def_id: def_id, param_env, const_kind }
e74abb32
XL
44 }
45
46 /// Returns the kind of const context this `Item` represents (`const`, `static`, etc.).
47 ///
48 /// Panics if this `Item` is not const.
f9f354fc 49 pub fn const_kind(&self) -> hir::ConstContext {
e74abb32
XL
50 self.const_kind.expect("`const_kind` must not be called on a non-const fn")
51 }
52}
53
e74abb32
XL
54/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
55pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
dfeec247 56 Some(def_id) == tcx.lang_items().panic_fn() || Some(def_id) == tcx.lang_items().begin_panic_fn()
e74abb32 57}