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