1 use rustc_index::vec::IndexVec;
2 use rustc_middle::mir::tcx::RvalueInitializationState;
3 use rustc_middle::mir::*;
4 use rustc_middle::ty::{self, TyCtxt};
5 use smallvec::{smallvec, SmallVec};
6
7 use std::iter;
8 use std::mem;
9
10 use super::abs_domain::Lift;
11 use super::IllegalMoveOriginKind::*;
12 use super::{Init, InitIndex, InitKind, InitLocation, LookupResult, MoveError};
13 use super::{
14 LocationMap, MoveData, MoveOut, MoveOutIndex, MovePath, MovePathIndex, MovePathLookup,
15 };
16
17 struct MoveDataBuilder<'a, 'tcx> {
18 body: &'a Body<'tcx>,
19 tcx: TyCtxt<'tcx>,
20 param_env: ty::ParamEnv<'tcx>,
21 data: MoveData<'tcx>,
22 errors: Vec<(Place<'tcx>, MoveError<'tcx>)>,
23 }
24
25 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
new(body: &'a Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self26 fn new(body: &'a Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
27 let mut move_paths = IndexVec::new();
28 let mut path_map = IndexVec::new();
29 let mut init_path_map = IndexVec::new();
30
31 MoveDataBuilder {
32 body,
33 tcx,
34 param_env,
35 errors: Vec::new(),
36 data: MoveData {
37 moves: IndexVec::new(),
38 loc_map: LocationMap::new(body),
39 rev_lookup: MovePathLookup {
40 locals: body
41 .local_decls
42 .indices()
43 .map(|i| {
44 Self::new_move_path(
45 &mut move_paths,
46 &mut path_map,
47 &mut init_path_map,
48 None,
49 Place::from(i),
50 )
51 })
52 .collect(),
53 projections: Default::default(),
54 },
55 move_paths,
56 path_map,
57 inits: IndexVec::new(),
58 init_loc_map: LocationMap::new(body),
59 init_path_map,
60 },
61 }
62 }
63
new_move_path( move_paths: &mut IndexVec<MovePathIndex, MovePath<'tcx>>, path_map: &mut IndexVec<MovePathIndex, SmallVec<[MoveOutIndex; 4]>>, init_path_map: &mut IndexVec<MovePathIndex, SmallVec<[InitIndex; 4]>>, parent: Option<MovePathIndex>, place: Place<'tcx>, ) -> MovePathIndex64 fn new_move_path(
65 move_paths: &mut IndexVec<MovePathIndex, MovePath<'tcx>>,
66 path_map: &mut IndexVec<MovePathIndex, SmallVec<[MoveOutIndex; 4]>>,
67 init_path_map: &mut IndexVec<MovePathIndex, SmallVec<[InitIndex; 4]>>,
68 parent: Option<MovePathIndex>,
69 place: Place<'tcx>,
70 ) -> MovePathIndex {
71 let move_path =
72 move_paths.push(MovePath { next_sibling: None, first_child: None, parent, place });
73
74 if let Some(parent) = parent {
75 let next_sibling = mem::replace(&mut move_paths[parent].first_child, Some(move_path));
76 move_paths[move_path].next_sibling = next_sibling;
77 }
78
79 let path_map_ent = path_map.push(smallvec![]);
80 assert_eq!(path_map_ent, move_path);
81
82 let init_path_map_ent = init_path_map.push(smallvec![]);
83 assert_eq!(init_path_map_ent, move_path);
84
85 move_path
86 }
87 }
88
89 impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
90 /// This creates a MovePath for a given place, returning an `MovePathError`
91 /// if that place can't be moved from.
92 ///
93 /// NOTE: places behind references *do not* get a move path, which is
94 /// problematic for borrowck.
95 ///
96 /// Maybe we should have separate "borrowck" and "moveck" modes.
move_path_for(&mut self, place: Place<'tcx>) -> Result<MovePathIndex, MoveError<'tcx>>97 fn move_path_for(&mut self, place: Place<'tcx>) -> Result<MovePathIndex, MoveError<'tcx>> {
98 debug!("lookup({:?})", place);
99 let mut base = self.builder.data.rev_lookup.locals[place.local];
100
101 // The move path index of the first union that we find. Once this is
102 // some we stop creating child move paths, since moves from unions
103 // move the whole thing.
104 // We continue looking for other move errors though so that moving
105 // from `*(u.f: &_)` isn't allowed.
106 let mut union_path = None;
107
108 for (i, elem) in place.projection.iter().enumerate() {
109 let proj_base = &place.projection[..i];
110 let body = self.builder.body;
111 let tcx = self.builder.tcx;
112 let place_ty = Place::ty_from(place.local, proj_base, body, tcx).ty;
113 match place_ty.kind() {
114 ty::Ref(..) | ty::RawPtr(..) => {
115 let proj = &place.projection[..i + 1];
116 return Err(MoveError::cannot_move_out_of(
117 self.loc,
118 BorrowedContent {
119 target_place: Place {
120 local: place.local,
121 projection: tcx.intern_place_elems(proj),
122 },
123 },
124 ));
125 }
126 ty::Adt(adt, _) if adt.has_dtor(tcx) && !adt.is_box() => {
127 return Err(MoveError::cannot_move_out_of(
128 self.loc,
129 InteriorOfTypeWithDestructor { container_ty: place_ty },
130 ));
131 }
132 ty::Adt(adt, _) if adt.is_union() => {
133 union_path.get_or_insert(base);
134 }
135 ty::Slice(_) => {
136 return Err(MoveError::cannot_move_out_of(
137 self.loc,
138 InteriorOfSliceOrArray {
139 ty: place_ty,
140 is_index: matches!(elem, ProjectionElem::Index(..)),
141 },
142 ));
143 }
144
145 ty::Array(..) => {
146 if let ProjectionElem::Index(..) = elem {
147 return Err(MoveError::cannot_move_out_of(
148 self.loc,
149 InteriorOfSliceOrArray { ty: place_ty, is_index: true },
150 ));
151 }
152 }
153
154 _ => {}
155 };
156
157 if union_path.is_none() {
158 base = self.add_move_path(base, elem, |tcx| Place {
159 local: place.local,
160 projection: tcx.intern_place_elems(&place.projection[..i + 1]),
161 });
162 }
163 }
164
165 if let Some(base) = union_path {
166 // Move out of union - always move the entire union.
167 Err(MoveError::UnionMove { path: base })
168 } else {
169 Ok(base)
170 }
171 }
172
add_move_path( &mut self, base: MovePathIndex, elem: PlaceElem<'tcx>, mk_place: impl FnOnce(TyCtxt<'tcx>) -> Place<'tcx>, ) -> MovePathIndex173 fn add_move_path(
174 &mut self,
175 base: MovePathIndex,
176 elem: PlaceElem<'tcx>,
177 mk_place: impl FnOnce(TyCtxt<'tcx>) -> Place<'tcx>,
178 ) -> MovePathIndex {
179 let MoveDataBuilder {
180 data: MoveData { rev_lookup, move_paths, path_map, init_path_map, .. },
181 tcx,
182 ..
183 } = self.builder;
184 *rev_lookup.projections.entry((base, elem.lift())).or_insert_with(move || {
185 MoveDataBuilder::new_move_path(
186 move_paths,
187 path_map,
188 init_path_map,
189 Some(base),
190 mk_place(*tcx),
191 )
192 })
193 }
194
create_move_path(&mut self, place: Place<'tcx>)195 fn create_move_path(&mut self, place: Place<'tcx>) {
196 // This is an non-moving access (such as an overwrite or
197 // drop), so this not being a valid move path is OK.
198 let _ = self.move_path_for(place);
199 }
200 }
201
202 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
finalize( self, ) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)>203 fn finalize(
204 self,
205 ) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
206 debug!("{}", {
207 debug!("moves for {:?}:", self.body.span);
208 for (j, mo) in self.data.moves.iter_enumerated() {
209 debug!(" {:?} = {:?}", j, mo);
210 }
211 debug!("move paths for {:?}:", self.body.span);
212 for (j, path) in self.data.move_paths.iter_enumerated() {
213 debug!(" {:?} = {:?}", j, path);
214 }
215 "done dumping moves"
216 });
217
218 if !self.errors.is_empty() { Err((self.data, self.errors)) } else { Ok(self.data) }
219 }
220 }
221
gather_moves<'tcx>( body: &Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)>222 pub(super) fn gather_moves<'tcx>(
223 body: &Body<'tcx>,
224 tcx: TyCtxt<'tcx>,
225 param_env: ty::ParamEnv<'tcx>,
226 ) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
227 let mut builder = MoveDataBuilder::new(body, tcx, param_env);
228
229 builder.gather_args();
230
231 for (bb, block) in body.basic_blocks().iter_enumerated() {
232 for (i, stmt) in block.statements.iter().enumerate() {
233 let source = Location { block: bb, statement_index: i };
234 builder.gather_statement(source, stmt);
235 }
236
237 let terminator_loc = Location { block: bb, statement_index: block.statements.len() };
238 builder.gather_terminator(terminator_loc, block.terminator());
239 }
240
241 builder.finalize()
242 }
243
244 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
gather_args(&mut self)245 fn gather_args(&mut self) {
246 for arg in self.body.args_iter() {
247 let path = self.data.rev_lookup.locals[arg];
248
249 let init = self.data.inits.push(Init {
250 path,
251 kind: InitKind::Deep,
252 location: InitLocation::Argument(arg),
253 });
254
255 debug!("gather_args: adding init {:?} of {:?} for argument {:?}", init, path, arg);
256
257 self.data.init_path_map[path].push(init);
258 }
259 }
260
gather_statement(&mut self, loc: Location, stmt: &Statement<'tcx>)261 fn gather_statement(&mut self, loc: Location, stmt: &Statement<'tcx>) {
262 debug!("gather_statement({:?}, {:?})", loc, stmt);
263 (Gatherer { builder: self, loc }).gather_statement(stmt);
264 }
265
gather_terminator(&mut self, loc: Location, term: &Terminator<'tcx>)266 fn gather_terminator(&mut self, loc: Location, term: &Terminator<'tcx>) {
267 debug!("gather_terminator({:?}, {:?})", loc, term);
268 (Gatherer { builder: self, loc }).gather_terminator(term);
269 }
270 }
271
272 struct Gatherer<'b, 'a, 'tcx> {
273 builder: &'b mut MoveDataBuilder<'a, 'tcx>,
274 loc: Location,
275 }
276
277 impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
gather_statement(&mut self, stmt: &Statement<'tcx>)278 fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
279 match &stmt.kind {
280 StatementKind::Assign(box (place, rval)) => {
281 self.create_move_path(*place);
282 if let RvalueInitializationState::Shallow = rval.initialization_state() {
283 // Box starts out uninitialized - need to create a separate
284 // move-path for the interior so it will be separate from
285 // the exterior.
286 self.create_move_path(self.builder.tcx.mk_place_deref(*place));
287 self.gather_init(place.as_ref(), InitKind::Shallow);
288 } else {
289 self.gather_init(place.as_ref(), InitKind::Deep);
290 }
291 self.gather_rvalue(rval);
292 }
293 StatementKind::FakeRead(box (_, place)) => {
294 self.create_move_path(*place);
295 }
296 StatementKind::LlvmInlineAsm(ref asm) => {
297 for (output, kind) in iter::zip(&*asm.outputs, &asm.asm.outputs) {
298 if !kind.is_indirect {
299 self.gather_init(output.as_ref(), InitKind::Deep);
300 }
301 }
302 for (_, input) in asm.inputs.iter() {
303 self.gather_operand(input);
304 }
305 }
306 StatementKind::StorageLive(_) => {}
307 StatementKind::StorageDead(local) => {
308 self.gather_move(Place::from(*local));
309 }
310 StatementKind::SetDiscriminant { .. } => {
311 span_bug!(
312 stmt.source_info.span,
313 "SetDiscriminant should not exist during borrowck"
314 );
315 }
316 StatementKind::Retag { .. }
317 | StatementKind::AscribeUserType(..)
318 | StatementKind::Coverage(..)
319 | StatementKind::CopyNonOverlapping(..)
320 | StatementKind::Nop => {}
321 }
322 }
323
gather_rvalue(&mut self, rvalue: &Rvalue<'tcx>)324 fn gather_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
325 match *rvalue {
326 Rvalue::ThreadLocalRef(_) => {} // not-a-move
327 Rvalue::Use(ref operand)
328 | Rvalue::Repeat(ref operand, _)
329 | Rvalue::Cast(_, ref operand, _)
330 | Rvalue::ShallowInitBox(ref operand, _)
331 | Rvalue::UnaryOp(_, ref operand) => self.gather_operand(operand),
332 Rvalue::BinaryOp(ref _binop, box (ref lhs, ref rhs))
333 | Rvalue::CheckedBinaryOp(ref _binop, box (ref lhs, ref rhs)) => {
334 self.gather_operand(lhs);
335 self.gather_operand(rhs);
336 }
337 Rvalue::Aggregate(ref _kind, ref operands) => {
338 for operand in operands {
339 self.gather_operand(operand);
340 }
341 }
342 Rvalue::Ref(..)
343 | Rvalue::AddressOf(..)
344 | Rvalue::Discriminant(..)
345 | Rvalue::Len(..)
346 | Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf, _)
347 | Rvalue::NullaryOp(NullOp::Box, _) => {
348 // This returns an rvalue with uninitialized contents. We can't
349 // move out of it here because it is an rvalue - assignments always
350 // completely initialize their place.
351 //
352 // However, this does not matter - MIR building is careful to
353 // only emit a shallow free for the partially-initialized
354 // temporary.
355 //
356 // In any case, if we want to fix this, we have to register a
357 // special move and change the `statement_effect` functions.
358 }
359 }
360 }
361
gather_terminator(&mut self, term: &Terminator<'tcx>)362 fn gather_terminator(&mut self, term: &Terminator<'tcx>) {
363 match term.kind {
364 TerminatorKind::Goto { target: _ }
365 | TerminatorKind::FalseEdge { .. }
366 | TerminatorKind::FalseUnwind { .. }
367 // In some sense returning moves the return place into the current
368 // call's destination, however, since there are no statements after
369 // this that could possibly access the return place, this doesn't
370 // need recording.
371 | TerminatorKind::Return
372 | TerminatorKind::Resume
373 | TerminatorKind::Abort
374 | TerminatorKind::GeneratorDrop
375 | TerminatorKind::Unreachable => {}
376
377 TerminatorKind::Assert { ref cond, .. } => {
378 self.gather_operand(cond);
379 }
380
381 TerminatorKind::SwitchInt { ref discr, .. } => {
382 self.gather_operand(discr);
383 }
384
385 TerminatorKind::Yield { ref value, resume_arg: place, .. } => {
386 self.gather_operand(value);
387 self.create_move_path(place);
388 self.gather_init(place.as_ref(), InitKind::Deep);
389 }
390
391 TerminatorKind::Drop { place, target: _, unwind: _ } => {
392 self.gather_move(place);
393 }
394 TerminatorKind::DropAndReplace { place, ref value, .. } => {
395 self.create_move_path(place);
396 self.gather_operand(value);
397 self.gather_init(place.as_ref(), InitKind::Deep);
398 }
399 TerminatorKind::Call {
400 ref func,
401 ref args,
402 ref destination,
403 cleanup: _,
404 from_hir_call: _,
405 fn_span: _,
406 } => {
407 self.gather_operand(func);
408 for arg in args {
409 self.gather_operand(arg);
410 }
411 if let Some((destination, _bb)) = *destination {
412 self.create_move_path(destination);
413 self.gather_init(destination.as_ref(), InitKind::NonPanicPathOnly);
414 }
415 }
416 TerminatorKind::InlineAsm {
417 template: _,
418 ref operands,
419 options: _,
420 line_spans: _,
421 destination: _,
422 } => {
423 for op in operands {
424 match *op {
425 InlineAsmOperand::In { reg: _, ref value }
426 => {
427 self.gather_operand(value);
428 }
429 InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
430 if let Some(place) = place {
431 self.create_move_path(place);
432 self.gather_init(place.as_ref(), InitKind::Deep);
433 }
434 }
435 InlineAsmOperand::InOut { reg: _, late: _, ref in_value, out_place } => {
436 self.gather_operand(in_value);
437 if let Some(out_place) = out_place {
438 self.create_move_path(out_place);
439 self.gather_init(out_place.as_ref(), InitKind::Deep);
440 }
441 }
442 InlineAsmOperand::Const { value: _ }
443 | InlineAsmOperand::SymFn { value: _ }
444 | InlineAsmOperand::SymStatic { def_id: _ } => {}
445 }
446 }
447 }
448 }
449 }
450
gather_operand(&mut self, operand: &Operand<'tcx>)451 fn gather_operand(&mut self, operand: &Operand<'tcx>) {
452 match *operand {
453 Operand::Constant(..) | Operand::Copy(..) => {} // not-a-move
454 Operand::Move(place) => {
455 // a move
456 self.gather_move(place);
457 }
458 }
459 }
460
gather_move(&mut self, place: Place<'tcx>)461 fn gather_move(&mut self, place: Place<'tcx>) {
462 debug!("gather_move({:?}, {:?})", self.loc, place);
463
464 if let [ref base @ .., ProjectionElem::Subslice { from, to, from_end: false }] =
465 **place.projection
466 {
467 // Split `Subslice` patterns into the corresponding list of
468 // `ConstIndex` patterns. This is done to ensure that all move paths
469 // are disjoint, which is expected by drop elaboration.
470 let base_place =
471 Place { local: place.local, projection: self.builder.tcx.intern_place_elems(base) };
472 let base_path = match self.move_path_for(base_place) {
473 Ok(path) => path,
474 Err(MoveError::UnionMove { path }) => {
475 self.record_move(place, path);
476 return;
477 }
478 Err(error @ MoveError::IllegalMove { .. }) => {
479 self.builder.errors.push((base_place, error));
480 return;
481 }
482 };
483 let base_ty = base_place.ty(self.builder.body, self.builder.tcx).ty;
484 let len: u64 = match base_ty.kind() {
485 ty::Array(_, size) => size.eval_usize(self.builder.tcx, self.builder.param_env),
486 _ => bug!("from_end: false slice pattern of non-array type"),
487 };
488 for offset in from..to {
489 let elem =
490 ProjectionElem::ConstantIndex { offset, min_length: len, from_end: false };
491 let path =
492 self.add_move_path(base_path, elem, |tcx| tcx.mk_place_elem(base_place, elem));
493 self.record_move(place, path);
494 }
495 } else {
496 match self.move_path_for(place) {
497 Ok(path) | Err(MoveError::UnionMove { path }) => self.record_move(place, path),
498 Err(error @ MoveError::IllegalMove { .. }) => {
499 self.builder.errors.push((place, error));
500 }
501 };
502 }
503 }
504
record_move(&mut self, place: Place<'tcx>, path: MovePathIndex)505 fn record_move(&mut self, place: Place<'tcx>, path: MovePathIndex) {
506 let move_out = self.builder.data.moves.push(MoveOut { path, source: self.loc });
507 debug!(
508 "gather_move({:?}, {:?}): adding move {:?} of {:?}",
509 self.loc, place, move_out, path
510 );
511 self.builder.data.path_map[path].push(move_out);
512 self.builder.data.loc_map[self.loc].push(move_out);
513 }
514
gather_init(&mut self, place: PlaceRef<'tcx>, kind: InitKind)515 fn gather_init(&mut self, place: PlaceRef<'tcx>, kind: InitKind) {
516 debug!("gather_init({:?}, {:?})", self.loc, place);
517
518 let mut place = place;
519
520 // Check if we are assigning into a field of a union, if so, lookup the place
521 // of the union so it is marked as initialized again.
522 if let Some((place_base, ProjectionElem::Field(_, _))) = place.last_projection() {
523 if place_base.ty(self.builder.body, self.builder.tcx).ty.is_union() {
524 place = place_base;
525 }
526 }
527
528 if let LookupResult::Exact(path) = self.builder.data.rev_lookup.find(place) {
529 let init = self.builder.data.inits.push(Init {
530 location: InitLocation::Statement(self.loc),
531 path,
532 kind,
533 });
534
535 debug!(
536 "gather_init({:?}, {:?}): adding init {:?} of {:?}",
537 self.loc, place, init, path
538 );
539
540 self.builder.data.init_path_map[path].push(init);
541 self.builder.data.init_loc_map[self.loc].push(init);
542 }
543 }
544 }
545