]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/check_consts/mod.rs
a5b711e75a603e01535d1bde8c7d74bfe312be10
[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::{self, def_id::DefId};
8 use rustc::mir;
9 use rustc::ty::{self, TyCtxt};
10
11 use std::fmt;
12
13 pub use self::qualifs::Qualif;
14
15 pub mod ops;
16 pub mod qualifs;
17 mod resolver;
18 pub mod validation;
19
20 /// Information about the item currently being const-checked, as well as a reference to the global
21 /// context.
22 pub struct Item<'mir, 'tcx> {
23     pub body: &'mir mir::Body<'tcx>,
24     pub tcx: TyCtxt<'tcx>,
25     pub def_id: DefId,
26     pub param_env: ty::ParamEnv<'tcx>,
27     pub const_kind: Option<ConstKind>,
28 }
29
30 impl Item<'mir, 'tcx> {
31     pub fn new(
32         tcx: TyCtxt<'tcx>,
33         def_id: DefId,
34         body: &'mir mir::Body<'tcx>,
35     ) -> Self {
36         let param_env = tcx.param_env(def_id);
37         let const_kind = ConstKind::for_item(tcx, def_id);
38
39         Item {
40             body,
41             tcx,
42             def_id,
43             param_env,
44             const_kind,
45         }
46     }
47
48     /// Returns the kind of const context this `Item` represents (`const`, `static`, etc.).
49     ///
50     /// Panics if this `Item` is not const.
51     pub fn const_kind(&self) -> ConstKind {
52         self.const_kind.expect("`const_kind` must not be called on a non-const fn")
53     }
54 }
55
56 /// The kinds of items which require compile-time evaluation.
57 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
58 pub enum ConstKind {
59     /// A `static` item.
60     Static,
61     /// A `static mut` item.
62     StaticMut,
63     /// A `const fn` item.
64     ConstFn,
65     /// A `const` item or an anonymous constant (e.g. in array lengths).
66     Const,
67 }
68
69 impl ConstKind {
70     /// Returns the validation mode for the item with the given `DefId`, or `None` if this item
71     /// does not require validation (e.g. a non-const `fn`).
72     pub fn for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Self> {
73         use hir::BodyOwnerKind as HirKind;
74
75         let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
76
77         let mode = match tcx.hir().body_owner_kind(hir_id) {
78             HirKind::Closure => return None,
79
80             HirKind::Fn if tcx.is_const_fn(def_id) => ConstKind::ConstFn,
81             HirKind::Fn => return None,
82
83             HirKind::Const => ConstKind::Const,
84
85             HirKind::Static(hir::Mutability::Immutable) => ConstKind::Static,
86             HirKind::Static(hir::Mutability::Mutable) => ConstKind::StaticMut,
87         };
88
89         Some(mode)
90     }
91
92     pub fn is_static(self) -> bool {
93         match self {
94             ConstKind::Static | ConstKind::StaticMut => true,
95             ConstKind::ConstFn | ConstKind::Const => false,
96         }
97     }
98
99     /// Returns `true` if the value returned by this item must be `Sync`.
100     ///
101     /// This returns false for `StaticMut` since all accesses to one are `unsafe` anyway.
102     pub fn requires_sync(self) -> bool {
103         match self {
104             ConstKind::Static => true,
105             ConstKind::ConstFn | ConstKind::Const |  ConstKind::StaticMut => false,
106         }
107     }
108 }
109
110 impl fmt::Display for ConstKind {
111     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112         match *self {
113             ConstKind::Const => write!(f, "constant"),
114             ConstKind::Static | ConstKind::StaticMut => write!(f, "static"),
115             ConstKind::ConstFn => write!(f, "constant function"),
116         }
117     }
118 }
119
120 /// Returns `true` if this `DefId` points to one of the official `panic` lang items.
121 pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
122     Some(def_id) == tcx.lang_items().panic_fn() ||
123     Some(def_id) == tcx.lang_items().begin_panic_fn()
124 }