1 use rustc::mir::visit::Visitor;
2 use rustc::mir::{self, Local, Location};
3 use rustc::ty::{self, TyCtxt};
4 use rustc_index::bit_set::BitSet;
5 use syntax_pos::DUMMY_SP;
7 use crate::dataflow::{self, GenKillSet};
9 /// Whether a borrow to a `Local` has been created that could allow that `Local` to be mutated
10 /// indirectly. This could either be a mutable reference (`&mut`) or a shared borrow if the type of
11 /// that `Local` allows interior mutability. Operations that can mutate local's indirectly include:
12 /// assignments through a pointer (`*p = 42`), function calls, drop terminators and inline assembly.
14 /// If this returns false for a `Local` at a given statement (or terminator), that `Local` could
15 /// not possibly have been mutated indirectly prior to that statement.
16 #[derive(Copy, Clone)]
17 pub struct IndirectlyMutableLocals<'mir, 'tcx> {
18 body: &'mir mir::Body<'tcx>,
20 param_env: ty::ParamEnv<'tcx>,
23 impl<'mir, 'tcx> IndirectlyMutableLocals<'mir, 'tcx> {
26 body: &'mir mir::Body<'tcx>,
27 param_env: ty::ParamEnv<'tcx>,
29 IndirectlyMutableLocals { body, tcx, param_env }
32 fn transfer_function<'a>(
34 trans: &'a mut GenKillSet<Local>,
35 ) -> TransferFunction<'a, 'mir, 'tcx> {
39 param_env: self.param_env,
45 impl<'mir, 'tcx> dataflow::BitDenotation<'tcx> for IndirectlyMutableLocals<'mir, 'tcx> {
48 fn name() -> &'static str { "mut_borrowed_locals" }
50 fn bits_per_block(&self) -> usize {
51 self.body.local_decls.len()
54 fn start_block_effect(&self, _entry_set: &mut BitSet<Local>) {
55 // Nothing is borrowed on function entry
60 trans: &mut GenKillSet<Local>,
63 let stmt = &self.body[loc.block].statements[loc.statement_index];
64 self.transfer_function(trans).visit_statement(stmt, loc);
69 trans: &mut GenKillSet<Local>,
72 let terminator = self.body[loc.block].terminator();
73 self.transfer_function(trans).visit_terminator(terminator, loc);
76 fn propagate_call_return(
78 _in_out: &mut BitSet<Local>,
79 _call_bb: mir::BasicBlock,
80 _dest_bb: mir::BasicBlock,
81 _dest_place: &mir::Place<'tcx>,
83 // Nothing to do when a call returns successfully
87 impl<'mir, 'tcx> dataflow::BottomValue for IndirectlyMutableLocals<'mir, 'tcx> {
88 // bottom = unborrowed
89 const BOTTOM_VALUE: bool = false;
92 /// A `Visitor` that defines the transfer function for `IndirectlyMutableLocals`.
93 struct TransferFunction<'a, 'mir, 'tcx> {
94 trans: &'a mut GenKillSet<Local>,
95 body: &'mir mir::Body<'tcx>,
97 param_env: ty::ParamEnv<'tcx>,
100 impl<'tcx> TransferFunction<'_, '_, 'tcx> {
101 /// Returns `true` if this borrow would allow mutation of the `borrowed_place`.
102 fn borrow_allows_mutation(
104 kind: mir::BorrowKind,
105 borrowed_place: &mir::Place<'tcx>,
108 mir::BorrowKind::Mut { .. } => true,
110 | mir::BorrowKind::Shared
111 | mir::BorrowKind::Shallow
112 | mir::BorrowKind::Unique
114 .ty(self.body, self.tcx)
116 .is_freeze(self.tcx, self.param_env, DUMMY_SP),
121 impl<'tcx> Visitor<'tcx> for TransferFunction<'_, '_, 'tcx> {
124 rvalue: &mir::Rvalue<'tcx>,
127 if let mir::Rvalue::Ref(_, kind, ref borrowed_place) = *rvalue {
128 if self.borrow_allows_mutation(kind, borrowed_place) {
129 match borrowed_place.base {
130 mir::PlaceBase::Local(borrowed_local) if !borrowed_place.is_indirect()
131 => self.trans.gen(borrowed_local),
138 self.super_rvalue(rvalue, location);
142 fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
143 // This method purposely does nothing except call `super_terminator`. It exists solely to
144 // document the subtleties around drop terminators.
146 self.super_terminator(terminator, location);
148 if let mir::TerminatorKind::Drop { location: _, .. }
149 | mir::TerminatorKind::DropAndReplace { location: _, .. } = &terminator.kind
151 // Although drop terminators mutably borrow the location being dropped, that borrow
152 // cannot live beyond the drop terminator because the dropped location is invalidated.