Skip to content

Commit e81297d

Browse files
Add analysis to determine if a local is indirectly mutable
This adds a dataflow analysis that determines if a reference to a given `Local` or part of a `Local` that would allow mutation exists before a point in the CFG. If no such reference exists, we know for sure that that `Local` cannot have been mutated via an indirect assignment or function call.
1 parent 457c3aa commit e81297d

File tree

3 files changed

+157
-4
lines changed

3 files changed

+157
-4
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
use rustc::mir::visit::Visitor;
2+
use rustc::mir::{self, Local, Location};
3+
use rustc::ty::{self, TyCtxt};
4+
use rustc_data_structures::bit_set::BitSet;
5+
use syntax_pos::DUMMY_SP;
6+
7+
use crate::dataflow::{self, GenKillSet};
8+
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.
12+
///
13+
/// If this returns `false` for a `Local` at a given `Location`, the user can assume that `Local`
14+
/// has not been mutated as a result of an indirect assignment (`*p = x`) or as a side-effect of a
15+
/// function call or drop terminator.
16+
#[derive(Copy, Clone)]
17+
pub struct IndirectlyMutableLocals<'mir, 'tcx> {
18+
body: &'mir mir::Body<'tcx>,
19+
tcx: TyCtxt<'tcx>,
20+
param_env: ty::ParamEnv<'tcx>,
21+
}
22+
23+
impl<'mir, 'tcx> IndirectlyMutableLocals<'mir, 'tcx> {
24+
pub fn new(
25+
tcx: TyCtxt<'tcx>,
26+
body: &'mir mir::Body<'tcx>,
27+
param_env: ty::ParamEnv<'tcx>,
28+
) -> Self {
29+
IndirectlyMutableLocals { body, tcx, param_env }
30+
}
31+
32+
fn transfer_function<'a>(
33+
&self,
34+
trans: &'a mut GenKillSet<Local>,
35+
) -> TransferFunction<'a, 'mir, 'tcx> {
36+
TransferFunction {
37+
body: self.body,
38+
tcx: self.tcx,
39+
param_env: self.param_env,
40+
trans
41+
}
42+
}
43+
}
44+
45+
impl<'mir, 'tcx> dataflow::BitDenotation<'tcx> for IndirectlyMutableLocals<'mir, 'tcx> {
46+
type Idx = Local;
47+
48+
fn name() -> &'static str { "mut_borrowed_locals" }
49+
50+
fn bits_per_block(&self) -> usize {
51+
self.body.local_decls.len()
52+
}
53+
54+
fn start_block_effect(&self, _entry_set: &mut BitSet<Local>) {
55+
// Nothing is borrowed on function entry
56+
}
57+
58+
fn statement_effect(
59+
&self,
60+
trans: &mut GenKillSet<Local>,
61+
loc: Location,
62+
) {
63+
let stmt = &self.body[loc.block].statements[loc.statement_index];
64+
self.transfer_function(trans).visit_statement(stmt, loc);
65+
}
66+
67+
fn terminator_effect(
68+
&self,
69+
trans: &mut GenKillSet<Local>,
70+
loc: Location,
71+
) {
72+
let terminator = self.body[loc.block].terminator();
73+
self.transfer_function(trans).visit_terminator(terminator, loc);
74+
}
75+
76+
fn propagate_call_return(
77+
&self,
78+
_in_out: &mut BitSet<Local>,
79+
_call_bb: mir::BasicBlock,
80+
_dest_bb: mir::BasicBlock,
81+
_dest_place: &mir::Place<'tcx>,
82+
) {
83+
// Nothing to do when a call returns successfully
84+
}
85+
}
86+
87+
impl<'mir, 'tcx> dataflow::BottomValue for IndirectlyMutableLocals<'mir, 'tcx> {
88+
// bottom = unborrowed
89+
const BOTTOM_VALUE: bool = false;
90+
}
91+
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>,
96+
tcx: TyCtxt<'tcx>,
97+
param_env: ty::ParamEnv<'tcx>,
98+
}
99+
100+
impl<'tcx> Visitor<'tcx> for TransferFunction<'_, '_, 'tcx> {
101+
fn visit_rvalue(
102+
&mut self,
103+
rvalue: &mir::Rvalue<'tcx>,
104+
location: Location,
105+
) {
106+
if let mir::Rvalue::Ref(_, kind, ref borrowed_place) = *rvalue {
107+
let is_mut = match kind {
108+
mir::BorrowKind::Mut { .. } => true,
109+
110+
| mir::BorrowKind::Shared
111+
| mir::BorrowKind::Shallow
112+
| mir::BorrowKind::Unique
113+
=> {
114+
!borrowed_place
115+
.ty(self.body, self.tcx)
116+
.ty
117+
.is_freeze(self.tcx, self.param_env, DUMMY_SP)
118+
}
119+
};
120+
121+
if is_mut {
122+
match borrowed_place.base {
123+
mir::PlaceBase::Local(borrowed_local) if !borrowed_place.is_indirect()
124+
=> self.trans.gen(borrowed_local),
125+
126+
_ => (),
127+
}
128+
}
129+
}
130+
131+
self.super_rvalue(rvalue, location);
132+
}
133+
134+
135+
fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) {
136+
self.super_terminator(terminator, location);
137+
138+
match &terminator.kind {
139+
// Drop terminators borrow the location
140+
mir::TerminatorKind::Drop { location: dropped_place, .. } |
141+
mir::TerminatorKind::DropAndReplace { location: dropped_place, .. } => {
142+
match dropped_place.base {
143+
mir::PlaceBase::Local(dropped_local) if !dropped_place.is_indirect()
144+
=> self.trans.gen(dropped_local),
145+
146+
_ => (),
147+
}
148+
}
149+
_ => (),
150+
}
151+
}
152+
}

src/librustc_mir/dataflow/impls/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ use super::drop_flag_effects_for_function_entry;
1818
use super::drop_flag_effects_for_location;
1919
use super::on_lookup_result_bits;
2020

21-
mod storage_liveness;
22-
23-
pub use self::storage_liveness::*;
24-
2521
mod borrowed_locals;
22+
mod indirect_mutation;
23+
mod storage_liveness;
2624

2725
pub use self::borrowed_locals::*;
26+
pub use self::indirect_mutation::IndirectlyMutableLocals;
27+
pub use self::storage_liveness::*;
2828

2929
pub(super) mod borrows;
3030

src/librustc_mir/dataflow/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub use self::impls::DefinitelyInitializedPlaces;
2323
pub use self::impls::EverInitializedPlaces;
2424
pub use self::impls::borrows::Borrows;
2525
pub use self::impls::HaveBeenBorrowedLocals;
26+
pub use self::impls::IndirectlyMutableLocals;
2627
pub use self::at_location::{FlowAtLocation, FlowsAtLocation};
2728
pub(crate) use self::drop_flag_effects::*;
2829

0 commit comments

Comments
 (0)