]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/check_consts/mod.rs
Use `Body` everywhere
[rust.git] / src / librustc_mir / transform / check_consts / mod.rs
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
7 use rustc_hir as hir;
8 use rustc_hir::def_id::DefId;
9 use rustc_middle::mir;
10 use rustc_middle::ty::{self, TyCtxt};
11
12 use std::fmt;
13
14 pub use self::qualifs::Qualif;
15
16 mod ops;
17 pub mod qualifs;
18 mod resolver;
19 pub mod validation;
20
21 /// Information about the item currently being const-checked, as well as a reference to the global
22 /// context.
23 pub struct Item<'mir, 'tcx> {
24     pub body: &'mir mir::Body<'tcx>,
25     pub tcx: TyCtxt<'tcx>,
26     pub def_id: DefId,
27     pub param_env: ty::ParamEnv<'tcx>,
28     pub const_kind: Option<ConstKind>,
29 }
30
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);
35
36         Item { body, tcx, def_id, param_env, const_kind }
37     }
38
39     /// Returns the kind of const context this `Item` represents (`const`, `static`, etc.).
40     ///
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")
44     }
45 }
46
47 /// The kinds of items which require compile-time evaluation.
48 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
49 pub enum ConstKind {
50     /// A `static` item.
51     Static,
52     /// A `static mut` item.
53     StaticMut,
54     /// A `const fn` item.
55     ConstFn,
56     /// A `const` item or an anonymous constant (e.g. in array lengths).
57     Const,
58 }
59
60 impl ConstKind {
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;
65
66         let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
67
68         let mode = match tcx.hir().body_owner_kind(hir_id) {
69             HirKind::Closure => return None,
70
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,
78
79             HirKind::Const => ConstKind::Const,
80
81             HirKind::Static(hir::Mutability::Not) => ConstKind::Static,
82             HirKind::Static(hir::Mutability::Mut) => ConstKind::StaticMut,
83         };
84
85         Some(mode)
86     }
87
88     pub fn is_static(self) -> bool {
89         match self {
90             ConstKind::Static | ConstKind::StaticMut => true,
91             ConstKind::ConstFn | ConstKind::Const => false,
92         }
93     }
94 }
95
96 impl fmt::Display for ConstKind {
97     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98         match *self {
99             ConstKind::Const => write!(f, "constant"),
100             ConstKind::Static | ConstKind::StaticMut => write!(f, "static"),
101             ConstKind::ConstFn => write!(f, "constant function"),
102         }
103     }
104 }
105
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()
109 }