1
Fork 0

Auto merge of #103947 - camsteffen:place-clones, r=cjgillot

Reduce `PlaceBuilder` cloning

Some API tweaks with an eye towards reducing clones.
This commit is contained in:
bors 2022-11-23 13:13:50 +00:00
commit 80b3c6dbde
7 changed files with 136 additions and 152 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`.
@ -167,23 +167,20 @@ fn find_capture_matching_projections<'a, 'tcx>(
}) })
} }
/// Takes a PlaceBuilder and resolves the upvar (if any) within it, so that the /// Takes an upvar place and tries to resolve it into a `PlaceBuilder`
/// `PlaceBuilder` now starts from `PlaceBase::Local`. /// with `PlaceBase::Local`
///
/// Returns a Result with the error being the PlaceBuilder (`from_builder`) that was not found.
#[instrument(level = "trace", skip(cx), ret)] #[instrument(level = "trace", skip(cx), ret)]
fn to_upvars_resolved_place_builder<'tcx>( fn to_upvars_resolved_place_builder<'tcx>(
from_builder: PlaceBuilder<'tcx>,
cx: &Builder<'_, 'tcx>, cx: &Builder<'_, 'tcx>,
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> { var_hir_id: LocalVarId,
match from_builder.base { closure_def_id: LocalDefId,
PlaceBase::Local(_) => Ok(from_builder), projection: &[PlaceElem<'tcx>],
PlaceBase::Upvar { var_hir_id, closure_def_id } => { ) -> Option<PlaceBuilder<'tcx>> {
let Some((capture_index, capture)) = let Some((capture_index, capture)) =
find_capture_matching_projections( find_capture_matching_projections(
&cx.upvars, &cx.upvars,
var_hir_id, var_hir_id,
&from_builder.projection, &projection,
) else { ) else {
let closure_span = cx.tcx.def_span(closure_def_id); let closure_span = cx.tcx.def_span(closure_def_id);
if !enable_precise_capture(cx.tcx, closure_span) { if !enable_precise_capture(cx.tcx, closure_span) {
@ -191,15 +188,15 @@ fn to_upvars_resolved_place_builder<'tcx>(
"No associated capture found for {:?}[{:#?}] even though \ "No associated capture found for {:?}[{:#?}] even though \
capture_disjoint_fields isn't enabled", capture_disjoint_fields isn't enabled",
var_hir_id, var_hir_id,
from_builder.projection projection
) )
} else { } else {
debug!( debug!(
"No associated capture found for {:?}[{:#?}]", "No associated capture found for {:?}[{:#?}]",
var_hir_id, from_builder.projection, var_hir_id, projection,
); );
} }
return Err(from_builder); return None;
}; };
// Access the capture by accessing the field within the Closure struct. // Access the capture by accessing the field within the Closure struct.
@ -209,17 +206,15 @@ fn to_upvars_resolved_place_builder<'tcx>(
// We used some of the projections to build the capture itself, // We used some of the projections to build the capture itself,
// now we apply the remaining to the upvar resolved place. // now we apply the remaining to the upvar resolved place.
trace!(?capture.captured_place, ?from_builder.projection); trace!(?capture.captured_place, ?projection);
let remaining_projections = strip_prefix( let remaining_projections = strip_prefix(
capture.captured_place.place.base_ty, capture.captured_place.place.base_ty,
from_builder.projection, projection,
&capture.captured_place.place.projections, &capture.captured_place.place.projections,
); );
upvar_resolved_place_builder.projection.extend(remaining_projections); upvar_resolved_place_builder.projection.extend(remaining_projections);
Ok(upvar_resolved_place_builder) Some(upvar_resolved_place_builder)
}
}
} }
/// Returns projections remaining after stripping an initial prefix of HIR /// Returns projections remaining after stripping an initial prefix of HIR
@ -228,13 +223,14 @@ fn to_upvars_resolved_place_builder<'tcx>(
/// Supports only HIR projection kinds that represent a path that might be /// Supports only HIR projection kinds that represent a path that might be
/// captured by a closure or a generator, i.e., an `Index` or a `Subslice` /// captured by a closure or a generator, i.e., an `Index` or a `Subslice`
/// projection kinds are unsupported. /// projection kinds are unsupported.
fn strip_prefix<'tcx>( fn strip_prefix<'a, 'tcx>(
mut base_ty: Ty<'tcx>, mut base_ty: Ty<'tcx>,
projections: Vec<PlaceElem<'tcx>>, projections: &'a [PlaceElem<'tcx>],
prefix_projections: &[HirProjection<'tcx>], prefix_projections: &[HirProjection<'tcx>],
) -> impl Iterator<Item = PlaceElem<'tcx>> { ) -> impl Iterator<Item = PlaceElem<'tcx>> + 'a {
let mut iter = projections let mut iter = projections
.into_iter() .iter()
.copied()
// Filter out opaque casts, they are unnecessary in the prefix. // Filter out opaque casts, they are unnecessary in the prefix.
.filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..))); .filter(|elem| !matches!(elem, ProjectionElem::OpaqueCast(..)));
for projection in prefix_projections { for projection in prefix_projections {
@ -258,21 +254,21 @@ fn strip_prefix<'tcx>(
} }
impl<'tcx> PlaceBuilder<'tcx> { impl<'tcx> PlaceBuilder<'tcx> {
pub(in crate::build) fn into_place(self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> { pub(in crate::build) fn to_place(&self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> {
if let PlaceBase::Local(local) = self.base { self.try_to_place(cx).unwrap()
Place { local, projection: cx.tcx.intern_place_elems(&self.projection) }
} else {
self.expect_upvars_resolved(cx).into_place(cx)
}
} }
fn expect_upvars_resolved(self, cx: &Builder<'_, 'tcx>) -> PlaceBuilder<'tcx> { /// Creates a `Place` or returns `None` if an upvar cannot be resolved
to_upvars_resolved_place_builder(self, cx).unwrap() pub(in crate::build) fn try_to_place(&self, cx: &Builder<'_, 'tcx>) -> Option<Place<'tcx>> {
let resolved = self.resolve_upvar(cx);
let builder = resolved.as_ref().unwrap_or(self);
let PlaceBase::Local(local) = builder.base else { return None };
let projection = cx.tcx.intern_place_elems(&builder.projection);
Some(Place { local, projection })
} }
/// Attempts to resolve the `PlaceBuilder`. /// Attempts to resolve the `PlaceBuilder`.
/// On success, it will return the resolved `PlaceBuilder`. /// Returns `None` if this is not an upvar.
/// On failure, it will return itself.
/// ///
/// Upvars resolve may fail for a `PlaceBuilder` when attempting to /// Upvars resolve may fail for a `PlaceBuilder` when attempting to
/// resolve a disjoint field whose root variable is not captured /// resolve a disjoint field whose root variable is not captured
@ -281,11 +277,14 @@ impl<'tcx> PlaceBuilder<'tcx> {
/// not captured. This can happen because the final mir that will be /// not captured. This can happen because the final mir that will be
/// generated doesn't require a read for this place. Failures will only /// generated doesn't require a read for this place. Failures will only
/// happen inside closures. /// happen inside closures.
pub(in crate::build) fn try_upvars_resolved( pub(in crate::build) fn resolve_upvar(
self, &self,
cx: &Builder<'_, 'tcx>, cx: &Builder<'_, 'tcx>,
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> { ) -> Option<PlaceBuilder<'tcx>> {
to_upvars_resolved_place_builder(self, cx) let PlaceBase::Upvar { var_hir_id, closure_def_id } = self.base else {
return None;
};
to_upvars_resolved_place_builder(cx, var_hir_id, closure_def_id, &self.projection)
} }
pub(crate) fn base(&self) -> PlaceBase { pub(crate) fn base(&self) -> PlaceBase {
@ -316,6 +315,14 @@ impl<'tcx> PlaceBuilder<'tcx> {
self.projection.push(elem); self.projection.push(elem);
self self
} }
/// Same as `.clone().project(..)` but more efficient
pub(crate) fn clone_project(&self, elem: PlaceElem<'tcx>) -> Self {
Self {
base: self.base,
projection: Vec::from_iter(self.projection.iter().copied().chain([elem])),
}
}
} }
impl<'tcx> From<Local> for PlaceBuilder<'tcx> { impl<'tcx> From<Local> for PlaceBuilder<'tcx> {
@ -355,7 +362,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
@ -379,7 +386,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
@ -474,7 +481,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 {
@ -599,7 +606,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
@ -607,14 +614,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,
@ -628,7 +634,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,
@ -640,7 +646,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,
@ -658,19 +664,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
@ -680,7 +682,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,
@ -698,14 +700,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

@ -369,8 +369,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let place_builder = let place_builder =
unpack!(block = this.as_place_builder(block, &this.thir[*thir_place])); unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));
if let Ok(place_builder_resolved) = place_builder.try_upvars_resolved(this) { if let Some(mir_place) = place_builder.try_to_place(this) {
let mir_place = place_builder_resolved.into_place(this);
this.cfg.push_fake_read( this.cfg.push_fake_read(
block, block,
this.source_info(this.tcx.hir().span(*hir_id)), this.source_info(this.tcx.hir().span(*hir_id)),
@ -661,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 {
@ -698,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

@ -358,10 +358,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
.map(|(n, ty)| match fields_map.get(&n) { .map(|(n, ty)| match fields_map.get(&n) {
Some(v) => v.clone(), Some(v) => v.clone(),
None => { None => {
let place_builder = place_builder.clone(); let place = place_builder.clone_project(PlaceElem::Field(n, *ty));
this.consume_by_copy_or_move( this.consume_by_copy_or_move(place.to_place(this))
place_builder.field(n, *ty).into_place(this),
)
} }
}) })
.collect() .collect()

View file

@ -169,7 +169,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let scrutinee_place = let scrutinee_place =
unpack!(block = self.lower_scrutinee(block, scrutinee, scrutinee_span,)); unpack!(block = self.lower_scrutinee(block, scrutinee, scrutinee_span,));
let mut arm_candidates = self.create_match_candidates(scrutinee_place.clone(), &arms); let mut arm_candidates = self.create_match_candidates(&scrutinee_place, &arms);
let match_has_guard = arm_candidates.iter().any(|(_, candidate)| candidate.has_guard); let match_has_guard = arm_candidates.iter().any(|(_, candidate)| candidate.has_guard);
let mut candidates = let mut candidates =
@ -221,8 +221,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let cause_matched_place = FakeReadCause::ForMatchedPlace(None); let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
let source_info = self.source_info(scrutinee_span); let source_info = self.source_info(scrutinee_span);
if let Ok(scrutinee_builder) = scrutinee_place_builder.clone().try_upvars_resolved(self) { if let Some(scrutinee_place) = scrutinee_place_builder.try_to_place(self) {
let scrutinee_place = scrutinee_builder.into_place(self);
self.cfg.push_fake_read(block, source_info, cause_matched_place, scrutinee_place); self.cfg.push_fake_read(block, source_info, cause_matched_place, scrutinee_place);
} }
@ -232,7 +231,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
/// Create the initial `Candidate`s for a `match` expression. /// Create the initial `Candidate`s for a `match` expression.
fn create_match_candidates<'pat>( fn create_match_candidates<'pat>(
&mut self, &mut self,
scrutinee: PlaceBuilder<'tcx>, scrutinee: &PlaceBuilder<'tcx>,
arms: &'pat [ArmId], arms: &'pat [ArmId],
) -> Vec<(&'pat Arm<'tcx>, Candidate<'pat, 'tcx>)> ) -> Vec<(&'pat Arm<'tcx>, Candidate<'pat, 'tcx>)>
where where
@ -335,7 +334,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let arm_scope = (arm.scope, arm_source_info); let arm_scope = (arm.scope, arm_source_info);
let match_scope = self.local_scope(); let match_scope = self.local_scope();
self.in_scope(arm_scope, arm.lint_level, |this| { self.in_scope(arm_scope, arm.lint_level, |this| {
// `try_upvars_resolved` may fail if it is unable to resolve the given // `try_to_place` may fail if it is unable to resolve the given
// `PlaceBuilder` inside a closure. In this case, we don't want to include // `PlaceBuilder` inside a closure. In this case, we don't want to include
// a scrutinee place. `scrutinee_place_builder` will fail to be resolved // a scrutinee place. `scrutinee_place_builder` will fail to be resolved
// if the only match arm is a wildcard (`_`). // if the only match arm is a wildcard (`_`).
@ -346,14 +345,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// match foo { _ => () }; // match foo { _ => () };
// }; // };
// ``` // ```
let mut opt_scrutinee_place: Option<(Option<&Place<'tcx>>, Span)> = None; let scrutinee_place = scrutinee_place_builder.try_to_place(this);
let scrutinee_place: Place<'tcx>; let opt_scrutinee_place =
if let Ok(scrutinee_builder) = scrutinee_place.as_ref().map(|place| (Some(place), scrutinee_span));
scrutinee_place_builder.clone().try_upvars_resolved(this)
{
scrutinee_place = scrutinee_builder.into_place(this);
opt_scrutinee_place = Some((Some(&scrutinee_place), scrutinee_span));
}
let scope = this.declare_bindings( let scope = this.declare_bindings(
None, None,
arm.span, arm.span,
@ -592,7 +586,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
while let Some(next) = { while let Some(next) = {
for binding in &candidate_ref.bindings { for binding in &candidate_ref.bindings {
let local = self.var_local_id(binding.var_id, OutsideGuard); let local = self.var_local_id(binding.var_id, OutsideGuard);
// `try_upvars_resolved` may fail if it is unable to resolve the given // `try_to_place` may fail if it is unable to resolve the given
// `PlaceBuilder` inside a closure. In this case, we don't want to include // `PlaceBuilder` inside a closure. In this case, we don't want to include
// a scrutinee place. `scrutinee_place_builder` will fail for destructured // a scrutinee place. `scrutinee_place_builder` will fail for destructured
// assignments. This is because a closure only captures the precise places // assignments. This is because a closure only captures the precise places
@ -606,9 +600,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// let (v1, v2) = foo; // let (v1, v2) = foo;
// }; // };
// ``` // ```
if let Ok(match_pair_resolved) = initializer.clone().try_upvars_resolved(self) { if let Some(place) = initializer.try_to_place(self) {
let place = match_pair_resolved.into_place(self);
let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var( let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. }, VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. },
)))) = self.local_decls[local].local_info else { )))) = self.local_decls[local].local_info else {
@ -1345,7 +1337,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
bug!("Or-patterns should have been sorted to the end"); bug!("Or-patterns should have been sorted to the end");
}; };
let or_span = match_pair.pattern.span; let or_span = match_pair.pattern.span;
let place = match_pair.place;
first_candidate.visit_leaves(|leaf_candidate| { first_candidate.visit_leaves(|leaf_candidate| {
self.test_or_pattern( self.test_or_pattern(
@ -1353,7 +1344,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
&mut otherwise, &mut otherwise,
pats, pats,
or_span, or_span,
place.clone(), &match_pair.place,
fake_borrows, fake_borrows,
); );
}); });
@ -1381,7 +1372,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
otherwise: &mut Option<BasicBlock>, otherwise: &mut Option<BasicBlock>,
pats: &'pat [Box<Pat<'tcx>>], pats: &'pat [Box<Pat<'tcx>>],
or_span: Span, or_span: Span,
place: PlaceBuilder<'tcx>, place: &PlaceBuilder<'tcx>,
fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>, fake_borrows: &mut Option<FxIndexSet<Place<'tcx>>>,
) { ) {
debug!("candidate={:#?}\npats={:#?}", candidate, pats); debug!("candidate={:#?}\npats={:#?}", candidate, pats);
@ -1599,10 +1590,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
} }
// Insert a Shallow borrow of any places that is switched on. // Insert a Shallow borrow of any places that is switched on.
if let Some(fb) = fake_borrows && let Ok(match_place_resolved) = if let Some(fb) = fake_borrows
match_place.clone().try_upvars_resolved(self) && let Some(resolved_place) = match_place.try_to_place(self)
{ {
let resolved_place = match_place_resolved.into_place(self);
fb.insert(resolved_place); fb.insert(resolved_place);
} }
@ -1621,7 +1611,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// encounter a candidate where the test is not relevant; at // encounter a candidate where the test is not relevant; at
// that point, we stop sorting. // that point, we stop sorting.
while let Some(candidate) = candidates.first_mut() { while let Some(candidate) = candidates.first_mut() {
let Some(idx) = self.sort_candidate(&match_place.clone(), &test, candidate) else { let Some(idx) = self.sort_candidate(&match_place, &test, candidate) else {
break; break;
}; };
let (candidate, rest) = candidates.split_first_mut().unwrap(); let (candidate, rest) = candidates.split_first_mut().unwrap();
@ -1690,7 +1680,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
target_blocks target_blocks
}; };
self.perform_test(span, scrutinee_span, block, match_place, &test, make_target_blocks); self.perform_test(span, scrutinee_span, block, &match_place, &test, make_target_blocks);
} }
/// Determine the fake borrows that are needed from a set of places that /// Determine the fake borrows that are needed from a set of places that
@ -1796,12 +1786,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
false, false,
&mut [&mut guard_candidate, &mut otherwise_candidate], &mut [&mut guard_candidate, &mut otherwise_candidate],
); );
let mut opt_expr_place: Option<(Option<&Place<'tcx>>, Span)> = None; let expr_place = expr_place_builder.try_to_place(self);
let expr_place: Place<'tcx>; let opt_expr_place = expr_place.as_ref().map(|place| (Some(place), expr_span));
if let Ok(expr_builder) = expr_place_builder.try_upvars_resolved(self) {
expr_place = expr_builder.into_place(self);
opt_expr_place = Some((Some(&expr_place), expr_span));
}
let otherwise_post_guard_block = otherwise_candidate.pre_binding_block.unwrap(); let otherwise_post_guard_block = otherwise_candidate.pre_binding_block.unwrap();
self.break_for_else(otherwise_post_guard_block, else_target, self.source_info(expr_span)); self.break_for_else(otherwise_post_guard_block, else_target, self.source_info(expr_span));

View file

@ -73,8 +73,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
{ {
existing_bindings.extend_from_slice(&new_bindings); existing_bindings.extend_from_slice(&new_bindings);
mem::swap(&mut candidate.bindings, &mut existing_bindings); mem::swap(&mut candidate.bindings, &mut existing_bindings);
candidate.subcandidates = candidate.subcandidates = self.create_or_subcandidates(candidate, &place, pats);
self.create_or_subcandidates(candidate, place.clone(), pats);
return true; return true;
} }
@ -127,7 +126,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
fn create_or_subcandidates<'pat>( fn create_or_subcandidates<'pat>(
&mut self, &mut self,
candidate: &Candidate<'pat, 'tcx>, candidate: &Candidate<'pat, 'tcx>,
place: PlaceBuilder<'tcx>, place: &PlaceBuilder<'tcx>,
pats: &'pat [Box<Pat<'tcx>>], pats: &'pat [Box<Pat<'tcx>>],
) -> Vec<Candidate<'pat, 'tcx>> { ) -> Vec<Candidate<'pat, 'tcx>> {
pats.iter() pats.iter()
@ -156,10 +155,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ascription: thir::Ascription { ref annotation, variance }, ascription: thir::Ascription { ref annotation, variance },
} => { } => {
// Apply the type ascription to the value at `match_pair.place`, which is the // Apply the type ascription to the value at `match_pair.place`, which is the
if let Ok(place_resolved) = match_pair.place.clone().try_upvars_resolved(self) { if let Some(source) = match_pair.place.try_to_place(self) {
candidate.ascriptions.push(Ascription { candidate.ascriptions.push(Ascription {
annotation: annotation.clone(), annotation: annotation.clone(),
source: place_resolved.into_place(self), source,
variance, variance,
}); });
} }
@ -183,10 +182,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ref subpattern, ref subpattern,
is_primary: _, is_primary: _,
} => { } => {
if let Ok(place_resolved) = match_pair.place.clone().try_upvars_resolved(self) { if let Some(source) = match_pair.place.try_to_place(self) {
candidate.bindings.push(Binding { candidate.bindings.push(Binding {
span: match_pair.pattern.span, span: match_pair.pattern.span,
source: place_resolved.into_place(self), source,
var_id: var, var_id: var,
binding_mode: mode, binding_mode: mode,
}); });

View file

@ -150,11 +150,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
match_start_span: Span, match_start_span: Span,
scrutinee_span: Span, scrutinee_span: Span,
block: BasicBlock, block: BasicBlock,
place_builder: PlaceBuilder<'tcx>, place_builder: &PlaceBuilder<'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,);
@ -760,7 +760,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let downcast_place = match_pair.place.downcast(adt_def, variant_index); // `(x as Variant)` let downcast_place = match_pair.place.downcast(adt_def, variant_index); // `(x as Variant)`
let consequent_match_pairs = subpatterns.iter().map(|subpattern| { let consequent_match_pairs = subpatterns.iter().map(|subpattern| {
// e.g., `(x as Variant).0` // e.g., `(x as Variant).0`
let place = downcast_place.clone().field(subpattern.field, subpattern.pattern.ty); let place = downcast_place
.clone_project(PlaceElem::Field(subpattern.field, subpattern.pattern.ty));
// e.g., `(x as Variant).0 @ P1` // e.g., `(x as Variant).0 @ P1`
MatchPair::new(place, &subpattern.pattern, self) MatchPair::new(place, &subpattern.pattern, self)
}); });

View file

@ -18,7 +18,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
subpatterns subpatterns
.iter() .iter()
.map(|fieldpat| { .map(|fieldpat| {
let place = place.clone().field(fieldpat.field, fieldpat.pattern.ty); let place =
place.clone_project(PlaceElem::Field(fieldpat.field, fieldpat.pattern.ty));
MatchPair::new(place, &fieldpat.pattern, self) MatchPair::new(place, &fieldpat.pattern, self)
}) })
.collect() .collect()
@ -33,9 +34,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
suffix: &'pat [Box<Pat<'tcx>>], suffix: &'pat [Box<Pat<'tcx>>],
) { ) {
let tcx = self.tcx; let tcx = self.tcx;
let (min_length, exact_size) = let (min_length, exact_size) = if let Some(place_resolved) = place.try_to_place(self) {
if let Ok(place_resolved) = place.clone().try_upvars_resolved(self) { match place_resolved.ty(&self.local_decls, tcx).ty.kind() {
match place_resolved.into_place(self).ty(&self.local_decls, tcx).ty.kind() {
ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true), ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false), _ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
} }
@ -46,13 +46,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| { match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
let elem = let elem =
ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false }; ProjectionElem::ConstantIndex { offset: idx as u64, min_length, from_end: false };
let place = place.clone().project(elem); MatchPair::new(place.clone_project(elem), subpattern, self)
MatchPair::new(place, subpattern, self)
})); }));
if let Some(subslice_pat) = opt_slice { if let Some(subslice_pat) = opt_slice {
let suffix_len = suffix.len() as u64; let suffix_len = suffix.len() as u64;
let subslice = place.clone().project(ProjectionElem::Subslice { let subslice = place.clone_project(PlaceElem::Subslice {
from: prefix.len() as u64, from: prefix.len() as u64,
to: if exact_size { min_length - suffix_len } else { suffix_len }, to: if exact_size { min_length - suffix_len } else { suffix_len },
from_end: !exact_size, from_end: !exact_size,
@ -67,7 +66,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
min_length, min_length,
from_end: !exact_size, from_end: !exact_size,
}; };
let place = place.clone().project(elem); let place = place.clone_project(elem);
MatchPair::new(place, subpattern, self) MatchPair::new(place, subpattern, self)
})); }));
} }
@ -97,15 +96,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
impl<'pat, 'tcx> MatchPair<'pat, 'tcx> { impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
pub(in crate::build) fn new( pub(in crate::build) fn new(
place: PlaceBuilder<'tcx>, mut place: PlaceBuilder<'tcx>,
pattern: &'pat Pat<'tcx>, pattern: &'pat Pat<'tcx>,
cx: &Builder<'_, 'tcx>, cx: &Builder<'_, 'tcx>,
) -> MatchPair<'pat, 'tcx> { ) -> MatchPair<'pat, 'tcx> {
// Force the place type to the pattern's type. // Force the place type to the pattern's type.
// FIXME(oli-obk): can we use this to simplify slice/array pattern hacks? // FIXME(oli-obk): can we use this to simplify slice/array pattern hacks?
let mut place = match place.try_upvars_resolved(cx) { if let Some(resolved) = place.resolve_upvar(cx) {
Ok(val) | Err(val) => val, place = resolved;
}; }
// Only add the OpaqueCast projection if the given place is an opaque type and the // Only add the OpaqueCast projection if the given place is an opaque type and the
// expected type from the pattern is not. // expected type from the pattern is not.