Skip to content

Commit 5d664f7

Browse files
committed
Merge two arguments into one
1 parent 22f6aec commit 5d664f7

File tree

11 files changed

+92
-127
lines changed

11 files changed

+92
-127
lines changed

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

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) enum PlaceBase {
6969
/// This is used internally when building a place for an expression like `a.b.c`. The fields `b`
7070
/// and `c` can be progressively pushed onto the place builder that is created when converting `a`.
7171
#[derive(Clone, Debug, PartialEq)]
72-
pub(crate) struct PlaceBuilder<'tcx> {
72+
pub(in crate::build) struct PlaceBuilder<'tcx> {
7373
base: PlaceBase,
7474
projection: Vec<PlaceElem<'tcx>>,
7575
}
@@ -168,22 +168,22 @@ fn find_capture_matching_projections<'a, 'tcx>(
168168
/// `PlaceBuilder` now starts from `PlaceBase::Local`.
169169
///
170170
/// Returns a Result with the error being the PlaceBuilder (`from_builder`) that was not found.
171-
fn to_upvars_resolved_place_builder<'a, 'tcx>(
171+
#[instrument(level = "trace", skip(cx))]
172+
fn to_upvars_resolved_place_builder<'tcx>(
172173
from_builder: PlaceBuilder<'tcx>,
173-
tcx: TyCtxt<'tcx>,
174-
upvars: &'a CaptureMap<'tcx>,
174+
cx: &Builder<'_, 'tcx>,
175175
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
176176
match from_builder.base {
177177
PlaceBase::Local(_) => Ok(from_builder),
178178
PlaceBase::Upvar { var_hir_id, closure_def_id } => {
179179
let Some((capture_index, capture)) =
180180
find_capture_matching_projections(
181-
upvars,
181+
&cx.upvars,
182182
var_hir_id,
183183
&from_builder.projection,
184184
) else {
185-
let closure_span = tcx.def_span(closure_def_id);
186-
if !enable_precise_capture(tcx, closure_span) {
185+
let closure_span = cx.tcx.def_span(closure_def_id);
186+
if !enable_precise_capture(cx.tcx, closure_span) {
187187
bug!(
188188
"No associated capture found for {:?}[{:#?}] even though \
189189
capture_disjoint_fields isn't enabled",
@@ -200,18 +200,20 @@ fn to_upvars_resolved_place_builder<'a, 'tcx>(
200200
};
201201

202202
// Access the capture by accessing the field within the Closure struct.
203-
let capture_info = &upvars[capture_index];
203+
let capture_info = &cx.upvars[capture_index];
204204

205205
let mut upvar_resolved_place_builder = PlaceBuilder::from(capture_info.use_place);
206206

207207
// We used some of the projections to build the capture itself,
208208
// now we apply the remaining to the upvar resolved place.
209+
trace!(?capture.captured_place, ?from_builder.projection);
209210
let remaining_projections = strip_prefix(
210211
capture.captured_place.place.base_ty,
211212
from_builder.projection,
212213
&capture.captured_place.place.projections,
213214
);
214215
upvar_resolved_place_builder.projection.extend(remaining_projections);
216+
trace!(?upvar_resolved_place_builder);
215217

216218
Ok(upvar_resolved_place_builder)
217219
}
@@ -251,24 +253,16 @@ fn strip_prefix<'tcx>(
251253
}
252254

253255
impl<'tcx> PlaceBuilder<'tcx> {
254-
pub(in crate::build) fn into_place<'a>(
255-
self,
256-
tcx: TyCtxt<'tcx>,
257-
upvars: &'a CaptureMap<'tcx>,
258-
) -> Place<'tcx> {
256+
pub(in crate::build) fn into_place(self, cx: &Builder<'_, 'tcx>) -> Place<'tcx> {
259257
if let PlaceBase::Local(local) = self.base {
260-
Place { local, projection: tcx.intern_place_elems(&self.projection) }
258+
Place { local, projection: cx.tcx.intern_place_elems(&self.projection) }
261259
} else {
262-
self.expect_upvars_resolved(tcx, upvars).into_place(tcx, upvars)
260+
self.expect_upvars_resolved(cx).into_place(cx)
263261
}
264262
}
265263

266-
fn expect_upvars_resolved<'a>(
267-
self,
268-
tcx: TyCtxt<'tcx>,
269-
upvars: &'a CaptureMap<'tcx>,
270-
) -> PlaceBuilder<'tcx> {
271-
to_upvars_resolved_place_builder(self, tcx, upvars).unwrap()
264+
fn expect_upvars_resolved(self, cx: &Builder<'_, 'tcx>) -> PlaceBuilder<'tcx> {
265+
to_upvars_resolved_place_builder(self, cx).unwrap()
272266
}
273267

274268
/// Attempts to resolve the `PlaceBuilder`.
@@ -282,12 +276,11 @@ impl<'tcx> PlaceBuilder<'tcx> {
282276
/// not captured. This can happen because the final mir that will be
283277
/// generated doesn't require a read for this place. Failures will only
284278
/// happen inside closures.
285-
pub(in crate::build) fn try_upvars_resolved<'a>(
279+
pub(in crate::build) fn try_upvars_resolved(
286280
self,
287-
tcx: TyCtxt<'tcx>,
288-
upvars: &'a CaptureMap<'tcx>,
281+
cx: &Builder<'_, 'tcx>,
289282
) -> Result<PlaceBuilder<'tcx>, PlaceBuilder<'tcx>> {
290-
to_upvars_resolved_place_builder(self, tcx, upvars)
283+
to_upvars_resolved_place_builder(self, cx)
291284
}
292285

293286
pub(crate) fn base(&self) -> PlaceBase {
@@ -353,7 +346,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
353346
expr: &Expr<'tcx>,
354347
) -> BlockAnd<Place<'tcx>> {
355348
let place_builder = unpack!(block = self.as_place_builder(block, expr));
356-
block.and(place_builder.into_place(self.tcx, &self.upvars))
349+
block.and(place_builder.into_place(self))
357350
}
358351

359352
/// This is used when constructing a compound `Place`, so that we can avoid creating
@@ -377,7 +370,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
377370
expr: &Expr<'tcx>,
378371
) -> BlockAnd<Place<'tcx>> {
379372
let place_builder = unpack!(block = self.as_read_only_place_builder(block, expr));
380-
block.and(place_builder.into_place(self.tcx, &self.upvars))
373+
block.and(place_builder.into_place(self))
381374
}
382375

383376
/// This is used when constructing a compound `Place`, so that we can avoid creating
@@ -472,7 +465,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
472465
inferred_ty: expr.ty,
473466
});
474467

475-
let place = place_builder.clone().into_place(this.tcx, &this.upvars);
468+
let place = place_builder.clone().into_place(this);
476469
this.cfg.push(
477470
block,
478471
Statement {
@@ -610,7 +603,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
610603
if is_outermost_index {
611604
self.read_fake_borrows(block, fake_borrow_temps, source_info)
612605
} else {
613-
base_place = base_place.expect_upvars_resolved(self.tcx, &self.upvars);
606+
base_place = base_place.expect_upvars_resolved(self);
614607
self.add_fake_borrows_of_base(
615608
&base_place,
616609
block,
@@ -638,12 +631,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
638631
let lt = self.temp(bool_ty, expr_span);
639632

640633
// len = len(slice)
641-
self.cfg.push_assign(
642-
block,
643-
source_info,
644-
len,
645-
Rvalue::Len(slice.into_place(self.tcx, &self.upvars)),
646-
);
634+
self.cfg.push_assign(block, source_info, len, Rvalue::Len(slice.into_place(self)));
647635
// lt = idx < len
648636
self.cfg.push_assign(
649637
block,

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
329329
let place_builder =
330330
unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));
331331

332-
if let Ok(place_builder_resolved) =
333-
place_builder.try_upvars_resolved(this.tcx, &this.upvars)
334-
{
335-
let mir_place = place_builder_resolved.into_place(this.tcx, &this.upvars);
332+
if let Ok(place_builder_resolved) = place_builder.try_upvars_resolved(this) {
333+
let mir_place = place_builder_resolved.into_place(this);
336334
this.cfg.push_fake_read(
337335
block,
338336
this.source_info(this.tcx.hir().span(*hir_id)),
@@ -623,8 +621,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
623621
// by the parent itself. The mutability of the current capture
624622
// is same as that of the capture in the parent closure.
625623
PlaceBase::Upvar { .. } => {
626-
let enclosing_upvars_resolved =
627-
arg_place_builder.clone().into_place(this.tcx, &this.upvars);
624+
let enclosing_upvars_resolved = arg_place_builder.clone().into_place(this);
628625

629626
match enclosing_upvars_resolved.as_ref() {
630627
PlaceRef {
@@ -661,7 +658,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
661658
Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
662659
};
663660

664-
let arg_place = arg_place_builder.into_place(this.tcx, &this.upvars);
661+
let arg_place = arg_place_builder.into_place(this);
665662

666663
this.cfg.push_assign(
667664
block,

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2323
ensure_sufficient_stack(|| self.as_temp_inner(block, temp_lifetime, expr, mutability))
2424
}
2525

26+
#[instrument(skip(self), level = "debug")]
2627
fn as_temp_inner(
2728
&mut self,
2829
mut block: BasicBlock,
2930
temp_lifetime: Option<region::Scope>,
3031
expr: &Expr<'tcx>,
3132
mutability: Mutability,
3233
) -> BlockAnd<Local> {
33-
debug!(
34-
"as_temp(block={:?}, temp_lifetime={:?}, expr={:?}, mutability={:?})",
35-
block, temp_lifetime, expr, mutability
36-
);
3734
let this = self;
3835

3936
let expr_span = expr.span;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ use std::iter;
1515
impl<'a, 'tcx> Builder<'a, 'tcx> {
1616
/// Compile `expr`, storing the result into `destination`, which
1717
/// is assumed to be uninitialized.
18+
#[instrument(level = "debug", skip(self))]
1819
pub(crate) fn expr_into_dest(
1920
&mut self,
2021
destination: Place<'tcx>,
2122
mut block: BasicBlock,
2223
expr: &Expr<'tcx>,
2324
) -> BlockAnd<()> {
24-
debug!("expr_into_dest(destination={:?}, block={:?}, expr={:?})", destination, block, expr);
25-
2625
// since we frequently have to reference `self` from within a
2726
// closure, where `self` would be shadowed, it's easier to
2827
// just use the name `this` uniformly
@@ -366,7 +365,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
366365
None => {
367366
let place_builder = place_builder.clone();
368367
this.consume_by_copy_or_move(
369-
place_builder.field(n, *ty).into_place(this.tcx, &this.upvars),
368+
place_builder.field(n, *ty).into_place(this),
370369
)
371370
}
372371
})

0 commit comments

Comments
 (0)