From 53481a5a8fde83ed1a5d7e8302be028a4c7d8da5 Mon Sep 17 00:00:00 2001 From: Rose Hudson Date: Wed, 22 Jun 2022 14:06:13 +0100 Subject: [PATCH] implement `iter_projections` function on `PlaceRef` this makes the api more flexible. the original function now calls the PlaceRef version to avoid duplicating the code. --- compiler/rustc_middle/src/mir/mod.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 4265559cd3197..3f5b16d5ea5f7 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2145,10 +2145,7 @@ impl<'tcx> Place<'tcx> { pub fn iter_projections( self, ) -> impl Iterator, PlaceElem<'tcx>)> + DoubleEndedIterator { - self.projection.iter().enumerate().map(move |(i, proj)| { - let base = PlaceRef { local: self.local, projection: &self.projection[..i] }; - (base, proj) - }) + self.as_ref().iter_projections() } /// Generates a new place by appending `more_projections` to the existing ones @@ -2208,6 +2205,23 @@ impl<'tcx> PlaceRef<'tcx> { None } } + + /// 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. + /// As a concrete example, given the place a.b.c, this would yield: + /// - (a, .b) + /// - (a.b, .c) + /// + /// Given a place without projections, the iterator is empty. + #[inline] + pub fn iter_projections( + self, + ) -> impl Iterator, PlaceElem<'tcx>)> + DoubleEndedIterator { + self.projection.iter().enumerate().map(move |(i, proj)| { + let base = PlaceRef { local: self.local, projection: &self.projection[..i] }; + (base, *proj) + }) + } } impl Debug for Place<'_> {