2019-09-25 19:30:25 +00:00
|
|
|
//! Check the bodies of `const`s, `static`s and `const fn`s for illegal operations.
|
|
|
|
//!
|
|
|
|
//! This module will eventually replace the parts of `qualify_consts.rs` that check whether a local
|
|
|
|
//! has interior mutability or needs to be dropped, as well as the visitor that emits errors when
|
|
|
|
//! it finds operations that are invalid in a certain context.
|
|
|
|
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::DefId;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::mir;
|
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2019-09-17 23:25:40 +00:00
|
|
|
|
2019-10-22 21:30:31 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2019-09-17 23:25:40 +00:00
|
|
|
pub use self::qualifs::Qualif;
|
|
|
|
|
2020-04-18 09:15:23 +00:00
|
|
|
mod ops;
|
2019-10-21 18:18:12 +00:00
|
|
|
pub mod qualifs;
|
2019-09-20 16:00:18 +00:00
|
|
|
mod resolver;
|
2019-09-17 23:25:40 +00:00
|
|
|
pub mod validation;
|
|
|
|
|
2019-10-22 21:30:31 +00:00
|
|
|
/// Information about the item currently being const-checked, as well as a reference to the global
|
2019-09-17 23:25:40 +00:00
|
|
|
/// context.
|
|
|
|
pub struct Item<'mir, 'tcx> {
|
2020-04-12 17:31:00 +00:00
|
|
|
pub body: &'mir mir::Body<'tcx>,
|
2019-10-22 21:54:10 +00:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
|
|
|
pub def_id: DefId,
|
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
|
|
|
pub const_kind: Option<ConstKind>,
|
2019-09-17 23:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Item<'mir, 'tcx> {
|
2020-04-12 17:31:00 +00:00
|
|
|
pub fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'mir mir::Body<'tcx>) -> Self {
|
2019-09-17 23:25:40 +00:00
|
|
|
let param_env = tcx.param_env(def_id);
|
2019-10-22 21:30:31 +00:00
|
|
|
let const_kind = ConstKind::for_item(tcx, def_id);
|
2019-09-17 23:25:40 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
Item { body, tcx, def_id, param_env, const_kind }
|
2019-10-21 18:18:12 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 21:30:31 +00:00
|
|
|
/// Returns the kind of const context this `Item` represents (`const`, `static`, etc.).
|
|
|
|
///
|
|
|
|
/// Panics if this `Item` is not const.
|
|
|
|
pub fn const_kind(&self) -> ConstKind {
|
|
|
|
self.const_kind.expect("`const_kind` must not be called on a non-const fn")
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 18:18:12 +00:00
|
|
|
|
2019-10-22 21:30:31 +00:00
|
|
|
/// The kinds of items which require compile-time evaluation.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum ConstKind {
|
|
|
|
/// A `static` item.
|
|
|
|
Static,
|
|
|
|
/// A `static mut` item.
|
|
|
|
StaticMut,
|
|
|
|
/// A `const fn` item.
|
|
|
|
ConstFn,
|
|
|
|
/// A `const` item or an anonymous constant (e.g. in array lengths).
|
|
|
|
Const,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConstKind {
|
|
|
|
/// Returns the validation mode for the item with the given `DefId`, or `None` if this item
|
|
|
|
/// does not require validation (e.g. a non-const `fn`).
|
|
|
|
pub fn for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<Self> {
|
|
|
|
use hir::BodyOwnerKind as HirKind;
|
|
|
|
|
|
|
|
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
|
|
|
|
|
|
|
|
let mode = match tcx.hir().body_owner_kind(hir_id) {
|
|
|
|
HirKind::Closure => return None,
|
|
|
|
|
2019-12-05 13:59:56 +00:00
|
|
|
// Note: this is deliberately checking for `is_const_fn_raw`, as the `is_const_fn`
|
|
|
|
// checks take into account the `rustc_const_unstable` attribute combined with enabled
|
2019-12-05 16:41:25 +00:00
|
|
|
// feature gates. Otherwise, const qualification would _not check_ whether this
|
|
|
|
// function body follows the `const fn` rules, as an unstable `const fn` would
|
|
|
|
// be considered "not const". More details are available in issue #67053.
|
2019-12-05 13:59:56 +00:00
|
|
|
HirKind::Fn if tcx.is_const_fn_raw(def_id) => ConstKind::ConstFn,
|
2019-10-22 21:30:31 +00:00
|
|
|
HirKind::Fn => return None,
|
|
|
|
|
|
|
|
HirKind::Const => ConstKind::Const,
|
|
|
|
|
2019-12-16 16:28:40 +00:00
|
|
|
HirKind::Static(hir::Mutability::Not) => ConstKind::Static,
|
|
|
|
HirKind::Static(hir::Mutability::Mut) => ConstKind::StaticMut,
|
2019-10-22 21:30:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Some(mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_static(self) -> bool {
|
|
|
|
match self {
|
|
|
|
ConstKind::Static | ConstKind::StaticMut => true,
|
|
|
|
ConstKind::ConstFn | ConstKind::Const => false,
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 23:25:40 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 21:30:31 +00:00
|
|
|
impl fmt::Display for ConstKind {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
ConstKind::Const => write!(f, "constant"),
|
|
|
|
ConstKind::Static | ConstKind::StaticMut => write!(f, "static"),
|
|
|
|
ConstKind::ConstFn => write!(f, "constant function"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 23:25:40 +00:00
|
|
|
|
2019-10-22 22:00:51 +00:00
|
|
|
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
|
|
|
|
pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
|
2019-12-22 22:42:04 +00:00
|
|
|
Some(def_id) == tcx.lang_items().panic_fn() || Some(def_id) == tcx.lang_items().begin_panic_fn()
|
2019-09-17 23:25:40 +00:00
|
|
|
}
|