1 //! From the NLL RFC: "The deep [aka 'supporting'] prefixes for an
2 //! place are formed by stripping away fields and derefs, except that
3 //! we stop when we reach the deref of a shared reference. [...] "
5 //! "Shallow prefixes are found by stripping away fields, but stop at
6 //! any dereference. So: writing a path like `a` is illegal if `a.b`
7 //! is borrowed. But: writing `a` is legal if `*a` is borrowed,
8 //! whether or not `a` is a shared or mutable reference. [...] "
10 use super::MirBorrowckCtxt;
13 use rustc_middle::mir::{Body, PlaceRef, ProjectionElem};
14 use rustc_middle::ty::{self, TyCtxt};
16 pub trait IsPrefixOf<'tcx> {
17 fn is_prefix_of(&self, other: PlaceRef<'tcx>) -> bool;
20 impl<'tcx> IsPrefixOf<'tcx> for PlaceRef<'tcx> {
21 fn is_prefix_of(&self, other: PlaceRef<'tcx>) -> bool {
22 self.local == other.local
23 && self.projection.len() <= other.projection.len()
24 && self.projection == &other.projection[..self.projection.len()]
28 pub(super) struct Prefixes<'cx, 'tcx> {
29 body: &'cx Body<'tcx>,
32 next: Option<PlaceRef<'tcx>>,
35 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
36 pub(super) enum PrefixSet {
37 /// Doesn't stop until it returns the base case (a Local or
40 /// Stops at any dereference.
42 /// Stops at the deref of a shared reference.
46 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
47 /// Returns an iterator over the prefixes of `place`
48 /// (inclusive) from longest to smallest, potentially
49 /// terminating the iteration early based on `kind`.
50 pub(super) fn prefixes(
52 place_ref: PlaceRef<'tcx>,
54 ) -> Prefixes<'cx, 'tcx> {
55 Prefixes { next: Some(place_ref), kind, body: self.body, tcx: self.infcx.tcx }
59 impl<'cx, 'tcx> Iterator for Prefixes<'cx, 'tcx> {
60 type Item = PlaceRef<'tcx>;
61 fn next(&mut self) -> Option<Self::Item> {
62 let mut cursor = self.next?;
64 // Post-processing `place`: Enqueue any remaining
65 // work. Also, `place` may not be a prefix itself, but
66 // may hold one further down (e.g., we never return
67 // downcasts here, but may return a base of a downcast).
70 match cursor.last_projection() {
75 Some((cursor_base, elem)) => {
77 ProjectionElem::Field(_ /*field*/, _ /*ty*/) => {
78 // FIXME: add union handling
79 self.next = Some(cursor_base);
82 ProjectionElem::Downcast(..)
83 | ProjectionElem::Subslice { .. }
84 | ProjectionElem::ConstantIndex { .. }
85 | ProjectionElem::Index(_) => {
89 ProjectionElem::Deref => {
94 assert_eq!(elem, ProjectionElem::Deref);
97 PrefixSet::Shallow => {
98 // Shallow prefixes are found by stripping away
99 // fields, but stop at *any* dereference.
100 // So we can just stop the traversal now.
105 // All prefixes: just blindly enqueue the base
106 // of the projection.
107 self.next = Some(cursor_base);
110 PrefixSet::Supporting => {
115 assert_eq!(self.kind, PrefixSet::Supporting);
116 // Supporting prefixes: strip away fields and
117 // derefs, except we stop at the deref of a shared
120 let ty = cursor_base.ty(self.body, self.tcx).ty;
122 ty::RawPtr(_) | ty::Ref(_ /*rgn*/, _ /*ty*/, hir::Mutability::Not) => {
123 // don't continue traversing over derefs of raw pointers or shared
129 ty::Ref(_ /*rgn*/, _ /*ty*/, hir::Mutability::Mut) => {
130 self.next = Some(cursor_base);
134 ty::Adt(..) if ty.is_box() => {
135 self.next = Some(cursor_base);
139 _ => panic!("unknown type fed to Projection Deref."),