]> git.lizzy.rs Git - rust.git/commitdiff
condense duplicate code into seperate functions
authorDrMeepster <19316085+DrMeepster@users.noreply.github.com>
Fri, 20 May 2022 01:28:11 +0000 (18:28 -0700)
committerDrMeepster <19316085+DrMeepster@users.noreply.github.com>
Thu, 16 Jun 2022 01:39:23 +0000 (18:39 -0700)
compiler/rustc_mir_transform/src/elaborate_box_derefs.rs
compiler/rustc_mir_transform/src/generator.rs

index 57f7b5662113a61b9694723f77bd3936042e12b8..cc91aaa374c3e033370196ac6471627fcae2903e 100644 (file)
@@ -9,7 +9,35 @@
 use rustc_middle::mir::visit::MutVisitor;
 use rustc_middle::mir::*;
 use rustc_middle::ty::subst::Subst;
-use rustc_middle::ty::TyCtxt;
+use rustc_middle::ty::{Ty, TyCtxt};
+
+/// Constructs the types used when accessing a Box's pointer
+pub fn build_ptr_tys<'tcx>(
+    tcx: TyCtxt<'tcx>,
+    pointee: Ty<'tcx>,
+    unique_did: DefId,
+    nonnull_did: DefId,
+) -> (Ty<'tcx>, Ty<'tcx>, Ty<'tcx>) {
+    let substs = tcx.intern_substs(&[pointee.into()]);
+    let unique_ty = tcx.bound_type_of(unique_did).subst(tcx, substs);
+    let nonnull_ty = tcx.bound_type_of(nonnull_did).subst(tcx, substs);
+    let ptr_ty = tcx.mk_imm_ptr(pointee);
+
+    (unique_ty, nonnull_ty, ptr_ty)
+}
+
+// Constructs the projection needed to access a Box's pointer
+pub fn build_projection<'tcx>(
+    unique_ty: Ty<'tcx>,
+    nonnull_ty: Ty<'tcx>,
+    ptr_ty: Ty<'tcx>,
+) -> [PlaceElem<'tcx>; 3] {
+    [
+        PlaceElem::Field(Field::new(0), unique_ty),
+        PlaceElem::Field(Field::new(0), nonnull_ty),
+        PlaceElem::Field(Field::new(0), ptr_ty),
+    ]
+}
 
 struct ElaborateBoxDerefVisitor<'tcx, 'a> {
     tcx: TyCtxt<'tcx>,
@@ -38,10 +66,8 @@ fn visit_place(
         if place.projection.first() == Some(&PlaceElem::Deref) && base_ty.is_box() {
             let source_info = self.local_decls[place.local].source_info;
 
-            let substs = tcx.intern_substs(&[base_ty.boxed_ty().into()]);
-            let unique_ty = tcx.bound_type_of(self.unique_did).subst(tcx, substs);
-            let nonnull_ty = tcx.bound_type_of(self.nonnull_did).subst(tcx, substs);
-            let ptr_ty = tcx.mk_imm_ptr(base_ty.boxed_ty());
+            let (unique_ty, nonnull_ty, ptr_ty) =
+                build_ptr_tys(tcx, base_ty.boxed_ty(), self.unique_did, self.nonnull_did);
 
             let ptr_local = self.patch.new_temp(ptr_ty, source_info.span);
             self.local_decls.push(LocalDecl::new(ptr_ty, source_info.span));
@@ -51,14 +77,10 @@ fn visit_place(
             self.patch.add_assign(
                 location,
                 Place::from(ptr_local),
-                Rvalue::Use(Operand::Copy(Place::from(place.local).project_deeper(
-                    &[
-                        PlaceElem::Field(Field::new(0), unique_ty),
-                        PlaceElem::Field(Field::new(0), nonnull_ty),
-                        PlaceElem::Field(Field::new(0), ptr_ty),
-                    ],
-                    tcx,
-                ))),
+                Rvalue::Use(Operand::Copy(
+                    Place::from(place.local)
+                        .project_deeper(&build_projection(unique_ty, nonnull_ty, ptr_ty), tcx),
+                )),
             );
 
             place.local = ptr_local;
index a5faf26916418d4c56a20482570158d66d0d0d6a..62c3a845ee26721d12863563951dab26eb657464 100644 (file)
@@ -1240,10 +1240,13 @@ fn create_cases<'tcx>(
 
                         let nonnull_did = nonnull_def.non_enum_variant().fields[0].did;
 
-                        let substs = tcx.intern_substs(&[box_ty.boxed_ty().into()]);
-                        let unique_ty = tcx.bound_type_of(unique_did).subst(tcx, substs);
-                        let nonnull_ty = tcx.bound_type_of(nonnull_did).subst(tcx, substs);
-                        let ptr_ty = tcx.mk_imm_ptr(box_ty.boxed_ty());
+                        let (unique_ty, nonnull_ty, ptr_ty) =
+                            crate::elaborate_box_derefs::build_ptr_tys(
+                                tcx,
+                                box_ty.boxed_ty(),
+                                unique_did,
+                                nonnull_did,
+                            );
 
                         let ptr_local = body.local_decls.push(LocalDecl::new(ptr_ty, body.span));
 
@@ -1257,11 +1260,9 @@ fn create_cases<'tcx>(
                             kind: StatementKind::Assign(Box::new((
                                 Place::from(ptr_local),
                                 Rvalue::Use(Operand::Copy(box_place.project_deeper(
-                                    &[
-                                        PlaceElem::Field(Field::new(0), unique_ty),
-                                        PlaceElem::Field(Field::new(0), nonnull_ty),
-                                        PlaceElem::Field(Field::new(0), ptr_ty),
-                                    ],
+                                    &crate::elaborate_box_derefs::build_projection(
+                                        unique_ty, nonnull_ty, ptr_ty,
+                                    ),
                                     tcx,
                                 ))),
                             ))),