about summary refs log tree commit diff
path: root/compiler/rustc_mir_transform/src
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-01-16 10:55:04 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-01-23 16:35:26 +0000
commitc5e371da19eed3154ce6794a3e13bbbc763ca435 (patch)
tree62824bcb709fedb82562c31b03dbc9f9b4a60205 /compiler/rustc_mir_transform/src
parent6a01dc9ad7c2f5ae88ee68177fddf3971dfd86a0 (diff)
downloadrust-c5e371da19eed3154ce6794a3e13bbbc763ca435.tar.gz
rust-c5e371da19eed3154ce6794a3e13bbbc763ca435.zip
Inline Index conversion into `project` method
Diffstat (limited to 'compiler/rustc_mir_transform/src')
-rw-r--r--compiler/rustc_mir_transform/src/const_prop_lint.rs35
1 files changed, 14 insertions, 21 deletions
diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs
index 76c8cdcc271..8bbb81354d8 100644
--- a/compiler/rustc_mir_transform/src/const_prop_lint.rs
+++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs
@@ -101,15 +101,24 @@ impl<'tcx> From<ImmTy<'tcx>> for Value<'tcx> {
 }
 
 impl<'tcx> Value<'tcx> {
-    fn project(&self, proj: impl Iterator<Item = Option<PlaceElem<'tcx>>>) -> Option<&Value<'tcx>> {
+    fn project(
+        &self,
+        proj: &[PlaceElem<'tcx>],
+        prop: &ConstPropagator<'_, 'tcx>,
+    ) -> Option<&Value<'tcx>> {
         let mut this = self;
         for proj in proj {
-            this = match (proj?, this) {
-                (ProjectionElem::Field(idx, _), Value::Aggregate { fields, .. }) => {
+            this = match (*proj, this) {
+                (PlaceElem::Field(idx, _), Value::Aggregate { fields, .. }) => {
                     fields.get(idx).unwrap_or(&Value::Uninit)
                 }
+                (PlaceElem::Index(idx), Value::Aggregate { fields, .. }) => {
+                    let idx = prop.get_const(idx.into())?.immediate()?;
+                    let idx = prop.ecx.read_target_usize(idx).ok()?;
+                    fields.get(FieldIdx::from_u32(idx.try_into().ok()?)).unwrap_or(&Value::Uninit)
+                }
                 (
-                    ProjectionElem::ConstantIndex { offset, min_length: 1, from_end: false },
+                    PlaceElem::ConstantIndex { offset, min_length: 1, from_end: false },
                     Value::Aggregate { fields, .. },
                 ) => fields
                     .get(FieldIdx::from_u32(offset.try_into().ok()?))
@@ -204,8 +213,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
     }
 
     fn get_const(&self, place: Place<'tcx>) -> Option<&Value<'tcx>> {
-        self.locals[place.local]
-            .project(place.projection.iter().map(|proj| self.try_eval_index_offset(proj)))
+        self.locals[place.local].project(&place.projection, self)
     }
 
     /// Remove `local` from the pool of `Locals`. Allows writing to them,
@@ -696,21 +704,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
 
         Some(())
     }
-
-    fn try_eval_index_offset(&self, proj: PlaceElem<'tcx>) -> Option<PlaceElem<'tcx>> {
-        Some(match proj {
-            ProjectionElem::Index(local) => {
-                let val = self.get_const(local.into())?;
-                let op = val.immediate()?;
-                ProjectionElem::ConstantIndex {
-                    offset: self.ecx.read_target_usize(op).ok()?,
-                    min_length: 1,
-                    from_end: false,
-                }
-            }
-            other => other,
-        })
-    }
 }
 
 impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {