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