1
Fork 0

Replace into_place with to_place

This commit is contained in:
Cameron Steffen 2022-11-17 18:55:06 -06:00
parent be5b7778c8
commit 105abe39c0
4 changed files with 20 additions and 34 deletions

View file

@ -65,7 +65,7 @@ pub(crate) enum PlaceBase {
/// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a /// `PlaceBuilder` is used to create places during MIR construction. It allows you to "build up" a
/// place by pushing more and more projections onto the end, and then convert the final set into a /// place by pushing more and more projections onto the end, and then convert the final set into a
/// place using the `into_place` method. /// place using the `to_place` method.
/// ///
/// This is used internally when building a place for an expression like `a.b.c`. The fields `b` /// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
/// and `c` can be progressively pushed onto the place builder that is created when converting `a`. /// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
@ -254,17 +254,8 @@ fn strip_prefix<'a, 'tcx>(
} }
impl<'tcx> PlaceBuilder<'tcx> { impl<'tcx> PlaceBuilder<'tcx> {
pub(in crate::build) fn into_place(mut self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> { pub(in crate::build) fn to_place(&self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> {
self = self.resolve_upvar(cx).unwrap_or(self); self.try_to_place(cx).unwrap()
let PlaceBase::Local(local) = self.base else { panic!("expected local") };
Place { local, projection: cx.tcx.intern_place_elems(&self.projection) }
}
fn expect_upvars_resolved(self, cx: &Builder<'_, 'tcx>) -> PlaceBuilder<'tcx> {
match self.base {
PlaceBase::Local(_) => self,
PlaceBase::Upvar {..} => self.resolve_upvar(cx).unwrap(),
}
} }
/// Creates a `Place` or returns `None` if an upvar cannot be resolved /// Creates a `Place` or returns `None` if an upvar cannot be resolved
@ -363,7 +354,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr: &Expr<'tcx>, expr: &Expr<'tcx>,
) -> BlockAnd<Place<'tcx>> { ) -> BlockAnd<Place<'tcx>> {
let place_builder = unpack!(block = self.as_place_builder(block, expr)); let place_builder = unpack!(block = self.as_place_builder(block, expr));
block.and(place_builder.into_place(self)) block.and(place_builder.to_place(self))
} }
/// This is used when constructing a compound `Place`, so that we can avoid creating /// This is used when constructing a compound `Place`, so that we can avoid creating
@ -387,7 +378,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr: &Expr<'tcx>, expr: &Expr<'tcx>,
) -> BlockAnd<Place<'tcx>> { ) -> BlockAnd<Place<'tcx>> {
let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr)); let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
block.and(place_builder.into_place(self)) block.and(place_builder.to_place(self))
} }
/// This is used when constructing a compound `Place`, so that we can avoid creating /// This is used when constructing a compound `Place`, so that we can avoid creating
@ -482,7 +473,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
inferred_ty: expr.ty, inferred_ty: expr.ty,
}); });
let place = place_builder.clone().into_place(this); let place = place_builder.to_place(this);
this.cfg.push( this.cfg.push(
block, block,
Statement { Statement {
@ -607,7 +598,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let is_outermost_index = fake_borrow_temps.is_none(); let is_outermost_index = fake_borrow_temps.is_none();
let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps); let fake_borrow_temps = fake_borrow_temps.unwrap_or(base_fake_borrow_temps);
let mut base_place = let base_place =
unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),)); unpack!(block = self.expr_as_place(block, base, mutability, Some(fake_borrow_temps),));
// Making this a *fresh* temporary means we do not have to worry about // Making this a *fresh* temporary means we do not have to worry about
@ -615,14 +606,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// The "retagging" transformation (for Stacked Borrows) relies on this. // The "retagging" transformation (for Stacked Borrows) relies on this.
let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,)); let idx = unpack!(block = self.as_temp(block, temp_lifetime, index, Mutability::Not,));
block = self.bounds_check(block, base_place.clone(), idx, expr_span, source_info); block = self.bounds_check(block, &base_place, idx, expr_span, source_info);
if is_outermost_index { if is_outermost_index {
self.read_fake_borrows(block, fake_borrow_temps, source_info) self.read_fake_borrows(block, fake_borrow_temps, source_info)
} else { } else {
base_place = base_place.expect_upvars_resolved(self);
self.add_fake_borrows_of_base( self.add_fake_borrows_of_base(
&base_place, base_place.to_place(self),
block, block,
fake_borrow_temps, fake_borrow_temps,
expr_span, expr_span,
@ -636,7 +626,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fn bounds_check( fn bounds_check(
&mut self, &mut self,
block: BasicBlock, block: BasicBlock,
slice: PlaceBuilder<'tcx>, slice: &PlaceBuilder<'tcx>,
index: Local, index: Local,
expr_span: Span, expr_span: Span,
source_info: SourceInfo, source_info: SourceInfo,
@ -648,7 +638,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let lt = self.temp(bool_ty, expr_span); let lt = self.temp(bool_ty, expr_span);
// len = len(slice) // len = len(slice)
self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.into_place(self))); self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.to_place(self)));
// lt = idx < len // lt = idx < len
self.cfg.push_assign( self.cfg.push_assign(
block, block,
@ -666,19 +656,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fn add_fake_borrows_of_base( fn add_fake_borrows_of_base(
&mut self, &mut self,
base_place: &PlaceBuilder<'tcx>, base_place: Place<'tcx>,
block: BasicBlock, block: BasicBlock,
fake_borrow_temps: &mut Vec<Local>, fake_borrow_temps: &mut Vec<Local>,
expr_span: Span, expr_span: Span,
source_info: SourceInfo, source_info: SourceInfo,
) { ) {
let tcx = self.tcx; let tcx = self.tcx;
let local = match base_place.base {
PlaceBase::Local(local) => local,
PlaceBase::Upvar { .. } => bug!("Expected PlacseBase::Local found Upvar"),
};
let place_ty = Place::ty_from(local, &base_place.projection, &self.local_decls, tcx); let place_ty = base_place.ty(&self.local_decls, tcx);
if let ty::Slice(_) = place_ty.ty.kind() { if let ty::Slice(_) = place_ty.ty.kind() {
// We need to create fake borrows to ensure that the bounds // We need to create fake borrows to ensure that the bounds
// check that we just did stays valid. Since we can't assign to // check that we just did stays valid. Since we can't assign to
@ -688,7 +674,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
match elem { match elem {
ProjectionElem::Deref => { ProjectionElem::Deref => {
let fake_borrow_deref_ty = Place::ty_from( let fake_borrow_deref_ty = Place::ty_from(
local, base_place.local,
&base_place.projection[..idx], &base_place.projection[..idx],
&self.local_decls, &self.local_decls,
tcx, tcx,
@ -706,14 +692,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Rvalue::Ref( Rvalue::Ref(
tcx.lifetimes.re_erased, tcx.lifetimes.re_erased,
BorrowKind::Shallow, BorrowKind::Shallow,
Place { local, projection }, Place { local: base_place.local, projection },
), ),
); );
fake_borrow_temps.push(fake_borrow_temp); fake_borrow_temps.push(fake_borrow_temp);
} }
ProjectionElem::Index(_) => { ProjectionElem::Index(_) => {
let index_ty = Place::ty_from( let index_ty = Place::ty_from(
local, base_place.local,
&base_place.projection[..idx], &base_place.projection[..idx],
&self.local_decls, &self.local_decls,
tcx, tcx,

View file

@ -660,7 +660,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// by the parent itself. The mutability of the current capture // by the parent itself. The mutability of the current capture
// is same as that of the capture in the parent closure. // is same as that of the capture in the parent closure.
PlaceBase::Upvar { .. } => { PlaceBase::Upvar { .. } => {
let enclosing_upvars_resolved = arg_place_builder.clone().into_place(this); let enclosing_upvars_resolved = arg_place_builder.to_place(this);
match enclosing_upvars_resolved.as_ref() { match enclosing_upvars_resolved.as_ref() {
PlaceRef { PlaceRef {
@ -697,7 +697,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false }, Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
}; };
let arg_place = arg_place_builder.into_place(this); let arg_place = arg_place_builder.to_place(this);
this.cfg.push_assign( this.cfg.push_assign(
block, block,

View file

@ -365,7 +365,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
None => { None => {
let place_builder = place_builder.clone(); let place_builder = place_builder.clone();
this.consume_by_copy_or_move( this.consume_by_copy_or_move(
place_builder.field(n, *ty).into_place(this), place_builder.field(n, *ty).to_place(this),
) )
} }
}) })

View file

@ -154,7 +154,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
test: &Test<'tcx>, test: &Test<'tcx>,
make_target_blocks: impl FnOnce(&mut Self) -> Vec<BasicBlock>, make_target_blocks: impl FnOnce(&mut Self) -> Vec<BasicBlock>,
) { ) {
let place = place_builder.into_place(self); let place = place_builder.to_place(self);
let place_ty = place.ty(&self.local_decls, self.tcx); let place_ty = place.ty(&self.local_decls, self.tcx);
debug!(?place, ?place_ty,); debug!(?place, ?place_ty,);