2019-02-08 06:28:15 +09:00
|
|
|
use crate::borrow_check::borrow_set::BorrowSet;
|
|
|
|
use crate::borrow_check::location::LocationTable;
|
|
|
|
use crate::borrow_check::{JustWrite, WriteAndRead};
|
|
|
|
use crate::borrow_check::{AccessDepth, Deep, Shallow};
|
|
|
|
use crate::borrow_check::{ReadOrWrite, Activation, Read, Reservation, Write};
|
|
|
|
use crate::borrow_check::{LocalMutationIsAllowed, MutateMode};
|
|
|
|
use crate::borrow_check::ArtificialField;
|
|
|
|
use crate::borrow_check::{ReadKind, WriteKind};
|
|
|
|
use crate::borrow_check::nll::facts::AllFacts;
|
|
|
|
use crate::borrow_check::path_utils::*;
|
2019-04-01 19:38:00 +01:00
|
|
|
use crate::dataflow::indexes::BorrowIndex;
|
2018-09-23 16:07:45 +01:00
|
|
|
use rustc::ty::TyCtxt;
|
2018-05-10 05:48:53 -04:00
|
|
|
use rustc::mir::visit::Visitor;
|
2019-02-22 05:24:03 +01:00
|
|
|
use rustc::mir::{BasicBlock, Location, Mir, Place, PlaceBase, Rvalue};
|
2018-05-10 05:48:53 -04:00
|
|
|
use rustc::mir::{Statement, StatementKind};
|
2019-04-22 21:08:52 +01:00
|
|
|
use rustc::mir::TerminatorKind;
|
2018-09-23 16:07:45 +01:00
|
|
|
use rustc::mir::{Operand, BorrowKind};
|
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>(
|
2018-09-23 16:07:45 +01:00
|
|
|
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
|
2018-05-10 05:48:53 -04:00
|
|
|
all_facts: &mut Option<AllFacts>,
|
|
|
|
location_table: &LocationTable,
|
|
|
|
mir: &Mir<'tcx>,
|
|
|
|
borrow_set: &BorrowSet<'tcx>,
|
|
|
|
) {
|
2018-10-17 16:58:12 +02:00
|
|
|
if all_facts.is_none() {
|
2018-05-10 05:48:53 -04:00
|
|
|
// Nothing to do if we don't have any facts
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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,
|
2018-09-23 16:07:45 +01:00
|
|
|
tcx,
|
2018-05-15 22:31:29 -07:00
|
|
|
location_table,
|
|
|
|
mir,
|
2018-05-18 23:47:48 -07:00
|
|
|
dominators,
|
2018-05-15 22:31:29 -07:00
|
|
|
};
|
|
|
|
ig.visit_mir(mir);
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
2018-09-23 16:07:45 +01:00
|
|
|
struct InvalidationGenerator<'cx, 'tcx: 'cx, 'gcx: 'tcx> {
|
|
|
|
tcx: TyCtxt<'cx, 'gcx, 'tcx>,
|
|
|
|
all_facts: &'cx mut AllFacts,
|
|
|
|
location_table: &'cx LocationTable,
|
|
|
|
mir: &'cx Mir<'tcx>,
|
2018-05-18 23:47:48 -07:00
|
|
|
dominators: Dominators<BasicBlock>,
|
2018-09-23 16:07:45 +01:00
|
|
|
borrow_set: &'cx BorrowSet<'tcx>,
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Visits the whole MIR and generates `invalidates()` facts.
|
|
|
|
/// Most of the code implementing this was stolen from `borrow_check/mod.rs`.
|
2018-09-23 16:07:45 +01:00
|
|
|
impl<'cx, 'tcx, 'gcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx, 'gcx> {
|
2018-12-04 10:03:34 -05:00
|
|
|
fn visit_statement(
|
|
|
|
&mut self,
|
|
|
|
statement: &Statement<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
self.check_activations(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(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-05-14 23:15:56 -07:00
|
|
|
rhs,
|
2018-05-10 05:48:53 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
self.mutate_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-05-14 23:15:56 -07:00
|
|
|
lhs,
|
2018-05-10 05:48:53 -04:00
|
|
|
Shallow(None),
|
|
|
|
JustWrite
|
|
|
|
);
|
|
|
|
}
|
2019-02-02 16:38:12 +00:00
|
|
|
StatementKind::FakeRead(_, _) => {
|
|
|
|
// Only relavent for initialized/liveness/safety checks.
|
2018-05-04 12:04:33 +02:00
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
StatementKind::SetDiscriminant {
|
|
|
|
ref place,
|
|
|
|
variant_index: _,
|
|
|
|
} => {
|
|
|
|
self.mutate_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-12-13 20:53:07 +00:00
|
|
|
Shallow(None),
|
2018-05-10 05:48:53 -04:00
|
|
|
JustWrite,
|
|
|
|
);
|
|
|
|
}
|
2019-04-02 20:07:09 +11:00
|
|
|
StatementKind::InlineAsm(ref asm) => {
|
|
|
|
for (o, output) in asm.asm.outputs.iter().zip(asm.outputs.iter()) {
|
2018-05-10 05:48:53 -04:00
|
|
|
if o.is_indirect {
|
|
|
|
// FIXME(eddyb) indirect inline asm outputs should
|
2019-04-02 20:07:09 +11:00
|
|
|
// be encoded through MIR place derefs instead.
|
2018-05-10 05:48:53 -04:00
|
|
|
self.access_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
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(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
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 },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 20:07:09 +11:00
|
|
|
for (_, input) in asm.inputs.iter() {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(location, input);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
StatementKind::Nop |
|
2018-08-31 18:59:35 -04:00
|
|
|
StatementKind::AscribeUserType(..) |
|
2018-10-24 11:47:17 +02:00
|
|
|
StatementKind::Retag { .. } |
|
2018-05-10 05:48:53 -04:00
|
|
|
StatementKind::StorageLive(..) => {
|
2018-10-24 11:47:17 +02:00
|
|
|
// `Nop`, `AscribeUserType`, `Retag`, and `StorageLive` are irrelevant
|
2018-05-10 05:48:53 -04:00
|
|
|
// to borrow check.
|
|
|
|
}
|
|
|
|
StatementKind::StorageDead(local) => {
|
|
|
|
self.access_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2019-02-22 05:24:03 +01:00
|
|
|
&Place::Base(PlaceBase::Local(local)),
|
2018-09-23 16:07:45 +01:00
|
|
|
(Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
|
2018-05-10 05:48:53 -04:00
|
|
|
LocalMutationIsAllowed::Yes,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:07:14 +01:00
|
|
|
self.super_statement(statement, location);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
2019-04-22 21:08:52 +01:00
|
|
|
fn visit_terminator_kind(
|
2018-05-10 05:48:53 -04:00
|
|
|
&mut self,
|
2019-04-22 21:08:52 +01:00
|
|
|
kind: &TerminatorKind<'tcx>,
|
2018-05-10 05:48:53 -04:00
|
|
|
location: Location
|
|
|
|
) {
|
2018-12-04 10:03:34 -05:00
|
|
|
self.check_activations(location);
|
|
|
|
|
2019-04-22 21:08:52 +01:00
|
|
|
match kind {
|
2018-05-10 05:48:53 -04:00
|
|
|
TerminatorKind::SwitchInt {
|
|
|
|
ref discr,
|
|
|
|
switch_ty: _,
|
|
|
|
values: _,
|
|
|
|
targets: _,
|
|
|
|
} => {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(location, discr);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
TerminatorKind::Drop {
|
|
|
|
location: ref drop_place,
|
|
|
|
target: _,
|
|
|
|
unwind: _,
|
|
|
|
} => {
|
2018-09-23 16:07:45 +01:00
|
|
|
self.access_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-09-23 16:07:45 +01:00
|
|
|
drop_place,
|
|
|
|
(AccessDepth::Drop, Write(WriteKind::StorageDeadOrDrop)),
|
|
|
|
LocalMutationIsAllowed::Yes,
|
|
|
|
);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
TerminatorKind::DropAndReplace {
|
|
|
|
location: ref drop_place,
|
|
|
|
value: ref new_value,
|
|
|
|
target: _,
|
|
|
|
unwind: _,
|
|
|
|
} => {
|
|
|
|
self.mutate_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-05-14 23:15:56 -07:00
|
|
|
drop_place,
|
2018-05-10 05:48:53 -04:00
|
|
|
Deep,
|
|
|
|
JustWrite,
|
|
|
|
);
|
|
|
|
self.consume_operand(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-05-14 23:15:56 -07:00
|
|
|
new_value,
|
2018-05-10 05:48:53 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
TerminatorKind::Call {
|
|
|
|
ref func,
|
|
|
|
ref args,
|
|
|
|
ref destination,
|
|
|
|
cleanup: _,
|
2018-09-29 10:34:12 +01:00
|
|
|
from_hir_call: _,
|
2018-05-10 05:48:53 -04:00
|
|
|
} => {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(location, func);
|
2018-05-10 05:48:53 -04:00
|
|
|
for arg in args {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(location, arg);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
if let Some((ref dest, _ /*bb*/)) = *destination {
|
|
|
|
self.mutate_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-05-14 23:15:56 -07:00
|
|
|
dest,
|
2018-05-10 05:48:53 -04:00
|
|
|
Deep,
|
|
|
|
JustWrite,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TerminatorKind::Assert {
|
|
|
|
ref cond,
|
|
|
|
expected: _,
|
|
|
|
ref msg,
|
|
|
|
target: _,
|
|
|
|
cleanup: _,
|
|
|
|
} => {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(location, cond);
|
2019-04-02 01:02:18 +09:00
|
|
|
use rustc::mir::interpret::InterpError::BoundsCheck;
|
2018-05-10 05:48:53 -04:00
|
|
|
if let BoundsCheck { ref len, ref index } = *msg {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(location, len);
|
|
|
|
self.consume_operand(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: _,
|
|
|
|
} => {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 21:08:52 +01:00
|
|
|
self.super_terminator_kind(kind, location);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 16:07:45 +01:00
|
|
|
impl<'cg, 'cx, 'tcx, 'gcx> InvalidationGenerator<'cx, 'tcx, 'gcx> {
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Simulates mutation of a place.
|
2018-05-10 05:48:53 -04:00
|
|
|
fn mutate_place(
|
|
|
|
&mut self,
|
2019-05-02 06:03:17 +09:00
|
|
|
location: Location,
|
2018-05-14 23:15:56 -07:00
|
|
|
place: &Place<'tcx>,
|
2018-09-23 16:07:45 +01:00
|
|
|
kind: AccessDepth,
|
2018-05-15 22:31:29 -07:00
|
|
|
_mode: MutateMode,
|
2018-05-10 05:48:53 -04:00
|
|
|
) {
|
|
|
|
self.access_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-05-10 05:48:53 -04:00
|
|
|
(kind, Write(WriteKind::Mutate)),
|
|
|
|
LocalMutationIsAllowed::ExceptUpvars,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Simulates consumption of an operand.
|
2018-05-10 05:48:53 -04:00
|
|
|
fn consume_operand(
|
|
|
|
&mut self,
|
2019-05-02 06:03:17 +09:00
|
|
|
location: Location,
|
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(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
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(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
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,
|
2019-05-02 06:03:17 +09:00
|
|
|
location: Location,
|
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 {
|
2018-09-10 22:33:45 +01:00
|
|
|
BorrowKind::Shallow => {
|
|
|
|
(Shallow(Some(ArtificialField::ShallowBorrow)), Read(ReadKind::Borrow(bk)))
|
|
|
|
},
|
2018-05-10 05:48:53 -04:00
|
|
|
BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
|
|
|
|
BorrowKind::Unique | BorrowKind::Mut { .. } => {
|
|
|
|
let wk = WriteKind::MutableBorrow(bk);
|
2019-04-28 21:13:33 +02:00
|
|
|
if allow_two_phase_borrow(bk) {
|
2018-05-10 05:48:53 -04:00
|
|
|
(Deep, Reservation(wk))
|
|
|
|
} else {
|
|
|
|
(Deep, Write(wk))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.access_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
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*/) => {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(location, operand)
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Rvalue::Len(ref place) | Rvalue::Discriminant(ref place) => {
|
|
|
|
let af = match *rvalue {
|
2018-12-13 20:53:07 +00:00
|
|
|
Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
|
|
|
|
Rvalue::Discriminant(..) => None,
|
2018-05-10 05:48:53 -04:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
self.access_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-12-13 20:53:07 +00:00
|
|
|
(Shallow(af), Read(ReadKind::Copy)),
|
2018-05-10 05:48:53 -04:00
|
|
|
LocalMutationIsAllowed::No,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rvalue::BinaryOp(_bin_op, ref operand1, ref operand2)
|
|
|
|
| Rvalue::CheckedBinaryOp(_bin_op, ref operand1, ref operand2) => {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(location, operand1);
|
|
|
|
self.consume_operand(location, 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 {
|
2019-05-02 06:03:17 +09:00
|
|
|
self.consume_operand(location, operand);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Simulates an access to a place.
|
2018-05-10 05:48:53 -04:00
|
|
|
fn access_place(
|
|
|
|
&mut self,
|
2019-05-02 06:03:17 +09:00
|
|
|
location: Location,
|
2018-05-14 23:15:56 -07:00
|
|
|
place: &Place<'tcx>,
|
2018-09-23 16:07:45 +01:00
|
|
|
kind: (AccessDepth, 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
|
2019-05-02 06:03:17 +09:00
|
|
|
self.check_access_for_conflict(location, place, sd, rw);
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_access_for_conflict(
|
|
|
|
&mut self,
|
2019-05-02 06:03:17 +09:00
|
|
|
location: Location,
|
2018-05-14 23:15:56 -07:00
|
|
|
place: &Place<'tcx>,
|
2018-09-23 16:07:45 +01:00
|
|
|
sd: AccessDepth,
|
2018-05-10 05:48:53 -04:00
|
|
|
rw: ReadOrWrite,
|
|
|
|
) {
|
|
|
|
debug!(
|
2019-05-02 06:03:17 +09:00
|
|
|
"invalidation::check_access_for_conflict(location={:?}, place={:?}, sd={:?}, \
|
2018-05-10 05:48:53 -04:00
|
|
|
rw={:?})",
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-05-14 23:15:56 -07:00
|
|
|
place,
|
2018-05-10 05:48:53 -04:00
|
|
|
sd,
|
|
|
|
rw,
|
|
|
|
);
|
2018-09-23 16:07:45 +01:00
|
|
|
let tcx = self.tcx;
|
2018-05-18 23:47:48 -07:00
|
|
|
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,
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
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
|
|
|
|
2019-03-01 20:09:54 +00:00
|
|
|
(Read(_), BorrowKind::Shallow)
|
|
|
|
| (Read(_), BorrowKind::Shared)
|
2019-02-02 16:38:12 +00:00
|
|
|
| (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Unique)
|
|
|
|
| (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Mut { .. }) => {
|
2019-03-26 13:18:17 +01:00
|
|
|
// Reads don't invalidate shared or shallow 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.
|
2019-05-02 06:03:17 +09:00
|
|
|
if !is_active(&this.dominators, borrow, location) {
|
2018-05-18 23:47:48 -07:00
|
|
|
// If the borrow isn't active yet, reads don't invalidate it
|
2019-04-28 21:13:33 +02:00
|
|
|
assert!(allow_two_phase_borrow(borrow.kind));
|
2018-05-18 23:47:48 -07:00
|
|
|
return Control::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unique and mutable borrows are invalidated by reads from any
|
|
|
|
// involved path
|
2019-05-02 06:03:17 +09:00
|
|
|
this.generate_invalidates(borrow_index, location);
|
2018-05-18 23:47:48 -07:00
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
|
2019-03-01 20:09:54 +00:00
|
|
|
(Reservation(_), _)
|
|
|
|
| (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
|
|
|
|
// FIXME(bob_twinkles) is this actually the right thing to do?
|
2019-05-02 06:03:17 +09:00
|
|
|
this.generate_invalidates(borrow_index, location);
|
2019-03-01 20:09:54 +00:00
|
|
|
}
|
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
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Generates a new `invalidates(L, B)` fact.
|
2018-05-14 23:15:56 -07:00
|
|
|
fn generate_invalidates(&mut self, b: BorrowIndex, l: Location) {
|
2018-09-26 20:41:14 +01:00
|
|
|
let lidx = self.location_table.start_index(l);
|
2018-05-14 23:15:56 -07:00
|
|
|
self.all_facts.invalidates.push((lidx, b));
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|
2018-12-04 10:03:34 -05:00
|
|
|
|
|
|
|
fn check_activations(
|
|
|
|
&mut self,
|
|
|
|
location: Location,
|
|
|
|
) {
|
|
|
|
// Two-phase borrow support: For each activation that is newly
|
|
|
|
// generated at this statement, check if it interferes with
|
|
|
|
// another borrow.
|
|
|
|
for &borrow_index in self.borrow_set.activations_at_location(location) {
|
|
|
|
let borrow = &self.borrow_set[borrow_index];
|
|
|
|
|
|
|
|
// only mutable borrows should be 2-phase
|
|
|
|
assert!(match borrow.kind {
|
|
|
|
BorrowKind::Shared | BorrowKind::Shallow => false,
|
|
|
|
BorrowKind::Unique | BorrowKind::Mut { .. } => true,
|
|
|
|
});
|
|
|
|
|
|
|
|
self.access_place(
|
2019-05-02 06:03:17 +09:00
|
|
|
location,
|
2018-12-04 10:03:34 -05:00
|
|
|
&borrow.borrowed_place,
|
|
|
|
(
|
|
|
|
Deep,
|
|
|
|
Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index),
|
|
|
|
),
|
|
|
|
LocalMutationIsAllowed::No,
|
|
|
|
);
|
|
|
|
|
|
|
|
// We do not need to call `check_if_path_or_subpath_is_moved`
|
|
|
|
// again, as we already called it when we made the
|
|
|
|
// initial reservation.
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 05:48:53 -04:00
|
|
|
}
|