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;
|
|
|
|
|
|
|
|
use rustc::hir;
|
|
|
|
use rustc::ty::{self, TyCtxt};
|
2019-05-17 16:55:04 -05:00
|
|
|
use rustc::mir::{Body, Place, PlaceBase, ProjectionElem};
|
2017-12-06 12:27:38 -06:00
|
|
|
|
|
|
|
pub trait IsPrefixOf<'tcx> {
|
|
|
|
fn is_prefix_of(&self, other: &Place<'tcx>) -> bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> IsPrefixOf<'tcx> for Place<'tcx> {
|
|
|
|
fn is_prefix_of(&self, other: &Place<'tcx>) -> bool {
|
|
|
|
let mut cursor = other;
|
|
|
|
loop {
|
|
|
|
if self == cursor {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
match *cursor {
|
2019-02-21 22:24:03 -06:00
|
|
|
Place::Base(PlaceBase::Local(_)) |
|
|
|
|
Place::Base(PlaceBase::Static(_)) => return false,
|
2017-12-06 12:27:38 -06:00
|
|
|
Place::Projection(ref proj) => {
|
|
|
|
cursor = &proj.base;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:48:52 -05:00
|
|
|
pub(super) struct Prefixes<'cx, 'tcx: 'cx> {
|
2019-06-03 17:26:48 -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,
|
|
|
|
next: Option<&'cx Place<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
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-06-13 17:32:15 -05:00
|
|
|
pub(super) fn prefixes(&self, place: &'cx Place<'tcx>, kind: PrefixSet) -> Prefixes<'cx, 'tcx> {
|
2017-12-06 12:27:38 -06:00
|
|
|
Prefixes {
|
|
|
|
next: Some(place),
|
|
|
|
kind,
|
2019-06-03 17:26:48 -05:00
|
|
|
body: self.body,
|
2018-09-11 09:38:35 -05:00
|
|
|
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> {
|
2017-12-06 12:27:38 -06:00
|
|
|
type Item = &'cx Place<'tcx>;
|
|
|
|
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 {
|
|
|
|
let proj = match *cursor {
|
2019-02-21 22:24:03 -06:00
|
|
|
Place::Base(PlaceBase::Local(_)) | // search yielded this leaf
|
|
|
|
Place::Base(PlaceBase::Static(_)) => {
|
2017-12-06 12:27:38 -06:00
|
|
|
self.next = None;
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
Place::Projection(ref proj) => proj,
|
|
|
|
};
|
|
|
|
|
|
|
|
match proj.elem {
|
|
|
|
ProjectionElem::Field(_ /*field*/, _ /*ty*/) => {
|
|
|
|
// FIXME: add union handling
|
|
|
|
self.next = Some(&proj.base);
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
ProjectionElem::Downcast(..) |
|
|
|
|
ProjectionElem::Subslice { .. } |
|
|
|
|
ProjectionElem::ConstantIndex { .. } |
|
|
|
|
ProjectionElem::Index(_) => {
|
|
|
|
cursor = &proj.base;
|
|
|
|
continue 'cursor;
|
|
|
|
}
|
|
|
|
ProjectionElem::Deref => {
|
|
|
|
// (handled below)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(proj.elem, ProjectionElem::Deref);
|
|
|
|
|
|
|
|
match self.kind {
|
|
|
|
PrefixSet::Shallow => {
|
|
|
|
// shallow prefixes are found by stripping away
|
|
|
|
// fields, but stop at *any* dereference.
|
|
|
|
// So we can just stop the traversal now.
|
|
|
|
self.next = None;
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
PrefixSet::All => {
|
|
|
|
// all prefixes: just blindly enqueue the base
|
|
|
|
// of the projection
|
|
|
|
self.next = Some(&proj.base);
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
PrefixSet::Supporting => {
|
|
|
|
// fall through!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(self.kind, PrefixSet::Supporting);
|
|
|
|
// supporting prefixes: strip away fields and
|
|
|
|
// derefs, except we stop at the deref of a shared
|
|
|
|
// reference.
|
|
|
|
|
2019-06-03 17:26:48 -05:00
|
|
|
let ty = proj.base.ty(self.body, self.tcx).ty;
|
2017-12-06 12:27:38 -06:00
|
|
|
match ty.sty {
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::RawPtr(_) |
|
|
|
|
ty::Ref(
|
2017-12-06 12:27:38 -06:00
|
|
|
_, /*rgn*/
|
2018-05-02 08:21:05 -05:00
|
|
|
_, /*ty*/
|
|
|
|
hir::MutImmutable
|
2017-12-06 12:27:38 -06:00
|
|
|
) => {
|
|
|
|
// don't continue traversing over derefs of raw pointers or shared borrows.
|
|
|
|
self.next = None;
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Ref(
|
2017-12-06 12:27:38 -06:00
|
|
|
_, /*rgn*/
|
2018-05-02 08:21:05 -05:00
|
|
|
_, /*ty*/
|
|
|
|
hir::MutMutable,
|
2017-12-06 12:27:38 -06:00
|
|
|
) => {
|
|
|
|
self.next = Some(&proj.base);
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
|
2018-08-21 19:35:02 -05:00
|
|
|
ty::Adt(..) if ty.is_box() => {
|
2017-12-06 12:27:38 -06:00
|
|
|
self.next = Some(&proj.base);
|
|
|
|
return Some(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => panic!("unknown type fed to Projection Deref."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|