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`.
This commit is contained in:
Nicholas Nethercote 2024-09-06 16:35:17 +10:00
parent 28a6dc4d1e
commit dc62f07458

View File

@ -16,6 +16,7 @@
struct MoveDataBuilder<'a, 'tcx, F> { struct MoveDataBuilder<'a, 'tcx, F> {
body: &'a Body<'tcx>, body: &'a Body<'tcx>,
loc: Location,
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
data: MoveData<'tcx>, data: MoveData<'tcx>,
@ -56,6 +57,7 @@ fn new(
MoveDataBuilder { MoveDataBuilder {
body, body,
loc: Location::START,
tcx, tcx,
param_env, param_env,
data: MoveData { data: MoveData {
@ -107,7 +109,7 @@ enum MovePathResult {
Error, Error,
} }
impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> { impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
/// This creates a MovePath for a given place, returning an `MovePathError` /// This creates a MovePath for a given place, returning an `MovePathError`
/// if that place can't be moved from. /// if that place can't be moved from.
/// ///
@ -116,7 +118,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
/// ///
/// Maybe we should have separate "borrowck" and "moveck" modes. /// Maybe we should have separate "borrowck" and "moveck" modes.
fn move_path_for(&mut self, place: Place<'tcx>) -> MovePathResult { fn move_path_for(&mut self, place: Place<'tcx>) -> MovePathResult {
let data = &mut self.builder.data; let data = &mut self.data;
debug!("lookup({:?})", place); debug!("lookup({:?})", place);
let Some(mut base) = data.rev_lookup.find_local(place.local) else { let Some(mut base) = data.rev_lookup.find_local(place.local) else {
@ -131,8 +133,8 @@ fn move_path_for(&mut self, place: Place<'tcx>) -> MovePathResult {
let mut union_path = None; let mut union_path = None;
for (place_ref, elem) in data.rev_lookup.un_derefer.iter_projections(place.as_ref()) { for (place_ref, elem) in data.rev_lookup.un_derefer.iter_projections(place.as_ref()) {
let body = self.builder.body; let body = self.body;
let tcx = self.builder.tcx; let tcx = self.tcx;
let place_ty = place_ref.ty(body, tcx).ty; let place_ty = place_ref.ty(body, tcx).ty;
if place_ty.references_error() { if place_ty.references_error() {
return MovePathResult::Error; return MovePathResult::Error;
@ -238,7 +240,7 @@ fn move_path_for(&mut self, place: Place<'tcx>) -> MovePathResult {
| ProjectionElem::Downcast(_, _) => (), | ProjectionElem::Downcast(_, _) => (),
} }
let elem_ty = PlaceTy::from_ty(place_ty).projection_ty(tcx, elem).ty; let elem_ty = PlaceTy::from_ty(place_ty).projection_ty(tcx, elem).ty;
if !(self.builder.filter)(elem_ty) { if !(self.filter)(elem_ty) {
return MovePathResult::Error; return MovePathResult::Error;
} }
if union_path.is_none() { if union_path.is_none() {
@ -274,7 +276,7 @@ fn add_move_path(
data: MoveData { rev_lookup, move_paths, path_map, init_path_map, .. }, data: MoveData { rev_lookup, move_paths, path_map, init_path_map, .. },
tcx, tcx,
.. ..
} = self.builder; } = self;
*rev_lookup.projections.entry((base, elem.lift())).or_insert_with(move || { *rev_lookup.projections.entry((base, elem.lift())).or_insert_with(move || {
new_move_path(move_paths, path_map, init_path_map, Some(base), mk_place(*tcx)) new_move_path(move_paths, path_map, init_path_map, Some(base), mk_place(*tcx))
}) })
@ -285,9 +287,7 @@ fn create_move_path(&mut self, place: Place<'tcx>) {
// drop), so this not being a valid move path is OK. // drop), so this not being a valid move path is OK.
let _ = self.move_path_for(place); let _ = self.move_path_for(place);
} }
}
impl<'a, 'tcx, F> MoveDataBuilder<'a, 'tcx, F> {
fn finalize(self) -> MoveData<'tcx> { fn finalize(self) -> MoveData<'tcx> {
debug!("{}", { debug!("{}", {
debug!("moves for {:?}:", self.body.span); debug!("moves for {:?}:", self.body.span);
@ -317,12 +317,12 @@ pub(super) fn gather_moves<'tcx>(
for (bb, block) in body.basic_blocks.iter_enumerated() { for (bb, block) in body.basic_blocks.iter_enumerated() {
for (i, stmt) in block.statements.iter().enumerate() { for (i, stmt) in block.statements.iter().enumerate() {
let source = Location { block: bb, statement_index: i }; builder.loc = Location { block: bb, statement_index: i };
builder.gather_statement(source, stmt); builder.gather_statement(stmt);
} }
let terminator_loc = Location { block: bb, statement_index: block.statements.len() }; builder.loc = Location { block: bb, statement_index: block.statements.len() };
builder.gather_terminator(terminator_loc, block.terminator()); builder.gather_terminator(block.terminator());
} }
builder.finalize() builder.finalize()
@ -345,30 +345,14 @@ fn gather_args(&mut self) {
} }
} }
fn gather_statement(&mut self, loc: Location, stmt: &Statement<'tcx>) {
debug!("gather_statement({:?}, {:?})", loc, stmt);
(Gatherer { builder: self, loc }).gather_statement(stmt);
}
fn gather_terminator(&mut self, loc: Location, term: &Terminator<'tcx>) {
debug!("gather_terminator({:?}, {:?})", loc, term);
(Gatherer { builder: self, loc }).gather_terminator(term);
}
}
struct Gatherer<'b, 'a, 'tcx, F> {
builder: &'b mut MoveDataBuilder<'a, 'tcx, F>,
loc: Location,
}
impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
fn gather_statement(&mut self, stmt: &Statement<'tcx>) { fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
debug!("gather_statement({:?}, {:?})", self.loc, stmt);
match &stmt.kind { match &stmt.kind {
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => { StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
let local = place.as_local().unwrap(); let local = place.as_local().unwrap();
assert!(self.builder.body.local_decls[local].is_deref_temp()); assert!(self.body.local_decls[local].is_deref_temp());
let rev_lookup = &mut self.builder.data.rev_lookup; let rev_lookup = &mut self.data.rev_lookup;
rev_lookup.un_derefer.insert(local, reffed.as_ref()); rev_lookup.un_derefer.insert(local, reffed.as_ref());
let base_local = rev_lookup.un_derefer.deref_chain(local).first().unwrap().local; let base_local = rev_lookup.un_derefer.deref_chain(local).first().unwrap().local;
@ -380,7 +364,7 @@ fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
// Box starts out uninitialized - need to create a separate // Box starts out uninitialized - need to create a separate
// move-path for the interior so it will be separate from // move-path for the interior so it will be separate from
// the exterior. // the exterior.
self.create_move_path(self.builder.tcx.mk_place_deref(*place)); self.create_move_path(self.tcx.mk_place_deref(*place));
self.gather_init(place.as_ref(), InitKind::Shallow); self.gather_init(place.as_ref(), InitKind::Shallow);
} else { } else {
self.gather_init(place.as_ref(), InitKind::Deep); self.gather_init(place.as_ref(), InitKind::Deep);
@ -393,7 +377,7 @@ fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
StatementKind::StorageLive(_) => {} StatementKind::StorageLive(_) => {}
StatementKind::StorageDead(local) => { StatementKind::StorageDead(local) => {
// DerefTemp locals (results of CopyForDeref) don't actually move anything. // DerefTemp locals (results of CopyForDeref) don't actually move anything.
if !self.builder.body.local_decls[*local].is_deref_temp() { if !self.body.local_decls[*local].is_deref_temp() {
self.gather_move(Place::from(*local)); self.gather_move(Place::from(*local));
} }
} }
@ -443,6 +427,7 @@ fn gather_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
} }
fn gather_terminator(&mut self, term: &Terminator<'tcx>) { fn gather_terminator(&mut self, term: &Terminator<'tcx>) {
debug!("gather_terminator({:?}, {:?})", self.loc, term);
match term.kind { match term.kind {
TerminatorKind::Goto { target: _ } TerminatorKind::Goto { target: _ }
| TerminatorKind::FalseEdge { .. } | TerminatorKind::FalseEdge { .. }
@ -551,7 +536,7 @@ fn gather_move(&mut self, place: Place<'tcx>) {
// `ConstIndex` patterns. This is done to ensure that all move paths // `ConstIndex` patterns. This is done to ensure that all move paths
// are disjoint, which is expected by drop elaboration. // are disjoint, which is expected by drop elaboration.
let base_place = let base_place =
Place { local: place.local, projection: self.builder.tcx.mk_place_elems(base) }; Place { local: place.local, projection: self.tcx.mk_place_elems(base) };
let base_path = match self.move_path_for(base_place) { let base_path = match self.move_path_for(base_place) {
MovePathResult::Path(path) => path, MovePathResult::Path(path) => path,
MovePathResult::Union(path) => { MovePathResult::Union(path) => {
@ -562,11 +547,9 @@ fn gather_move(&mut self, place: Place<'tcx>) {
return; return;
} }
}; };
let base_ty = base_place.ty(self.builder.body, self.builder.tcx).ty; let base_ty = base_place.ty(self.body, self.tcx).ty;
let len: u64 = match base_ty.kind() { let len: u64 = match base_ty.kind() {
ty::Array(_, size) => { ty::Array(_, size) => size.eval_target_usize(self.tcx, self.param_env),
size.eval_target_usize(self.builder.tcx, self.builder.param_env)
}
_ => bug!("from_end: false slice pattern of non-array type"), _ => bug!("from_end: false slice pattern of non-array type"),
}; };
for offset in from..to { for offset in from..to {
@ -587,13 +570,13 @@ fn gather_move(&mut self, place: Place<'tcx>) {
} }
fn record_move(&mut self, place: Place<'tcx>, path: MovePathIndex) { fn record_move(&mut self, place: Place<'tcx>, path: MovePathIndex) {
let move_out = self.builder.data.moves.push(MoveOut { path, source: self.loc }); let move_out = self.data.moves.push(MoveOut { path, source: self.loc });
debug!( debug!(
"gather_move({:?}, {:?}): adding move {:?} of {:?}", "gather_move({:?}, {:?}): adding move {:?} of {:?}",
self.loc, place, move_out, path self.loc, place, move_out, path
); );
self.builder.data.path_map[path].push(move_out); self.data.path_map[path].push(move_out);
self.builder.data.loc_map[self.loc].push(move_out); self.data.loc_map[self.loc].push(move_out);
} }
fn gather_init(&mut self, place: PlaceRef<'tcx>, kind: InitKind) { fn gather_init(&mut self, place: PlaceRef<'tcx>, kind: InitKind) {
@ -604,13 +587,13 @@ fn gather_init(&mut self, place: PlaceRef<'tcx>, kind: InitKind) {
// Check if we are assigning into a field of a union, if so, lookup the place // Check if we are assigning into a field of a union, if so, lookup the place
// of the union so it is marked as initialized again. // of the union so it is marked as initialized again.
if let Some((place_base, ProjectionElem::Field(_, _))) = place.last_projection() { if let Some((place_base, ProjectionElem::Field(_, _))) = place.last_projection() {
if place_base.ty(self.builder.body, self.builder.tcx).ty.is_union() { if place_base.ty(self.body, self.tcx).ty.is_union() {
place = place_base; place = place_base;
} }
} }
if let LookupResult::Exact(path) = self.builder.data.rev_lookup.find(place) { if let LookupResult::Exact(path) = self.data.rev_lookup.find(place) {
let init = self.builder.data.inits.push(Init { let init = self.data.inits.push(Init {
location: InitLocation::Statement(self.loc), location: InitLocation::Statement(self.loc),
path, path,
kind, kind,
@ -621,8 +604,8 @@ fn gather_init(&mut self, place: PlaceRef<'tcx>, kind: InitKind) {
self.loc, place, init, path self.loc, place, init, path
); );
self.builder.data.init_path_map[path].push(init); self.data.init_path_map[path].push(init);
self.builder.data.init_loc_map[self.loc].push(init); self.data.init_loc_map[self.loc].push(init);
} }
} }
} }