]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/path_utils.rs
ec3c0bf68ad87409c1854b40ddc41cab0ec4b228
[rust.git] / src / librustc_mir / borrow_check / path_utils.rs
1 use crate::borrow_check::borrow_set::{BorrowSet, BorrowData, TwoPhaseActivation};
2 use crate::borrow_check::places_conflict;
3 use crate::borrow_check::Context;
4 use crate::borrow_check::AccessDepth;
5 use crate::dataflow::indexes::BorrowIndex;
6 use rustc::mir::{BasicBlock, Location, Mir, Place, PlaceBase};
7 use rustc::mir::{ProjectionElem, BorrowKind};
8 use rustc::ty::TyCtxt;
9 use rustc_data_structures::graph::dominators::Dominators;
10
11 /// Returns `true` if the borrow represented by `kind` is
12 /// allowed to be split into separate Reservation and
13 /// Activation phases.
14 pub(super) fn allow_two_phase_borrow<'a, 'tcx, 'gcx: 'tcx>(
15     _tcx: TyCtxt<'a, 'gcx, 'tcx>,
16     kind: BorrowKind
17 ) -> bool {
18     kind.allows_two_phase_borrow()
19 }
20
21 /// Control for the path borrow checking code
22 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
23 pub(super) enum Control {
24     Continue,
25     Break,
26 }
27
28 /// Encapsulates the idea of iterating over every borrow that involves a particular path
29 pub(super) fn each_borrow_involving_path<'a, 'tcx, 'gcx: 'tcx, F, I, S> (
30     s: &mut S,
31     tcx: TyCtxt<'a, 'gcx, 'tcx>,
32     mir: &Mir<'tcx>,
33     _context: Context,
34     access_place: (AccessDepth, &Place<'tcx>),
35     borrow_set: &BorrowSet<'tcx>,
36     candidates: I,
37     mut op: F,
38 ) where
39     F: FnMut(&mut S, BorrowIndex, &BorrowData<'tcx>) -> Control,
40     I: Iterator<Item=BorrowIndex>
41 {
42     let (access, place) = access_place;
43
44     // FIXME: analogous code in check_loans first maps `place` to
45     // its base_path.
46
47     // check for loan restricting path P being used. Accounts for
48     // borrows of P, P.a.b, etc.
49     for i in candidates {
50         let borrowed = &borrow_set[i];
51
52         if places_conflict::borrow_conflicts_with_place(
53             tcx,
54             mir,
55             &borrowed.borrowed_place,
56             borrowed.kind,
57             place,
58             access,
59             places_conflict::PlaceConflictBias::Overlap,
60         ) {
61             debug!(
62                 "each_borrow_involving_path: {:?} @ {:?} vs. {:?}/{:?}",
63                 i, borrowed, place, access
64             );
65             let ctrl = op(s, i, borrowed);
66             if ctrl == Control::Break {
67                 return;
68             }
69         }
70     }
71 }
72
73 pub(super) fn is_active<'tcx>(
74     dominators: &Dominators<BasicBlock>,
75     borrow_data: &BorrowData<'tcx>,
76     location: Location
77 ) -> bool {
78     debug!("is_active(borrow_data={:?}, location={:?})", borrow_data, location);
79
80     let activation_location = match borrow_data.activation_location {
81         // If this is not a 2-phase borrow, it is always active.
82         TwoPhaseActivation::NotTwoPhase => return true,
83         // And if the unique 2-phase use is not an activation, then it is *never* active.
84         TwoPhaseActivation::NotActivated => return false,
85         // Otherwise, we derive info from the activation point `loc`:
86         TwoPhaseActivation::ActivatedAt(loc) => loc,
87     };
88
89     // Otherwise, it is active for every location *except* in between
90     // the reservation and the activation:
91     //
92     //       X
93     //      /
94     //     R      <--+ Except for this
95     //    / \        | diamond
96     //    \ /        |
97     //     A  <------+
98     //     |
99     //     Z
100     //
101     // Note that we assume that:
102     // - the reservation R dominates the activation A
103     // - the activation A post-dominates the reservation R (ignoring unwinding edges).
104     //
105     // This means that there can't be an edge that leaves A and
106     // comes back into that diamond unless it passes through R.
107     //
108     // Suboptimal: In some cases, this code walks the dominator
109     // tree twice when it only has to be walked once. I am
110     // lazy. -nmatsakis
111
112     // If dominated by the activation A, then it is active. The
113     // activation occurs upon entering the point A, so this is
114     // also true if location == activation_location.
115     if activation_location.dominates(location, dominators) {
116         return true;
117     }
118
119     // The reservation starts *on exiting* the reservation block,
120     // so check if the location is dominated by R.successor. If so,
121     // this point falls in between the reservation and location.
122     let reserve_location = borrow_data.reserve_location.successor_within_block();
123     if reserve_location.dominates(location, dominators) {
124         false
125     } else {
126         // Otherwise, this point is outside the diamond, so
127         // consider the borrow active. This could happen for
128         // example if the borrow remains active around a loop (in
129         // which case it would be active also for the point R,
130         // which would generate an error).
131         true
132     }
133 }
134
135 /// Determines if a given borrow is borrowing local data
136 /// This is called for all Yield statements on movable generators
137 pub(super) fn borrow_of_local_data<'tcx>(place: &Place<'tcx>) -> bool {
138     match place {
139         Place::Base(PlaceBase::Static(..)) => false,
140         Place::Base(PlaceBase::Local(..)) => true,
141         Place::Projection(box proj) => {
142             match proj.elem {
143                 // Reborrow of already borrowed data is ignored
144                 // Any errors will be caught on the initial borrow
145                 ProjectionElem::Deref => false,
146
147                 // For interior references and downcasts, find out if the base is local
148                 ProjectionElem::Field(..)
149                     | ProjectionElem::Index(..)
150                     | ProjectionElem::ConstantIndex { .. }
151                 | ProjectionElem::Subslice { .. }
152                 | ProjectionElem::Downcast(..) => borrow_of_local_data(&proj.base),
153             }
154         }
155     }
156 }