Avoid some clones.

`Builder::expr_into_pattern` has a single call site. Currently the
`pattern` argument at the call site is always cloned.

This commit changes things so that we instead do a clone within
`expr_into_pattern`, but only if the pattern has the
`PatKind::AscribeUserType` kind, and we only clone the annotation within
the pattern instead of the entire pattern.
This commit is contained in:
Nicholas Nethercote 2022-08-25 14:05:01 +10:00
parent 053874eecc
commit bd1e6836a0
3 changed files with 9 additions and 6 deletions

View File

@ -160,7 +160,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
ArmHasGuard(false),
Some((None, initializer_span)),
);
this.expr_into_pattern(block, (**pattern).clone(), init) // irrefutable pattern
this.expr_into_pattern(block, pattern, init) // irrefutable pattern
}
})
},

View File

@ -490,7 +490,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
pub(super) fn expr_into_pattern(
&mut self,
mut block: BasicBlock,
irrefutable_pat: Pat<'tcx>,
irrefutable_pat: &Pat<'tcx>,
initializer: &Expr<'tcx>,
) -> BlockAnd<()> {
match irrefutable_pat.kind {
@ -525,7 +525,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
},
..
},
ascription: thir::Ascription { annotation, variance: _ },
ascription: thir::Ascription { ref annotation, variance: _ },
} => {
let place =
self.storage_live_binding(block, var, irrefutable_pat.span, OutsideGuard, true);
@ -538,7 +538,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let ty_source_info = self.source_info(annotation.span);
let base = self.canonical_user_type_annotations.push(annotation);
let base = self.canonical_user_type_annotations.push(annotation.clone());
self.cfg.push(
block,
Statement {
@ -578,7 +578,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
pub(crate) fn place_into_pattern(
&mut self,
block: BasicBlock,
irrefutable_pat: Pat<'tcx>,
irrefutable_pat: &Pat<'tcx>,
initializer: PlaceBuilder<'tcx>,
set_match_place: bool,
) -> BlockAnd<()> {

View File

@ -1052,7 +1052,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Some((Some(&place), span)),
);
let place_builder = PlaceBuilder::from(local);
unpack!(block = self.place_into_pattern(block, *pattern, place_builder, false));
unpack!(
block =
self.place_into_pattern(block, pattern.as_ref(), place_builder, false)
);
}
}
self.source_scope = original_source_scope;