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};
9 use rustc_data_structures::graph::dominators::Dominators;
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>,
18 kind.allows_two_phase_borrow()
21 /// Control for the path borrow checking code
22 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
23 pub(super) enum Control {
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> (
31 tcx: TyCtxt<'a, 'gcx, 'tcx>,
34 access_place: (AccessDepth, &Place<'tcx>),
35 borrow_set: &BorrowSet<'tcx>,
39 F: FnMut(&mut S, BorrowIndex, &BorrowData<'tcx>) -> Control,
40 I: Iterator<Item=BorrowIndex>
42 let (access, place) = access_place;
44 // FIXME: analogous code in check_loans first maps `place` to
47 // check for loan restricting path P being used. Accounts for
48 // borrows of P, P.a.b, etc.
50 let borrowed = &borrow_set[i];
52 if places_conflict::borrow_conflicts_with_place(
55 &borrowed.borrowed_place,
59 places_conflict::PlaceConflictBias::Overlap,
62 "each_borrow_involving_path: {:?} @ {:?} vs. {:?}/{:?}",
63 i, borrowed, place, access
65 let ctrl = op(s, i, borrowed);
66 if ctrl == Control::Break {
73 pub(super) fn is_active<'tcx>(
74 dominators: &Dominators<BasicBlock>,
75 borrow_data: &BorrowData<'tcx>,
78 debug!("is_active(borrow_data={:?}, location={:?})", borrow_data, location);
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,
89 // Otherwise, it is active for every location *except* in between
90 // the reservation and the activation:
94 // R <--+ Except for this
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).
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.
108 // Suboptimal: In some cases, this code walks the dominator
109 // tree twice when it only has to be walked once. I am
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) {
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) {
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).
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 {
139 Place::Base(PlaceBase::Static(..)) => false,
140 Place::Base(PlaceBase::Local(..)) => true,
141 Place::Projection(box proj) => {
143 // Reborrow of already borrowed data is ignored
144 // Any errors will be caught on the initial borrow
145 ProjectionElem::Deref => false,
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),