Skip to content

Commit 905818f

Browse files
Add getter for Place which ignores Derefs
1 parent 2628f57 commit 905818f

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/librustc/mir/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,30 @@ impl<'tcx> Place<'tcx> {
18891889
}
18901890
}
18911891

1892+
/// Finds the innermost `PlaceBase` for this `Place`.
1893+
pub fn base(&self) -> &PlaceBase<'tcx> {
1894+
&self.base
1895+
}
1896+
1897+
/// Finds the innermost `PlaceBase` for this `Place`, or `None` if the target of this `Place`
1898+
/// is behind an indirection.
1899+
pub fn base_direct(&self) -> Option<&PlaceBase<'tcx>> {
1900+
self.iterate(|base, projections| {
1901+
for proj in projections {
1902+
if let PlaceElem::Deref = proj.elem {
1903+
return None;
1904+
}
1905+
}
1906+
1907+
Some(base)
1908+
})
1909+
}
1910+
1911+
/// Finds the innermost `Local` from this `Place`.
1912+
pub fn base_local(&self) -> Option<Local> {
1913+
self.base().local()
1914+
}
1915+
18921916
/// Recursively "iterates" over place components, generating a `PlaceBase` and
18931917
/// `Projections` list and invoking `op` with a `ProjectionsIter`.
18941918
pub fn iterate<R>(
@@ -1984,6 +2008,15 @@ impl<'a, 'tcx> PlaceRef<'a, 'tcx> {
19842008
}
19852009
}
19862010

2011+
impl PlaceBase<'_> {
2012+
pub fn local(&self) -> Option<Local> {
2013+
match *self {
2014+
PlaceBase::Local(local) => Some(local),
2015+
PlaceBase::Static(_) => None,
2016+
}
2017+
}
2018+
}
2019+
19872020
/// A linked list of projections running up the stack; begins with the
19882021
/// innermost projection and extends to the outermost (e.g., `a.b.c`
19892022
/// would have the place `b` with a "next" pointer to `b.c`).

0 commit comments

Comments
 (0)