1 use crate::move_paths::FxHashMap;
2 use crate::un_derefer::UnDerefer;
3 use rustc_index::vec::IndexVec;
4 use rustc_middle::mir::tcx::RvalueInitializationState;
5 use rustc_middle::mir::*;
6 use rustc_middle::ty::{self, TyCtxt};
7 use smallvec::{smallvec, SmallVec};
11 use super::abs_domain::Lift;
12 use super::IllegalMoveOriginKind::*;
13 use super::{Init, InitIndex, InitKind, InitLocation, LookupResult, MoveError};
15 LocationMap, MoveData, MoveOut, MoveOutIndex, MovePath, MovePathIndex, MovePathLookup,
18 struct MoveDataBuilder<'a, 'tcx> {
21 param_env: ty::ParamEnv<'tcx>,
23 errors: Vec<(Place<'tcx>, MoveError<'tcx>)>,
24 un_derefer: UnDerefer<'tcx>,
27 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
28 fn new(body: &'a Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
29 let mut move_paths = IndexVec::new();
30 let mut path_map = IndexVec::new();
31 let mut init_path_map = IndexVec::new();
38 un_derefer: UnDerefer { tcx: tcx, derefer_sidetable: Default::default() },
40 moves: IndexVec::new(),
41 loc_map: LocationMap::new(body),
42 rev_lookup: MovePathLookup {
56 projections: Default::default(),
60 inits: IndexVec::new(),
61 init_loc_map: LocationMap::new(body),
68 move_paths: &mut IndexVec<MovePathIndex, MovePath<'tcx>>,
69 path_map: &mut IndexVec<MovePathIndex, SmallVec<[MoveOutIndex; 4]>>,
70 init_path_map: &mut IndexVec<MovePathIndex, SmallVec<[InitIndex; 4]>>,
71 parent: Option<MovePathIndex>,
75 move_paths.push(MovePath { next_sibling: None, first_child: None, parent, place });
77 if let Some(parent) = parent {
78 let next_sibling = mem::replace(&mut move_paths[parent].first_child, Some(move_path));
79 move_paths[move_path].next_sibling = next_sibling;
82 let path_map_ent = path_map.push(smallvec![]);
83 assert_eq!(path_map_ent, move_path);
85 let init_path_map_ent = init_path_map.push(smallvec![]);
86 assert_eq!(init_path_map_ent, move_path);
92 impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
93 /// This creates a MovePath for a given place, returning an `MovePathError`
94 /// if that place can't be moved from.
96 /// NOTE: places behind references *do not* get a move path, which is
97 /// problematic for borrowck.
99 /// Maybe we should have separate "borrowck" and "moveck" modes.
100 fn move_path_for(&mut self, place: Place<'tcx>) -> Result<MovePathIndex, MoveError<'tcx>> {
101 if let Some(new_place) = self.builder.un_derefer.derefer(place.as_ref(), self.builder.body)
103 return self.move_path_for(new_place);
106 debug!("lookup({:?})", place);
107 let mut base = self.builder.data.rev_lookup.locals[place.local];
109 // The move path index of the first union that we find. Once this is
110 // some we stop creating child move paths, since moves from unions
111 // move the whole thing.
112 // We continue looking for other move errors though so that moving
113 // from `*(u.f: &_)` isn't allowed.
114 let mut union_path = None;
116 for (i, elem) in place.projection.iter().enumerate() {
117 let proj_base = &place.projection[..i];
118 let body = self.builder.body;
119 let tcx = self.builder.tcx;
120 let place_ty = Place::ty_from(place.local, proj_base, body, tcx).ty;
121 match place_ty.kind() {
122 ty::Ref(..) | ty::RawPtr(..) => {
123 let proj = &place.projection[..i + 1];
124 return Err(MoveError::cannot_move_out_of(
127 target_place: Place {
129 projection: tcx.intern_place_elems(proj),
134 ty::Adt(adt, _) if adt.has_dtor(tcx) && !adt.is_box() => {
135 return Err(MoveError::cannot_move_out_of(
137 InteriorOfTypeWithDestructor { container_ty: place_ty },
140 ty::Adt(adt, _) if adt.is_union() => {
141 union_path.get_or_insert(base);
144 return Err(MoveError::cannot_move_out_of(
146 InteriorOfSliceOrArray {
148 is_index: matches!(elem, ProjectionElem::Index(..)),
154 if let ProjectionElem::Index(..) = elem {
155 return Err(MoveError::cannot_move_out_of(
157 InteriorOfSliceOrArray { ty: place_ty, is_index: true },
165 if union_path.is_none() {
166 base = self.add_move_path(base, elem, |tcx| Place {
168 projection: tcx.intern_place_elems(&place.projection[..i + 1]),
173 if let Some(base) = union_path {
174 // Move out of union - always move the entire union.
175 Err(MoveError::UnionMove { path: base })
184 elem: PlaceElem<'tcx>,
185 mk_place: impl FnOnce(TyCtxt<'tcx>) -> Place<'tcx>,
187 let MoveDataBuilder {
188 data: MoveData { rev_lookup, move_paths, path_map, init_path_map, .. },
192 *rev_lookup.projections.entry((base, elem.lift())).or_insert_with(move || {
193 MoveDataBuilder::new_move_path(
203 fn create_move_path(&mut self, place: Place<'tcx>) {
204 // This is an non-moving access (such as an overwrite or
205 // drop), so this not being a valid move path is OK.
206 let _ = self.move_path_for(place);
210 pub type MoveDat<'tcx> = Result<
211 (FxHashMap<Local, Place<'tcx>>, MoveData<'tcx>),
212 (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>),
215 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
216 fn finalize(self) -> MoveDat<'tcx> {
218 debug!("moves for {:?}:", self.body.span);
219 for (j, mo) in self.data.moves.iter_enumerated() {
220 debug!(" {:?} = {:?}", j, mo);
222 debug!("move paths for {:?}:", self.body.span);
223 for (j, path) in self.data.move_paths.iter_enumerated() {
224 debug!(" {:?} = {:?}", j, path);
229 if self.errors.is_empty() {
230 Ok((self.un_derefer.derefer_sidetable, self.data))
232 Err((self.data, self.errors))
237 pub(super) fn gather_moves<'tcx>(
240 param_env: ty::ParamEnv<'tcx>,
242 let mut builder = MoveDataBuilder::new(body, tcx, param_env);
244 builder.gather_args();
246 for (bb, block) in body.basic_blocks.iter_enumerated() {
247 for (i, stmt) in block.statements.iter().enumerate() {
248 let source = Location { block: bb, statement_index: i };
249 builder.gather_statement(source, stmt);
252 let terminator_loc = Location { block: bb, statement_index: block.statements.len() };
253 builder.gather_terminator(terminator_loc, block.terminator());
259 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
260 fn gather_args(&mut self) {
261 for arg in self.body.args_iter() {
262 let path = self.data.rev_lookup.locals[arg];
264 let init = self.data.inits.push(Init {
266 kind: InitKind::Deep,
267 location: InitLocation::Argument(arg),
270 debug!("gather_args: adding init {:?} of {:?} for argument {:?}", init, path, arg);
272 self.data.init_path_map[path].push(init);
276 fn gather_statement(&mut self, loc: Location, stmt: &Statement<'tcx>) {
277 debug!("gather_statement({:?}, {:?})", loc, stmt);
278 (Gatherer { builder: self, loc }).gather_statement(stmt);
281 fn gather_terminator(&mut self, loc: Location, term: &Terminator<'tcx>) {
282 debug!("gather_terminator({:?}, {:?})", loc, term);
283 (Gatherer { builder: self, loc }).gather_terminator(term);
287 struct Gatherer<'b, 'a, 'tcx> {
288 builder: &'b mut MoveDataBuilder<'a, 'tcx>,
292 impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
293 fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
295 StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
296 assert!(place.projection.is_empty());
297 if self.builder.body.local_decls[place.local].is_deref_temp() {
298 self.builder.un_derefer.derefer_sidetable.insert(place.local, *reffed);
301 StatementKind::Assign(box (place, rval)) => {
302 self.create_move_path(*place);
303 if let RvalueInitializationState::Shallow = rval.initialization_state() {
304 // Box starts out uninitialized - need to create a separate
305 // move-path for the interior so it will be separate from
307 self.create_move_path(self.builder.tcx.mk_place_deref(*place));
308 self.gather_init(place.as_ref(), InitKind::Shallow);
310 self.gather_init(place.as_ref(), InitKind::Deep);
312 self.gather_rvalue(rval);
314 StatementKind::FakeRead(box (_, place)) => {
315 self.create_move_path(*place);
317 StatementKind::StorageLive(_) => {}
318 StatementKind::StorageDead(local) => {
319 // DerefTemp locals (results of CopyForDeref) don't actually move anything.
320 if !self.builder.un_derefer.derefer_sidetable.contains_key(&local) {
321 self.gather_move(Place::from(*local));
324 StatementKind::SetDiscriminant { .. } | StatementKind::Deinit(..) => {
326 stmt.source_info.span,
327 "SetDiscriminant/Deinit should not exist during borrowck"
330 StatementKind::Retag { .. }
331 | StatementKind::AscribeUserType(..)
332 | StatementKind::Coverage(..)
333 | StatementKind::Intrinsic(..)
334 | StatementKind::ConstEvalCounter
335 | StatementKind::Nop => {}
339 fn gather_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
341 Rvalue::ThreadLocalRef(_) => {} // not-a-move
342 Rvalue::Use(ref operand)
343 | Rvalue::Repeat(ref operand, _)
344 | Rvalue::Cast(_, ref operand, _)
345 | Rvalue::ShallowInitBox(ref operand, _)
346 | Rvalue::UnaryOp(_, ref operand) => self.gather_operand(operand),
347 Rvalue::BinaryOp(ref _binop, box (ref lhs, ref rhs))
348 | Rvalue::CheckedBinaryOp(ref _binop, box (ref lhs, ref rhs)) => {
349 self.gather_operand(lhs);
350 self.gather_operand(rhs);
352 Rvalue::Aggregate(ref _kind, ref operands) => {
353 for operand in operands {
354 self.gather_operand(operand);
357 Rvalue::CopyForDeref(..) => unreachable!(),
359 | Rvalue::AddressOf(..)
360 | Rvalue::Discriminant(..)
362 | Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _) => {}
366 fn gather_terminator(&mut self, term: &Terminator<'tcx>) {
368 TerminatorKind::Goto { target: _ }
369 | TerminatorKind::FalseEdge { .. }
370 | TerminatorKind::FalseUnwind { .. }
371 // In some sense returning moves the return place into the current
372 // call's destination, however, since there are no statements after
373 // this that could possibly access the return place, this doesn't
375 | TerminatorKind::Return
376 | TerminatorKind::Resume
377 | TerminatorKind::Abort
378 | TerminatorKind::GeneratorDrop
379 | TerminatorKind::Unreachable => {}
381 TerminatorKind::Assert { ref cond, .. } => {
382 self.gather_operand(cond);
385 TerminatorKind::SwitchInt { ref discr, .. } => {
386 self.gather_operand(discr);
389 TerminatorKind::Yield { ref value, resume_arg: place, .. } => {
390 self.gather_operand(value);
391 self.create_move_path(place);
392 self.gather_init(place.as_ref(), InitKind::Deep);
395 TerminatorKind::Drop { place, target: _, unwind: _ } => {
396 self.gather_move(place);
398 TerminatorKind::DropAndReplace { place, ref value, .. } => {
399 self.create_move_path(place);
400 self.gather_operand(value);
401 self.gather_init(place.as_ref(), InitKind::Deep);
403 TerminatorKind::Call {
412 self.gather_operand(func);
414 self.gather_operand(arg);
416 if let Some(_bb) = target {
417 self.create_move_path(destination);
418 self.gather_init(destination.as_ref(), InitKind::NonPanicPathOnly);
421 TerminatorKind::InlineAsm {
431 InlineAsmOperand::In { reg: _, ref value }
433 self.gather_operand(value);
435 InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
436 if let Some(place) = place {
437 self.create_move_path(place);
438 self.gather_init(place.as_ref(), InitKind::Deep);
441 InlineAsmOperand::InOut { reg: _, late: _, ref in_value, out_place } => {
442 self.gather_operand(in_value);
443 if let Some(out_place) = out_place {
444 self.create_move_path(out_place);
445 self.gather_init(out_place.as_ref(), InitKind::Deep);
448 InlineAsmOperand::Const { value: _ }
449 | InlineAsmOperand::SymFn { value: _ }
450 | InlineAsmOperand::SymStatic { def_id: _ } => {}
457 fn gather_operand(&mut self, operand: &Operand<'tcx>) {
459 Operand::Constant(..) | Operand::Copy(..) => {} // not-a-move
460 Operand::Move(place) => {
462 self.gather_move(place);
467 fn gather_move(&mut self, place: Place<'tcx>) {
468 debug!("gather_move({:?}, {:?})", self.loc, place);
469 if let Some(new_place) = self.builder.un_derefer.derefer(place.as_ref(), self.builder.body)
471 self.gather_move(new_place);
475 if let [ref base @ .., ProjectionElem::Subslice { from, to, from_end: false }] =
478 // Split `Subslice` patterns into the corresponding list of
479 // `ConstIndex` patterns. This is done to ensure that all move paths
480 // are disjoint, which is expected by drop elaboration.
482 Place { local: place.local, projection: self.builder.tcx.intern_place_elems(base) };
483 let base_path = match self.move_path_for(base_place) {
485 Err(MoveError::UnionMove { path }) => {
486 self.record_move(place, path);
489 Err(error @ MoveError::IllegalMove { .. }) => {
490 self.builder.errors.push((base_place, error));
494 let base_ty = base_place.ty(self.builder.body, self.builder.tcx).ty;
495 let len: u64 = match base_ty.kind() {
496 ty::Array(_, size) => size.eval_usize(self.builder.tcx, self.builder.param_env),
497 _ => bug!("from_end: false slice pattern of non-array type"),
499 for offset in from..to {
501 ProjectionElem::ConstantIndex { offset, min_length: len, from_end: false };
503 self.add_move_path(base_path, elem, |tcx| tcx.mk_place_elem(base_place, elem));
504 self.record_move(place, path);
507 match self.move_path_for(place) {
508 Ok(path) | Err(MoveError::UnionMove { path }) => self.record_move(place, path),
509 Err(error @ MoveError::IllegalMove { .. }) => {
510 self.builder.errors.push((place, error));
516 fn record_move(&mut self, place: Place<'tcx>, path: MovePathIndex) {
517 let move_out = self.builder.data.moves.push(MoveOut { path, source: self.loc });
519 "gather_move({:?}, {:?}): adding move {:?} of {:?}",
520 self.loc, place, move_out, path
522 self.builder.data.path_map[path].push(move_out);
523 self.builder.data.loc_map[self.loc].push(move_out);
526 fn gather_init(&mut self, place: PlaceRef<'tcx>, kind: InitKind) {
527 debug!("gather_init({:?}, {:?})", self.loc, place);
529 if let Some(new_place) = self.builder.un_derefer.derefer(place, self.builder.body) {
530 self.gather_init(new_place.as_ref(), kind);
534 let mut place = place;
536 // Check if we are assigning into a field of a union, if so, lookup the place
537 // of the union so it is marked as initialized again.
538 if let Some((place_base, ProjectionElem::Field(_, _))) = place.last_projection() {
539 if place_base.ty(self.builder.body, self.builder.tcx).ty.is_union() {
544 if let LookupResult::Exact(path) = self.builder.data.rev_lookup.find(place) {
545 let init = self.builder.data.inits.push(Init {
546 location: InitLocation::Statement(self.loc),
552 "gather_init({:?}, {:?}): adding init {:?} of {:?}",
553 self.loc, place, init, path
556 self.builder.data.init_path_map[path].push(init);
557 self.builder.data.init_loc_map[self.loc].push(init);