Skip to content

Commit 413faf3

Browse files
Auto merge of #144477 - cjgillot:gvn-index, r=<try>
Gvn index
2 parents 430d6ed + 783ac7a commit 413faf3

File tree

5 files changed

+265
-163
lines changed

5 files changed

+265
-163
lines changed

compiler/rustc_middle/src/mir/statement.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ impl<'tcx> PlaceTy<'tcx> {
160160
/// Convenience wrapper around `projection_ty_core` for `PlaceElem`,
161161
/// where we can just use the `Ty` that is already stored inline on
162162
/// field projection elems.
163-
pub fn projection_ty(self, tcx: TyCtxt<'tcx>, elem: PlaceElem<'tcx>) -> PlaceTy<'tcx> {
163+
pub fn projection_ty<V: ::std::fmt::Debug>(
164+
self,
165+
tcx: TyCtxt<'tcx>,
166+
elem: ProjectionElem<V, Ty<'tcx>>,
167+
) -> PlaceTy<'tcx> {
164168
self.projection_ty_core(tcx, &elem, |ty| ty, |_, _, _, ty| ty, |ty| ty)
165169
}
166170

@@ -290,6 +294,30 @@ impl<V, T> ProjectionElem<V, T> {
290294
Self::UnwrapUnsafeBinder(..) => false,
291295
}
292296
}
297+
298+
pub fn try_map<V2, T2>(
299+
self,
300+
v: impl FnOnce(V) -> Option<V2>,
301+
t: impl FnOnce(T) -> T2,
302+
) -> Option<ProjectionElem<V2, T2>> {
303+
Some(match self {
304+
ProjectionElem::Deref => ProjectionElem::Deref,
305+
ProjectionElem::Downcast(name, read_variant) => {
306+
ProjectionElem::Downcast(name, read_variant)
307+
}
308+
ProjectionElem::Field(f, ty) => ProjectionElem::Field(f, t(ty)),
309+
ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
310+
ProjectionElem::ConstantIndex { offset, min_length, from_end }
311+
}
312+
ProjectionElem::Subslice { from, to, from_end } => {
313+
ProjectionElem::Subslice { from, to, from_end }
314+
}
315+
ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(t(ty)),
316+
ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(t(ty)),
317+
ProjectionElem::UnwrapUnsafeBinder(ty) => ProjectionElem::UnwrapUnsafeBinder(t(ty)),
318+
ProjectionElem::Index(val) => ProjectionElem::Index(v(val)?),
319+
})
320+
}
293321
}
294322

295323
/// Alias for projections as they appear in `UserTypeProjection`, where we

0 commit comments

Comments
 (0)