Skip to content

Commit dc62f07

Browse files
committed
Remove Gatherer.
It's a very thin wrapper that pairs `MoveDataBuilder` with a `Location`, and it has four lifetime arguments. This commit removes it by just adding a `Location` to `MoveDataBuilder`.
1 parent 28a6dc4 commit dc62f07

File tree

1 file changed

+29
-46
lines changed
  • compiler/rustc_mir_dataflow/src/move_paths

1 file changed

+29
-46
lines changed

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+29-46
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use super::{
1616

1717
struct MoveDataBuilder<'a, 'tcx, F> {
1818
body: &'a Body<'tcx>,
19+
loc: Location,
1920
tcx: TyCtxt<'tcx>,
2021
param_env: ty::ParamEnv<'tcx>,
2122
data: MoveData<'tcx>,
@@ -56,6 +57,7 @@ impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
5657

5758
MoveDataBuilder {
5859
body,
60+
loc: Location::START,
5961
tcx,
6062
param_env,
6163
data: MoveData {
@@ -107,7 +109,7 @@ enum MovePathResult {
107109
Error,
108110
}
109111

110-
impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
112+
impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
111113
/// This creates a MovePath for a given place, returning an `MovePathError`
112114
/// if that place can't be moved from.
113115
///
@@ -116,7 +118,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
116118
///
117119
/// Maybe we should have separate "borrowck" and "moveck" modes.
118120
fn move_path_for(&mut self, place: Place<'tcx>) -> MovePathResult {
119-
let data = &mut self.builder.data;
121+
let data = &mut self.data;
120122

121123
debug!("lookup({:?})", place);
122124
let Some(mut base) = data.rev_lookup.find_local(place.local) else {
@@ -131,8 +133,8 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
131133
let mut union_path = None;
132134

133135
for (place_ref, elem) in data.rev_lookup.un_derefer.iter_projections(place.as_ref()) {
134-
let body = self.builder.body;
135-
let tcx = self.builder.tcx;
136+
let body = self.body;
137+
let tcx = self.tcx;
136138
let place_ty = place_ref.ty(body, tcx).ty;
137139
if place_ty.references_error() {
138140
return MovePathResult::Error;
@@ -238,7 +240,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
238240
| ProjectionElem::Downcast(_, _) => (),
239241
}
240242
let elem_ty = PlaceTy::from_ty(place_ty).projection_ty(tcx, elem).ty;
241-
if !(self.builder.filter)(elem_ty) {
243+
if !(self.filter)(elem_ty) {
242244
return MovePathResult::Error;
243245
}
244246
if union_path.is_none() {
@@ -274,7 +276,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
274276
data: MoveData { rev_lookup, move_paths, path_map, init_path_map, .. },
275277
tcx,
276278
..
277-
} = self.builder;
279+
} = self;
278280
*rev_lookup.projections.entry((base, elem.lift())).or_insert_with(move || {
279281
new_move_path(move_paths, path_map, init_path_map, Some(base), mk_place(*tcx))
280282
})
@@ -285,9 +287,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
285287
// drop), so this not being a valid move path is OK.
286288
let _ = self.move_path_for(place);
287289
}
288-
}
289290

290-
impl<'a, 'tcx, F> MoveDataBuilder<'a, 'tcx, F> {
291291
fn finalize(self) -> MoveData<'tcx> {
292292
debug!("{}", {
293293
debug!("moves for {:?}:", self.body.span);
@@ -317,12 +317,12 @@ pub(super) fn gather_moves<'tcx>(
317317

318318
for (bb, block) in body.basic_blocks.iter_enumerated() {
319319
for (i, stmt) in block.statements.iter().enumerate() {
320-
let source = Location { block: bb, statement_index: i };
321-
builder.gather_statement(source, stmt);
320+
builder.loc = Location { block: bb, statement_index: i };
321+
builder.gather_statement(stmt);
322322
}
323323

324-
let terminator_loc = Location { block: bb, statement_index: block.statements.len() };
325-
builder.gather_terminator(terminator_loc, block.terminator());
324+
builder.loc = Location { block: bb, statement_index: block.statements.len() };
325+
builder.gather_terminator(block.terminator());
326326
}
327327

328328
builder.finalize()
@@ -345,30 +345,14 @@ impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
345345
}
346346
}
347347

348-
fn gather_statement(&mut self, loc: Location, stmt: &Statement<'tcx>) {
349-
debug!("gather_statement({:?}, {:?})", loc, stmt);
350-
(Gatherer { builder: self, loc }).gather_statement(stmt);
351-
}
352-
353-
fn gather_terminator(&mut self, loc: Location, term: &Terminator<'tcx>) {
354-
debug!("gather_terminator({:?}, {:?})", loc, term);
355-
(Gatherer { builder: self, loc }).gather_terminator(term);
356-
}
357-
}
358-
359-
struct Gatherer<'b, 'a, 'tcx, F> {
360-
builder: &'b mut MoveDataBuilder<'a, 'tcx, F>,
361-
loc: Location,
362-
}
363-
364-
impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
365348
fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
349+
debug!("gather_statement({:?}, {:?})", self.loc, stmt);
366350
match &stmt.kind {
367351
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
368352
let local = place.as_local().unwrap();
369-
assert!(self.builder.body.local_decls[local].is_deref_temp());
353+
assert!(self.body.local_decls[local].is_deref_temp());
370354

371-
let rev_lookup = &mut self.builder.data.rev_lookup;
355+
let rev_lookup = &mut self.data.rev_lookup;
372356

373357
rev_lookup.un_derefer.insert(local, reffed.as_ref());
374358
let base_local = rev_lookup.un_derefer.deref_chain(local).first().unwrap().local;
@@ -380,7 +364,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
380364
// Box starts out uninitialized - need to create a separate
381365
// move-path for the interior so it will be separate from
382366
// the exterior.
383-
self.create_move_path(self.builder.tcx.mk_place_deref(*place));
367+
self.create_move_path(self.tcx.mk_place_deref(*place));
384368
self.gather_init(place.as_ref(), InitKind::Shallow);
385369
} else {
386370
self.gather_init(place.as_ref(), InitKind::Deep);
@@ -393,7 +377,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
393377
StatementKind::StorageLive(_) => {}
394378
StatementKind::StorageDead(local) => {
395379
// DerefTemp locals (results of CopyForDeref) don't actually move anything.
396-
if !self.builder.body.local_decls[*local].is_deref_temp() {
380+
if !self.body.local_decls[*local].is_deref_temp() {
397381
self.gather_move(Place::from(*local));
398382
}
399383
}
@@ -443,6 +427,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
443427
}
444428

445429
fn gather_terminator(&mut self, term: &Terminator<'tcx>) {
430+
debug!("gather_terminator({:?}, {:?})", self.loc, term);
446431
match term.kind {
447432
TerminatorKind::Goto { target: _ }
448433
| TerminatorKind::FalseEdge { .. }
@@ -551,7 +536,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
551536
// `ConstIndex` patterns. This is done to ensure that all move paths
552537
// are disjoint, which is expected by drop elaboration.
553538
let base_place =
554-
Place { local: place.local, projection: self.builder.tcx.mk_place_elems(base) };
539+
Place { local: place.local, projection: self.tcx.mk_place_elems(base) };
555540
let base_path = match self.move_path_for(base_place) {
556541
MovePathResult::Path(path) => path,
557542
MovePathResult::Union(path) => {
@@ -562,11 +547,9 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
562547
return;
563548
}
564549
};
565-
let base_ty = base_place.ty(self.builder.body, self.builder.tcx).ty;
550+
let base_ty = base_place.ty(self.body, self.tcx).ty;
566551
let len: u64 = match base_ty.kind() {
567-
ty::Array(_, size) => {
568-
size.eval_target_usize(self.builder.tcx, self.builder.param_env)
569-
}
552+
ty::Array(_, size) => size.eval_target_usize(self.tcx, self.param_env),
570553
_ => bug!("from_end: false slice pattern of non-array type"),
571554
};
572555
for offset in from..to {
@@ -587,13 +570,13 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
587570
}
588571

589572
fn record_move(&mut self, place: Place<'tcx>, path: MovePathIndex) {
590-
let move_out = self.builder.data.moves.push(MoveOut { path, source: self.loc });
573+
let move_out = self.data.moves.push(MoveOut { path, source: self.loc });
591574
debug!(
592575
"gather_move({:?}, {:?}): adding move {:?} of {:?}",
593576
self.loc, place, move_out, path
594577
);
595-
self.builder.data.path_map[path].push(move_out);
596-
self.builder.data.loc_map[self.loc].push(move_out);
578+
self.data.path_map[path].push(move_out);
579+
self.data.loc_map[self.loc].push(move_out);
597580
}
598581

599582
fn gather_init(&mut self, place: PlaceRef<'tcx>, kind: InitKind) {
@@ -604,13 +587,13 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
604587
// Check if we are assigning into a field of a union, if so, lookup the place
605588
// of the union so it is marked as initialized again.
606589
if let Some((place_base, ProjectionElem::Field(_, _))) = place.last_projection() {
607-
if place_base.ty(self.builder.body, self.builder.tcx).ty.is_union() {
590+
if place_base.ty(self.body, self.tcx).ty.is_union() {
608591
place = place_base;
609592
}
610593
}
611594

612-
if let LookupResult::Exact(path) = self.builder.data.rev_lookup.find(place) {
613-
let init = self.builder.data.inits.push(Init {
595+
if let LookupResult::Exact(path) = self.data.rev_lookup.find(place) {
596+
let init = self.data.inits.push(Init {
614597
location: InitLocation::Statement(self.loc),
615598
path,
616599
kind,
@@ -621,8 +604,8 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
621604
self.loc, place, init, path
622605
);
623606

624-
self.builder.data.init_path_map[path].push(init);
625-
self.builder.data.init_loc_map[self.loc].push(init);
607+
self.data.init_path_map[path].push(init);
608+
self.data.init_loc_map[self.loc].push(init);
626609
}
627610
}
628611
}

0 commit comments

Comments
 (0)