2018-05-10 05:48:53 -04:00
|
|
|
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-05-23 07:57:21 -07:00
|
|
|
use borrow_check::borrow_set::BorrowSet;
|
2018-05-10 05:48:53 -04:00
|
|
|
use borrow_check::location::LocationTable;
|
|
|
|
use borrow_check::{JustWrite, WriteAndRead};
|
|
|
|
use borrow_check::{ShallowOrDeep, Deep, Shallow};
|
|
|
|
use borrow_check::{ReadOrWrite, Activation, Read, Reservation, Write};
|
|
|
|
use borrow_check::{Context, ContextKind};
|
|
|
|
use borrow_check::{LocalMutationIsAllowed, MutateMode};
|
2018-05-14 23:15:56 -07:00
|
|
|
use borrow_check::ArtificialField;
|
2018-05-18 23:47:48 -07:00
|
|
|
use borrow_check::{ReadKind, WriteKind};
|
2018-05-10 05:48:53 -04:00
|
|
|
use borrow_check::nll::facts::AllFacts;
|
2018-05-18 23:47:48 -07:00
|
|
|
use borrow_check::path_utils::*;
|
2018-05-14 23:15:56 -07:00
|
|
|
use dataflow::move_paths::indexes::BorrowIndex;
|
2018-05-15 22:31:29 -07:00
|
|
|
use rustc::hir::def_id::DefId;
|
2018-05-10 05:48:53 -04:00
|
|
|
use rustc::infer::InferCtxt;
|
|
|
|
use rustc::mir::visit::Visitor;
|
2018-05-18 23:47:48 -07:00
|
|
|
use rustc::mir::{BasicBlock, Location, Mir, Place, Rvalue, Local};
|
2018-05-10 05:48:53 -04:00
|
|
|
use rustc::mir::{Statement, StatementKind};
|
|
|
|
use rustc::mir::{Terminator, TerminatorKind};
|
2018-05-14 23:15:56 -07:00
|
|
|
use rustc::mir::{Field, Operand, BorrowKind};
|
2018-05-15 22:31:29 -07:00
|
|
|
use rustc::ty::{self, ParamEnv};
|
2018-05-14 23:15:56 -07:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2018-07-02 06:14:49 -04:00
|
|
|
use rustc_data_structures::graph::dominators::Dominators;
|
2018-05-10 05:48:53 -04:00
|
|
|
|
|
|
|
pub(super) fn generate_invalidates<'cx, 'gcx, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'cx, 'gcx, 'tcx>,
|
|
|
|
all_facts: &mut Option<AllFacts>,
|
|
|
|
location_table: &LocationTable,
|
|
|
|
mir: &Mir<'tcx>,
|
2018-05-15 22:31:29 -07:00
|
|
|
mir_def_id: DefId,
|
2018-05-10 05:48:53 -04:00
|
|
|
borrow_set: &BorrowSet<'tcx>,
|
|
|
|
) {
|
|
|
|
if !all_facts.is_some() {
|
|
|
|
// Nothing to do if we don't have any facts
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:31:29 -07:00
|
|
|
let param_env = infcx.tcx.param_env(mir_def_id);
|
|
|
|
|
2018-05-18 23:47:48 -07:00
|
|
|
if let Some(all_facts) = all_facts {
|
|
|
|
let dominators = mir.dominators();
|
2018-05-15 22:31:29 -07:00
|
|
|
let mut ig = InvalidationGenerator {
|
2018-05-18 23:47:48 -07:00
|
|
|
all_facts,
|
2018-05-15 22:31:29 -07:00
|
|
|
borrow_set,
|
|
|
|
infcx,
|
|
|
|
location_table,
|
|
|
|
mir,
|
2018-05-18 23:47:48 -07:00
|
|
|
dominators,
|
2018-05-15 22:31:29 -07:00
|
|
|
param_env,
|
|
|
|
};
|
|
|
|
ig.visit_mir(mir);
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 'cg = the duration of the constraint generation process itself.
|
2018-05-15 22:31:29 -07:00
|
|
|
struct InvalidationGenerator<'cg, 'cx: 'cg, 'tcx: 'cx, 'gcx: 'tcx> {
|
2018-05-10 05:48:53 -04:00
|
|
|
infcx: &'cg InferCtxt<'cx, 'gcx, 'tcx>,
|
|
|
|
all_facts: &'cg mut AllFacts,
|
|
|
|
location_table: &'cg LocationTable,
|
|
|
|
mir: &'cg Mir<'tcx>,
|
2018-05-18 23:47:48 -07:00
|
|
|
dominators: Dominators<BasicBlock>,
|
2018-05-10 05:48:53 -04:00
|
|
|
borrow_set: &'cg BorrowSet<'tcx>,
|
2018-05-15 22:31:29 -07:00
|
|
|
param_env: ParamEnv<'gcx>,
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Visits the whole MIR and generates invalidates() facts
|
|
|
|
/// Most of the code implementing this was stolen from borrow_check/mod.rs
|
2018-05-15 22:31:29 -07:00
|
|
|
impl<'cg, 'cx, 'tcx, 'gcx> Visitor<'tcx> for InvalidationGenerator<'cg, 'cx, 'tcx, 'gcx> {
|
2018-05-16 07:08:45 -07:00
|
|
|
fn visit_statement(&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
statement: &Statement<'tcx>,
|
|
|
|
location: Location) {
|
2018-05-10 05:48:53 -04:00
|
|
|
match statement.kind {
|
|
|
|
StatementKind::Assign(ref lhs, ref rhs) => {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_rvalue(
|
2018-05-10 05:48:53 -04:00
|
|
|
ContextKind::AssignRhs.new(location),
|
2018-05-14 23:15:56 -07:00
|
|
|
rhs,
|
2018-05-10 05:48:53 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
self.mutate_place(
|
|
|
|
ContextKind::AssignLhs.new(location),
|
2018-05-14 23:15:56 -07:00
|
|
|
lhs,
|
2018-05-10 05:48:53 -04:00
|
|
|
Shallow(None),
|
|
|
|
JustWrite
|
|
|
|
);
|
|
|
|
}
|
2018-05-04 12:04:33 +02:00
|
|
|
StatementKind::ReadForMatch(ref place) => {
|
|
|
|
self.access_place(
|
|
|
|
ContextKind::ReadForMatch.new(location),
|
|
|
|
place,
|
|
|
|
(Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
|
|
|
|
LocalMutationIsAllowed::No,
|
|
|
|
);
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
StatementKind::SetDiscriminant {
|
|
|
|
ref place,
|
|
|
|
variant_index: _,
|
|
|
|
} => {
|
|
|
|
self.mutate_place(
|
|
|
|
ContextKind::SetDiscrim.new(location),
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-05-10 05:48:53 -04:00
|
|
|
Shallow(Some(ArtificialField::Discriminant)),
|
|
|
|
JustWrite,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
StatementKind::InlineAsm {
|
|
|
|
ref asm,
|
|
|
|
ref outputs,
|
|
|
|
ref inputs,
|
|
|
|
} => {
|
|
|
|
let context = ContextKind::InlineAsm.new(location);
|
|
|
|
for (o, output) in asm.outputs.iter().zip(outputs) {
|
|
|
|
if o.is_indirect {
|
|
|
|
// FIXME(eddyb) indirect inline asm outputs should
|
|
|
|
// be encoeded through MIR place derefs instead.
|
|
|
|
self.access_place(
|
|
|
|
context,
|
2018-05-14 23:15:56 -07:00
|
|
|
output,
|
2018-05-10 05:48:53 -04:00
|
|
|
(Deep, Read(ReadKind::Copy)),
|
|
|
|
LocalMutationIsAllowed::No,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
self.mutate_place(
|
|
|
|
context,
|
2018-05-14 23:15:56 -07:00
|
|
|
output,
|
2018-05-10 05:48:53 -04:00
|
|
|
if o.is_rw { Deep } else { Shallow(None) },
|
|
|
|
if o.is_rw { WriteAndRead } else { JustWrite },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for input in inputs {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(context, input);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// EndRegion matters to older NLL/MIR AST borrowck, not to alias NLL
|
|
|
|
StatementKind::EndRegion(..) |
|
|
|
|
StatementKind::Nop |
|
|
|
|
StatementKind::UserAssertTy(..) |
|
|
|
|
StatementKind::Validate(..) |
|
|
|
|
StatementKind::StorageLive(..) => {
|
|
|
|
// `Nop`, `UserAssertTy`, `Validate`, and `StorageLive` are irrelevant
|
|
|
|
// to borrow check.
|
|
|
|
}
|
|
|
|
StatementKind::StorageDead(local) => {
|
|
|
|
self.access_place(
|
|
|
|
ContextKind::StorageDead.new(location),
|
2018-05-14 23:15:56 -07:00
|
|
|
&Place::Local(local),
|
2018-05-10 05:48:53 -04:00
|
|
|
(Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
|
|
|
|
LocalMutationIsAllowed::Yes,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 23:15:56 -07:00
|
|
|
self.super_statement(block, statement, location);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_terminator(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
|
|
|
terminator: &Terminator<'tcx>,
|
|
|
|
location: Location
|
|
|
|
) {
|
|
|
|
match terminator.kind {
|
|
|
|
TerminatorKind::SwitchInt {
|
|
|
|
ref discr,
|
|
|
|
switch_ty: _,
|
|
|
|
values: _,
|
|
|
|
targets: _,
|
|
|
|
} => {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(ContextKind::SwitchInt.new(location), discr);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
TerminatorKind::Drop {
|
|
|
|
location: ref drop_place,
|
|
|
|
target: _,
|
|
|
|
unwind: _,
|
|
|
|
} => {
|
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
let gcx = tcx.global_tcx();
|
|
|
|
let drop_place_ty = drop_place.ty(self.mir, tcx);
|
|
|
|
let drop_place_ty = tcx.erase_regions(&drop_place_ty).to_ty(tcx);
|
2018-05-15 22:31:29 -07:00
|
|
|
let drop_place_ty = gcx.lift(&drop_place_ty).unwrap();
|
2018-05-14 23:15:56 -07:00
|
|
|
self.visit_terminator_drop(location, terminator, drop_place, drop_place_ty);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
TerminatorKind::DropAndReplace {
|
|
|
|
location: ref drop_place,
|
|
|
|
value: ref new_value,
|
|
|
|
target: _,
|
|
|
|
unwind: _,
|
|
|
|
} => {
|
|
|
|
self.mutate_place(
|
2018-05-14 23:15:56 -07:00
|
|
|
ContextKind::DropAndReplace.new(location),
|
|
|
|
drop_place,
|
2018-05-10 05:48:53 -04:00
|
|
|
Deep,
|
|
|
|
JustWrite,
|
|
|
|
);
|
|
|
|
self.consume_operand(
|
2018-05-14 23:15:56 -07:00
|
|
|
ContextKind::DropAndReplace.new(location),
|
|
|
|
new_value,
|
2018-05-10 05:48:53 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
TerminatorKind::Call {
|
|
|
|
ref func,
|
|
|
|
ref args,
|
|
|
|
ref destination,
|
|
|
|
cleanup: _,
|
|
|
|
} => {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(ContextKind::CallOperator.new(location), func);
|
2018-05-10 05:48:53 -04:00
|
|
|
for arg in args {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(ContextKind::CallOperand.new(location), arg);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
if let Some((ref dest, _ /*bb*/)) = *destination {
|
|
|
|
self.mutate_place(
|
2018-05-14 23:15:56 -07:00
|
|
|
ContextKind::CallDest.new(location),
|
|
|
|
dest,
|
2018-05-10 05:48:53 -04:00
|
|
|
Deep,
|
|
|
|
JustWrite,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Assert {
|
|
|
|
ref cond,
|
|
|
|
expected: _,
|
|
|
|
ref msg,
|
|
|
|
target: _,
|
|
|
|
cleanup: _,
|
|
|
|
} => {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(ContextKind::Assert.new(location), cond);
|
2018-05-10 05:48:53 -04:00
|
|
|
use rustc::mir::interpret::EvalErrorKind::BoundsCheck;
|
|
|
|
if let BoundsCheck { ref len, ref index } = *msg {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(ContextKind::Assert.new(location), len);
|
|
|
|
self.consume_operand(ContextKind::Assert.new(location), index);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Yield {
|
|
|
|
ref value,
|
2018-05-17 00:49:06 -07:00
|
|
|
resume,
|
2018-05-10 05:48:53 -04:00
|
|
|
drop: _,
|
|
|
|
} => {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(ContextKind::Yield.new(location), value);
|
2018-05-10 05:48:53 -04:00
|
|
|
|
2018-05-17 00:49:06 -07:00
|
|
|
// Invalidate all borrows of local places
|
|
|
|
let borrow_set = self.borrow_set.clone();
|
|
|
|
let resume = self.location_table.start_index(resume.start_location());
|
|
|
|
for i in borrow_set.borrows.indices() {
|
|
|
|
if borrow_of_local_data(&borrow_set.borrows[i].borrowed_place) {
|
|
|
|
self.all_facts.invalidates.push((resume, i));
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
TerminatorKind::Resume | TerminatorKind::Return | TerminatorKind::GeneratorDrop => {
|
2018-05-17 00:49:06 -07:00
|
|
|
// Invalidate all borrows of local places
|
|
|
|
let borrow_set = self.borrow_set.clone();
|
|
|
|
let start = self.location_table.start_index(location);
|
|
|
|
for i in borrow_set.borrows.indices() {
|
|
|
|
if borrow_of_local_data(&borrow_set.borrows[i].borrowed_place) {
|
|
|
|
self.all_facts.invalidates.push((start, i));
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
TerminatorKind::Goto { target: _ }
|
|
|
|
| TerminatorKind::Abort
|
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
| TerminatorKind::FalseEdges {
|
|
|
|
real_target: _,
|
|
|
|
imaginary_targets: _,
|
|
|
|
}
|
|
|
|
| TerminatorKind::FalseUnwind {
|
|
|
|
real_target: _,
|
|
|
|
unwind: _,
|
|
|
|
} => {
|
|
|
|
// no data used, thus irrelevant to borrowck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_terminator(block, terminator, location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:31:29 -07:00
|
|
|
impl<'cg, 'cx, 'tcx, 'gcx> InvalidationGenerator<'cg, 'cx, 'tcx, 'gcx> {
|
2018-05-10 05:48:53 -04:00
|
|
|
/// Simulates dropping of a variable
|
|
|
|
fn visit_terminator_drop(
|
|
|
|
&mut self,
|
|
|
|
loc: Location,
|
|
|
|
term: &Terminator<'tcx>,
|
|
|
|
drop_place: &Place<'tcx>,
|
|
|
|
erased_drop_place_ty: ty::Ty<'gcx>,
|
|
|
|
) {
|
|
|
|
let gcx = self.infcx.tcx.global_tcx();
|
|
|
|
let drop_field = |
|
2018-05-15 22:31:29 -07:00
|
|
|
ig: &mut InvalidationGenerator<'cg, 'cx, 'gcx, 'tcx>,
|
|
|
|
(index, field): (usize, ty::Ty<'gcx>),
|
2018-05-10 05:48:53 -04:00
|
|
|
| {
|
2018-05-15 22:31:29 -07:00
|
|
|
let field_ty = gcx.normalize_erasing_regions(ig.param_env, field);
|
2018-05-10 05:48:53 -04:00
|
|
|
let place = drop_place.clone().field(Field::new(index), field_ty);
|
|
|
|
|
2018-05-14 23:15:56 -07:00
|
|
|
ig.visit_terminator_drop(loc, term, &place, field_ty);
|
2018-05-10 05:48:53 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
match erased_drop_place_ty.sty {
|
|
|
|
// When a struct is being dropped, we need to check
|
|
|
|
// whether it has a destructor, if it does, then we can
|
|
|
|
// call it, if it does not then we need to check the
|
|
|
|
// individual fields instead. This way if `foo` has a
|
|
|
|
// destructor but `bar` does not, we will only check for
|
|
|
|
// borrows of `x.foo` and not `x.bar`. See #47703.
|
|
|
|
ty::TyAdt(def, substs) if def.is_struct() && !def.has_dtor(self.infcx.tcx) => {
|
|
|
|
def.all_fields()
|
|
|
|
.map(|field| field.ty(gcx, substs))
|
|
|
|
.enumerate()
|
|
|
|
.for_each(|field| drop_field(self, field));
|
|
|
|
}
|
|
|
|
// Same as above, but for tuples.
|
|
|
|
ty::TyTuple(tys) => {
|
|
|
|
tys.iter().cloned().enumerate()
|
|
|
|
.for_each(|field| drop_field(self, field));
|
|
|
|
}
|
|
|
|
// Closures and generators also have disjoint fields, but they are only
|
|
|
|
// directly accessed in the body of the closure/generator.
|
2018-05-14 23:15:56 -07:00
|
|
|
ty::TyGenerator(def, substs, ..)
|
2018-05-10 05:48:53 -04:00
|
|
|
if *drop_place == Place::Local(Local::new(1)) && !self.mir.upvar_decls.is_empty()
|
|
|
|
=> {
|
|
|
|
substs.upvar_tys(def, self.infcx.tcx).enumerate()
|
|
|
|
.for_each(|field| drop_field(self, field));
|
|
|
|
}
|
2018-05-14 23:15:56 -07:00
|
|
|
ty::TyClosure(def, substs)
|
|
|
|
if *drop_place == Place::Local(Local::new(1)) && !self.mir.upvar_decls.is_empty()
|
|
|
|
=> {
|
|
|
|
substs.upvar_tys(def, self.infcx.tcx).enumerate()
|
|
|
|
.for_each(|field| drop_field(self, field));
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
_ => {
|
|
|
|
// We have now refined the type of the value being
|
|
|
|
// dropped (potentially) to just the type of a
|
|
|
|
// subfield; so check whether that field's type still
|
|
|
|
// "needs drop". If so, we assume that the destructor
|
|
|
|
// may access any data it likes (i.e., a Deep Write).
|
|
|
|
if erased_drop_place_ty.needs_drop(gcx, self.param_env) {
|
|
|
|
self.access_place(
|
|
|
|
ContextKind::Drop.new(loc),
|
2018-05-14 23:15:56 -07:00
|
|
|
drop_place,
|
2018-05-10 05:48:53 -04:00
|
|
|
(Deep, Write(WriteKind::StorageDeadOrDrop)),
|
|
|
|
LocalMutationIsAllowed::Yes,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simulates mutation of a place
|
|
|
|
fn mutate_place(
|
|
|
|
&mut self,
|
|
|
|
context: Context,
|
2018-05-14 23:15:56 -07:00
|
|
|
place: &Place<'tcx>,
|
|
|
|
kind: ShallowOrDeep,
|
2018-05-15 22:31:29 -07:00
|
|
|
_mode: MutateMode,
|
2018-05-10 05:48:53 -04:00
|
|
|
) {
|
|
|
|
self.access_place(
|
|
|
|
context,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-05-10 05:48:53 -04:00
|
|
|
(kind, Write(WriteKind::Mutate)),
|
|
|
|
LocalMutationIsAllowed::ExceptUpvars,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simulates consumption of an operand
|
|
|
|
fn consume_operand(
|
|
|
|
&mut self,
|
|
|
|
context: Context,
|
2018-05-14 23:15:56 -07:00
|
|
|
operand: &Operand<'tcx>,
|
2018-05-10 05:48:53 -04:00
|
|
|
) {
|
|
|
|
match *operand {
|
|
|
|
Operand::Copy(ref place) => {
|
|
|
|
self.access_place(
|
|
|
|
context,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-05-10 05:48:53 -04:00
|
|
|
(Deep, Read(ReadKind::Copy)),
|
|
|
|
LocalMutationIsAllowed::No,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Operand::Move(ref place) => {
|
|
|
|
self.access_place(
|
|
|
|
context,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
|
|
|
(Deep, Write(WriteKind::Move)),
|
2018-05-10 05:48:53 -04:00
|
|
|
LocalMutationIsAllowed::Yes,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Operand::Constant(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulates consumption of an rvalue
|
|
|
|
fn consume_rvalue(
|
|
|
|
&mut self,
|
|
|
|
context: Context,
|
2018-05-14 23:15:56 -07:00
|
|
|
rvalue: &Rvalue<'tcx>,
|
2018-05-10 05:48:53 -04:00
|
|
|
) {
|
|
|
|
match *rvalue {
|
|
|
|
Rvalue::Ref(_ /*rgn*/, bk, ref place) => {
|
|
|
|
let access_kind = match bk {
|
|
|
|
BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
|
|
|
|
BorrowKind::Unique | BorrowKind::Mut { .. } => {
|
|
|
|
let wk = WriteKind::MutableBorrow(bk);
|
2018-05-18 23:47:48 -07:00
|
|
|
if allow_two_phase_borrow(&self.infcx.tcx, bk) {
|
2018-05-10 05:48:53 -04:00
|
|
|
(Deep, Reservation(wk))
|
|
|
|
} else {
|
|
|
|
(Deep, Write(wk))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.access_place(
|
|
|
|
context,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-05-10 05:48:53 -04:00
|
|
|
access_kind,
|
|
|
|
LocalMutationIsAllowed::No,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rvalue::Use(ref operand)
|
|
|
|
| Rvalue::Repeat(ref operand, _)
|
|
|
|
| Rvalue::UnaryOp(_ /*un_op*/, ref operand)
|
|
|
|
| Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/) => {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(context, operand)
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Rvalue::Len(ref place) | Rvalue::Discriminant(ref place) => {
|
|
|
|
let af = match *rvalue {
|
|
|
|
Rvalue::Len(..) => ArtificialField::ArrayLength,
|
|
|
|
Rvalue::Discriminant(..) => ArtificialField::Discriminant,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
self.access_place(
|
|
|
|
context,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-05-10 05:48:53 -04:00
|
|
|
(Shallow(Some(af)), Read(ReadKind::Copy)),
|
|
|
|
LocalMutationIsAllowed::No,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rvalue::BinaryOp(_bin_op, ref operand1, ref operand2)
|
|
|
|
| Rvalue::CheckedBinaryOp(_bin_op, ref operand1, ref operand2) => {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(context, operand1);
|
|
|
|
self.consume_operand(context, operand2);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Rvalue::NullaryOp(_op, _ty) => {
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:31:29 -07:00
|
|
|
Rvalue::Aggregate(_, ref operands) => {
|
2018-05-10 05:48:53 -04:00
|
|
|
for operand in operands {
|
2018-05-14 23:15:56 -07:00
|
|
|
self.consume_operand(context, operand);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Simulates an access to a place
|
|
|
|
fn access_place(
|
|
|
|
&mut self,
|
|
|
|
context: Context,
|
2018-05-14 23:15:56 -07:00
|
|
|
place: &Place<'tcx>,
|
2018-05-10 05:48:53 -04:00
|
|
|
kind: (ShallowOrDeep, ReadOrWrite),
|
2018-05-15 22:31:29 -07:00
|
|
|
_is_local_mutation_allowed: LocalMutationIsAllowed,
|
2018-05-10 05:48:53 -04:00
|
|
|
) {
|
|
|
|
let (sd, rw) = kind;
|
|
|
|
// note: not doing check_access_permissions checks because they don't generate invalidates
|
2018-05-14 23:15:56 -07:00
|
|
|
self.check_access_for_conflict(context, place, sd, rw);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_access_for_conflict(
|
|
|
|
&mut self,
|
|
|
|
context: Context,
|
2018-05-14 23:15:56 -07:00
|
|
|
place: &Place<'tcx>,
|
2018-05-10 05:48:53 -04:00
|
|
|
sd: ShallowOrDeep,
|
|
|
|
rw: ReadOrWrite,
|
|
|
|
) {
|
|
|
|
debug!(
|
2018-05-14 23:15:56 -07:00
|
|
|
"invalidation::check_access_for_conflict(context={:?}, place={:?}, sd={:?}, \
|
2018-05-10 05:48:53 -04:00
|
|
|
rw={:?})",
|
|
|
|
context,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-05-10 05:48:53 -04:00
|
|
|
sd,
|
|
|
|
rw,
|
|
|
|
);
|
2018-05-18 23:47:48 -07:00
|
|
|
let tcx = self.infcx.tcx;
|
|
|
|
let mir = self.mir;
|
|
|
|
let borrow_set = self.borrow_set.clone();
|
|
|
|
let indices = self.borrow_set.borrows.indices();
|
|
|
|
each_borrow_involving_path(
|
|
|
|
self,
|
|
|
|
tcx,
|
|
|
|
mir,
|
2018-05-10 05:48:53 -04:00
|
|
|
context,
|
2018-05-14 23:15:56 -07:00
|
|
|
(sd, place),
|
2018-05-18 23:47:48 -07:00
|
|
|
&borrow_set.clone(),
|
|
|
|
indices,
|
2018-05-23 07:57:21 -07:00
|
|
|
|this, borrow_index, borrow| {
|
2018-05-18 23:47:48 -07:00
|
|
|
match (rw, borrow.kind) {
|
|
|
|
// Obviously an activation is compatible with its own
|
|
|
|
// reservation (or even prior activating uses of same
|
|
|
|
// borrow); so don't check if they interfere.
|
|
|
|
//
|
|
|
|
// NOTE: *reservations* do conflict with themselves;
|
|
|
|
// thus aren't injecting unsoundenss w/ this check.)
|
|
|
|
(Activation(_, activating), _) if activating == borrow_index => {
|
|
|
|
// Activating a borrow doesn't generate any invalidations, since we
|
|
|
|
// have already taken the reservation
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
|
2018-05-18 23:47:48 -07:00
|
|
|
(Read(_), BorrowKind::Shared) | (Reservation(..), BorrowKind::Shared) => {
|
|
|
|
// Reads/reservations don't invalidate shared borrows
|
2018-05-14 23:15:56 -07:00
|
|
|
}
|
|
|
|
|
2018-05-18 23:47:48 -07:00
|
|
|
(Read(_), BorrowKind::Unique) | (Read(_), BorrowKind::Mut { .. }) => {
|
|
|
|
// Reading from mere reservations of mutable-borrows is OK.
|
|
|
|
if !is_active(&this.dominators, borrow, context.loc) {
|
|
|
|
// If the borrow isn't active yet, reads don't invalidate it
|
|
|
|
assert!(allow_two_phase_borrow(&this.infcx.tcx, borrow.kind));
|
|
|
|
return Control::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unique and mutable borrows are invalidated by reads from any
|
|
|
|
// involved path
|
|
|
|
this.generate_invalidates(borrow_index, context.loc);
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
|
2018-05-18 23:47:48 -07:00
|
|
|
(Reservation(_), BorrowKind::Unique)
|
|
|
|
| (Reservation(_), BorrowKind::Mut { .. })
|
|
|
|
| (Activation(_, _), _)
|
|
|
|
| (Write(_), _) => {
|
|
|
|
// unique or mutable borrows are invalidated by writes.
|
|
|
|
// Reservations count as writes since we need to check
|
|
|
|
// that activating the borrow will be OK
|
|
|
|
// TOOD(bob_twinkles) is this actually the right thing to do?
|
|
|
|
this.generate_invalidates(borrow_index, context.loc);
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
2018-05-18 23:47:48 -07:00
|
|
|
Control::Continue
|
2018-05-10 05:48:53 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Generate a new invalidates(L, B) fact
|
2018-05-14 23:15:56 -07:00
|
|
|
fn generate_invalidates(&mut self, b: BorrowIndex, l: Location) {
|
|
|
|
let lidx = self.location_table.mid_index(l);
|
|
|
|
self.all_facts.invalidates.push((lidx, b));
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
}
|
2018-05-17 00:49:06 -07:00
|
|
|
|