Skip to content

Commit eeb1033

Browse files
committed
Pass one argument instead of fetching two fields of it at every call site
1 parent a034446 commit eeb1033

File tree

7 files changed

+60
-100
lines changed

7 files changed

+60
-100
lines changed

compiler/rustc_mir_build/src/build/expr/as_place.rs

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) enum PlaceBase {
7171
/// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
7272
/// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
7373
#[derive(Clone, Debug, PartialEq)]
74-
pub(crate) struct PlaceBuilder<'tcx> {
74+
pub(in crate::build) struct PlaceBuilder<'tcx> {
7575
base: PlaceBase,
7676
projection: Vec<PlaceElem<'tcx>>,
7777
}
@@ -202,10 +202,9 @@ fn find_capture_matching_projections<'a, 'tcx>(
202202
/// `PlaceBuilder` now starts from `PlaceBase::Local`.
203203
///
204204
/// Returns a Result with the error being the PlaceBuilder (`from_builder`) that was not found.
205-
fn to_upvars_resolved_place_builder<'a, 'tcx>(
205+
fn to_upvars_resolved_place_builder<'tcx>(
206206
from_builder: PlaceBuilder<'tcx>,
207-
tcx: TyCtxt<'tcx>,
208-
typeck_results: &'a ty::TypeckResults<'tcx>,
207+
cx: &Builder<'_, 'tcx>,
209208
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
210209
match from_builder.base {
211210
PlaceBase::Local(_) => Ok(from_builder),
@@ -220,13 +219,13 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
220219

221220
let Some((capture_index, capture)) =
222221
find_capture_matching_projections(
223-
typeck_results,
222+
cx.typeck_results,
224223
var_hir_id,
225224
closure_def_id,
226225
&from_builder.projection,
227226
) else {
228-
let closure_span = tcx.def_span(closure_def_id);
229-
if !enable_precise_capture(tcx, closure_span) {
227+
let closure_span = cx.tcx.def_span(closure_def_id);
228+
if !enable_precise_capture(cx.tcx, closure_span) {
230229
bug!(
231230
"No associated capture found for {:?}[{:#?}] even though \
232231
capture_disjoint_fields isn't enabled",
@@ -243,8 +242,8 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
243242
};
244243

245244
// We won't be building MIR if the closure wasn't local
246-
let closure_hir_id = tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local());
247-
let closure_ty = typeck_results.node_type(closure_hir_id);
245+
let closure_hir_id = cx.tcx.hir().local_def_id_to_hir_id(closure_def_id.expect_local());
246+
let closure_ty = cx.typeck_results.node_type(closure_hir_id);
248247

249248
let substs = match closure_ty.kind() {
250249
ty::Closure(_, substs) => ty::UpvarSubsts::Closure(substs),
@@ -316,24 +315,16 @@ fn strip_prefix<'tcx>(
316315
}
317316

318317
impl<'tcx> PlaceBuilder<'tcx> {
319-
pub(crate) fn into_place<'a>(
320-
self,
321-
tcx: TyCtxt<'tcx>,
322-
typeck_results: &'a ty::TypeckResults<'tcx>,
323-
) -> Place<'tcx> {
318+
pub(crate) fn into_place(self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> {
324319
if let PlaceBase::Local(local) = self.base {
325-
Place { local, projection: tcx.intern_place_elems(&self.projection) }
320+
Place { local, projection: cx.tcx.intern_place_elems(&self.projection) }
326321
} else {
327-
self.expect_upvars_resolved(tcx, typeck_results).into_place(tcx, typeck_results)
322+
self.expect_upvars_resolved(cx).into_place(cx)
328323
}
329324
}
330325

331-
fn expect_upvars_resolved<'a>(
332-
self,
333-
tcx: TyCtxt<'tcx>,
334-
typeck_results: &'a ty::TypeckResults<'tcx>,
335-
) -> PlaceBuilder<'tcx> {
336-
to_upvars_resolved_place_builder(self, tcx, typeck_results).unwrap()
326+
fn expect_upvars_resolved(self, cx: &Builder<'_, 'tcx>) -> PlaceBuilder<'tcx> {
327+
to_upvars_resolved_place_builder(self, cx).unwrap()
337328
}
338329

339330
/// Attempts to resolve the `PlaceBuilder`.
@@ -347,12 +338,11 @@ impl<'tcx> PlaceBuilder<'tcx> {
347338
/// not captured. This can happen because the final mir that will be
348339
/// generated doesn't require a read for this place. Failures will only
349340
/// happen inside closures.
350-
pub(crate) fn try_upvars_resolved<'a>(
341+
pub(crate) fn try_upvars_resolved(
351342
self,
352-
tcx: TyCtxt<'tcx>,
353-
typeck_results: &'a ty::TypeckResults<'tcx>,
343+
cx: &Builder<'_, 'tcx>,
354344
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
355-
to_upvars_resolved_place_builder(self, tcx, typeck_results)
345+
to_upvars_resolved_place_builder(self, cx)
356346
}
357347

358348
pub(crate) fn base(&self) -> PlaceBase {
@@ -412,7 +402,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
412402
expr: &Expr<'tcx>,
413403
) -> BlockAnd<Place<'tcx>> {
414404
let place_builder = unpack!(block = self.as_place_builder(block, expr));
415-
block.and(place_builder.into_place(self.tcx, self.typeck_results))
405+
block.and(place_builder.into_place(self))
416406
}
417407

418408
/// This is used when constructing a compound `Place`, so that we can avoid creating
@@ -436,7 +426,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
436426
expr: &Expr<'tcx>,
437427
) -> BlockAnd<Place<'tcx>> {
438428
let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
439-
block.and(place_builder.into_place(self.tcx, self.typeck_results))
429+
block.and(place_builder.into_place(self))
440430
}
441431

442432
/// This is used when constructing a compound `Place`, so that we can avoid creating
@@ -531,7 +521,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
531521
inferred_ty: expr.ty,
532522
});
533523

534-
let place = place_builder.clone().into_place(this.tcx, this.typeck_results);
524+
let place = place_builder.clone().into_place(this);
535525
this.cfg.push(
536526
block,
537527
Statement {
@@ -683,7 +673,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
683673
if is_outermost_index {
684674
self.read_fake_borrows(block, fake_borrow_temps, source_info)
685675
} else {
686-
base_place = base_place.expect_upvars_resolved(self.tcx, self.typeck_results);
676+
base_place = base_place.expect_upvars_resolved(self);
687677
self.add_fake_borrows_of_base(
688678
&base_place,
689679
block,
@@ -711,12 +701,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
711701
let lt = self.temp(bool_ty, expr_span);
712702

713703
// len = len(slice)
714-
self.cfg.push_assign(
715-
block,
716-
source_info,
717-
len,
718-
Rvalue::Len(slice.into_place(self.tcx, self.typeck_results)),
719-
);
704+
self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.into_place(self)));
720705
// lt = idx < len
721706
self.cfg.push_assign(
722707
block,

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
321321
let place_builder =
322322
unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));
323323

324-
if let Ok(place_builder_resolved) =
325-
place_builder.try_upvars_resolved(this.tcx, this.typeck_results)
326-
{
327-
let mir_place =
328-
place_builder_resolved.into_place(this.tcx, this.typeck_results);
324+
if let Ok(place_builder_resolved) = place_builder.try_upvars_resolved(this) {
325+
let mir_place = place_builder_resolved.into_place(this);
329326
this.cfg.push_fake_read(
330327
block,
331328
this.source_info(this.tcx.hir().span(*hir_id)),
@@ -616,8 +613,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
616613
// by the parent itself. The mutability of the current capture
617614
// is same as that of the capture in the parent closure.
618615
PlaceBase::Upvar { .. } => {
619-
let enclosing_upvars_resolved =
620-
arg_place_builder.clone().into_place(this.tcx, this.typeck_results);
616+
let enclosing_upvars_resolved = arg_place_builder.clone().into_place(this);
621617

622618
match enclosing_upvars_resolved.as_ref() {
623619
PlaceRef {
@@ -654,7 +650,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
654650
Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
655651
};
656652

657-
let arg_place = arg_place_builder.into_place(this.tcx, this.typeck_results);
653+
let arg_place = arg_place_builder.into_place(this);
658654

659655
this.cfg.push_assign(
660656
block,

compiler/rustc_mir_build/src/build/expr/into.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
365365
None => {
366366
let place_builder = place_builder.clone();
367367
this.consume_by_copy_or_move(
368-
place_builder
369-
.field(n, *ty)
370-
.into_place(this.tcx, this.typeck_results),
368+
place_builder.field(n, *ty).into_place(this),
371369
)
372370
}
373371
})

compiler/rustc_mir_build/src/build/matches/mod.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
220220
let cause_matched_place = FakeReadCause::ForMatchedPlace(None);
221221
let source_info = self.source_info(scrutinee_span);
222222

223-
if let Ok(scrutinee_builder) =
224-
scrutinee_place_builder.clone().try_upvars_resolved(self.tcx, self.typeck_results)
225-
{
226-
let scrutinee_place = scrutinee_builder.into_place(self.tcx, self.typeck_results);
223+
if let Ok(scrutinee_builder) = scrutinee_place_builder.clone().try_upvars_resolved(self) {
224+
let scrutinee_place = scrutinee_builder.into_place(self);
227225
self.cfg.push_fake_read(block, source_info, cause_matched_place, scrutinee_place);
228226
}
229227

@@ -348,12 +346,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
348346
// ```
349347
let mut opt_scrutinee_place: Option<(Option<&Place<'tcx>>, Span)> = None;
350348
let scrutinee_place: Place<'tcx>;
351-
if let Ok(scrutinee_builder) = scrutinee_place_builder
352-
.clone()
353-
.try_upvars_resolved(this.tcx, this.typeck_results)
349+
if let Ok(scrutinee_builder) =
350+
scrutinee_place_builder.clone().try_upvars_resolved(this)
354351
{
355-
scrutinee_place =
356-
scrutinee_builder.into_place(this.tcx, this.typeck_results);
352+
scrutinee_place = scrutinee_builder.into_place(this);
357353
opt_scrutinee_place = Some((Some(&scrutinee_place), scrutinee_span));
358354
}
359355
let scope = this.declare_bindings(
@@ -602,12 +598,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
602598
while let Some(next) = {
603599
for binding in &candidate_ref.bindings {
604600
let local = self.var_local_id(binding.var_id, OutsideGuard);
605-
606-
let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
607-
VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. },
608-
)))) = self.local_decls[local].local_info else {
609-
bug!("Let binding to non-user variable.")
610-
};
611601
// `try_upvars_resolved` may fail if it is unable to resolve the given
612602
// `PlaceBuilder` inside a closure. In this case, we don't want to include
613603
// a scrutinee place. `scrutinee_place_builder` will fail for destructured
@@ -622,10 +612,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
622612
// let (v1, v2) = foo;
623613
// };
624614
// ```
625-
if let Ok(match_pair_resolved) =
626-
initializer.clone().try_upvars_resolved(self.tcx, self.typeck_results)
627-
{
628-
let place = match_pair_resolved.into_place(self.tcx, self.typeck_results);
615+
if let Ok(match_pair_resolved) = initializer.clone().try_upvars_resolved(self) {
616+
let place = match_pair_resolved.into_place(self);
617+
618+
let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
619+
VarBindingForm { opt_match_place: Some((ref mut match_place, _)), .. },
620+
)))) = self.local_decls[local].local_info else {
621+
bug!("Let binding to non-user variable.")
622+
};
623+
629624
*match_place = Some(place);
630625
}
631626
}
@@ -1605,9 +1600,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
16051600

16061601
// Insert a Shallow borrow of any places that is switched on.
16071602
if let Some(fb) = fake_borrows && let Ok(match_place_resolved) =
1608-
match_place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
1603+
match_place.clone().try_upvars_resolved(self)
16091604
{
1610-
let resolved_place = match_place_resolved.into_place(self.tcx, self.typeck_results);
1605+
let resolved_place = match_place_resolved.into_place(self);
16111606
fb.insert(resolved_place);
16121607
}
16131608

@@ -1799,10 +1794,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
17991794
);
18001795
let mut opt_expr_place: Option<(Option<&Place<'tcx>>, Span)> = None;
18011796
let expr_place: Place<'tcx>;
1802-
if let Ok(expr_builder) =
1803-
expr_place_builder.try_upvars_resolved(self.tcx, self.typeck_results)
1804-
{
1805-
expr_place = expr_builder.into_place(self.tcx, self.typeck_results);
1797+
if let Ok(expr_builder) = expr_place_builder.try_upvars_resolved(self) {
1798+
expr_place = expr_builder.into_place(self);
18061799
opt_expr_place = Some((Some(&expr_place), expr_span));
18071800
}
18081801
let otherwise_post_guard_block = otherwise_candidate.pre_binding_block.unwrap();

compiler/rustc_mir_build/src/build/matches/simplify.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
156156
ascription: thir::Ascription { ref annotation, variance },
157157
} => {
158158
// Apply the type ascription to the value at `match_pair.place`, which is the
159-
if let Ok(place_resolved) =
160-
match_pair.place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
161-
{
159+
if let Ok(place_resolved) = match_pair.place.clone().try_upvars_resolved(self) {
162160
candidate.ascriptions.push(Ascription {
163161
annotation: annotation.clone(),
164-
source: place_resolved.into_place(self.tcx, self.typeck_results),
162+
source: place_resolved.into_place(self),
165163
variance,
166164
});
167165
}
@@ -185,12 +183,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
185183
ref subpattern,
186184
is_primary: _,
187185
} => {
188-
if let Ok(place_resolved) =
189-
match_pair.place.clone().try_upvars_resolved(self.tcx, self.typeck_results)
190-
{
186+
if let Ok(place_resolved) = match_pair.place.clone().try_upvars_resolved(self) {
191187
candidate.bindings.push(Binding {
192188
span: match_pair.pattern.span,
193-
source: place_resolved.into_place(self.tcx, self.typeck_results),
189+
source: place_resolved.into_place(self),
194190
var_id: var,
195191
binding_mode: mode,
196192
});

compiler/rustc_mir_build/src/build/matches/test.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
155155
make_target_blocks: impl FnOnce(&mut Self) -> Vec<BasicBlock>,
156156
) {
157157
let place: Place<'tcx>;
158-
if let Ok(test_place_builder) =
159-
place_builder.try_upvars_resolved(self.tcx, self.typeck_results)
160-
{
161-
place = test_place_builder.into_place(self.tcx, self.typeck_results);
158+
if let Ok(test_place_builder) = place_builder.try_upvars_resolved(self) {
159+
place = test_place_builder.into_place(self);
162160
} else {
163161
return;
164162
}

compiler/rustc_mir_build/src/build/matches/util.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3131
suffix: &'pat [Pat<'tcx>],
3232
) {
3333
let tcx = self.tcx;
34-
let (min_length, exact_size) = if let Ok(place_resolved) =
35-
place.clone().try_upvars_resolved(tcx, self.typeck_results)
36-
{
37-
match place_resolved
38-
.into_place(tcx, self.typeck_results)
39-
.ty(&self.local_decls, tcx)
40-
.ty
41-
.kind()
42-
{
43-
ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
44-
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
45-
}
46-
} else {
47-
((prefix.len() + suffix.len()).try_into().unwrap(), false)
48-
};
34+
let (min_length, exact_size) =
35+
if let Ok(place_resolved) = place.clone().try_upvars_resolved(self) {
36+
match place_resolved.into_place(self).ty(&self.local_decls, tcx).ty.kind() {
37+
ty::Array(_, length) => (length.eval_usize(tcx, self.param_env), true),
38+
_ => ((prefix.len() + suffix.len()).try_into().unwrap(), false),
39+
}
40+
} else {
41+
((prefix.len() + suffix.len()).try_into().unwrap(), false)
42+
};
4943

5044
match_pairs.extend(prefix.iter().enumerate().map(|(idx, subpattern)| {
5145
let elem =
@@ -100,7 +94,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
10094
}
10195

10296
impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
103-
pub(crate) fn new(
97+
pub(in crate::build) fn new(
10498
place: PlaceBuilder<'tcx>,
10599
pattern: &'pat Pat<'tcx>,
106100
) -> MatchPair<'pat, 'tcx> {

0 commit comments

Comments
 (0)