Skip to content

Commit c5e371d

Browse files
committed
Inline Index conversion into project method
1 parent 6a01dc9 commit c5e371d

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

compiler/rustc_mir_transform/src/const_prop_lint.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,24 @@ impl<'tcx> From<ImmTy<'tcx>> for Value<'tcx> {
101101
}
102102

103103
impl<'tcx> Value<'tcx> {
104-
fn project(&self, proj: impl Iterator<Item = Option<PlaceElem<'tcx>>>) -> Option<&Value<'tcx>> {
104+
fn project(
105+
&self,
106+
proj: &[PlaceElem<'tcx>],
107+
prop: &ConstPropagator<'_, 'tcx>,
108+
) -> Option<&Value<'tcx>> {
105109
let mut this = self;
106110
for proj in proj {
107-
this = match (proj?, this) {
108-
(ProjectionElem::Field(idx, _), Value::Aggregate { fields, .. }) => {
111+
this = match (*proj, this) {
112+
(PlaceElem::Field(idx, _), Value::Aggregate { fields, .. }) => {
109113
fields.get(idx).unwrap_or(&Value::Uninit)
110114
}
115+
(PlaceElem::Index(idx), Value::Aggregate { fields, .. }) => {
116+
let idx = prop.get_const(idx.into())?.immediate()?;
117+
let idx = prop.ecx.read_target_usize(idx).ok()?;
118+
fields.get(FieldIdx::from_u32(idx.try_into().ok()?)).unwrap_or(&Value::Uninit)
119+
}
111120
(
112-
ProjectionElem::ConstantIndex { offset, min_length: 1, from_end: false },
121+
PlaceElem::ConstantIndex { offset, min_length: 1, from_end: false },
113122
Value::Aggregate { fields, .. },
114123
) => fields
115124
.get(FieldIdx::from_u32(offset.try_into().ok()?))
@@ -204,8 +213,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
204213
}
205214

206215
fn get_const(&self, place: Place<'tcx>) -> Option<&Value<'tcx>> {
207-
self.locals[place.local]
208-
.project(place.projection.iter().map(|proj| self.try_eval_index_offset(proj)))
216+
self.locals[place.local].project(&place.projection, self)
209217
}
210218

211219
/// Remove `local` from the pool of `Locals`. Allows writing to them,
@@ -696,21 +704,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
696704

697705
Some(())
698706
}
699-
700-
fn try_eval_index_offset(&self, proj: PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
701-
Some(match proj {
702-
ProjectionElem::Index(local) => {
703-
let val = self.get_const(local.into())?;
704-
let op = val.immediate()?;
705-
ProjectionElem::ConstantIndex {
706-
offset: self.ecx.read_target_usize(op).ok()?,
707-
min_length: 1,
708-
from_end: false,
709-
}
710-
}
711-
other => other,
712-
})
713-
}
714707
}
715708

716709
impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {

0 commit comments

Comments
 (0)