about summary refs log tree commit diff
path: root/compiler/rustc_mir_transform/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_mir_transform/src')
-rw-r--r--compiler/rustc_mir_transform/src/deref_separator.rs131
1 files changed, 76 insertions, 55 deletions
diff --git a/compiler/rustc_mir_transform/src/deref_separator.rs b/compiler/rustc_mir_transform/src/deref_separator.rs
index 24b626ad966..7d81bb74cd6 100644
--- a/compiler/rustc_mir_transform/src/deref_separator.rs
+++ b/compiler/rustc_mir_transform/src/deref_separator.rs
@@ -1,68 +1,89 @@
 use crate::MirPass;
+use rustc_index::vec::IndexVec;
 use rustc_middle::mir::patch::MirPatch;
+use rustc_middle::mir::visit::{MutVisitor, PlaceContext};
 use rustc_middle::mir::*;
 use rustc_middle::ty::TyCtxt;
 pub struct Derefer;
 
-pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
-    let mut patch = MirPatch::new(body);
-    let (basic_blocks, local_decl) = body.basic_blocks_and_local_decls_mut();
-    for (block, data) in basic_blocks.iter_enumerated_mut() {
-        for (i, stmt) in data.statements.iter_mut().enumerate() {
-            match stmt.kind {
-                StatementKind::Assign(box (og_place, Rvalue::Ref(region, borrow_knd, place))) => {
-                    let mut place_local = place.local;
-                    let mut last_len = 0;
-                    for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
-                        if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() {
-                            // The type that we are derefing.
-                            let ty = p_ref.ty(local_decl, tcx).ty;
-                            let temp = patch.new_temp(ty, stmt.source_info.span);
-
-                            // Because we are assigning this right before original statement
-                            // we are using index i of statement.
-                            let loc = Location { block: block, statement_index: i };
-                            patch.add_statement(loc, StatementKind::StorageLive(temp));
-
-                            // We are adding current p_ref's projections to our
-                            // temp value, excluding projections we already covered.
-                            let deref_place = Place::from(place_local)
-                                .project_deeper(&p_ref.projection[last_len..], tcx);
-                            patch.add_assign(
-                                loc,
-                                Place::from(temp),
-                                Rvalue::Use(Operand::Move(deref_place)),
-                            );
-
-                            place_local = temp;
-                            last_len = p_ref.projection.len();
-
-                            // We are creating a place by using our temp value's location
-                            // and copying derefed values which we need to create new statement.
-                            let temp_place =
-                                Place::from(temp).project_deeper(&place.projection[idx..], tcx);
-                            let new_stmt = Statement {
-                                source_info: stmt.source_info,
-                                kind: StatementKind::Assign(Box::new((
-                                    og_place,
-                                    Rvalue::Ref(region, borrow_knd, temp_place),
-                                ))),
-                            };
-
-                            // Replace current statement with newly created one.
-                            *stmt = new_stmt;
-
-                            // Since our job with the temp is done it should be gone
-                            let loc = Location { block: block, statement_index: i + 1 };
-                            patch.add_statement(loc, StatementKind::StorageDead(temp));
-                        }
-                    }
+pub struct DerefChecker<'tcx> {
+    tcx: TyCtxt<'tcx>,
+    patcher: MirPatch<'tcx>,
+    local_decls: IndexVec<Local, LocalDecl<'tcx>>,
+}
+
+impl<'tcx> MutVisitor<'tcx> for DerefChecker<'tcx> {
+    fn tcx(&self) -> TyCtxt<'tcx> {
+        self.tcx
+    }
+
+    fn visit_place(&mut self, place: &mut Place<'tcx>, _: PlaceContext, loc: Location) {
+        let mut place_local = place.local;
+        let mut last_len = 0;
+        let mut last_deref_idx = 0;
+
+        let mut prev_temp: Option<Local> = None;
+
+        for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
+            if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() {
+                last_deref_idx = idx;
+            }
+        }
+
+        for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() {
+            if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() {
+                let ty = p_ref.ty(&self.local_decls, self.tcx).ty;
+                let temp =
+                    self.patcher.new_temp(ty, self.local_decls[p_ref.local].source_info.span);
+
+                self.patcher.add_statement(loc, StatementKind::StorageLive(temp));
+
+                // We are adding current p_ref's projections to our
+                // temp value, excluding projections we already covered.
+                let deref_place = Place::from(place_local)
+                    .project_deeper(&p_ref.projection[last_len..], self.tcx);
+                self.patcher.add_assign(
+                    loc,
+                    Place::from(temp),
+                    Rvalue::Use(Operand::Move(deref_place)),
+                );
+
+                place_local = temp;
+                last_len = p_ref.projection.len();
+
+                // Change `Place` only if we are actually at the Place's last deref
+                if idx == last_deref_idx {
+                    let temp_place =
+                        Place::from(temp).project_deeper(&place.projection[idx..], self.tcx);
+                    *place = temp_place;
+                }
+
+                // We are destroying last temp since it's no longer used.
+                if let Some(prev_temp) = prev_temp {
+                    self.patcher.add_statement(loc, StatementKind::StorageDead(prev_temp));
                 }
-                _ => (),
+
+                prev_temp = Some(temp);
             }
         }
+
+        // Since we won't be able to reach final temp, we destroy it outside the loop.
+        if let Some(prev_temp) = prev_temp {
+            let last_loc = Location { block: loc.block, statement_index: loc.statement_index + 1 };
+            self.patcher.add_statement(last_loc, StatementKind::StorageDead(prev_temp));
+        }
     }
-    patch.apply(body);
+}
+
+pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
+    let patch = MirPatch::new(body);
+    let mut checker = DerefChecker { tcx, patcher: patch, local_decls: body.local_decls.clone() };
+
+    for (bb, data) in body.basic_blocks_mut().iter_enumerated_mut() {
+        checker.visit_basic_block_data(bb, data);
+    }
+
+    checker.patcher.apply(body);
 }
 
 impl<'tcx> MirPass<'tcx> for Derefer {