2020-03-29 10:19:48 -05:00
|
|
|
use rustc_data_structures::graph::dominators::Dominators;
|
2023-03-28 14:32:57 -05:00
|
|
|
use rustc_middle::mir::{BasicBlock, Body, BorrowKind, Location, Place, PlaceRef, ProjectionElem};
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2023-03-28 14:32:57 -05:00
|
|
|
use rustc_target::abi::FieldIdx;
|
2024-08-29 17:39:41 -05:00
|
|
|
use tracing::debug;
|
2018-05-19 01:47:48 -05:00
|
|
|
|
2020-12-30 11:48:40 -06:00
|
|
|
use crate::borrow_set::{BorrowData, BorrowSet, TwoPhaseActivation};
|
|
|
|
use crate::{AccessDepth, BorrowIndex, places_conflict};
|
2024-07-28 17:13:50 -05:00
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Returns `true` if the borrow represented by `kind` is
|
2018-06-27 10:16:24 -05:00
|
|
|
/// allowed to be split into separate Reservation and
|
|
|
|
/// Activation phases.
|
2019-06-11 04:47:30 -05:00
|
|
|
pub(super) fn allow_two_phase_borrow(kind: BorrowKind) -> bool {
|
2019-01-26 11:25:37 -06:00
|
|
|
kind.allows_two_phase_borrow()
|
2018-05-19 01:47:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Control for the path borrow checking code
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub(super) enum Control {
|
|
|
|
Continue,
|
|
|
|
Break,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Encapsulates the idea of iterating over every borrow that involves a particular path
|
2019-06-13 16:48:52 -05:00
|
|
|
pub(super) fn each_borrow_involving_path<'tcx, F, I, S>(
|
2018-05-19 01:47:48 -05:00
|
|
|
s: &mut S,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-03 17:26:48 -05:00
|
|
|
body: &Body<'tcx>,
|
2020-03-30 15:49:33 -05:00
|
|
|
access_place: (AccessDepth, Place<'tcx>),
|
2018-05-19 01:47:48 -05:00
|
|
|
borrow_set: &BorrowSet<'tcx>,
|
2023-05-19 03:26:31 -05:00
|
|
|
is_candidate: I,
|
2018-05-19 01:47:48 -05:00
|
|
|
mut op: F,
|
|
|
|
) where
|
|
|
|
F: FnMut(&mut S, BorrowIndex, &BorrowData<'tcx>) -> Control,
|
2023-05-19 03:26:31 -05:00
|
|
|
I: Fn(BorrowIndex) -> bool,
|
2018-05-19 01:47:48 -05:00
|
|
|
{
|
|
|
|
let (access, place) = access_place;
|
|
|
|
|
2023-05-19 03:26:31 -05:00
|
|
|
// The number of candidates can be large, but borrows for different locals cannot conflict with
|
|
|
|
// each other, so we restrict the working set a priori.
|
|
|
|
let Some(borrows_for_place_base) = borrow_set.local_map.get(&place.local) else { return };
|
2018-05-19 01:47:48 -05:00
|
|
|
|
|
|
|
// check for loan restricting path P being used. Accounts for
|
|
|
|
// borrows of P, P.a.b, etc.
|
2023-05-19 03:26:31 -05:00
|
|
|
for &i in borrows_for_place_base {
|
|
|
|
if !is_candidate(i) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-19 01:47:48 -05:00
|
|
|
let borrowed = &borrow_set[i];
|
|
|
|
|
2018-09-23 05:33:52 -05:00
|
|
|
if places_conflict::borrow_conflicts_with_place(
|
2018-09-10 16:33:45 -05:00
|
|
|
tcx,
|
2019-06-03 17:26:48 -05:00
|
|
|
body,
|
2020-03-30 15:49:33 -05:00
|
|
|
borrowed.borrowed_place,
|
2018-09-10 16:33:45 -05:00
|
|
|
borrowed.kind,
|
2019-07-21 15:38:30 -05:00
|
|
|
place.as_ref(),
|
2018-09-10 16:33:45 -05:00
|
|
|
access,
|
2018-12-17 06:11:33 -06:00
|
|
|
places_conflict::PlaceConflictBias::Overlap,
|
2018-09-10 16:33:45 -05:00
|
|
|
) {
|
2018-05-19 01:47:48 -05:00
|
|
|
debug!(
|
|
|
|
"each_borrow_involving_path: {:?} @ {:?} vs. {:?}/{:?}",
|
|
|
|
i, borrowed, place, access
|
|
|
|
);
|
|
|
|
let ctrl = op(s, i, borrowed);
|
|
|
|
if ctrl == Control::Break {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(super) fn is_active<'tcx>(
|
|
|
|
dominators: &Dominators<BasicBlock>,
|
|
|
|
borrow_data: &BorrowData<'tcx>,
|
|
|
|
location: Location,
|
|
|
|
) -> bool {
|
|
|
|
debug!("is_active(borrow_data={:?}, location={:?})", borrow_data, location);
|
|
|
|
|
|
|
|
let activation_location = match borrow_data.activation_location {
|
2018-05-11 16:32:13 -05:00
|
|
|
// If this is not a 2-phase borrow, it is always active.
|
2018-07-04 14:08:15 -05:00
|
|
|
TwoPhaseActivation::NotTwoPhase => return true,
|
2018-05-11 16:32:13 -05:00
|
|
|
// And if the unique 2-phase use is not an activation, then it is *never* active.
|
2018-07-04 14:08:15 -05:00
|
|
|
TwoPhaseActivation::NotActivated => return false,
|
|
|
|
// Otherwise, we derive info from the activation point `loc`:
|
|
|
|
TwoPhaseActivation::ActivatedAt(loc) => loc,
|
2018-05-19 01:47:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Otherwise, it is active for every location *except* in between
|
|
|
|
// the reservation and the activation:
|
|
|
|
//
|
|
|
|
// X
|
|
|
|
// /
|
|
|
|
// R <--+ Except for this
|
|
|
|
// / \ | diamond
|
|
|
|
// \ / |
|
|
|
|
// A <------+
|
|
|
|
// |
|
|
|
|
// Z
|
|
|
|
//
|
|
|
|
// Note that we assume that:
|
|
|
|
// - the reservation R dominates the activation A
|
|
|
|
// - the activation A post-dominates the reservation R (ignoring unwinding edges).
|
|
|
|
//
|
|
|
|
// This means that there can't be an edge that leaves A and
|
|
|
|
// comes back into that diamond unless it passes through R.
|
|
|
|
//
|
|
|
|
// Suboptimal: In some cases, this code walks the dominator
|
|
|
|
// tree twice when it only has to be walked once. I am
|
|
|
|
// lazy. -nmatsakis
|
|
|
|
|
|
|
|
// If dominated by the activation A, then it is active. The
|
|
|
|
// activation occurs upon entering the point A, so this is
|
|
|
|
// also true if location == activation_location.
|
|
|
|
if activation_location.dominates(location, dominators) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The reservation starts *on exiting* the reservation block,
|
|
|
|
// so check if the location is dominated by R.successor. If so,
|
|
|
|
// this point falls in between the reservation and location.
|
|
|
|
let reserve_location = borrow_data.reserve_location.successor_within_block();
|
|
|
|
if reserve_location.dominates(location, dominators) {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
// Otherwise, this point is outside the diamond, so
|
|
|
|
// consider the borrow active. This could happen for
|
|
|
|
// example if the borrow remains active around a loop (in
|
|
|
|
// which case it would be active also for the point R,
|
|
|
|
// which would generate an error).
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines if a given borrow is borrowing local data
|
2023-10-19 16:46:28 -05:00
|
|
|
/// This is called for all Yield expressions on movable coroutines
|
2020-03-30 16:51:48 -05:00
|
|
|
pub(super) fn borrow_of_local_data(place: Place<'_>) -> bool {
|
2019-12-11 13:50:03 -06:00
|
|
|
// Reborrow of already borrowed data is ignored
|
|
|
|
// Any errors will be caught on the initial borrow
|
|
|
|
!place.is_indirect()
|
2018-05-19 01:47:48 -05:00
|
|
|
}
|
2020-05-23 20:40:55 -05:00
|
|
|
|
|
|
|
/// If `place` is a field projection, and the field is being projected from a closure type,
|
|
|
|
/// then returns the index of the field being projected. Note that this closure will always
|
|
|
|
/// be `self` in the current MIR, because that is the only time we directly access the fields
|
|
|
|
/// of a closure type.
|
2021-12-15 01:38:12 -06:00
|
|
|
pub(crate) fn is_upvar_field_projection<'tcx>(
|
2020-05-23 20:40:55 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-11-26 07:39:57 -06:00
|
|
|
upvars: &[&rustc_middle::ty::CapturedPlace<'tcx>],
|
2020-05-23 20:40:55 -05:00
|
|
|
place_ref: PlaceRef<'tcx>,
|
|
|
|
body: &Body<'tcx>,
|
2023-03-28 14:32:57 -05:00
|
|
|
) -> Option<FieldIdx> {
|
2021-01-09 23:33:38 -06:00
|
|
|
let mut place_ref = place_ref;
|
2020-05-23 20:40:55 -05:00
|
|
|
let mut by_ref = false;
|
|
|
|
|
2021-01-09 23:33:38 -06:00
|
|
|
if let Some((place_base, ProjectionElem::Deref)) = place_ref.last_projection() {
|
|
|
|
place_ref = place_base;
|
2020-05-23 20:40:55 -05:00
|
|
|
by_ref = true;
|
|
|
|
}
|
|
|
|
|
2021-01-09 23:33:38 -06:00
|
|
|
match place_ref.last_projection() {
|
|
|
|
Some((place_base, ProjectionElem::Field(field, _ty))) => {
|
|
|
|
let base_ty = place_base.ty(body, tcx).ty;
|
2024-01-24 12:01:56 -06:00
|
|
|
if (base_ty.is_closure() || base_ty.is_coroutine() || base_ty.is_coroutine_closure())
|
2023-11-26 07:39:57 -06:00
|
|
|
&& (!by_ref || upvars[field.index()].is_by_ref())
|
2020-05-23 20:40:55 -05:00
|
|
|
{
|
2021-01-09 23:33:38 -06:00
|
|
|
Some(field)
|
2020-05-23 20:40:55 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|