2019-09-25 12:30:25 -07:00
|
|
|
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
|
|
|
|
|
2019-10-29 17:19:58 -07:00
|
|
|
use rustc::middle::lang_items;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
|
2019-09-17 16:25:40 -07:00
|
|
|
use rustc::mir::*;
|
|
|
|
use rustc::ty::cast::CastTy;
|
2020-02-04 14:03:16 -08:00
|
|
|
use rustc::ty::{self, Instance, InstanceDef, TyCtxt};
|
2019-12-31 21:25:16 +01:00
|
|
|
use rustc_errors::struct_span_err;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::{def_id::DefId, HirId};
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2020-01-06 23:21:41 +01:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2020-02-11 21:19:40 +01:00
|
|
|
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
|
|
|
|
use rustc_trait_selection::traits::{self, TraitEngine};
|
2019-09-17 16:25:40 -07:00
|
|
|
|
2019-10-29 17:19:58 -07:00
|
|
|
use std::borrow::Cow;
|
2019-09-17 16:25:40 -07:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
2019-09-20 09:00:18 -07:00
|
|
|
use super::ops::{self, NonConstOp};
|
2019-11-14 09:16:08 -08:00
|
|
|
use super::qualifs::{self, HasMutInterior, NeedsDrop};
|
2019-10-23 12:10:08 -07:00
|
|
|
use super::resolver::FlowSensitiveAnalysis;
|
2019-12-22 17:42:04 -05:00
|
|
|
use super::{is_lang_panic_fn, ConstKind, Item, Qualif};
|
2020-01-01 18:06:00 +01:00
|
|
|
use crate::const_eval::{is_const_fn, is_unstable_const_fn};
|
2020-02-12 13:42:31 -08:00
|
|
|
use crate::dataflow::MaybeMutBorrowedLocals;
|
2020-02-28 22:02:20 -08:00
|
|
|
use crate::dataflow::{self, Analysis};
|
2019-09-17 16:25:40 -07:00
|
|
|
|
2020-02-12 13:42:31 -08:00
|
|
|
// We are using `MaybeMutBorrowedLocals` as a proxy for whether an item may have been mutated
|
|
|
|
// through a pointer prior to the given point. This is okay even though `MaybeMutBorrowedLocals`
|
|
|
|
// kills locals upon `StorageDead` because a local will never be used after a `StorageDead`.
|
2019-10-23 12:10:08 -07:00
|
|
|
pub type IndirectlyMutableResults<'mir, 'tcx> =
|
2020-02-12 13:42:31 -08:00
|
|
|
dataflow::ResultsCursor<'mir, 'tcx, MaybeMutBorrowedLocals<'mir, 'tcx>>;
|
2019-10-23 12:10:08 -07:00
|
|
|
|
|
|
|
struct QualifCursor<'a, 'mir, 'tcx, Q: Qualif> {
|
|
|
|
cursor: dataflow::ResultsCursor<'mir, 'tcx, FlowSensitiveAnalysis<'a, 'mir, 'tcx, Q>>,
|
|
|
|
in_any_value_of_ty: BitSet<Local>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<Q: Qualif> QualifCursor<'a, 'mir, 'tcx, Q> {
|
2019-11-11 11:50:27 -08:00
|
|
|
pub fn new(q: Q, item: &'a Item<'mir, 'tcx>) -> Self {
|
2020-01-20 15:05:26 -08:00
|
|
|
let cursor = FlowSensitiveAnalysis::new(q, item)
|
|
|
|
.into_engine(item.tcx, &item.body, item.def_id)
|
|
|
|
.iterate_to_fixpoint()
|
|
|
|
.into_results_cursor(*item.body);
|
2019-10-23 12:10:08 -07:00
|
|
|
|
|
|
|
let mut in_any_value_of_ty = BitSet::new_empty(item.body.local_decls.len());
|
|
|
|
for (local, decl) in item.body.local_decls.iter_enumerated() {
|
|
|
|
if Q::in_any_value_of_ty(item, decl.ty) {
|
|
|
|
in_any_value_of_ty.insert(local);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
QualifCursor { cursor, in_any_value_of_ty }
|
2019-10-23 12:10:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 16:25:40 -07:00
|
|
|
pub struct Qualifs<'a, 'mir, 'tcx> {
|
2019-10-23 12:10:08 -07:00
|
|
|
has_mut_interior: QualifCursor<'a, 'mir, 'tcx, HasMutInterior>,
|
|
|
|
needs_drop: QualifCursor<'a, 'mir, 'tcx, NeedsDrop>,
|
|
|
|
indirectly_mutable: IndirectlyMutableResults<'mir, 'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Qualifs<'a, 'mir, 'tcx> {
|
|
|
|
fn indirectly_mutable(&mut self, local: Local, location: Location) -> bool {
|
2020-02-12 13:42:31 -08:00
|
|
|
self.indirectly_mutable.seek_before(location);
|
2019-10-23 12:10:08 -07:00
|
|
|
self.indirectly_mutable.get().contains(local)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if `local` is `NeedsDrop` at the given `Location`.
|
|
|
|
///
|
|
|
|
/// Only updates the cursor if absolutely necessary
|
2020-02-03 10:33:29 -08:00
|
|
|
fn needs_drop(&mut self, local: Local, location: Location) -> bool {
|
2019-10-23 12:10:08 -07:00
|
|
|
if !self.needs_drop.in_any_value_of_ty.contains(local) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.needs_drop.cursor.seek_before(location);
|
2019-12-22 17:42:04 -05:00
|
|
|
self.needs_drop.cursor.get().contains(local) || self.indirectly_mutable(local, location)
|
2019-10-23 12:10:08 -07:00
|
|
|
}
|
|
|
|
|
2019-10-28 10:24:29 -07:00
|
|
|
/// Returns `true` if `local` is `HasMutInterior` at the given `Location`.
|
|
|
|
///
|
|
|
|
/// Only updates the cursor if absolutely necessary.
|
2020-02-03 10:33:29 -08:00
|
|
|
fn has_mut_interior(&mut self, local: Local, location: Location) -> bool {
|
2019-10-28 10:24:29 -07:00
|
|
|
if !self.has_mut_interior.in_any_value_of_ty.contains(local) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.has_mut_interior.cursor.seek_before(location);
|
|
|
|
self.has_mut_interior.cursor.get().contains(local)
|
|
|
|
|| self.indirectly_mutable(local, location)
|
|
|
|
}
|
|
|
|
|
2019-11-14 11:58:50 -08:00
|
|
|
fn in_return_place(&mut self, item: &Item<'_, 'tcx>) -> ConstQualifs {
|
2019-10-28 10:24:29 -07:00
|
|
|
// Find the `Return` terminator if one exists.
|
|
|
|
//
|
|
|
|
// If no `Return` terminator exists, this MIR is divergent. Just return the conservative
|
|
|
|
// qualifs for the return type.
|
2019-12-22 17:42:04 -05:00
|
|
|
let return_block = item
|
|
|
|
.body
|
2019-10-28 10:24:29 -07:00
|
|
|
.basic_blocks()
|
|
|
|
.iter_enumerated()
|
2019-12-22 17:42:04 -05:00
|
|
|
.find(|(_, block)| match block.terminator().kind {
|
|
|
|
TerminatorKind::Return => true,
|
|
|
|
_ => false,
|
2019-10-28 10:24:29 -07:00
|
|
|
})
|
|
|
|
.map(|(bb, _)| bb);
|
|
|
|
|
|
|
|
let return_block = match return_block {
|
2019-11-14 09:16:08 -08:00
|
|
|
None => return qualifs::in_any_value_of_ty(item, item.body.return_ty()),
|
2019-10-28 10:24:29 -07:00
|
|
|
Some(bb) => bb,
|
|
|
|
};
|
|
|
|
|
|
|
|
let return_loc = item.body.terminator_loc(return_block);
|
|
|
|
|
2019-11-14 11:58:50 -08:00
|
|
|
ConstQualifs {
|
2020-02-03 10:33:29 -08:00
|
|
|
needs_drop: self.needs_drop(RETURN_PLACE, return_loc),
|
|
|
|
has_mut_interior: self.has_mut_interior(RETURN_PLACE, return_loc),
|
2019-11-14 09:16:08 -08:00
|
|
|
}
|
2019-10-28 10:24:29 -07:00
|
|
|
}
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Validator<'a, 'mir, 'tcx> {
|
|
|
|
item: &'a Item<'mir, 'tcx>,
|
|
|
|
qualifs: Qualifs<'a, 'mir, 'tcx>,
|
|
|
|
|
|
|
|
/// The span of the current statement.
|
|
|
|
span: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for Validator<'_, 'mir, 'tcx> {
|
|
|
|
type Target = Item<'mir, 'tcx>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Validator<'a, 'mir, 'tcx> {
|
2019-12-22 17:42:04 -05:00
|
|
|
pub fn new(item: &'a Item<'mir, 'tcx>) -> Self {
|
2020-02-12 13:42:31 -08:00
|
|
|
let Item { tcx, body, def_id, param_env, .. } = *item;
|
|
|
|
|
2019-11-11 11:50:27 -08:00
|
|
|
let needs_drop = QualifCursor::new(NeedsDrop, item);
|
|
|
|
let has_mut_interior = QualifCursor::new(HasMutInterior, item);
|
2019-09-17 16:25:40 -07:00
|
|
|
|
2020-02-13 13:57:01 -08:00
|
|
|
// We can use `unsound_ignore_borrow_on_drop` here because custom drop impls are not
|
|
|
|
// allowed in a const.
|
|
|
|
//
|
|
|
|
// FIXME(ecstaticmorse): Someday we want to allow custom drop impls. How do we do this
|
|
|
|
// without breaking stable code?
|
2020-02-13 13:56:19 -08:00
|
|
|
let indirectly_mutable = MaybeMutBorrowedLocals::mut_borrows_only(tcx, *body, param_env)
|
2020-02-13 13:57:01 -08:00
|
|
|
.unsound_ignore_borrow_on_drop()
|
2020-02-12 13:42:31 -08:00
|
|
|
.into_engine(tcx, *body, def_id)
|
|
|
|
.iterate_to_fixpoint()
|
|
|
|
.into_results_cursor(*body);
|
2019-10-23 12:10:08 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let qualifs = Qualifs { needs_drop, has_mut_interior, indirectly_mutable };
|
2019-09-17 16:25:40 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
Validator { span: item.body.span, item, qualifs }
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
2019-10-29 17:19:58 -07:00
|
|
|
pub fn check_body(&mut self) {
|
2019-12-22 17:42:04 -05:00
|
|
|
let Item { tcx, body, def_id, const_kind, .. } = *self.item;
|
2019-10-29 17:19:58 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let use_min_const_fn_checks = (const_kind == Some(ConstKind::ConstFn)
|
2020-01-01 18:06:00 +01:00
|
|
|
&& crate::const_eval::is_min_const_fn(tcx, def_id))
|
2019-10-29 17:19:58 -07:00
|
|
|
&& !tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you;
|
|
|
|
|
|
|
|
if use_min_const_fn_checks {
|
|
|
|
// Enforce `min_const_fn` for stable `const fn`s.
|
|
|
|
use crate::transform::qualify_min_const_fn::is_min_const_fn;
|
2019-11-21 10:50:52 -05:00
|
|
|
if let Err((span, err)) = is_min_const_fn(tcx, def_id, &body) {
|
2019-10-29 17:19:58 -07:00
|
|
|
error_min_const_fn_violation(tcx, span, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check_short_circuiting_in_const_local(self.item);
|
|
|
|
|
|
|
|
if body.is_cfg_cyclic() {
|
2019-11-10 10:39:27 -08:00
|
|
|
// We can't provide a good span for the error here, but this should be caught by the
|
|
|
|
// HIR const-checker anyways.
|
|
|
|
self.check_op_spanned(ops::Loop, body.span);
|
2019-10-29 17:19:58 -07:00
|
|
|
}
|
|
|
|
|
2020-03-26 16:02:10 -07:00
|
|
|
self.visit_body(*body);
|
2019-10-29 17:19:58 -07:00
|
|
|
|
|
|
|
// Ensure that the end result is `Sync` in a non-thread local `static`.
|
2019-12-22 17:42:04 -05:00
|
|
|
let should_check_for_sync =
|
|
|
|
const_kind == Some(ConstKind::Static) && !tcx.has_attr(def_id, sym::thread_local);
|
2019-10-29 17:19:58 -07:00
|
|
|
|
|
|
|
if should_check_for_sync {
|
|
|
|
let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
|
2019-11-21 10:50:52 -05:00
|
|
|
check_return_ty_is_sync(tcx, &body, hir_id);
|
2019-10-29 17:19:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 11:58:50 -08:00
|
|
|
pub fn qualifs_in_return_place(&mut self) -> ConstQualifs {
|
2019-10-29 17:19:58 -07:00
|
|
|
self.qualifs.in_return_place(self.item)
|
|
|
|
}
|
|
|
|
|
2019-09-17 16:25:40 -07:00
|
|
|
/// Emits an error at the given `span` if an expression cannot be evaluated in the current
|
2019-11-27 14:29:09 -08:00
|
|
|
/// context.
|
|
|
|
pub fn check_op_spanned<O>(&mut self, op: O, span: Span)
|
2019-09-17 16:25:40 -07:00
|
|
|
where
|
2019-12-22 17:42:04 -05:00
|
|
|
O: NonConstOp,
|
2019-09-17 16:25:40 -07:00
|
|
|
{
|
|
|
|
trace!("check_op: op={:?}", op);
|
|
|
|
|
|
|
|
if op.is_allowed_in_item(self) {
|
2019-11-27 14:29:09 -08:00
|
|
|
return;
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// If an operation is supported in miri (and is not already controlled by a feature gate) it
|
|
|
|
// can be turned on with `-Zunleash-the-miri-inside-of-you`.
|
2020-03-14 15:59:10 -07:00
|
|
|
let is_unleashable = O::IS_SUPPORTED_IN_MIRI && O::feature_gate().is_none();
|
2019-09-17 16:25:40 -07:00
|
|
|
|
|
|
|
if is_unleashable && self.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you {
|
|
|
|
self.tcx.sess.span_warn(span, "skipping const checks");
|
2019-11-27 14:29:09 -08:00
|
|
|
return;
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
2019-11-10 10:41:42 -08:00
|
|
|
op.emit_error(self, span);
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emits an error if an expression cannot be evaluated in the current context.
|
2019-11-27 14:29:09 -08:00
|
|
|
pub fn check_op(&mut self, op: impl NonConstOp) {
|
2019-09-17 16:25:40 -07:00
|
|
|
let span = self.span;
|
|
|
|
self.check_op_spanned(op, span)
|
|
|
|
}
|
2019-11-18 23:04:06 +00:00
|
|
|
|
2019-11-27 14:29:09 -08:00
|
|
|
fn check_static(&mut self, def_id: DefId, span: Span) {
|
2019-11-18 23:04:06 +00:00
|
|
|
let is_thread_local = self.tcx.has_attr(def_id, sym::thread_local);
|
|
|
|
if is_thread_local {
|
|
|
|
self.check_op_spanned(ops::ThreadLocalAccess, span)
|
|
|
|
} else {
|
|
|
|
self.check_op_spanned(ops::StaticAccess, span)
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Visitor<'tcx> for Validator<'_, 'mir, 'tcx> {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_basic_block_data(&mut self, bb: BasicBlock, block: &BasicBlockData<'tcx>) {
|
2019-10-29 17:19:58 -07:00
|
|
|
trace!("visit_basic_block_data: bb={:?} is_cleanup={:?}", bb, block.is_cleanup);
|
|
|
|
|
|
|
|
// Just as the old checker did, we skip const-checking basic blocks on the unwind path.
|
|
|
|
// These blocks often drop locals that would otherwise be returned from the function.
|
|
|
|
//
|
|
|
|
// FIXME: This shouldn't be unsound since a panic at compile time will cause a compiler
|
|
|
|
// error anyway, but maybe we should do more here?
|
|
|
|
if block.is_cleanup {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_basic_block_data(bb, block);
|
|
|
|
}
|
|
|
|
|
2019-09-17 16:25:40 -07:00
|
|
|
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
|
|
|
|
trace!("visit_rvalue: rvalue={:?} location={:?}", rvalue, location);
|
|
|
|
|
2019-11-22 15:52:59 -08:00
|
|
|
// Special-case reborrows to be more like a copy of a reference.
|
2018-12-23 19:00:58 +00:00
|
|
|
match *rvalue {
|
|
|
|
Rvalue::Ref(_, kind, ref place) => {
|
|
|
|
if let Some(reborrowed_proj) = place_as_reborrow(self.tcx, *self.body, place) {
|
|
|
|
let ctx = match kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
BorrowKind::Shared => {
|
|
|
|
PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
|
|
|
|
}
|
|
|
|
BorrowKind::Shallow => {
|
|
|
|
PlaceContext::NonMutatingUse(NonMutatingUseContext::ShallowBorrow)
|
|
|
|
}
|
|
|
|
BorrowKind::Unique => {
|
|
|
|
PlaceContext::NonMutatingUse(NonMutatingUseContext::UniqueBorrow)
|
|
|
|
}
|
|
|
|
BorrowKind::Mut { .. } => {
|
|
|
|
PlaceContext::MutatingUse(MutatingUseContext::Borrow)
|
|
|
|
}
|
2018-12-23 19:00:58 +00:00
|
|
|
};
|
2019-12-11 16:50:03 -03:00
|
|
|
self.visit_place_base(&place.local, ctx, location);
|
2020-03-06 18:22:17 -03:00
|
|
|
self.visit_projection(place.local, reborrowed_proj, ctx, location);
|
2018-12-23 19:00:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Rvalue::AddressOf(mutbl, ref place) => {
|
|
|
|
if let Some(reborrowed_proj) = place_as_reborrow(self.tcx, *self.body, place) {
|
|
|
|
let ctx = match mutbl {
|
2019-12-22 17:42:04 -05:00
|
|
|
Mutability::Not => {
|
|
|
|
PlaceContext::NonMutatingUse(NonMutatingUseContext::AddressOf)
|
|
|
|
}
|
|
|
|
Mutability::Mut => PlaceContext::MutatingUse(MutatingUseContext::AddressOf),
|
2018-12-23 19:00:58 +00:00
|
|
|
};
|
2019-12-11 16:50:03 -03:00
|
|
|
self.visit_place_base(&place.local, ctx, location);
|
2020-03-06 18:22:17 -03:00
|
|
|
self.visit_projection(place.local, reborrowed_proj, ctx, location);
|
2018-12-23 19:00:58 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
2018-12-23 19:00:58 +00:00
|
|
|
_ => {}
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
2019-11-22 15:52:59 -08:00
|
|
|
self.super_rvalue(rvalue, location);
|
|
|
|
|
2019-09-17 16:25:40 -07:00
|
|
|
match *rvalue {
|
2019-12-22 17:42:04 -05:00
|
|
|
Rvalue::Use(_)
|
|
|
|
| Rvalue::Repeat(..)
|
|
|
|
| Rvalue::UnaryOp(UnOp::Neg, _)
|
|
|
|
| Rvalue::UnaryOp(UnOp::Not, _)
|
|
|
|
| Rvalue::NullaryOp(NullOp::SizeOf, _)
|
|
|
|
| Rvalue::CheckedBinaryOp(..)
|
|
|
|
| Rvalue::Cast(CastKind::Pointer(_), ..)
|
|
|
|
| Rvalue::Discriminant(..)
|
|
|
|
| Rvalue::Len(_)
|
|
|
|
| Rvalue::Aggregate(..) => {}
|
|
|
|
|
|
|
|
Rvalue::Ref(_, kind @ BorrowKind::Mut { .. }, ref place)
|
|
|
|
| Rvalue::Ref(_, kind @ BorrowKind::Unique, ref place) => {
|
2019-12-03 12:55:58 +02:00
|
|
|
let ty = place.ty(*self.body, self.tcx).ty;
|
2019-11-22 15:52:59 -08:00
|
|
|
let is_allowed = match ty.kind {
|
|
|
|
// Inside a `static mut`, `&mut [...]` is allowed.
|
2019-12-22 17:42:04 -05:00
|
|
|
ty::Array(..) | ty::Slice(_) if self.const_kind() == ConstKind::StaticMut => {
|
|
|
|
true
|
|
|
|
}
|
2019-11-22 15:52:59 -08:00
|
|
|
|
|
|
|
// FIXME(ecstaticmorse): We could allow `&mut []` inside a const context given
|
|
|
|
// that this is merely a ZST and it is already eligible for promotion.
|
|
|
|
// This may require an RFC?
|
|
|
|
/*
|
|
|
|
ty::Array(_, len) if len.try_eval_usize(cx.tcx, cx.param_env) == Some(0)
|
|
|
|
=> true,
|
|
|
|
*/
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if !is_allowed {
|
2019-12-22 17:42:04 -05:00
|
|
|
if let BorrowKind::Mut { .. } = kind {
|
2019-11-26 10:00:41 -05:00
|
|
|
self.check_op(ops::MutBorrow);
|
|
|
|
} else {
|
|
|
|
self.check_op(ops::CellBorrow);
|
|
|
|
}
|
2019-11-22 15:52:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
Rvalue::AddressOf(Mutability::Mut, _) => self.check_op(ops::MutAddressOf),
|
2018-12-23 19:00:58 +00:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
Rvalue::Ref(_, BorrowKind::Shared, ref place)
|
2020-02-03 10:37:36 -08:00
|
|
|
| Rvalue::Ref(_, BorrowKind::Shallow, ref place)
|
|
|
|
| Rvalue::AddressOf(Mutability::Not, ref place) => {
|
2019-12-13 13:20:16 -08:00
|
|
|
let borrowed_place_has_mut_interior = qualifs::in_place::<HasMutInterior, _>(
|
2020-02-03 10:37:36 -08:00
|
|
|
&self.item,
|
|
|
|
&mut |local| self.qualifs.has_mut_interior(local, location),
|
|
|
|
place.as_ref(),
|
|
|
|
);
|
|
|
|
|
|
|
|
if borrowed_place_has_mut_interior {
|
|
|
|
self.check_op(ops::CellBorrow);
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-11-22 15:52:59 -08:00
|
|
|
|
2019-09-17 16:25:40 -07:00
|
|
|
Rvalue::Cast(CastKind::Misc, ref operand, cast_ty) => {
|
2019-12-03 12:55:58 +02:00
|
|
|
let operand_ty = operand.ty(*self.body, self.tcx);
|
2019-09-17 16:25:40 -07:00
|
|
|
let cast_in = CastTy::from_ty(operand_ty).expect("bad input type for cast");
|
|
|
|
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
if let (CastTy::Ptr(_), CastTy::Int(_)) | (CastTy::FnPtr, CastTy::Int(_)) =
|
|
|
|
(cast_in, cast_out)
|
|
|
|
{
|
2019-09-17 16:25:40 -07:00
|
|
|
self.check_op(ops::RawPtrToIntCast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rvalue::BinaryOp(op, ref lhs, _) => {
|
2019-12-03 12:55:58 +02:00
|
|
|
if let ty::RawPtr(_) | ty::FnPtr(..) = lhs.ty(*self.body, self.tcx).kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
assert!(
|
|
|
|
op == BinOp::Eq
|
|
|
|
|| op == BinOp::Ne
|
|
|
|
|| op == BinOp::Le
|
|
|
|
|| op == BinOp::Lt
|
|
|
|
|| op == BinOp::Ge
|
|
|
|
|| op == BinOp::Gt
|
|
|
|
|| op == BinOp::Offset
|
|
|
|
);
|
2019-09-17 16:25:40 -07:00
|
|
|
|
|
|
|
self.check_op(ops::RawPtrComparison);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rvalue::NullaryOp(NullOp::Box, _) => {
|
|
|
|
self.check_op(ops::HeapAllocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 16:50:03 -03:00
|
|
|
fn visit_place_base(&mut self, place_local: &Local, context: PlaceContext, location: Location) {
|
2019-09-17 16:25:40 -07:00
|
|
|
trace!(
|
2019-12-11 16:50:03 -03:00
|
|
|
"visit_place_base: place_local={:?} context={:?} location={:?}",
|
|
|
|
place_local,
|
2019-09-17 16:25:40 -07:00
|
|
|
context,
|
|
|
|
location,
|
|
|
|
);
|
2019-12-11 16:50:03 -03:00
|
|
|
self.super_place_base(place_local, context, location);
|
2019-11-11 12:15:38 +01:00
|
|
|
}
|
2019-09-17 16:25:40 -07:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn visit_operand(&mut self, op: &Operand<'tcx>, location: Location) {
|
2019-11-11 12:15:38 +01:00
|
|
|
self.super_operand(op, location);
|
|
|
|
if let Operand::Constant(c) = op {
|
|
|
|
if let Some(def_id) = c.check_static_ptr(self.tcx) {
|
2019-11-18 23:04:06 +00:00
|
|
|
self.check_static(def_id, self.span);
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-02 19:11:08 -03:00
|
|
|
fn visit_projection_elem(
|
2019-09-17 16:25:40 -07:00
|
|
|
&mut self,
|
2020-03-06 18:22:17 -03:00
|
|
|
place_local: Local,
|
2019-10-02 19:11:08 -03:00
|
|
|
proj_base: &[PlaceElem<'tcx>],
|
|
|
|
elem: &PlaceElem<'tcx>,
|
2019-09-17 16:25:40 -07:00
|
|
|
context: PlaceContext,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
trace!(
|
2019-12-11 16:50:03 -03:00
|
|
|
"visit_projection_elem: place_local={:?} proj_base={:?} elem={:?} \
|
2019-10-02 19:11:08 -03:00
|
|
|
context={:?} location={:?}",
|
2019-12-11 16:50:03 -03:00
|
|
|
place_local,
|
2019-10-02 19:11:08 -03:00
|
|
|
proj_base,
|
|
|
|
elem,
|
2019-09-17 16:25:40 -07:00
|
|
|
context,
|
|
|
|
location,
|
|
|
|
);
|
|
|
|
|
2019-12-11 16:50:03 -03:00
|
|
|
self.super_projection_elem(place_local, proj_base, elem, context, location);
|
2019-09-17 16:25:40 -07:00
|
|
|
|
|
|
|
match elem {
|
|
|
|
ProjectionElem::Deref => {
|
2020-03-06 18:22:17 -03:00
|
|
|
let base_ty = Place::ty_from(place_local, proj_base, *self.body, self.tcx).ty;
|
2019-09-28 07:20:06 -07:00
|
|
|
if let ty::RawPtr(_) = base_ty.kind {
|
2019-11-18 23:04:06 +00:00
|
|
|
if proj_base.is_empty() {
|
2019-12-11 16:50:03 -03:00
|
|
|
if let (local, []) = (place_local, proj_base) {
|
2020-03-06 18:22:17 -03:00
|
|
|
let decl = &self.body.local_decls[local];
|
2019-11-18 23:04:06 +00:00
|
|
|
if let LocalInfo::StaticRef { def_id, .. } = decl.local_info {
|
|
|
|
let span = decl.source_info.span;
|
|
|
|
self.check_static(def_id, span);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-17 16:25:40 -07:00
|
|
|
self.check_op(ops::RawPtrDeref);
|
|
|
|
}
|
2019-11-18 23:04:06 +00:00
|
|
|
|
|
|
|
if context.is_mutating_use() {
|
|
|
|
self.check_op(ops::MutDeref);
|
|
|
|
}
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
ProjectionElem::ConstantIndex { .. }
|
|
|
|
| ProjectionElem::Subslice { .. }
|
|
|
|
| ProjectionElem::Field(..)
|
|
|
|
| ProjectionElem::Index(_) => {
|
2020-03-06 18:22:17 -03:00
|
|
|
let base_ty = Place::ty_from(place_local, proj_base, *self.body, self.tcx).ty;
|
2019-09-17 16:25:40 -07:00
|
|
|
match base_ty.ty_adt_def() {
|
|
|
|
Some(def) if def.is_union() => {
|
|
|
|
self.check_op(ops::UnionAccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProjectionElem::Downcast(..) => {
|
|
|
|
self.check_op(ops::Downcast);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_source_info(&mut self, source_info: &SourceInfo) {
|
|
|
|
trace!("visit_source_info: source_info={:?}", source_info);
|
|
|
|
self.span = source_info.span;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
|
|
|
|
trace!("visit_statement: statement={:?} location={:?}", statement, location);
|
|
|
|
|
|
|
|
match statement.kind {
|
2019-11-30 12:25:45 -05:00
|
|
|
StatementKind::Assign(..) | StatementKind::SetDiscriminant { .. } => {
|
2019-09-17 16:25:40 -07:00
|
|
|
self.super_statement(statement, location);
|
|
|
|
}
|
|
|
|
StatementKind::FakeRead(FakeReadCause::ForMatchedPlace, _) => {
|
2019-11-10 10:39:27 -08:00
|
|
|
self.check_op(ops::IfOrMatch);
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
// FIXME(eddyb) should these really do nothing?
|
2019-12-22 17:42:04 -05:00
|
|
|
StatementKind::FakeRead(..)
|
|
|
|
| StatementKind::StorageLive(_)
|
|
|
|
| StatementKind::StorageDead(_)
|
2020-01-14 13:40:42 +00:00
|
|
|
| StatementKind::LlvmInlineAsm { .. }
|
2019-12-22 17:42:04 -05:00
|
|
|
| StatementKind::Retag { .. }
|
|
|
|
| StatementKind::AscribeUserType(..)
|
|
|
|
| StatementKind::Nop => {}
|
2019-09-17 16:25:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_terminator_kind(&mut self, kind: &TerminatorKind<'tcx>, location: Location) {
|
|
|
|
trace!("visit_terminator_kind: kind={:?} location={:?}", kind, location);
|
|
|
|
self.super_terminator_kind(kind, location);
|
|
|
|
|
|
|
|
match kind {
|
|
|
|
TerminatorKind::Call { func, .. } => {
|
2019-12-03 12:55:58 +02:00
|
|
|
let fn_ty = func.ty(*self.body, self.tcx);
|
2019-09-17 16:25:40 -07:00
|
|
|
|
2020-02-04 14:03:16 -08:00
|
|
|
let (def_id, substs) = match fn_ty.kind {
|
|
|
|
ty::FnDef(def_id, substs) => (def_id, substs),
|
2019-09-17 16:25:40 -07:00
|
|
|
|
|
|
|
ty::FnPtr(_) => {
|
|
|
|
self.check_op(ops::FnCallIndirect);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.check_op(ops::FnCallOther);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// At this point, we are calling a function whose `DefId` is known...
|
2020-01-01 18:06:00 +01:00
|
|
|
if is_const_fn(self.tcx, def_id) {
|
2019-09-17 16:25:40 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-04 14:03:16 -08:00
|
|
|
// See if this is a trait method for a concrete type whose impl of that trait is
|
|
|
|
// `const`.
|
|
|
|
if self.tcx.features().const_trait_impl {
|
|
|
|
let instance = Instance::resolve(self.tcx, self.param_env, def_id, substs);
|
|
|
|
debug!("Resolving ({:?}) -> {:?}", def_id, instance);
|
|
|
|
if let Some(func) = instance {
|
|
|
|
if let InstanceDef::Item(def_id) = func.def {
|
|
|
|
if is_const_fn(self.tcx, def_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 16:25:40 -07:00
|
|
|
if is_lang_panic_fn(self.tcx, def_id) {
|
|
|
|
self.check_op(ops::Panic);
|
2020-01-01 18:06:00 +01:00
|
|
|
} else if let Some(feature) = is_unstable_const_fn(self.tcx, def_id) {
|
2019-09-17 16:25:40 -07:00
|
|
|
// Exempt unstable const fns inside of macros with
|
|
|
|
// `#[allow_internal_unstable]`.
|
|
|
|
if !self.span.allows_unstable(feature) {
|
|
|
|
self.check_op(ops::FnCallUnstable(def_id, feature));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.check_op(ops::FnCallNonConst(def_id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forbid all `Drop` terminators unless the place being dropped is a local with no
|
|
|
|
// projections that cannot be `NeedsDrop`.
|
2019-12-22 17:42:04 -05:00
|
|
|
TerminatorKind::Drop { location: dropped_place, .. }
|
|
|
|
| TerminatorKind::DropAndReplace { location: dropped_place, .. } => {
|
2019-09-17 16:25:40 -07:00
|
|
|
let mut err_span = self.span;
|
|
|
|
|
|
|
|
// Check to see if the type of this place can ever have a drop impl. If not, this
|
|
|
|
// `Drop` terminator is frivolous.
|
2019-12-22 17:42:04 -05:00
|
|
|
let ty_needs_drop =
|
|
|
|
dropped_place.ty(*self.body, self.tcx).ty.needs_drop(self.tcx, self.param_env);
|
2019-09-17 16:25:40 -07:00
|
|
|
|
|
|
|
if !ty_needs_drop {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-20 16:09:36 -04:00
|
|
|
let needs_drop = if let Some(local) = dropped_place.as_local() {
|
2019-09-17 16:25:40 -07:00
|
|
|
// Use the span where the local was declared as the span of the drop error.
|
|
|
|
err_span = self.body.local_decls[local].source_info.span;
|
2020-02-03 10:33:29 -08:00
|
|
|
self.qualifs.needs_drop(local, location)
|
2019-09-17 16:25:40 -07:00
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
if needs_drop {
|
|
|
|
self.check_op_spanned(ops::LiveDrop, err_span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 17:19:58 -07:00
|
|
|
|
|
|
|
fn error_min_const_fn_violation(tcx: TyCtxt<'_>, span: Span, msg: Cow<'_, str>) {
|
|
|
|
struct_span_err!(tcx.sess, span, E0723, "{}", msg)
|
2020-02-07 13:06:35 +01:00
|
|
|
.note(
|
|
|
|
"see issue #57563 <https://github.com/rust-lang/rust/issues/57563> \
|
|
|
|
for more information",
|
|
|
|
)
|
2019-10-29 17:19:58 -07:00
|
|
|
.help("add `#![feature(const_fn)]` to the crate attributes to enable")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_short_circuiting_in_const_local(item: &Item<'_, 'tcx>) {
|
|
|
|
let body = item.body;
|
|
|
|
|
|
|
|
if body.control_flow_destroyed.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut locals = body.vars_iter();
|
|
|
|
if let Some(local) = locals.next() {
|
|
|
|
let span = body.local_decls[local].source_info.span;
|
|
|
|
let mut error = item.tcx.sess.struct_span_err(
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"new features like let bindings are not permitted in {}s \
|
|
|
|
which also use short circuiting operators",
|
|
|
|
item.const_kind(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
for (span, kind) in body.control_flow_destroyed.iter() {
|
|
|
|
error.span_note(
|
|
|
|
*span,
|
2019-12-22 17:42:04 -05:00
|
|
|
&format!(
|
|
|
|
"use of {} here does not actually short circuit due to \
|
2020-02-07 13:06:35 +01:00
|
|
|
the const evaluator presently not being able to do control flow. \
|
|
|
|
See issue #49146 <https://github.com/rust-lang/rust/issues/49146> \
|
|
|
|
for more information.",
|
2019-12-22 17:42:04 -05:00
|
|
|
kind
|
|
|
|
),
|
2019-10-29 17:19:58 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
for local in locals {
|
|
|
|
let span = body.local_decls[local].source_info.span;
|
2020-01-22 23:57:38 +00:00
|
|
|
error.span_note(span, "more locals are defined here");
|
2019-10-29 17:19:58 -07:00
|
|
|
}
|
|
|
|
error.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_return_ty_is_sync(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, hir_id: HirId) {
|
|
|
|
let ty = body.return_ty();
|
|
|
|
tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
let cause = traits::ObligationCause::new(body.span, hir_id, traits::SharedStatic);
|
|
|
|
let mut fulfillment_cx = traits::FulfillmentContext::new();
|
|
|
|
let sync_def_id = tcx.require_lang_item(lang_items::SyncTraitLangItem, Some(body.span));
|
|
|
|
fulfillment_cx.register_bound(&infcx, ty::ParamEnv::empty(), ty, sync_def_id, cause);
|
|
|
|
if let Err(err) = fulfillment_cx.select_all_or_error(&infcx) {
|
|
|
|
infcx.report_fulfillment_errors(&err, None, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-11-22 15:52:59 -08:00
|
|
|
|
|
|
|
fn place_as_reborrow(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &Body<'tcx>,
|
|
|
|
place: &'a Place<'tcx>,
|
|
|
|
) -> Option<&'a [PlaceElem<'tcx>]> {
|
2019-12-22 17:42:04 -05:00
|
|
|
place.projection.split_last().and_then(|(outermost, inner)| {
|
|
|
|
if outermost != &ProjectionElem::Deref {
|
|
|
|
return None;
|
|
|
|
}
|
2019-11-22 15:52:59 -08:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
// A borrow of a `static` also looks like `&(*_1)` in the MIR, but `_1` is a `const`
|
|
|
|
// that points to the allocation for the static. Don't treat these as reborrows.
|
2019-12-11 16:50:03 -03:00
|
|
|
if body.local_decls[place.local].is_ref_to_static() {
|
|
|
|
return None;
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2019-11-27 14:04:11 -08:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
// Ensure the type being derefed is a reference and not a raw pointer.
|
|
|
|
//
|
|
|
|
// This is sufficient to prevent an access to a `static mut` from being marked as a
|
|
|
|
// reborrow, even if the check above were to disappear.
|
2020-01-14 02:21:42 -03:00
|
|
|
let inner_ty = Place::ty_from(place.local, inner, body, tcx).ty;
|
2019-12-22 17:42:04 -05:00
|
|
|
match inner_ty.kind {
|
|
|
|
ty::Ref(..) => Some(inner),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
2019-11-22 15:52:59 -08:00
|
|
|
}
|