diff options
| author | DrMeepster <19316085+DrMeepster@users.noreply.github.com> | 2022-05-19 18:28:11 -0700 |
|---|---|---|
| committer | DrMeepster <19316085+DrMeepster@users.noreply.github.com> | 2022-06-15 18:39:23 -0700 |
| commit | 6003c2501c63176dbb7f15293ec91758c82416db (patch) | |
| tree | 8f8bb9bf913fd2033ef54ced9a44b53b4aac7046 | |
| parent | dff1f9f6bbb4a6a9c86da8d81695fec37054b0fb (diff) | |
| download | rust-6003c2501c63176dbb7f15293ec91758c82416db.tar.gz rust-6003c2501c63176dbb7f15293ec91758c82416db.zip | |
condense duplicate code into seperate functions
| -rw-r--r-- | compiler/rustc_mir_transform/src/elaborate_box_derefs.rs | 48 | ||||
| -rw-r--r-- | compiler/rustc_mir_transform/src/generator.rs | 19 |
2 files changed, 45 insertions, 22 deletions
diff --git a/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs b/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs index 57f7b566211..cc91aaa374c 100644 --- a/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs +++ b/compiler/rustc_mir_transform/src/elaborate_box_derefs.rs @@ -9,7 +9,35 @@ use rustc_middle::mir::patch::MirPatch; 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 @@ impl<'tcx, 'a> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'tcx, 'a> { 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 @@ impl<'tcx, 'a> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'tcx, 'a> { 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; diff --git a/compiler/rustc_mir_transform/src/generator.rs b/compiler/rustc_mir_transform/src/generator.rs index a5faf269164..62c3a845ee2 100644 --- a/compiler/rustc_mir_transform/src/generator.rs +++ b/compiler/rustc_mir_transform/src/generator.rs @@ -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, ))), ))), |
