2017-12-06 12:27:38 -06:00
|
|
|
//! From the NLL RFC: "The deep [aka 'supporting'] prefixes for an
|
|
|
|
//! place are formed by stripping away fields and derefs, except that
|
|
|
|
//! we stop when we reach the deref of a shared reference. [...] "
|
|
|
|
//!
|
|
|
|
//! "Shallow prefixes are found by stripping away fields, but stop at
|
|
|
|
//! any dereference. So: writing a path like `a` is illegal if `a.b`
|
|
|
|
//! is borrowed. But: writing `a` is legal if `*a` is borrowed,
|
|
|
|
//! whether or not `a` is a shared or mutable reference. [...] "
|
|
|
|
|
|
|
|
use super::MirBorrowckCtxt;
|
|
|
|
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_hir as hir;
|
2021-01-02 12:31:37 -06:00
|
|
|
use rustc_middle::mir::{Body, PlaceRef, ProjectionElem};
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2017-12-06 12:27:38 -06:00
|
|
|
|
2020-03-04 15:13:47 -06:00
|
|
|
pub trait IsPrefixOf<'tcx> {
|
2020-03-04 15:25:03 -06:00
|
|
|
fn is_prefix_of(&self, other: PlaceRef<'tcx>) -> bool;
|
2017-12-06 12:27:38 -06:00
|
|
|
}
|
|
|
|
|
2020-03-04 15:25:03 -06:00
|
|
|
impl<'tcx> IsPrefixOf<'tcx> for PlaceRef<'tcx> {
|
|
|
|
fn is_prefix_of(&self, other: PlaceRef<'tcx>) -> bool {
|
2019-12-11 13:50:03 -06:00
|
|
|
self.local == other.local
|
2019-07-29 17:07:28 -05:00
|
|
|
&& self.projection.len() <= other.projection.len()
|
|
|
|
&& self.projection == &other.projection[..self.projection.len()]
|
2017-12-06 12:27:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:39:39 -05:00
|
|
|
pub(super) struct Prefixes<'cx, 'tcx> {
|
2020-04-12 12:31:00 -05:00
|
|
|
body: &'cx Body<'tcx>,
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2017-12-06 12:27:38 -06:00
|
|
|
kind: PrefixSet,
|
2020-03-04 15:25:03 -06:00
|
|
|
next: Option<PlaceRef<'tcx>>,
|
2017-12-06 12:27:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub(super) enum PrefixSet {
|
|
|
|
/// Doesn't stop until it returns the base case (a Local or
|
|
|
|
/// Static prefix).
|
|
|
|
All,
|
|
|
|
/// Stops at any dereference.
|
|
|
|
Shallow,
|
|
|
|
/// Stops at the deref of a shared reference.
|
|
|
|
Supporting,
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:48:52 -05:00
|
|
|
impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
2017-12-06 12:27:38 -06:00
|
|
|
/// Returns an iterator over the prefixes of `place`
|
|
|
|
/// (inclusive) from longest to smallest, potentially
|
|
|
|
/// terminating the iteration early based on `kind`.
|
2019-04-30 11:58:24 -05:00
|
|
|
pub(super) fn prefixes(
|
|
|
|
&self,
|
2020-03-04 15:25:03 -06:00
|
|
|
place_ref: PlaceRef<'tcx>,
|
2019-04-30 11:58:24 -05:00
|
|
|
kind: PrefixSet,
|
|
|
|
) -> Prefixes<'cx, 'tcx> {
|
2019-12-22 16:42:04 -06:00
|
|
|
Prefixes { next: Some(place_ref), kind, body: self.body, tcx: self.infcx.tcx }
|
2017-12-06 12:27:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:48:52 -05:00
|
|
|
impl<'cx, 'tcx> Iterator for Prefixes<'cx, 'tcx> {
|
2020-03-04 15:25:03 -06:00
|
|
|
type Item = PlaceRef<'tcx>;
|
2017-12-06 12:27:38 -06:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2018-11-20 22:01:56 -06:00
|
|
|
let mut cursor = self.next?;
|
2017-12-06 12:27:38 -06:00
|
|
|
|
|
|
|
// Post-processing `place`: Enqueue any remaining
|
|
|
|
// work. Also, `place` may not be a prefix itself, but
|
2018-11-26 20:59:49 -06:00
|
|
|
// may hold one further down (e.g., we never return
|
2017-12-06 12:27:38 -06:00
|
|
|
// downcasts here, but may return a base of a downcast).
|
|
|
|
|
|
|
|
'cursor: loop {
|
2021-01-02 12:31:37 -06:00
|
|
|
match cursor.last_projection() {
|
|
|
|
None => {
|
2017-12-06 12:27:38 -06:00
|
|
|
self.next = None;
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
2021-01-02 12:31:37 -06:00
|
|
|
Some((cursor_base, elem)) => {
|
2019-07-29 17:07:28 -05:00
|
|
|
match elem {
|
|
|
|
ProjectionElem::Field(_ /*field*/, _ /*ty*/) => {
|
|
|
|
// FIXME: add union handling
|
2021-01-02 12:31:37 -06:00
|
|
|
self.next = Some(cursor_base);
|
2019-07-29 17:07:28 -05:00
|
|
|
return Some(cursor);
|
|
|
|
}
|
2019-12-11 07:39:24 -06:00
|
|
|
ProjectionElem::Downcast(..)
|
|
|
|
| ProjectionElem::Subslice { .. }
|
|
|
|
| ProjectionElem::ConstantIndex { .. }
|
|
|
|
| ProjectionElem::Index(_) => {
|
2021-01-02 12:31:37 -06:00
|
|
|
cursor = cursor_base;
|
2019-07-29 17:07:28 -05:00
|
|
|
continue 'cursor;
|
|
|
|
}
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
// (handled below)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 12:31:37 -06:00
|
|
|
assert_eq!(elem, ProjectionElem::Deref);
|
2019-07-29 17:07:28 -05:00
|
|
|
|
|
|
|
match self.kind {
|
|
|
|
PrefixSet::Shallow => {
|
2019-09-12 14:20:20 -05:00
|
|
|
// Shallow prefixes are found by stripping away
|
2019-07-29 17:07:28 -05:00
|
|
|
// fields, but stop at *any* dereference.
|
|
|
|
// So we can just stop the traversal now.
|
|
|
|
self.next = None;
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
PrefixSet::All => {
|
2019-09-12 14:20:20 -05:00
|
|
|
// All prefixes: just blindly enqueue the base
|
|
|
|
// of the projection.
|
2021-01-02 12:31:37 -06:00
|
|
|
self.next = Some(cursor_base);
|
2019-07-29 17:07:28 -05:00
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
PrefixSet::Supporting => {
|
2019-09-12 14:20:20 -05:00
|
|
|
// Fall through!
|
2019-07-29 17:07:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(self.kind, PrefixSet::Supporting);
|
2019-09-12 14:20:20 -05:00
|
|
|
// Supporting prefixes: strip away fields and
|
2019-07-29 17:07:28 -05:00
|
|
|
// derefs, except we stop at the deref of a shared
|
|
|
|
// reference.
|
|
|
|
|
2021-01-16 04:34:22 -06:00
|
|
|
let ty = cursor_base.ty(self.body, self.tcx).ty;
|
2020-08-02 17:49:11 -05:00
|
|
|
match ty.kind() {
|
2019-12-11 07:39:24 -06:00
|
|
|
ty::RawPtr(_) | ty::Ref(_ /*rgn*/, _ /*ty*/, hir::Mutability::Not) => {
|
2019-07-29 17:07:28 -05:00
|
|
|
// don't continue traversing over derefs of raw pointers or shared
|
|
|
|
// borrows.
|
|
|
|
self.next = None;
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:39:24 -06:00
|
|
|
ty::Ref(_ /*rgn*/, _ /*ty*/, hir::Mutability::Mut) => {
|
2021-01-02 12:31:37 -06:00
|
|
|
self.next = Some(cursor_base);
|
2019-07-29 17:07:28 -05:00
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::Adt(..) if ty.is_box() => {
|
2021-01-02 12:31:37 -06:00
|
|
|
self.next = Some(cursor_base);
|
2019-07-29 17:07:28 -05:00
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => panic!("unknown type fed to Projection Deref."),
|
|
|
|
}
|
2017-12-06 12:27:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|