2019-09-25 12:30:25 -07: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-09-02 14:35:02 -07:00
|
|
|
use rustc_attr as attr;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2020-04-12 13:45:41 +01:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::mir;
|
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2020-09-02 14:35:02 -07:00
|
|
|
use rustc_span::Symbol;
|
2019-09-17 16:25:40 -07:00
|
|
|
|
|
|
|
pub use self::qualifs::Qualif;
|
|
|
|
|
2020-04-18 11:15:23 +02:00
|
|
|
mod ops;
|
2020-05-03 11:18:26 -07:00
|
|
|
pub mod post_drop_elaboration;
|
2019-10-21 21:18:12 +03:00
|
|
|
pub mod qualifs;
|
2019-09-20 09:00:18 -07:00
|
|
|
mod resolver;
|
2019-09-17 16:25:40 -07:00
|
|
|
pub mod validation;
|
|
|
|
|
2019-10-22 14:30:31 -07:00
|
|
|
/// Information about the item currently being const-checked, as well as a reference to the global
|
2019-09-17 16:25:40 -07:00
|
|
|
/// context.
|
2020-03-23 14:02:58 +01:00
|
|
|
pub struct ConstCx<'mir, 'tcx> {
|
2020-04-12 10:31:00 -07:00
|
|
|
pub body: &'mir mir::Body<'tcx>,
|
2019-10-22 14:54:10 -07:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2020-07-04 14:02:41 +02:00
|
|
|
pub def_id: LocalDefId,
|
2019-10-22 14:54:10 -07:00
|
|
|
pub param_env: ty::ParamEnv<'tcx>,
|
2020-05-07 10:24:20 -07:00
|
|
|
pub const_kind: Option<hir::ConstContext>,
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
2020-03-23 14:02:58 +01:00
|
|
|
impl ConstCx<'mir, 'tcx> {
|
2020-04-12 13:45:41 +01:00
|
|
|
pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'mir mir::Body<'tcx>) -> Self {
|
2019-09-17 16:25:40 -07:00
|
|
|
let param_env = tcx.param_env(def_id);
|
2020-04-02 16:14:11 +02:00
|
|
|
Self::new_with_param_env(tcx, def_id, body, param_env)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_with_param_env(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-04-12 13:45:41 +01:00
|
|
|
def_id: LocalDefId,
|
2020-04-02 16:14:11 +02:00
|
|
|
body: &'mir mir::Body<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
) -> Self {
|
2020-05-07 10:24:20 -07:00
|
|
|
let const_kind = tcx.hir().body_const_context(def_id);
|
2020-07-04 14:02:41 +02:00
|
|
|
ConstCx { body, tcx, def_id: def_id, param_env, const_kind }
|
2019-10-21 21:18:12 +03:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:30:31 -07:00
|
|
|
/// Returns the kind of const context this `Item` represents (`const`, `static`, etc.).
|
|
|
|
///
|
|
|
|
/// Panics if this `Item` is not const.
|
2020-05-07 10:24:20 -07:00
|
|
|
pub fn const_kind(&self) -> hir::ConstContext {
|
2019-10-22 14:30:31 -07:00
|
|
|
self.const_kind.expect("`const_kind` must not be called on a non-const fn")
|
|
|
|
}
|
2020-09-17 11:05:51 -07:00
|
|
|
|
|
|
|
pub fn is_const_stable_const_fn(&self) -> bool {
|
|
|
|
self.const_kind == Some(hir::ConstContext::ConstFn)
|
|
|
|
&& self.tcx.features().staged_api
|
2020-09-18 09:17:13 -07:00
|
|
|
&& is_const_stable_const_fn(self.tcx, self.def_id.to_def_id())
|
2020-09-17 11:05:51 -07:00
|
|
|
}
|
2019-10-22 14:30:31 -07:00
|
|
|
}
|
2019-10-21 21:18:12 +03:00
|
|
|
|
2019-10-22 15:00:51 -07: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 17:42:04 -05:00
|
|
|
Some(def_id) == tcx.lang_items().panic_fn() || Some(def_id) == tcx.lang_items().begin_panic_fn()
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
2020-09-02 14:35:02 -07:00
|
|
|
|
|
|
|
pub fn allow_internal_unstable(tcx: TyCtxt<'tcx>, def_id: DefId, feature_gate: Symbol) -> bool {
|
|
|
|
let attrs = tcx.get_attrs(def_id);
|
|
|
|
attr::allow_internal_unstable(&tcx.sess, attrs)
|
|
|
|
.map_or(false, |mut features| features.any(|name| name == feature_gate))
|
|
|
|
}
|
2020-09-17 11:05:51 -07:00
|
|
|
|
|
|
|
// Returns `true` if the given `const fn` is "const-stable".
|
|
|
|
//
|
2020-09-18 09:17:13 -07:00
|
|
|
// Panics if the given `DefId` does not refer to a `const fn`.
|
|
|
|
//
|
2020-09-17 11:05:51 -07:00
|
|
|
// Const-stability is only relevant for `const fn` within a `staged_api` crate. Only "const-stable"
|
|
|
|
// functions can be called in a const-context by users of the stable compiler. "const-stable"
|
|
|
|
// functions are subject to more stringent restrictions than "const-unstable" functions: They
|
|
|
|
// cannot use unstable features and can only call other "const-stable" functions.
|
2020-09-18 09:17:13 -07:00
|
|
|
pub fn is_const_stable_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
|
2020-09-17 11:05:51 -07:00
|
|
|
use attr::{ConstStability, Stability, StabilityLevel};
|
|
|
|
|
|
|
|
// Const-stability is only relevant for `const fn`.
|
|
|
|
assert!(tcx.is_const_fn_raw(def_id));
|
|
|
|
|
|
|
|
// Functions with `#[rustc_const_unstable]` are const-unstable.
|
|
|
|
match tcx.lookup_const_stability(def_id) {
|
|
|
|
Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. }) => return false,
|
|
|
|
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }) => return true,
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions with `#[unstable]` are const-unstable.
|
|
|
|
//
|
|
|
|
// FIXME(ecstaticmorse): We should keep const-stability attributes wholly separate from normal stability
|
|
|
|
// attributes. `#[unstable]` should be irrelevant.
|
|
|
|
if let Some(Stability { level: StabilityLevel::Unstable { .. }, .. }) =
|
|
|
|
tcx.lookup_stability(def_id)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|