4 use rustc::mir::visit::Visitor;
5 use crate::dataflow::BitDenotation;
7 /// This calculates if any part of a MIR local could have previously been borrowed.
8 /// This means that once a local has been borrowed, its bit will be set
9 /// from that point and onwards, until we see a StorageDead statement for the local,
10 /// at which points there is no memory associated with the local, so it cannot be borrowed.
11 /// This is used to compute which locals are live during a yield expression for
12 /// immovable generators.
13 #[derive(Copy, Clone)]
14 pub struct HaveBeenBorrowedLocals<'a, 'tcx: 'a> {
18 impl<'a, 'tcx: 'a> HaveBeenBorrowedLocals<'a, 'tcx> {
19 pub fn new(mir: &'a Body<'tcx>)
21 HaveBeenBorrowedLocals { mir }
24 pub fn mir(&self) -> &Body<'tcx> {
29 impl<'a, 'tcx> BitDenotation<'tcx> for HaveBeenBorrowedLocals<'a, 'tcx> {
31 fn name() -> &'static str { "has_been_borrowed_locals" }
32 fn bits_per_block(&self) -> usize {
33 self.mir.local_decls.len()
36 fn start_block_effect(&self, _sets: &mut BitSet<Local>) {
37 // Nothing is borrowed on function entry
40 fn statement_effect(&self,
41 sets: &mut BlockSets<'_, Local>,
43 let stmt = &self.mir[loc.block].statements[loc.statement_index];
45 BorrowedLocalsVisitor {
47 }.visit_statement(stmt, loc);
49 // StorageDead invalidates all borrows and raw pointers to a local
51 StatementKind::StorageDead(l) => sets.kill(l),
56 fn terminator_effect(&self,
57 sets: &mut BlockSets<'_, Local>,
59 BorrowedLocalsVisitor {
61 }.visit_terminator(self.mir[loc.block].terminator(), loc);
64 fn propagate_call_return(
66 _in_out: &mut BitSet<Local>,
67 _call_bb: mir::BasicBlock,
68 _dest_bb: mir::BasicBlock,
69 _dest_place: &mir::Place<'tcx>,
71 // Nothing to do when a call returns successfully
75 impl<'a, 'tcx> BitSetOperator for HaveBeenBorrowedLocals<'a, 'tcx> {
77 fn join<T: Idx>(&self, inout_set: &mut BitSet<T>, in_set: &BitSet<T>) -> bool {
78 inout_set.union(in_set) // "maybe" means we union effects of both preds
82 impl<'a, 'tcx> InitialFlow for HaveBeenBorrowedLocals<'a, 'tcx> {
84 fn bottom_value() -> bool {
85 false // bottom = unborrowed
89 struct BorrowedLocalsVisitor<'b, 'c: 'b> {
90 sets: &'b mut BlockSets<'c, Local>,
93 fn find_local<'tcx>(place: &Place<'tcx>) -> Option<Local> {
94 place.iterate(|place_base, place_projection| {
95 for proj in place_projection {
96 if proj.elem == ProjectionElem::Deref {
101 if let PlaceBase::Local(local) = place_base {
109 impl<'tcx, 'b, 'c> Visitor<'tcx> for BorrowedLocalsVisitor<'b, 'c> {
110 fn visit_rvalue(&mut self,
111 rvalue: &Rvalue<'tcx>,
112 location: Location) {
113 if let Rvalue::Ref(_, _, ref place) = *rvalue {
114 if let Some(local) = find_local(place) {
115 self.sets.gen(local);
119 self.super_rvalue(rvalue, location)