1 use rustc::mir::tcx::RvalueInitializationState;
3 use rustc::ty::{self, TyCtxt};
4 use rustc_data_structures::indexed_vec::IndexVec;
5 use smallvec::{smallvec, SmallVec};
7 use std::collections::hash_map::Entry;
10 use super::abs_domain::Lift;
11 use super::IllegalMoveOriginKind::*;
12 use super::{Init, InitIndex, InitKind, InitLocation, LookupResult, MoveError};
14 LocationMap, MoveData, MoveOut, MoveOutIndex, MovePath, MovePathIndex, MovePathLookup,
17 struct MoveDataBuilder<'a, 'tcx> {
21 errors: Vec<(Place<'tcx>, MoveError<'tcx>)>,
24 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
25 fn new(body: &'a Body<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
26 let mut move_paths = IndexVec::new();
27 let mut path_map = IndexVec::new();
28 let mut init_path_map = IndexVec::new();
35 moves: IndexVec::new(),
36 loc_map: LocationMap::new(body),
37 rev_lookup: MovePathLookup {
51 projections: Default::default(),
55 inits: IndexVec::new(),
56 init_loc_map: LocationMap::new(body),
63 move_paths: &mut IndexVec<MovePathIndex, MovePath<'tcx>>,
64 path_map: &mut IndexVec<MovePathIndex, SmallVec<[MoveOutIndex; 4]>>,
65 init_path_map: &mut IndexVec<MovePathIndex, SmallVec<[InitIndex; 4]>>,
66 parent: Option<MovePathIndex>,
70 move_paths.push(MovePath { next_sibling: None, first_child: None, parent, place });
72 if let Some(parent) = parent {
73 let next_sibling = mem::replace(&mut move_paths[parent].first_child, Some(move_path));
74 move_paths[move_path].next_sibling = next_sibling;
77 let path_map_ent = path_map.push(smallvec![]);
78 assert_eq!(path_map_ent, move_path);
80 let init_path_map_ent = init_path_map.push(smallvec![]);
81 assert_eq!(init_path_map_ent, move_path);
87 impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
88 /// This creates a MovePath for a given place, returning an `MovePathError`
89 /// if that place can't be moved from.
91 /// NOTE: places behind references *do not* get a move path, which is
92 /// problematic for borrowck.
94 /// Maybe we should have separate "borrowck" and "moveck" modes.
95 fn move_path_for(&mut self, place: &Place<'tcx>) -> Result<MovePathIndex, MoveError<'tcx>> {
96 debug!("lookup({:?})", place);
97 let mut base = match place.base {
98 PlaceBase::Local(local) => self.builder.data.rev_lookup.locals[local],
99 PlaceBase::Static(..) => {
100 return Err(MoveError::cannot_move_out_of(self.loc, Static));
104 for (i, elem) in place.projection.iter().enumerate() {
105 let proj_base = &place.projection[..i];
106 let body = self.builder.body;
107 let tcx = self.builder.tcx;
108 let place_ty = Place::ty_from(&place.base, proj_base, body, tcx).ty;
110 ty::Ref(..) | ty::RawPtr(..) => {
111 let proj = &place.projection[..i+1];
112 return Err(MoveError::cannot_move_out_of(
115 target_place: Place {
116 base: place.base.clone(),
117 projection: proj.to_vec().into_boxed_slice(),
122 ty::Adt(adt, _) if adt.has_dtor(tcx) && !adt.is_box() => {
123 return Err(MoveError::cannot_move_out_of(
125 InteriorOfTypeWithDestructor { container_ty: place_ty },
128 // move out of union - always move the entire union
129 ty::Adt(adt, _) if adt.is_union() => {
130 return Err(MoveError::UnionMove { path: base });
133 return Err(MoveError::cannot_move_out_of(
135 InteriorOfSliceOrArray {
137 is_index: match elem {
138 ProjectionElem::Index(..) => true,
144 ty::Array(..) => match elem {
145 ProjectionElem::Index(..) => {
146 return Err(MoveError::cannot_move_out_of(
148 InteriorOfSliceOrArray { ty: place_ty, is_index: true },
152 // FIXME: still badly broken
158 let proj = &place.projection[..i+1];
164 .entry((base, elem.lift()))
166 Entry::Occupied(ent) => *ent.get(),
167 Entry::Vacant(ent) => {
168 let path = MoveDataBuilder::new_move_path(
169 &mut self.builder.data.move_paths,
170 &mut self.builder.data.path_map,
171 &mut self.builder.data.init_path_map,
174 base: place.base.clone(),
175 projection: proj.to_vec().into_boxed_slice(),
187 fn create_move_path(&mut self, place: &Place<'tcx>) {
188 // This is an non-moving access (such as an overwrite or
189 // drop), so this not being a valid move path is OK.
190 let _ = self.move_path_for(place);
194 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
197 ) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
199 debug!("moves for {:?}:", self.body.span);
200 for (j, mo) in self.data.moves.iter_enumerated() {
201 debug!(" {:?} = {:?}", j, mo);
203 debug!("move paths for {:?}:", self.body.span);
204 for (j, path) in self.data.move_paths.iter_enumerated() {
205 debug!(" {:?} = {:?}", j, path);
210 if !self.errors.is_empty() { Err((self.data, self.errors)) } else { Ok(self.data) }
214 pub(super) fn gather_moves<'tcx>(
217 ) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
218 let mut builder = MoveDataBuilder::new(body, tcx);
220 builder.gather_args();
222 for (bb, block) in body.basic_blocks().iter_enumerated() {
223 for (i, stmt) in block.statements.iter().enumerate() {
224 let source = Location { block: bb, statement_index: i };
225 builder.gather_statement(source, stmt);
228 let terminator_loc = Location { block: bb, statement_index: block.statements.len() };
229 builder.gather_terminator(terminator_loc, block.terminator());
235 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
236 fn gather_args(&mut self) {
237 for arg in self.body.args_iter() {
238 let path = self.data.rev_lookup.locals[arg];
240 let init = self.data.inits.push(Init {
242 kind: InitKind::Deep,
243 location: InitLocation::Argument(arg),
246 debug!("gather_args: adding init {:?} of {:?} for argument {:?}", init, path, arg);
248 self.data.init_path_map[path].push(init);
252 fn gather_statement(&mut self, loc: Location, stmt: &Statement<'tcx>) {
253 debug!("gather_statement({:?}, {:?})", loc, stmt);
254 (Gatherer { builder: self, loc }).gather_statement(stmt);
257 fn gather_terminator(&mut self, loc: Location, term: &Terminator<'tcx>) {
258 debug!("gather_terminator({:?}, {:?})", loc, term);
259 (Gatherer { builder: self, loc }).gather_terminator(term);
263 struct Gatherer<'b, 'a, 'tcx> {
264 builder: &'b mut MoveDataBuilder<'a, 'tcx>,
268 impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
269 fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
271 StatementKind::Assign(ref place, ref rval) => {
272 self.create_move_path(place);
273 if let RvalueInitializationState::Shallow = rval.initialization_state() {
274 // Box starts out uninitialized - need to create a separate
275 // move-path for the interior so it will be separate from
277 self.create_move_path(&place.clone().deref());
278 self.gather_init(place.as_ref(), InitKind::Shallow);
280 self.gather_init(place.as_ref(), InitKind::Deep);
282 self.gather_rvalue(rval);
284 StatementKind::FakeRead(_, ref place) => {
285 self.create_move_path(place);
287 StatementKind::InlineAsm(ref asm) => {
288 for (output, kind) in asm.outputs.iter().zip(&asm.asm.outputs) {
289 if !kind.is_indirect {
290 self.gather_init(output.as_ref(), InitKind::Deep);
293 for (_, input) in asm.inputs.iter() {
294 self.gather_operand(input);
297 StatementKind::StorageLive(_) => {}
298 StatementKind::StorageDead(local) => {
299 self.gather_move(&Place::from(local));
301 StatementKind::SetDiscriminant { .. } => {
303 stmt.source_info.span,
304 "SetDiscriminant should not exist during borrowck"
307 StatementKind::Retag { .. }
308 | StatementKind::AscribeUserType(..)
309 | StatementKind::Nop => {}
313 fn gather_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
315 Rvalue::Use(ref operand)
316 | Rvalue::Repeat(ref operand, _)
317 | Rvalue::Cast(_, ref operand, _)
318 | Rvalue::UnaryOp(_, ref operand) => self.gather_operand(operand),
319 Rvalue::BinaryOp(ref _binop, ref lhs, ref rhs)
320 | Rvalue::CheckedBinaryOp(ref _binop, ref lhs, ref rhs) => {
321 self.gather_operand(lhs);
322 self.gather_operand(rhs);
324 Rvalue::Aggregate(ref _kind, ref operands) => {
325 for operand in operands {
326 self.gather_operand(operand);
330 | Rvalue::Discriminant(..)
332 | Rvalue::NullaryOp(NullOp::SizeOf, _)
333 | Rvalue::NullaryOp(NullOp::Box, _) => {
334 // This returns an rvalue with uninitialized contents. We can't
335 // move out of it here because it is an rvalue - assignments always
336 // completely initialize their place.
338 // However, this does not matter - MIR building is careful to
339 // only emit a shallow free for the partially-initialized
342 // In any case, if we want to fix this, we have to register a
343 // special move and change the `statement_effect` functions.
348 fn gather_terminator(&mut self, term: &Terminator<'tcx>) {
350 TerminatorKind::Goto { target: _ }
351 | TerminatorKind::Resume
352 | TerminatorKind::Abort
353 | TerminatorKind::GeneratorDrop
354 | TerminatorKind::FalseEdges { .. }
355 | TerminatorKind::FalseUnwind { .. }
356 | TerminatorKind::Unreachable => {}
358 TerminatorKind::Return => {
359 self.gather_move(&Place::return_place());
362 TerminatorKind::Assert { ref cond, .. } => {
363 self.gather_operand(cond);
366 TerminatorKind::SwitchInt { ref discr, .. } => {
367 self.gather_operand(discr);
370 TerminatorKind::Yield { ref value, .. } => {
371 self.gather_operand(value);
374 TerminatorKind::Drop { ref location, target: _, unwind: _ } => {
375 self.gather_move(location);
377 TerminatorKind::DropAndReplace { ref location, ref value, .. } => {
378 self.create_move_path(location);
379 self.gather_operand(value);
380 self.gather_init(location.as_ref(), InitKind::Deep);
382 TerminatorKind::Call {
389 self.gather_operand(func);
391 self.gather_operand(arg);
393 if let Some((ref destination, _bb)) = *destination {
394 self.create_move_path(destination);
395 self.gather_init(destination.as_ref(), InitKind::NonPanicPathOnly);
401 fn gather_operand(&mut self, operand: &Operand<'tcx>) {
403 Operand::Constant(..) | Operand::Copy(..) => {} // not-a-move
404 Operand::Move(ref place) => {
406 self.gather_move(place);
411 fn gather_move(&mut self, place: &Place<'tcx>) {
412 debug!("gather_move({:?}, {:?})", self.loc, place);
414 let path = match self.move_path_for(place) {
415 Ok(path) | Err(MoveError::UnionMove { path }) => path,
416 Err(error @ MoveError::IllegalMove { .. }) => {
417 self.builder.errors.push((place.clone(), error));
421 let move_out = self.builder.data.moves.push(MoveOut { path: path, source: self.loc });
424 "gather_move({:?}, {:?}): adding move {:?} of {:?}",
425 self.loc, place, move_out, path
428 self.builder.data.path_map[path].push(move_out);
429 self.builder.data.loc_map[self.loc].push(move_out);
432 fn gather_init(&mut self, place: PlaceRef<'cx, 'tcx>, kind: InitKind) {
433 debug!("gather_init({:?}, {:?})", self.loc, place);
435 let mut place = place;
437 // Check if we are assigning into a field of a union, if so, lookup the place
438 // of the union so it is marked as initialized again.
439 if let [proj_base @ .., ProjectionElem::Field(_, _)] = place.projection {
440 if let ty::Adt(def, _) =
441 Place::ty_from(place.base, proj_base, self.builder.body, self.builder.tcx).ty.sty
444 place = PlaceRef { base: place.base, projection: proj_base }
449 if let LookupResult::Exact(path) = self.builder.data.rev_lookup.find(place) {
450 let init = self.builder.data.inits.push(Init {
451 location: InitLocation::Statement(self.loc),
457 "gather_init({:?}, {:?}): adding init {:?} of {:?}",
458 self.loc, place, init, path
461 self.builder.data.init_path_map[path].push(init);
462 self.builder.data.init_loc_map[self.loc].push(init);