Rename rustc_mir to rustc_const_eval.
This commit is contained in:
1110
compiler/rustc_const_eval/src/transform/check_consts/check.rs
Normal file
1110
compiler/rustc_const_eval/src/transform/check_consts/check.rs
Normal file
File diff suppressed because it is too large
Load Diff
135
compiler/rustc_const_eval/src/transform/check_consts/mod.rs
Normal file
135
compiler/rustc_const_eval/src/transform/check_consts/mod.rs
Normal file
@@ -0,0 +1,135 @@
|
||||
//! 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.
|
||||
|
||||
use rustc_attr as attr;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::{DefId, LocalDefId};
|
||||
use rustc_middle::mir;
|
||||
use rustc_middle::ty::{self, TyCtxt};
|
||||
use rustc_span::{sym, Symbol};
|
||||
|
||||
pub use self::qualifs::Qualif;
|
||||
|
||||
pub mod check;
|
||||
mod ops;
|
||||
pub mod post_drop_elaboration;
|
||||
pub mod qualifs;
|
||||
mod resolver;
|
||||
|
||||
/// Information about the item currently being const-checked, as well as a reference to the global
|
||||
/// context.
|
||||
pub struct ConstCx<'mir, 'tcx> {
|
||||
pub body: &'mir mir::Body<'tcx>,
|
||||
pub tcx: TyCtxt<'tcx>,
|
||||
pub param_env: ty::ParamEnv<'tcx>,
|
||||
pub const_kind: Option<hir::ConstContext>,
|
||||
}
|
||||
|
||||
impl ConstCx<'mir, 'tcx> {
|
||||
pub fn new(tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>) -> Self {
|
||||
let def_id = body.source.def_id().expect_local();
|
||||
let param_env = tcx.param_env(def_id);
|
||||
Self::new_with_param_env(tcx, body, param_env)
|
||||
}
|
||||
|
||||
pub fn new_with_param_env(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
body: &'mir mir::Body<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
) -> Self {
|
||||
let const_kind = tcx.hir().body_const_context(body.source.def_id().expect_local());
|
||||
ConstCx { body, tcx, param_env, const_kind }
|
||||
}
|
||||
|
||||
pub fn def_id(&self) -> LocalDefId {
|
||||
self.body.source.def_id().expect_local()
|
||||
}
|
||||
|
||||
/// Returns the kind of const context this `Item` represents (`const`, `static`, etc.).
|
||||
///
|
||||
/// Panics if this `Item` is not const.
|
||||
pub fn const_kind(&self) -> hir::ConstContext {
|
||||
self.const_kind.expect("`const_kind` must not be called on a non-const fn")
|
||||
}
|
||||
|
||||
pub fn is_const_stable_const_fn(&self) -> bool {
|
||||
self.const_kind == Some(hir::ConstContext::ConstFn)
|
||||
&& self.tcx.features().staged_api
|
||||
&& is_const_stable_const_fn(self.tcx, self.def_id().to_def_id())
|
||||
}
|
||||
|
||||
/// Returns the function signature of the item being const-checked if it is a `fn` or `const fn`.
|
||||
pub fn fn_sig(&self) -> Option<&'tcx hir::FnSig<'tcx>> {
|
||||
// Get this from the HIR map instead of a query to avoid cycle errors.
|
||||
//
|
||||
// FIXME: Is this still an issue?
|
||||
let hir_map = self.tcx.hir();
|
||||
let hir_id = hir_map.local_def_id_to_hir_id(self.def_id());
|
||||
hir_map.fn_sig_by_hir_id(hir_id)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
// We can allow calls to these functions because `hook_panic_fn` in
|
||||
// `const_eval/machine.rs` ensures the calls are handled specially.
|
||||
// Keep in sync with what that function handles!
|
||||
Some(def_id) == tcx.lang_items().panic_fn()
|
||||
|| Some(def_id) == tcx.lang_items().panic_str()
|
||||
|| Some(def_id) == tcx.lang_items().begin_panic_fn()
|
||||
|| Some(def_id) == tcx.lang_items().panic_fmt()
|
||||
|| Some(def_id) == tcx.lang_items().begin_panic_fmt()
|
||||
}
|
||||
|
||||
pub fn rustc_allow_const_fn_unstable(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def_id: DefId,
|
||||
feature_gate: Symbol,
|
||||
) -> bool {
|
||||
let attrs = tcx.get_attrs(def_id);
|
||||
attr::rustc_allow_const_fn_unstable(&tcx.sess, attrs).any(|name| name == feature_gate)
|
||||
}
|
||||
|
||||
// Returns `true` if the given `const fn` is "const-stable".
|
||||
//
|
||||
// Panics if the given `DefId` does not refer to a `const fn`.
|
||||
//
|
||||
// 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.
|
||||
pub fn is_const_stable_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
|
||||
use attr::{ConstStability, Stability, StabilityLevel};
|
||||
|
||||
// A default body marked const is not const-stable because const
|
||||
// trait fns currently cannot be const-stable. We shouldn't
|
||||
// restrict default bodies to only call const-stable functions.
|
||||
if tcx.has_attr(def_id, sym::default_method_body_is_const) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
628
compiler/rustc_const_eval/src/transform/check_consts/ops.rs
Normal file
628
compiler/rustc_const_eval/src/transform/check_consts/ops.rs
Normal file
@@ -0,0 +1,628 @@
|
||||
//! Concrete error types for all operations which may be invalid in a certain const context.
|
||||
|
||||
use rustc_errors::{struct_span_err, DiagnosticBuilder};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_middle::mir;
|
||||
use rustc_session::parse::feature_err;
|
||||
use rustc_span::symbol::sym;
|
||||
use rustc_span::{Span, Symbol};
|
||||
|
||||
use super::ConstCx;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Status {
|
||||
Allowed,
|
||||
Unstable(Symbol),
|
||||
Forbidden,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum DiagnosticImportance {
|
||||
/// An operation that must be removed for const-checking to pass.
|
||||
Primary,
|
||||
|
||||
/// An operation that causes const-checking to fail, but is usually a side-effect of a `Primary` operation elsewhere.
|
||||
Secondary,
|
||||
}
|
||||
|
||||
/// An operation that is not *always* allowed in a const context.
|
||||
pub trait NonConstOp: std::fmt::Debug {
|
||||
/// Returns an enum indicating whether this operation is allowed within the given item.
|
||||
fn status_in_item(&self, _ccx: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Forbidden
|
||||
}
|
||||
|
||||
fn importance(&self) -> DiagnosticImportance {
|
||||
DiagnosticImportance::Primary
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FloatingPointOp;
|
||||
impl NonConstOp for FloatingPointOp {
|
||||
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
|
||||
if ccx.const_kind() == hir::ConstContext::ConstFn {
|
||||
Status::Unstable(sym::const_fn_floating_point_arithmetic)
|
||||
} else {
|
||||
Status::Allowed
|
||||
}
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_fn_floating_point_arithmetic,
|
||||
span,
|
||||
&format!("floating point arithmetic is not allowed in {}s", ccx.const_kind()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A function call where the callee is a pointer.
|
||||
#[derive(Debug)]
|
||||
pub struct FnCallIndirect;
|
||||
impl NonConstOp for FnCallIndirect {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
ccx.tcx.sess.struct_span_err(span, "function pointers are not allowed in const fn")
|
||||
}
|
||||
}
|
||||
|
||||
/// A function call where the callee is not marked as `const`.
|
||||
#[derive(Debug)]
|
||||
pub struct FnCallNonConst;
|
||||
impl NonConstOp for FnCallNonConst {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
struct_span_err!(
|
||||
ccx.tcx.sess,
|
||||
span,
|
||||
E0015,
|
||||
"calls in {}s are limited to constant functions, \
|
||||
tuple structs and tuple variants",
|
||||
ccx.const_kind(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A call to an `#[unstable]` const fn or `#[rustc_const_unstable]` function.
|
||||
///
|
||||
/// Contains the name of the feature that would allow the use of this function.
|
||||
#[derive(Debug)]
|
||||
pub struct FnCallUnstable(pub DefId, pub Option<Symbol>);
|
||||
|
||||
impl NonConstOp for FnCallUnstable {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let FnCallUnstable(def_id, feature) = *self;
|
||||
|
||||
let mut err = ccx.tcx.sess.struct_span_err(
|
||||
span,
|
||||
&format!("`{}` is not yet stable as a const fn", ccx.tcx.def_path_str(def_id)),
|
||||
);
|
||||
|
||||
if ccx.is_const_stable_const_fn() {
|
||||
err.help("Const-stable functions can only call other const-stable functions");
|
||||
} else if ccx.tcx.sess.is_nightly_build() {
|
||||
if let Some(feature) = feature {
|
||||
err.help(&format!(
|
||||
"add `#![feature({})]` to the crate attributes to enable",
|
||||
feature
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
err
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FnPtrCast;
|
||||
impl NonConstOp for FnPtrCast {
|
||||
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
|
||||
if ccx.const_kind() != hir::ConstContext::ConstFn {
|
||||
Status::Allowed
|
||||
} else {
|
||||
Status::Unstable(sym::const_fn_fn_ptr_basics)
|
||||
}
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_fn_fn_ptr_basics,
|
||||
span,
|
||||
&format!("function pointer casts are not allowed in {}s", ccx.const_kind()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Generator(pub hir::GeneratorKind);
|
||||
impl NonConstOp for Generator {
|
||||
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
|
||||
if let hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) = self.0 {
|
||||
Status::Unstable(sym::const_async_blocks)
|
||||
} else {
|
||||
Status::Forbidden
|
||||
}
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let msg = format!("{}s are not allowed in {}s", self.0, ccx.const_kind());
|
||||
if let hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) = self.0 {
|
||||
feature_err(&ccx.tcx.sess.parse_sess, sym::const_async_blocks, span, &msg)
|
||||
} else {
|
||||
ccx.tcx.sess.struct_span_err(span, &msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct HeapAllocation;
|
||||
impl NonConstOp for HeapAllocation {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let mut err = struct_span_err!(
|
||||
ccx.tcx.sess,
|
||||
span,
|
||||
E0010,
|
||||
"allocations are not allowed in {}s",
|
||||
ccx.const_kind()
|
||||
);
|
||||
err.span_label(span, format!("allocation not allowed in {}s", ccx.const_kind()));
|
||||
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
|
||||
err.note(
|
||||
"The value of statics and constants must be known at compile time, \
|
||||
and they live for the entire lifetime of a program. Creating a boxed \
|
||||
value allocates memory on the heap at runtime, and therefore cannot \
|
||||
be done at compile time.",
|
||||
);
|
||||
}
|
||||
err
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct InlineAsm;
|
||||
impl NonConstOp for InlineAsm {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
struct_span_err!(
|
||||
ccx.tcx.sess,
|
||||
span,
|
||||
E0015,
|
||||
"inline assembly is not allowed in {}s",
|
||||
ccx.const_kind()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LiveDrop {
|
||||
pub dropped_at: Option<Span>,
|
||||
}
|
||||
impl NonConstOp for LiveDrop {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let mut err = struct_span_err!(
|
||||
ccx.tcx.sess,
|
||||
span,
|
||||
E0493,
|
||||
"destructors cannot be evaluated at compile-time"
|
||||
);
|
||||
err.span_label(span, format!("{}s cannot evaluate destructors", ccx.const_kind()));
|
||||
if let Some(span) = self.dropped_at {
|
||||
err.span_label(span, "value is dropped here");
|
||||
}
|
||||
err
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// A borrow of a type that contains an `UnsafeCell` somewhere. The borrow never escapes to
|
||||
/// the final value of the constant.
|
||||
pub struct TransientCellBorrow;
|
||||
impl NonConstOp for TransientCellBorrow {
|
||||
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Unstable(sym::const_refs_to_cell)
|
||||
}
|
||||
fn importance(&self) -> DiagnosticImportance {
|
||||
// The cases that cannot possibly work will already emit a `CellBorrow`, so we should
|
||||
// not additionally emit a feature gate error if activating the feature gate won't work.
|
||||
DiagnosticImportance::Secondary
|
||||
}
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_refs_to_cell,
|
||||
span,
|
||||
"cannot borrow here, since the borrowed element may contain interior mutability",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// A borrow of a type that contains an `UnsafeCell` somewhere. The borrow might escape to
|
||||
/// the final value of the constant, and thus we cannot allow this (for now). We may allow
|
||||
/// it in the future for static items.
|
||||
pub struct CellBorrow;
|
||||
impl NonConstOp for CellBorrow {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let mut err = struct_span_err!(
|
||||
ccx.tcx.sess,
|
||||
span,
|
||||
E0492,
|
||||
"{}s cannot refer to interior mutable data",
|
||||
ccx.const_kind(),
|
||||
);
|
||||
err.span_label(
|
||||
span,
|
||||
"this borrow of an interior mutable value may end up in the final value",
|
||||
);
|
||||
if let hir::ConstContext::Static(_) = ccx.const_kind() {
|
||||
err.help(
|
||||
"to fix this, the value can be extracted to a separate \
|
||||
`static` item and then referenced",
|
||||
);
|
||||
}
|
||||
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
|
||||
err.note(
|
||||
"A constant containing interior mutable data behind a reference can allow you
|
||||
to modify that data. This would make multiple uses of a constant to be able to
|
||||
see different values and allow circumventing the `Send` and `Sync` requirements
|
||||
for shared mutable data, which is unsound.",
|
||||
);
|
||||
}
|
||||
err
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// This op is for `&mut` borrows in the trailing expression of a constant
|
||||
/// which uses the "enclosing scopes rule" to leak its locals into anonymous
|
||||
/// static or const items.
|
||||
pub struct MutBorrow(pub hir::BorrowKind);
|
||||
|
||||
impl NonConstOp for MutBorrow {
|
||||
fn status_in_item(&self, _ccx: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Forbidden
|
||||
}
|
||||
|
||||
fn importance(&self) -> DiagnosticImportance {
|
||||
// If there were primary errors (like non-const function calls), do not emit further
|
||||
// errors about mutable references.
|
||||
DiagnosticImportance::Secondary
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let raw = match self.0 {
|
||||
hir::BorrowKind::Raw => "raw ",
|
||||
hir::BorrowKind::Ref => "",
|
||||
};
|
||||
|
||||
let mut err = struct_span_err!(
|
||||
ccx.tcx.sess,
|
||||
span,
|
||||
E0764,
|
||||
"{}mutable references are not allowed in the final value of {}s",
|
||||
raw,
|
||||
ccx.const_kind(),
|
||||
);
|
||||
|
||||
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
|
||||
err.note(
|
||||
"References in statics and constants may only refer \
|
||||
to immutable values.\n\n\
|
||||
Statics are shared everywhere, and if they refer to \
|
||||
mutable data one might violate memory safety since \
|
||||
holding multiple mutable references to shared data \
|
||||
is not allowed.\n\n\
|
||||
If you really want global mutable state, try using \
|
||||
static mut or a global UnsafeCell.",
|
||||
);
|
||||
}
|
||||
err
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TransientMutBorrow(pub hir::BorrowKind);
|
||||
|
||||
impl NonConstOp for TransientMutBorrow {
|
||||
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Unstable(sym::const_mut_refs)
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let raw = match self.0 {
|
||||
hir::BorrowKind::Raw => "raw ",
|
||||
hir::BorrowKind::Ref => "",
|
||||
};
|
||||
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_mut_refs,
|
||||
span,
|
||||
&format!("{}mutable references are not allowed in {}s", raw, ccx.const_kind()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MutDeref;
|
||||
impl NonConstOp for MutDeref {
|
||||
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Unstable(sym::const_mut_refs)
|
||||
}
|
||||
|
||||
fn importance(&self) -> DiagnosticImportance {
|
||||
// Usually a side-effect of a `TransientMutBorrow` somewhere.
|
||||
DiagnosticImportance::Secondary
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_mut_refs,
|
||||
span,
|
||||
&format!("mutation through a reference is not allowed in {}s", ccx.const_kind()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Panic;
|
||||
impl NonConstOp for Panic {
|
||||
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Unstable(sym::const_panic)
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_panic,
|
||||
span,
|
||||
&format!("panicking in {}s is unstable", ccx.const_kind()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A call to a `panic()` lang item where the first argument is _not_ a `&str`.
|
||||
#[derive(Debug)]
|
||||
pub struct PanicNonStr;
|
||||
impl NonConstOp for PanicNonStr {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
ccx.tcx.sess.struct_span_err(
|
||||
span,
|
||||
"argument to `panic!()` in a const context must have type `&str`",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Comparing raw pointers for equality.
|
||||
/// Not currently intended to ever be allowed, even behind a feature gate: operation depends on
|
||||
/// allocation base addresses that are not known at compile-time.
|
||||
#[derive(Debug)]
|
||||
pub struct RawPtrComparison;
|
||||
impl NonConstOp for RawPtrComparison {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let mut err = ccx
|
||||
.tcx
|
||||
.sess
|
||||
.struct_span_err(span, "pointers cannot be reliably compared during const eval.");
|
||||
err.note(
|
||||
"see issue #53020 <https://github.com/rust-lang/rust/issues/53020> \
|
||||
for more information",
|
||||
);
|
||||
err
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RawPtrDeref;
|
||||
impl NonConstOp for RawPtrDeref {
|
||||
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Unstable(sym::const_raw_ptr_deref)
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_raw_ptr_deref,
|
||||
span,
|
||||
&format!("dereferencing raw pointers in {}s is unstable", ccx.const_kind(),),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Casting raw pointer or function pointer to an integer.
|
||||
/// Not currently intended to ever be allowed, even behind a feature gate: operation depends on
|
||||
/// allocation base addresses that are not known at compile-time.
|
||||
#[derive(Debug)]
|
||||
pub struct RawPtrToIntCast;
|
||||
impl NonConstOp for RawPtrToIntCast {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let mut err = ccx
|
||||
.tcx
|
||||
.sess
|
||||
.struct_span_err(span, "pointers cannot be cast to integers during const eval.");
|
||||
err.note("at compile-time, pointers do not have an integer value");
|
||||
err.note(
|
||||
"avoiding this restriction via `transmute`, `union`, or raw pointers leads to compile-time undefined behavior",
|
||||
);
|
||||
err
|
||||
}
|
||||
}
|
||||
|
||||
/// An access to a (non-thread-local) `static`.
|
||||
#[derive(Debug)]
|
||||
pub struct StaticAccess;
|
||||
impl NonConstOp for StaticAccess {
|
||||
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
|
||||
if let hir::ConstContext::Static(_) = ccx.const_kind() {
|
||||
Status::Allowed
|
||||
} else {
|
||||
Status::Forbidden
|
||||
}
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
let mut err = struct_span_err!(
|
||||
ccx.tcx.sess,
|
||||
span,
|
||||
E0013,
|
||||
"{}s cannot refer to statics",
|
||||
ccx.const_kind()
|
||||
);
|
||||
err.help(
|
||||
"consider extracting the value of the `static` to a `const`, and referring to that",
|
||||
);
|
||||
if ccx.tcx.sess.teach(&err.get_code().unwrap()) {
|
||||
err.note(
|
||||
"`static` and `const` variables can refer to other `const` variables. \
|
||||
A `const` variable, however, cannot refer to a `static` variable.",
|
||||
);
|
||||
err.help("To fix this, the value can be extracted to a `const` and then used.");
|
||||
}
|
||||
err
|
||||
}
|
||||
}
|
||||
|
||||
/// An access to a thread-local `static`.
|
||||
#[derive(Debug)]
|
||||
pub struct ThreadLocalAccess;
|
||||
impl NonConstOp for ThreadLocalAccess {
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
struct_span_err!(
|
||||
ccx.tcx.sess,
|
||||
span,
|
||||
E0625,
|
||||
"thread-local statics cannot be \
|
||||
accessed at compile-time"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// Types that cannot appear in the signature or locals of a `const fn`.
|
||||
pub mod ty {
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MutRef(pub mir::LocalKind);
|
||||
impl NonConstOp for MutRef {
|
||||
fn status_in_item(&self, _ccx: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Unstable(sym::const_mut_refs)
|
||||
}
|
||||
|
||||
fn importance(&self) -> DiagnosticImportance {
|
||||
match self.0 {
|
||||
mir::LocalKind::Var | mir::LocalKind::Temp => DiagnosticImportance::Secondary,
|
||||
mir::LocalKind::ReturnPointer | mir::LocalKind::Arg => {
|
||||
DiagnosticImportance::Primary
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_mut_refs,
|
||||
span,
|
||||
&format!("mutable references are not allowed in {}s", ccx.const_kind()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FnPtr(pub mir::LocalKind);
|
||||
impl NonConstOp for FnPtr {
|
||||
fn importance(&self) -> DiagnosticImportance {
|
||||
match self.0 {
|
||||
mir::LocalKind::Var | mir::LocalKind::Temp => DiagnosticImportance::Secondary,
|
||||
mir::LocalKind::ReturnPointer | mir::LocalKind::Arg => {
|
||||
DiagnosticImportance::Primary
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
|
||||
if ccx.const_kind() != hir::ConstContext::ConstFn {
|
||||
Status::Allowed
|
||||
} else {
|
||||
Status::Unstable(sym::const_fn_fn_ptr_basics)
|
||||
}
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_fn_fn_ptr_basics,
|
||||
span,
|
||||
&format!("function pointers cannot appear in {}s", ccx.const_kind()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ImplTrait;
|
||||
impl NonConstOp for ImplTrait {
|
||||
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Unstable(sym::const_impl_trait)
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_impl_trait,
|
||||
span,
|
||||
&format!("`impl Trait` is not allowed in {}s", ccx.const_kind()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TraitBound(pub mir::LocalKind);
|
||||
impl NonConstOp for TraitBound {
|
||||
fn importance(&self) -> DiagnosticImportance {
|
||||
match self.0 {
|
||||
mir::LocalKind::Var | mir::LocalKind::Temp => DiagnosticImportance::Secondary,
|
||||
mir::LocalKind::ReturnPointer | mir::LocalKind::Arg => {
|
||||
DiagnosticImportance::Primary
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn status_in_item(&self, ccx: &ConstCx<'_, '_>) -> Status {
|
||||
if ccx.const_kind() != hir::ConstContext::ConstFn {
|
||||
Status::Allowed
|
||||
} else {
|
||||
Status::Unstable(sym::const_fn_trait_bound)
|
||||
}
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_fn_trait_bound,
|
||||
span,
|
||||
"trait bounds other than `Sized` on const fn parameters are unstable",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait bound with the `?const Trait` opt-out
|
||||
#[derive(Debug)]
|
||||
pub struct TraitBoundNotConst;
|
||||
impl NonConstOp for TraitBoundNotConst {
|
||||
fn status_in_item(&self, _: &ConstCx<'_, '_>) -> Status {
|
||||
Status::Unstable(sym::const_trait_bound_opt_out)
|
||||
}
|
||||
|
||||
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
|
||||
feature_err(
|
||||
&ccx.tcx.sess.parse_sess,
|
||||
sym::const_trait_bound_opt_out,
|
||||
span,
|
||||
"`?const Trait` syntax is unstable",
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
use rustc_middle::mir::visit::Visitor;
|
||||
use rustc_middle::mir::{self, BasicBlock, Location};
|
||||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_span::Span;
|
||||
|
||||
use super::check::Qualifs;
|
||||
use super::ops::{self, NonConstOp};
|
||||
use super::qualifs::{NeedsDrop, Qualif};
|
||||
use super::ConstCx;
|
||||
|
||||
/// Returns `true` if we should use the more precise live drop checker that runs after drop
|
||||
/// elaboration.
|
||||
pub fn checking_enabled(ccx: &ConstCx<'_, '_>) -> bool {
|
||||
// Const-stable functions must always use the stable live drop checker.
|
||||
if ccx.is_const_stable_const_fn() {
|
||||
return false;
|
||||
}
|
||||
|
||||
ccx.tcx.features().const_precise_live_drops
|
||||
}
|
||||
|
||||
/// Look for live drops in a const context.
|
||||
///
|
||||
/// This is separate from the rest of the const checking logic because it must run after drop
|
||||
/// elaboration.
|
||||
pub fn check_live_drops(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) {
|
||||
let def_id = body.source.def_id().expect_local();
|
||||
let const_kind = tcx.hir().body_const_context(def_id);
|
||||
if const_kind.is_none() {
|
||||
return;
|
||||
}
|
||||
|
||||
let ccx = ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) };
|
||||
if !checking_enabled(&ccx) {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut visitor = CheckLiveDrops { ccx: &ccx, qualifs: Qualifs::default() };
|
||||
|
||||
visitor.visit_body(body);
|
||||
}
|
||||
|
||||
struct CheckLiveDrops<'mir, 'tcx> {
|
||||
ccx: &'mir ConstCx<'mir, 'tcx>,
|
||||
qualifs: Qualifs<'mir, 'tcx>,
|
||||
}
|
||||
|
||||
// So we can access `body` and `tcx`.
|
||||
impl std::ops::Deref for CheckLiveDrops<'mir, 'tcx> {
|
||||
type Target = ConstCx<'mir, 'tcx>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.ccx
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckLiveDrops<'mir, 'tcx> {
|
||||
fn check_live_drop(&self, span: Span) {
|
||||
ops::LiveDrop { dropped_at: None }.build_error(self.ccx, span).emit();
|
||||
}
|
||||
}
|
||||
|
||||
impl Visitor<'tcx> for CheckLiveDrops<'mir, 'tcx> {
|
||||
fn visit_basic_block_data(&mut self, bb: BasicBlock, block: &mir::BasicBlockData<'tcx>) {
|
||||
trace!("visit_basic_block_data: bb={:?} is_cleanup={:?}", bb, block.is_cleanup);
|
||||
|
||||
// Ignore drop terminators in cleanup blocks.
|
||||
if block.is_cleanup {
|
||||
return;
|
||||
}
|
||||
|
||||
self.super_basic_block_data(bb, block);
|
||||
}
|
||||
|
||||
fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
|
||||
trace!("visit_terminator: terminator={:?} location={:?}", terminator, location);
|
||||
|
||||
match &terminator.kind {
|
||||
mir::TerminatorKind::Drop { place: dropped_place, .. } => {
|
||||
let dropped_ty = dropped_place.ty(self.body, self.tcx).ty;
|
||||
if !NeedsDrop::in_any_value_of_ty(self.ccx, dropped_ty) {
|
||||
bug!(
|
||||
"Drop elaboration left behind a Drop for a type that does not need dropping"
|
||||
);
|
||||
}
|
||||
|
||||
if dropped_place.is_indirect() {
|
||||
self.check_live_drop(terminator.source_info.span);
|
||||
return;
|
||||
}
|
||||
|
||||
// Drop elaboration is not precise enough to accept code like
|
||||
// `src/test/ui/consts/control-flow/drop-pass.rs`; e.g., when an `Option<Vec<T>>` is
|
||||
// initialized with `None` and never changed, it still emits drop glue.
|
||||
// Hence we additionally check the qualifs here to allow more code to pass.
|
||||
if self.qualifs.needs_drop(self.ccx, dropped_place.local, location) {
|
||||
// Use the span where the dropped local was declared for the error.
|
||||
let span = self.body.local_decls[dropped_place.local].source_info.span;
|
||||
self.check_live_drop(span);
|
||||
}
|
||||
}
|
||||
|
||||
mir::TerminatorKind::DropAndReplace { .. } => span_bug!(
|
||||
terminator.source_info.span,
|
||||
"`DropAndReplace` should be removed by drop elaboration",
|
||||
),
|
||||
|
||||
mir::TerminatorKind::Abort
|
||||
| mir::TerminatorKind::Call { .. }
|
||||
| mir::TerminatorKind::Assert { .. }
|
||||
| mir::TerminatorKind::FalseEdge { .. }
|
||||
| mir::TerminatorKind::FalseUnwind { .. }
|
||||
| mir::TerminatorKind::GeneratorDrop
|
||||
| mir::TerminatorKind::Goto { .. }
|
||||
| mir::TerminatorKind::InlineAsm { .. }
|
||||
| mir::TerminatorKind::Resume
|
||||
| mir::TerminatorKind::Return
|
||||
| mir::TerminatorKind::SwitchInt { .. }
|
||||
| mir::TerminatorKind::Unreachable
|
||||
| mir::TerminatorKind::Yield { .. } => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
272
compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs
Normal file
272
compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs
Normal file
@@ -0,0 +1,272 @@
|
||||
//! Structural const qualification.
|
||||
//!
|
||||
//! See the `Qualif` trait for more info.
|
||||
|
||||
use rustc_errors::ErrorReported;
|
||||
use rustc_middle::mir::*;
|
||||
use rustc_middle::ty::{self, subst::SubstsRef, AdtDef, Ty};
|
||||
use rustc_span::DUMMY_SP;
|
||||
use rustc_trait_selection::traits;
|
||||
|
||||
use super::ConstCx;
|
||||
|
||||
pub fn in_any_value_of_ty(
|
||||
cx: &ConstCx<'_, 'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
error_occured: Option<ErrorReported>,
|
||||
) -> ConstQualifs {
|
||||
ConstQualifs {
|
||||
has_mut_interior: HasMutInterior::in_any_value_of_ty(cx, ty),
|
||||
needs_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
|
||||
custom_eq: CustomEq::in_any_value_of_ty(cx, ty),
|
||||
error_occured,
|
||||
}
|
||||
}
|
||||
|
||||
/// A "qualif"(-ication) is a way to look for something "bad" in the MIR that would disqualify some
|
||||
/// code for promotion or prevent it from evaluating at compile time.
|
||||
///
|
||||
/// Normally, we would determine what qualifications apply to each type and error when an illegal
|
||||
/// operation is performed on such a type. However, this was found to be too imprecise, especially
|
||||
/// in the presence of `enum`s. If only a single variant of an enum has a certain qualification, we
|
||||
/// needn't reject code unless it actually constructs and operates on the qualified variant.
|
||||
///
|
||||
/// To accomplish this, const-checking and promotion use a value-based analysis (as opposed to a
|
||||
/// type-based one). Qualifications propagate structurally across variables: If a local (or a
|
||||
/// projection of a local) is assigned a qualified value, that local itself becomes qualified.
|
||||
pub trait Qualif {
|
||||
/// The name of the file used to debug the dataflow analysis that computes this qualif.
|
||||
const ANALYSIS_NAME: &'static str;
|
||||
|
||||
/// Whether this `Qualif` is cleared when a local is moved from.
|
||||
const IS_CLEARED_ON_MOVE: bool = false;
|
||||
|
||||
/// Extracts the field of `ConstQualifs` that corresponds to this `Qualif`.
|
||||
fn in_qualifs(qualifs: &ConstQualifs) -> bool;
|
||||
|
||||
/// Returns `true` if *any* value of the given type could possibly have this `Qualif`.
|
||||
///
|
||||
/// This function determines `Qualif`s when we cannot do a value-based analysis. Since qualif
|
||||
/// propagation is context-insenstive, this includes function arguments and values returned
|
||||
/// from a call to another function.
|
||||
///
|
||||
/// It also determines the `Qualif`s for primitive types.
|
||||
fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool;
|
||||
|
||||
/// Returns `true` if this `Qualif` is inherent to the given struct or enum.
|
||||
///
|
||||
/// By default, `Qualif`s propagate into ADTs in a structural way: An ADT only becomes
|
||||
/// qualified if part of it is assigned a value with that `Qualif`. However, some ADTs *always*
|
||||
/// have a certain `Qualif`, regardless of whether their fields have it. For example, a type
|
||||
/// with a custom `Drop` impl is inherently `NeedsDrop`.
|
||||
///
|
||||
/// Returning `true` for `in_adt_inherently` but `false` for `in_any_value_of_ty` is unsound.
|
||||
fn in_adt_inherently(
|
||||
cx: &ConstCx<'_, 'tcx>,
|
||||
adt: &'tcx AdtDef,
|
||||
substs: SubstsRef<'tcx>,
|
||||
) -> bool;
|
||||
}
|
||||
|
||||
/// Constant containing interior mutability (`UnsafeCell<T>`).
|
||||
/// This must be ruled out to make sure that evaluating the constant at compile-time
|
||||
/// and at *any point* during the run-time would produce the same result. In particular,
|
||||
/// promotion of temporaries must not change program behavior; if the promoted could be
|
||||
/// written to, that would be a problem.
|
||||
pub struct HasMutInterior;
|
||||
|
||||
impl Qualif for HasMutInterior {
|
||||
const ANALYSIS_NAME: &'static str = "flow_has_mut_interior";
|
||||
|
||||
fn in_qualifs(qualifs: &ConstQualifs) -> bool {
|
||||
qualifs.has_mut_interior
|
||||
}
|
||||
|
||||
fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
|
||||
!ty.is_freeze(cx.tcx.at(DUMMY_SP), cx.param_env)
|
||||
}
|
||||
|
||||
fn in_adt_inherently(cx: &ConstCx<'_, 'tcx>, adt: &'tcx AdtDef, _: SubstsRef<'tcx>) -> bool {
|
||||
// Exactly one type, `UnsafeCell`, has the `HasMutInterior` qualif inherently.
|
||||
// It arises structurally for all other types.
|
||||
Some(adt.did) == cx.tcx.lang_items().unsafe_cell_type()
|
||||
}
|
||||
}
|
||||
|
||||
/// Constant containing an ADT that implements `Drop`.
|
||||
/// This must be ruled out (a) because we cannot run `Drop` during compile-time
|
||||
/// as that might not be a `const fn`, and (b) because implicit promotion would
|
||||
/// remove side-effects that occur as part of dropping that value.
|
||||
pub struct NeedsDrop;
|
||||
|
||||
impl Qualif for NeedsDrop {
|
||||
const ANALYSIS_NAME: &'static str = "flow_needs_drop";
|
||||
const IS_CLEARED_ON_MOVE: bool = true;
|
||||
|
||||
fn in_qualifs(qualifs: &ConstQualifs) -> bool {
|
||||
qualifs.needs_drop
|
||||
}
|
||||
|
||||
fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
|
||||
ty.needs_drop(cx.tcx, cx.param_env)
|
||||
}
|
||||
|
||||
fn in_adt_inherently(cx: &ConstCx<'_, 'tcx>, adt: &'tcx AdtDef, _: SubstsRef<'tcx>) -> bool {
|
||||
adt.has_dtor(cx.tcx)
|
||||
}
|
||||
}
|
||||
|
||||
/// A constant that cannot be used as part of a pattern in a `match` expression.
|
||||
pub struct CustomEq;
|
||||
|
||||
impl Qualif for CustomEq {
|
||||
const ANALYSIS_NAME: &'static str = "flow_custom_eq";
|
||||
|
||||
fn in_qualifs(qualifs: &ConstQualifs) -> bool {
|
||||
qualifs.custom_eq
|
||||
}
|
||||
|
||||
fn in_any_value_of_ty(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
|
||||
// If *any* component of a composite data type does not implement `Structural{Partial,}Eq`,
|
||||
// we know that at least some values of that type are not structural-match. I say "some"
|
||||
// because that component may be part of an enum variant (e.g.,
|
||||
// `Option::<NonStructuralMatchTy>::Some`), in which case some values of this type may be
|
||||
// structural-match (`Option::None`).
|
||||
let id = cx.tcx.hir().local_def_id_to_hir_id(cx.def_id());
|
||||
traits::search_for_structural_match_violation(id, cx.body.span, cx.tcx, ty).is_some()
|
||||
}
|
||||
|
||||
fn in_adt_inherently(
|
||||
cx: &ConstCx<'_, 'tcx>,
|
||||
adt: &'tcx AdtDef,
|
||||
substs: SubstsRef<'tcx>,
|
||||
) -> bool {
|
||||
let ty = cx.tcx.mk_ty(ty::Adt(adt, substs));
|
||||
!ty.is_structural_eq_shallow(cx.tcx)
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Use `mir::visit::Visitor` for the `in_*` functions if/when it supports early return.
|
||||
|
||||
/// Returns `true` if this `Rvalue` contains qualif `Q`.
|
||||
pub fn in_rvalue<Q, F>(cx: &ConstCx<'_, 'tcx>, in_local: &mut F, rvalue: &Rvalue<'tcx>) -> bool
|
||||
where
|
||||
Q: Qualif,
|
||||
F: FnMut(Local) -> bool,
|
||||
{
|
||||
match rvalue {
|
||||
Rvalue::ThreadLocalRef(_) | Rvalue::NullaryOp(..) => {
|
||||
Q::in_any_value_of_ty(cx, rvalue.ty(cx.body, cx.tcx))
|
||||
}
|
||||
|
||||
Rvalue::Discriminant(place) | Rvalue::Len(place) => {
|
||||
in_place::<Q, _>(cx, in_local, place.as_ref())
|
||||
}
|
||||
|
||||
Rvalue::Use(operand)
|
||||
| Rvalue::Repeat(operand, _)
|
||||
| Rvalue::UnaryOp(_, operand)
|
||||
| Rvalue::Cast(_, operand, _) => in_operand::<Q, _>(cx, in_local, operand),
|
||||
|
||||
Rvalue::BinaryOp(_, box (lhs, rhs)) | Rvalue::CheckedBinaryOp(_, box (lhs, rhs)) => {
|
||||
in_operand::<Q, _>(cx, in_local, lhs) || in_operand::<Q, _>(cx, in_local, rhs)
|
||||
}
|
||||
|
||||
Rvalue::Ref(_, _, place) | Rvalue::AddressOf(_, place) => {
|
||||
// Special-case reborrows to be more like a copy of the reference.
|
||||
if let Some((place_base, ProjectionElem::Deref)) = place.as_ref().last_projection() {
|
||||
let base_ty = place_base.ty(cx.body, cx.tcx).ty;
|
||||
if let ty::Ref(..) = base_ty.kind() {
|
||||
return in_place::<Q, _>(cx, in_local, place_base);
|
||||
}
|
||||
}
|
||||
|
||||
in_place::<Q, _>(cx, in_local, place.as_ref())
|
||||
}
|
||||
|
||||
Rvalue::Aggregate(kind, operands) => {
|
||||
// Return early if we know that the struct or enum being constructed is always
|
||||
// qualified.
|
||||
if let AggregateKind::Adt(def, _, substs, ..) = **kind {
|
||||
if Q::in_adt_inherently(cx, def, substs) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, proceed structurally...
|
||||
operands.iter().any(|o| in_operand::<Q, _>(cx, in_local, o))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if this `Place` contains qualif `Q`.
|
||||
pub fn in_place<Q, F>(cx: &ConstCx<'_, 'tcx>, in_local: &mut F, place: PlaceRef<'tcx>) -> bool
|
||||
where
|
||||
Q: Qualif,
|
||||
F: FnMut(Local) -> bool,
|
||||
{
|
||||
let mut place = place;
|
||||
while let Some((place_base, elem)) = place.last_projection() {
|
||||
match elem {
|
||||
ProjectionElem::Index(index) if in_local(index) => return true,
|
||||
|
||||
ProjectionElem::Deref
|
||||
| ProjectionElem::Field(_, _)
|
||||
| ProjectionElem::ConstantIndex { .. }
|
||||
| ProjectionElem::Subslice { .. }
|
||||
| ProjectionElem::Downcast(_, _)
|
||||
| ProjectionElem::Index(_) => {}
|
||||
}
|
||||
|
||||
let base_ty = place_base.ty(cx.body, cx.tcx);
|
||||
let proj_ty = base_ty.projection_ty(cx.tcx, elem).ty;
|
||||
if !Q::in_any_value_of_ty(cx, proj_ty) {
|
||||
return false;
|
||||
}
|
||||
|
||||
place = place_base;
|
||||
}
|
||||
|
||||
assert!(place.projection.is_empty());
|
||||
in_local(place.local)
|
||||
}
|
||||
|
||||
/// Returns `true` if this `Operand` contains qualif `Q`.
|
||||
pub fn in_operand<Q, F>(cx: &ConstCx<'_, 'tcx>, in_local: &mut F, operand: &Operand<'tcx>) -> bool
|
||||
where
|
||||
Q: Qualif,
|
||||
F: FnMut(Local) -> bool,
|
||||
{
|
||||
let constant = match operand {
|
||||
Operand::Copy(place) | Operand::Move(place) => {
|
||||
return in_place::<Q, _>(cx, in_local, place.as_ref());
|
||||
}
|
||||
|
||||
Operand::Constant(c) => c,
|
||||
};
|
||||
|
||||
// Check the qualifs of the value of `const` items.
|
||||
if let Some(ct) = constant.literal.const_for_ty() {
|
||||
if let ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs_: _, promoted }) = ct.val {
|
||||
assert!(promoted.is_none());
|
||||
// Don't peek inside trait associated constants.
|
||||
if cx.tcx.trait_of_item(def.did).is_none() {
|
||||
let qualifs = if let Some((did, param_did)) = def.as_const_arg() {
|
||||
cx.tcx.at(constant.span).mir_const_qualif_const_arg((did, param_did))
|
||||
} else {
|
||||
cx.tcx.at(constant.span).mir_const_qualif(def.did)
|
||||
};
|
||||
|
||||
if !Q::in_qualifs(&qualifs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Just in case the type is more specific than
|
||||
// the definition, e.g., impl associated const
|
||||
// with type parameters, take it into account.
|
||||
}
|
||||
}
|
||||
}
|
||||
// Otherwise use the qualifs of the type.
|
||||
Q::in_any_value_of_ty(cx, constant.literal.ty())
|
||||
}
|
||||
216
compiler/rustc_const_eval/src/transform/check_consts/resolver.rs
Normal file
216
compiler/rustc_const_eval/src/transform/check_consts/resolver.rs
Normal file
@@ -0,0 +1,216 @@
|
||||
//! Propagate `Qualif`s between locals and query the results.
|
||||
//!
|
||||
//! This contains the dataflow analysis used to track `Qualif`s on complex control-flow graphs.
|
||||
|
||||
use rustc_index::bit_set::BitSet;
|
||||
use rustc_middle::mir::visit::Visitor;
|
||||
use rustc_middle::mir::{self, BasicBlock, Local, Location};
|
||||
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use super::{qualifs, ConstCx, Qualif};
|
||||
|
||||
/// A `Visitor` that propagates qualifs between locals. This defines the transfer function of
|
||||
/// `FlowSensitiveAnalysis`.
|
||||
///
|
||||
/// This transfer does nothing when encountering an indirect assignment. Consumers should rely on
|
||||
/// the `MaybeMutBorrowedLocals` dataflow pass to see if a `Local` may have become qualified via
|
||||
/// an indirect assignment or function call.
|
||||
struct TransferFunction<'a, 'mir, 'tcx, Q> {
|
||||
ccx: &'a ConstCx<'mir, 'tcx>,
|
||||
qualifs_per_local: &'a mut BitSet<Local>,
|
||||
|
||||
_qualif: PhantomData<Q>,
|
||||
}
|
||||
|
||||
impl<Q> TransferFunction<'a, 'mir, 'tcx, Q>
|
||||
where
|
||||
Q: Qualif,
|
||||
{
|
||||
fn new(ccx: &'a ConstCx<'mir, 'tcx>, qualifs_per_local: &'a mut BitSet<Local>) -> Self {
|
||||
TransferFunction { ccx, qualifs_per_local, _qualif: PhantomData }
|
||||
}
|
||||
|
||||
fn initialize_state(&mut self) {
|
||||
self.qualifs_per_local.clear();
|
||||
|
||||
for arg in self.ccx.body.args_iter() {
|
||||
let arg_ty = self.ccx.body.local_decls[arg].ty;
|
||||
if Q::in_any_value_of_ty(self.ccx, arg_ty) {
|
||||
self.qualifs_per_local.insert(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn assign_qualif_direct(&mut self, place: &mir::Place<'tcx>, value: bool) {
|
||||
debug_assert!(!place.is_indirect());
|
||||
|
||||
match (value, place.as_ref()) {
|
||||
(true, mir::PlaceRef { local, .. }) => {
|
||||
self.qualifs_per_local.insert(local);
|
||||
}
|
||||
|
||||
// For now, we do not clear the qualif if a local is overwritten in full by
|
||||
// an unqualified rvalue (e.g. `y = 5`). This is to be consistent
|
||||
// with aggregates where we overwrite all fields with assignments, which would not
|
||||
// get this feature.
|
||||
(false, mir::PlaceRef { local: _, projection: &[] }) => {
|
||||
// self.qualifs_per_local.remove(*local);
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_call_return_effect(
|
||||
&mut self,
|
||||
_block: BasicBlock,
|
||||
_func: &mir::Operand<'tcx>,
|
||||
_args: &[mir::Operand<'tcx>],
|
||||
return_place: mir::Place<'tcx>,
|
||||
) {
|
||||
// We cannot reason about another function's internals, so use conservative type-based
|
||||
// qualification for the result of a function call.
|
||||
let return_ty = return_place.ty(self.ccx.body, self.ccx.tcx).ty;
|
||||
let qualif = Q::in_any_value_of_ty(self.ccx, return_ty);
|
||||
|
||||
if !return_place.is_indirect() {
|
||||
self.assign_qualif_direct(&return_place, qualif);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q> Visitor<'tcx> for TransferFunction<'_, '_, 'tcx, Q>
|
||||
where
|
||||
Q: Qualif,
|
||||
{
|
||||
fn visit_operand(&mut self, operand: &mir::Operand<'tcx>, location: Location) {
|
||||
self.super_operand(operand, location);
|
||||
|
||||
if !Q::IS_CLEARED_ON_MOVE {
|
||||
return;
|
||||
}
|
||||
|
||||
// If a local with no projections is moved from (e.g. `x` in `y = x`), record that
|
||||
// it no longer needs to be dropped.
|
||||
if let mir::Operand::Move(place) = operand {
|
||||
if let Some(local) = place.as_local() {
|
||||
self.qualifs_per_local.remove(local);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_assign(
|
||||
&mut self,
|
||||
place: &mir::Place<'tcx>,
|
||||
rvalue: &mir::Rvalue<'tcx>,
|
||||
location: Location,
|
||||
) {
|
||||
let qualif = qualifs::in_rvalue::<Q, _>(
|
||||
self.ccx,
|
||||
&mut |l| self.qualifs_per_local.contains(l),
|
||||
rvalue,
|
||||
);
|
||||
if !place.is_indirect() {
|
||||
self.assign_qualif_direct(place, qualif);
|
||||
}
|
||||
|
||||
// We need to assign qualifs to the left-hand side before visiting `rvalue` since
|
||||
// qualifs can be cleared on move.
|
||||
self.super_assign(place, rvalue, location);
|
||||
}
|
||||
|
||||
fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
|
||||
// The effect of assignment to the return place in `TerminatorKind::Call` is not applied
|
||||
// here; that occurs in `apply_call_return_effect`.
|
||||
|
||||
if let mir::TerminatorKind::DropAndReplace { value, place, .. } = &terminator.kind {
|
||||
let qualif = qualifs::in_operand::<Q, _>(
|
||||
self.ccx,
|
||||
&mut |l| self.qualifs_per_local.contains(l),
|
||||
value,
|
||||
);
|
||||
|
||||
if !place.is_indirect() {
|
||||
self.assign_qualif_direct(place, qualif);
|
||||
}
|
||||
}
|
||||
|
||||
// We need to assign qualifs to the dropped location before visiting the operand that
|
||||
// replaces it since qualifs can be cleared on move.
|
||||
self.super_terminator(terminator, location);
|
||||
}
|
||||
}
|
||||
|
||||
/// The dataflow analysis used to propagate qualifs on arbitrary CFGs.
|
||||
pub(super) struct FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q> {
|
||||
ccx: &'a ConstCx<'mir, 'tcx>,
|
||||
_qualif: PhantomData<Q>,
|
||||
}
|
||||
|
||||
impl<'a, 'mir, 'tcx, Q> FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q>
|
||||
where
|
||||
Q: Qualif,
|
||||
{
|
||||
pub(super) fn new(_: Q, ccx: &'a ConstCx<'mir, 'tcx>) -> Self {
|
||||
FlowSensitiveAnalysis { ccx, _qualif: PhantomData }
|
||||
}
|
||||
|
||||
fn transfer_function(
|
||||
&self,
|
||||
state: &'a mut BitSet<Local>,
|
||||
) -> TransferFunction<'a, 'mir, 'tcx, Q> {
|
||||
TransferFunction::<Q>::new(self.ccx, state)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q> rustc_mir_dataflow::AnalysisDomain<'tcx> for FlowSensitiveAnalysis<'_, '_, 'tcx, Q>
|
||||
where
|
||||
Q: Qualif,
|
||||
{
|
||||
type Domain = BitSet<Local>;
|
||||
|
||||
const NAME: &'static str = Q::ANALYSIS_NAME;
|
||||
|
||||
fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
|
||||
BitSet::new_empty(body.local_decls.len())
|
||||
}
|
||||
|
||||
fn initialize_start_block(&self, _body: &mir::Body<'tcx>, state: &mut Self::Domain) {
|
||||
self.transfer_function(state).initialize_state();
|
||||
}
|
||||
}
|
||||
|
||||
impl<Q> rustc_mir_dataflow::Analysis<'tcx> for FlowSensitiveAnalysis<'_, '_, 'tcx, Q>
|
||||
where
|
||||
Q: Qualif,
|
||||
{
|
||||
fn apply_statement_effect(
|
||||
&self,
|
||||
state: &mut Self::Domain,
|
||||
statement: &mir::Statement<'tcx>,
|
||||
location: Location,
|
||||
) {
|
||||
self.transfer_function(state).visit_statement(statement, location);
|
||||
}
|
||||
|
||||
fn apply_terminator_effect(
|
||||
&self,
|
||||
state: &mut Self::Domain,
|
||||
terminator: &mir::Terminator<'tcx>,
|
||||
location: Location,
|
||||
) {
|
||||
self.transfer_function(state).visit_terminator(terminator, location);
|
||||
}
|
||||
|
||||
fn apply_call_return_effect(
|
||||
&self,
|
||||
state: &mut Self::Domain,
|
||||
block: BasicBlock,
|
||||
func: &mir::Operand<'tcx>,
|
||||
args: &[mir::Operand<'tcx>],
|
||||
return_place: mir::Place<'tcx>,
|
||||
) {
|
||||
self.transfer_function(state).apply_call_return_effect(block, func, args, return_place)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user