]> git.lizzy.rs Git - rust.git/commitdiff
add function to iterate through all sub-places, and add PlaceRef::ty
authorRalf Jung <post@ralfj.de>
Fri, 20 Nov 2020 10:50:15 +0000 (11:50 +0100)
committerRalf Jung <post@ralfj.de>
Sat, 21 Nov 2020 11:48:56 +0000 (12:48 +0100)
compiler/rustc_middle/src/mir/mod.rs
compiler/rustc_middle/src/mir/tcx.rs

index 9289d4708de1e07fd5c21e54f9d3ea036095c803..4a7d4dab50794842c515251b18705559fab712cf 100644 (file)
@@ -1741,6 +1741,15 @@ pub fn as_local(&self) -> Option<Local> {
     pub fn as_ref(&self) -> PlaceRef<'tcx> {
         PlaceRef { local: self.local, projection: &self.projection }
     }
+
+    /// Iterate over the projections in evaluation order, i.e., the first element is the base with
+    /// its projection and then subsequently more projections are added.
+    pub fn iter_projections(self) -> impl Iterator<Item=(PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator {
+        self.projection.iter().enumerate().map(move |(i, proj)| {
+            let base = PlaceRef { local: self.local, projection: &self.projection[..i] };
+            (base, proj)
+        })
+    }
 }
 
 impl From<Local> for Place<'_> {
index f0bfdae261c643bdf2b6801ea8728aeba82a77e1..1b2c1076a6880317b30a613a889d8592e5a1b93c 100644 (file)
@@ -136,6 +136,15 @@ pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
     }
 }
 
+impl<'tcx> PlaceRef<'tcx> {
+    pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
+    where
+        D: HasLocalDecls<'tcx>,
+    {
+        Place::ty_from(self.local, &self.projection, local_decls, tcx)
+    }
+}
+
 pub enum RvalueInitializationState {
     Shallow,
     Deep,