1 #![deny(rustc::untranslatable_diagnostic)]
2 #![deny(rustc::diagnostic_outside_of_impl)]
3 //! From the NLL RFC: "The deep [aka 'supporting'] prefixes for an
4 //! place are formed by stripping away fields and derefs, except that
5 //! we stop when we reach the deref of a shared reference. [...] "
7 //! "Shallow prefixes are found by stripping away fields, but stop at
8 //! any dereference. So: writing a path like `a` is illegal if `a.b`
9 //! is borrowed. But: writing `a` is legal if `*a` is borrowed,
10 //! whether or not `a` is a shared or mutable reference. [...] "
12 use super::MirBorrowckCtxt;
15 use rustc_middle::mir::{Body, PlaceRef, ProjectionElem};
16 use rustc_middle::ty::{self, TyCtxt};
18 pub trait IsPrefixOf<'tcx> {
19 fn is_prefix_of(&self, other: PlaceRef<'tcx>) -> bool;
22 impl<'tcx> IsPrefixOf<'tcx> for PlaceRef<'tcx> {
23 fn is_prefix_of(&self, other: PlaceRef<'tcx>) -> bool {
24 self.local == other.local
25 && self.projection.len() <= other.projection.len()
26 && self.projection == &other.projection[..self.projection.len()]
30 pub(super) struct Prefixes<'cx, 'tcx> {
31 body: &'cx Body<'tcx>,
34 next: Option<PlaceRef<'tcx>>,
37 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
38 pub(super) enum PrefixSet {
39 /// Doesn't stop until it returns the base case (a Local or
42 /// Stops at any dereference.
44 /// Stops at the deref of a shared reference.
48 impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
49 /// Returns an iterator over the prefixes of `place`
50 /// (inclusive) from longest to smallest, potentially
51 /// terminating the iteration early based on `kind`.
52 pub(super) fn prefixes(
54 place_ref: PlaceRef<'tcx>,
56 ) -> Prefixes<'cx, 'tcx> {
57 Prefixes { next: Some(place_ref), kind, body: self.body, tcx: self.infcx.tcx }
61 impl<'cx, 'tcx> Iterator for Prefixes<'cx, 'tcx> {
62 type Item = PlaceRef<'tcx>;
63 fn next(&mut self) -> Option<Self::Item> {
64 let mut cursor = self.next?;
66 // Post-processing `place`: Enqueue any remaining
67 // work. Also, `place` may not be a prefix itself, but
68 // may hold one further down (e.g., we never return
69 // downcasts here, but may return a base of a downcast).
72 match cursor.last_projection() {
77 Some((cursor_base, elem)) => {
79 ProjectionElem::Field(_ /*field*/, _ /*ty*/) => {
80 // FIXME: add union handling
81 self.next = Some(cursor_base);
84 ProjectionElem::Downcast(..)
85 | ProjectionElem::Subslice { .. }
86 | ProjectionElem::OpaqueCast { .. }
87 | ProjectionElem::ConstantIndex { .. }
88 | ProjectionElem::Index(_) => {
92 ProjectionElem::Deref => {
97 assert_eq!(elem, ProjectionElem::Deref);
100 PrefixSet::Shallow => {
101 // Shallow prefixes are found by stripping away
102 // fields, but stop at *any* dereference.
103 // So we can just stop the traversal now.
108 // All prefixes: just blindly enqueue the base
109 // of the projection.
110 self.next = Some(cursor_base);
113 PrefixSet::Supporting => {
118 assert_eq!(self.kind, PrefixSet::Supporting);
119 // Supporting prefixes: strip away fields and
120 // derefs, except we stop at the deref of a shared
123 let ty = cursor_base.ty(self.body, self.tcx).ty;
125 ty::RawPtr(_) | ty::Ref(_ /*rgn*/, _ /*ty*/, hir::Mutability::Not) => {
126 // don't continue traversing over derefs of raw pointers or shared
132 ty::Ref(_ /*rgn*/, _ /*ty*/, hir::Mutability::Mut) => {
133 self.next = Some(cursor_base);
137 ty::Adt(..) if ty.is_box() => {
138 self.next = Some(cursor_base);
142 _ => panic!("unknown type fed to Projection Deref."),