1 //! Check the bodies of `const`s, `static`s and `const fn`s for illegal operations.
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.
8 use rustc_hir::def_id::DefId;
10 use rustc_middle::ty::{self, TyCtxt};
14 pub use self::qualifs::Qualif;
21 /// Information about the item currently being const-checked, as well as a reference to the global
23 pub struct Item<'mir, 'tcx> {
24 pub body: &'mir mir::Body<'tcx>,
25 pub tcx: TyCtxt<'tcx>,
27 pub param_env: ty::ParamEnv<'tcx>,
28 pub const_kind: Option<ConstKind>,
31 impl Item<'mir, 'tcx> {
32 pub fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'mir mir::Body<'tcx>) -> Self {
33 let param_env = tcx.param_env(def_id);
34 let const_kind = ConstKind::for_item(tcx, def_id);
36 Item { body, tcx, def_id, param_env, const_kind }
39 /// Returns the kind of const context this `Item` represents (`const`, `static`, etc.).
41 /// Panics if this `Item` is not const.
42 pub fn const_kind(&self) -> ConstKind {
43 self.const_kind.expect("`const_kind` must not be called on a non-const fn")
47 /// The kinds of items which require compile-time evaluation.
48 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
52 /// A `static mut` item.
54 /// A `const fn` item.
56 /// A `const` item or an anonymous constant (e.g. in array lengths).
61 /// Returns the validation mode for the item with the given `DefId`, or `None` if this item
62 /// does not require validation (e.g. a non-const `fn`).
63 pub fn for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Self> {
64 use hir::BodyOwnerKind as HirKind;
66 let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
68 let mode = match tcx.hir().body_owner_kind(hir_id) {
69 HirKind::Closure => return None,
71 // Note: this is deliberately checking for `is_const_fn_raw`, as the `is_const_fn`
72 // checks take into account the `rustc_const_unstable` attribute combined with enabled
73 // feature gates. Otherwise, const qualification would _not check_ whether this
74 // function body follows the `const fn` rules, as an unstable `const fn` would
75 // be considered "not const". More details are available in issue #67053.
76 HirKind::Fn if tcx.is_const_fn_raw(def_id) => ConstKind::ConstFn,
77 HirKind::Fn => return None,
79 HirKind::Const => ConstKind::Const,
81 HirKind::Static(hir::Mutability::Not) => ConstKind::Static,
82 HirKind::Static(hir::Mutability::Mut) => ConstKind::StaticMut,
88 pub fn is_static(self) -> bool {
90 ConstKind::Static | ConstKind::StaticMut => true,
91 ConstKind::ConstFn | ConstKind::Const => false,
96 impl fmt::Display for ConstKind {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 ConstKind::Const => write!(f, "constant"),
100 ConstKind::Static | ConstKind::StaticMut => write!(f, "static"),
101 ConstKind::ConstFn => write!(f, "constant function"),
106 /// Returns `true` if this `DefId` points to one of the official `panic` lang items.
107 pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
108 Some(def_id) == tcx.lang_items().panic_fn() || Some(def_id) == tcx.lang_items().begin_panic_fn()