1 use crate::borrow_check::borrow_set::BorrowSet;
2 use crate::borrow_check::location::LocationTable;
3 use crate::borrow_check::{JustWrite, WriteAndRead};
4 use crate::borrow_check::{AccessDepth, Deep, Shallow};
5 use crate::borrow_check::{ReadOrWrite, Activation, Read, Reservation, Write};
6 use crate::borrow_check::{Context, ContextKind};
7 use crate::borrow_check::{LocalMutationIsAllowed, MutateMode};
8 use crate::borrow_check::ArtificialField;
9 use crate::borrow_check::{ReadKind, WriteKind};
10 use crate::borrow_check::nll::facts::AllFacts;
11 use crate::borrow_check::path_utils::*;
12 use crate::dataflow::indexes::BorrowIndex;
13 use rustc::ty::TyCtxt;
14 use rustc::mir::visit::Visitor;
15 use rustc::mir::{BasicBlock, Location, Mir, Place, PlaceBase, Rvalue};
16 use rustc::mir::{Statement, StatementKind};
17 use rustc::mir::{Terminator, TerminatorKind};
18 use rustc::mir::{Operand, BorrowKind};
19 use rustc_data_structures::graph::dominators::Dominators;
21 pub(super) fn generate_invalidates<'cx, 'gcx, 'tcx>(
22 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
23 all_facts: &mut Option<AllFacts>,
24 location_table: &LocationTable,
26 borrow_set: &BorrowSet<'tcx>,
28 if all_facts.is_none() {
29 // Nothing to do if we don't have any facts
33 if let Some(all_facts) = all_facts {
34 let dominators = mir.dominators();
35 let mut ig = InvalidationGenerator {
47 struct InvalidationGenerator<'cx, 'tcx: 'cx, 'gcx: 'tcx> {
48 tcx: TyCtxt<'cx, 'gcx, 'tcx>,
49 all_facts: &'cx mut AllFacts,
50 location_table: &'cx LocationTable,
52 dominators: Dominators<BasicBlock>,
53 borrow_set: &'cx BorrowSet<'tcx>,
56 /// Visits the whole MIR and generates `invalidates()` facts.
57 /// Most of the code implementing this was stolen from `borrow_check/mod.rs`.
58 impl<'cx, 'tcx, 'gcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx, 'gcx> {
62 statement: &Statement<'tcx>,
65 self.check_activations(location);
67 match statement.kind {
68 StatementKind::Assign(ref lhs, ref rhs) => {
70 ContextKind::AssignRhs.new(location),
75 ContextKind::AssignLhs.new(location),
81 StatementKind::FakeRead(_, _) => {
82 // Only relavent for initialized/liveness/safety checks.
84 StatementKind::SetDiscriminant {
89 ContextKind::SetDiscrim.new(location),
95 StatementKind::InlineAsm(ref asm) => {
96 let context = ContextKind::InlineAsm.new(location);
97 for (o, output) in asm.asm.outputs.iter().zip(asm.outputs.iter()) {
99 // FIXME(eddyb) indirect inline asm outputs should
100 // be encoded through MIR place derefs instead.
104 (Deep, Read(ReadKind::Copy)),
105 LocalMutationIsAllowed::No,
111 if o.is_rw { Deep } else { Shallow(None) },
112 if o.is_rw { WriteAndRead } else { JustWrite },
116 for (_, input) in asm.inputs.iter() {
117 self.consume_operand(context, input);
121 StatementKind::AscribeUserType(..) |
122 StatementKind::Retag { .. } |
123 StatementKind::StorageLive(..) => {
124 // `Nop`, `AscribeUserType`, `Retag`, and `StorageLive` are irrelevant
127 StatementKind::StorageDead(local) => {
129 ContextKind::StorageDead.new(location),
130 &Place::Base(PlaceBase::Local(local)),
131 (Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
132 LocalMutationIsAllowed::Yes,
137 self.super_statement(block, statement, location);
143 terminator: &Terminator<'tcx>,
146 self.check_activations(location);
148 match terminator.kind {
149 TerminatorKind::SwitchInt {
155 self.consume_operand(ContextKind::SwitchInt.new(location), discr);
157 TerminatorKind::Drop {
158 location: ref drop_place,
163 ContextKind::Drop.new(location),
165 (AccessDepth::Drop, Write(WriteKind::StorageDeadOrDrop)),
166 LocalMutationIsAllowed::Yes,
169 TerminatorKind::DropAndReplace {
170 location: ref drop_place,
171 value: ref new_value,
176 ContextKind::DropAndReplace.new(location),
181 self.consume_operand(
182 ContextKind::DropAndReplace.new(location),
186 TerminatorKind::Call {
193 self.consume_operand(ContextKind::CallOperator.new(location), func);
195 self.consume_operand(ContextKind::CallOperand.new(location), arg);
197 if let Some((ref dest, _ /*bb*/)) = *destination {
199 ContextKind::CallDest.new(location),
206 TerminatorKind::Assert {
213 self.consume_operand(ContextKind::Assert.new(location), cond);
214 use rustc::mir::interpret::InterpError::BoundsCheck;
215 if let BoundsCheck { ref len, ref index } = *msg {
216 self.consume_operand(ContextKind::Assert.new(location), len);
217 self.consume_operand(ContextKind::Assert.new(location), index);
220 TerminatorKind::Yield {
225 self.consume_operand(ContextKind::Yield.new(location), value);
227 // Invalidate all borrows of local places
228 let borrow_set = self.borrow_set.clone();
229 let resume = self.location_table.start_index(resume.start_location());
230 for i in borrow_set.borrows.indices() {
231 if borrow_of_local_data(&borrow_set.borrows[i].borrowed_place) {
232 self.all_facts.invalidates.push((resume, i));
236 TerminatorKind::Resume | TerminatorKind::Return | TerminatorKind::GeneratorDrop => {
237 // Invalidate all borrows of local places
238 let borrow_set = self.borrow_set.clone();
239 let start = self.location_table.start_index(location);
240 for i in borrow_set.borrows.indices() {
241 if borrow_of_local_data(&borrow_set.borrows[i].borrowed_place) {
242 self.all_facts.invalidates.push((start, i));
246 TerminatorKind::Goto { target: _ }
247 | TerminatorKind::Abort
248 | TerminatorKind::Unreachable
249 | TerminatorKind::FalseEdges {
251 imaginary_targets: _,
253 | TerminatorKind::FalseUnwind {
257 // no data used, thus irrelevant to borrowck
261 self.super_terminator(block, terminator, location);
265 impl<'cg, 'cx, 'tcx, 'gcx> InvalidationGenerator<'cx, 'tcx, 'gcx> {
266 /// Simulates mutation of a place.
277 (kind, Write(WriteKind::Mutate)),
278 LocalMutationIsAllowed::ExceptUpvars,
282 /// Simulates consumption of an operand.
286 operand: &Operand<'tcx>,
289 Operand::Copy(ref place) => {
293 (Deep, Read(ReadKind::Copy)),
294 LocalMutationIsAllowed::No,
297 Operand::Move(ref place) => {
301 (Deep, Write(WriteKind::Move)),
302 LocalMutationIsAllowed::Yes,
305 Operand::Constant(_) => {}
309 // Simulates consumption of an rvalue
313 rvalue: &Rvalue<'tcx>,
316 Rvalue::Ref(_ /*rgn*/, bk, ref place) => {
317 let access_kind = match bk {
318 BorrowKind::Shallow => {
319 (Shallow(Some(ArtificialField::ShallowBorrow)), Read(ReadKind::Borrow(bk)))
321 BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
322 BorrowKind::Unique | BorrowKind::Mut { .. } => {
323 let wk = WriteKind::MutableBorrow(bk);
324 if allow_two_phase_borrow(bk) {
325 (Deep, Reservation(wk))
336 LocalMutationIsAllowed::No,
340 Rvalue::Use(ref operand)
341 | Rvalue::Repeat(ref operand, _)
342 | Rvalue::UnaryOp(_ /*un_op*/, ref operand)
343 | Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/) => {
344 self.consume_operand(context, operand)
347 Rvalue::Len(ref place) | Rvalue::Discriminant(ref place) => {
348 let af = match *rvalue {
349 Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
350 Rvalue::Discriminant(..) => None,
356 (Shallow(af), Read(ReadKind::Copy)),
357 LocalMutationIsAllowed::No,
361 Rvalue::BinaryOp(_bin_op, ref operand1, ref operand2)
362 | Rvalue::CheckedBinaryOp(_bin_op, ref operand1, ref operand2) => {
363 self.consume_operand(context, operand1);
364 self.consume_operand(context, operand2);
367 Rvalue::NullaryOp(_op, _ty) => {
370 Rvalue::Aggregate(_, ref operands) => {
371 for operand in operands {
372 self.consume_operand(context, operand);
378 /// Simulates an access to a place.
383 kind: (AccessDepth, ReadOrWrite),
384 _is_local_mutation_allowed: LocalMutationIsAllowed,
387 // note: not doing check_access_permissions checks because they don't generate invalidates
388 self.check_access_for_conflict(context, place, sd, rw);
391 fn check_access_for_conflict(
399 "invalidation::check_access_for_conflict(context={:?}, place={:?}, sd={:?}, \
408 let borrow_set = self.borrow_set.clone();
409 let indices = self.borrow_set.borrows.indices();
410 each_borrow_involving_path(
418 |this, borrow_index, borrow| {
419 match (rw, borrow.kind) {
420 // Obviously an activation is compatible with its own
421 // reservation (or even prior activating uses of same
422 // borrow); so don't check if they interfere.
424 // NOTE: *reservations* do conflict with themselves;
425 // thus aren't injecting unsoundenss w/ this check.)
426 (Activation(_, activating), _) if activating == borrow_index => {
427 // Activating a borrow doesn't generate any invalidations, since we
428 // have already taken the reservation
431 (Read(_), BorrowKind::Shallow)
432 | (Read(_), BorrowKind::Shared)
433 | (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Unique)
434 | (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Mut { .. }) => {
435 // Reads don't invalidate shared or shallow borrows
438 (Read(_), BorrowKind::Unique) | (Read(_), BorrowKind::Mut { .. }) => {
439 // Reading from mere reservations of mutable-borrows is OK.
440 if !is_active(&this.dominators, borrow, context.loc) {
441 // If the borrow isn't active yet, reads don't invalidate it
442 assert!(allow_two_phase_borrow(borrow.kind));
443 return Control::Continue;
446 // Unique and mutable borrows are invalidated by reads from any
448 this.generate_invalidates(borrow_index, context.loc);
452 | (Activation(_, _), _)
454 // unique or mutable borrows are invalidated by writes.
455 // Reservations count as writes since we need to check
456 // that activating the borrow will be OK
457 // FIXME(bob_twinkles) is this actually the right thing to do?
458 this.generate_invalidates(borrow_index, context.loc);
467 /// Generates a new `invalidates(L, B)` fact.
468 fn generate_invalidates(&mut self, b: BorrowIndex, l: Location) {
469 let lidx = self.location_table.start_index(l);
470 self.all_facts.invalidates.push((lidx, b));
473 fn check_activations(
477 // Two-phase borrow support: For each activation that is newly
478 // generated at this statement, check if it interferes with
480 for &borrow_index in self.borrow_set.activations_at_location(location) {
481 let borrow = &self.borrow_set[borrow_index];
483 // only mutable borrows should be 2-phase
484 assert!(match borrow.kind {
485 BorrowKind::Shared | BorrowKind::Shallow => false,
486 BorrowKind::Unique | BorrowKind::Mut { .. } => true,
490 ContextKind::Activation.new(location),
491 &borrow.borrowed_place,
494 Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index),
496 LocalMutationIsAllowed::No,
499 // We do not need to call `check_if_path_or_subpath_is_moved`
500 // again, as we already called it when we made the
501 // initial reservation.