1 // Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use rustc::ty::{self, TyCtxt, ParameterEnvironment};
14 use rustc::util::nodemap::FxHashMap;
15 use rustc_data_structures::indexed_vec::{IndexVec};
17 use syntax::codemap::DUMMY_SP;
19 use std::collections::hash_map::Entry;
22 use std::ops::{Index, IndexMut};
24 use super::abs_domain::{AbstractElem, Lift};
26 // This submodule holds some newtype'd Index wrappers that are using
27 // NonZero to ensure that Option<Index> occupies only a single word.
28 // They are in a submodule to impose privacy restrictions; namely, to
29 // ensure that other code does not accidentally access `index.0`
30 // (which is likely to yield a subtle off-by-one error).
33 use core::nonzero::NonZero;
34 use rustc_data_structures::indexed_vec::Idx;
36 macro_rules! new_index {
37 ($Index:ident, $debug_name:expr) => {
38 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
39 pub struct $Index(NonZero<usize>);
42 fn new(idx: usize) -> Self {
43 unsafe { $Index(NonZero::new(idx + 1)) }
45 fn index(self) -> usize {
50 impl fmt::Debug for $Index {
51 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
52 write!(fmt, "{}{}", $debug_name, self.index())
58 /// Index into MovePathData.move_paths
59 new_index!(MovePathIndex, "mp");
61 /// Index into MoveData.moves.
62 new_index!(MoveOutIndex, "mo");
65 pub use self::indexes::MovePathIndex;
66 pub use self::indexes::MoveOutIndex;
68 impl self::indexes::MoveOutIndex {
69 pub fn move_path_index(&self, move_data: &MoveData) -> MovePathIndex {
70 move_data.moves[*self].path
74 /// `MovePath` is a canonicalized representation of a path that is
75 /// moved or assigned to.
77 /// It follows a tree structure.
79 /// Given `struct X { m: M, n: N }` and `x: X`, moves like `drop x.m;`
80 /// move *out* of the l-value `x.m`.
82 /// The MovePaths representing `x.m` and `x.n` are siblings (that is,
83 /// one of them will link to the other via the `next_sibling` field,
84 /// and the other will have no entry in its `next_sibling` field), and
85 /// they both have the MovePath representing `x` as their parent.
87 pub struct MovePath<'tcx> {
88 pub next_sibling: Option<MovePathIndex>,
89 pub first_child: Option<MovePathIndex>,
90 pub parent: Option<MovePathIndex>,
91 pub lvalue: Lvalue<'tcx>,
94 impl<'tcx> fmt::Debug for MovePath<'tcx> {
95 fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
96 write!(w, "MovePath {{")?;
97 if let Some(parent) = self.parent {
98 write!(w, " parent: {:?},", parent)?;
100 if let Some(first_child) = self.first_child {
101 write!(w, " first_child: {:?},", first_child)?;
103 if let Some(next_sibling) = self.next_sibling {
104 write!(w, " next_sibling: {:?}", next_sibling)?;
106 write!(w, " lvalue: {:?} }}", self.lvalue)
111 pub struct MoveData<'tcx> {
112 pub move_paths: IndexVec<MovePathIndex, MovePath<'tcx>>,
113 pub moves: IndexVec<MoveOutIndex, MoveOut>,
114 /// Each Location `l` is mapped to the MoveOut's that are effects
115 /// of executing the code at `l`. (There can be multiple MoveOut's
116 /// for a given `l` because each MoveOut is associated with one
117 /// particular path being moved.)
118 pub loc_map: LocationMap<Vec<MoveOutIndex>>,
119 pub path_map: IndexVec<MovePathIndex, Vec<MoveOutIndex>>,
120 pub rev_lookup: MovePathLookup<'tcx>,
123 pub trait HasMoveData<'tcx> {
124 fn move_data(&self) -> &MoveData<'tcx>;
128 pub struct LocationMap<T> {
129 /// Location-indexed (BasicBlock for outer index, index within BB
130 /// for inner index) map.
131 map: IndexVec<BasicBlock, Vec<T>>,
134 impl<T> Index<Location> for LocationMap<T> {
136 fn index(&self, index: Location) -> &Self::Output {
137 &self.map[index.block][index.statement_index]
141 impl<T> IndexMut<Location> for LocationMap<T> {
142 fn index_mut(&mut self, index: Location) -> &mut Self::Output {
143 &mut self.map[index.block][index.statement_index]
147 impl<T> LocationMap<T> where T: Default + Clone {
148 fn new(mir: &Mir) -> Self {
150 map: mir.basic_blocks().iter().map(|block| {
151 vec![T::default(); block.statements.len()+1]
157 /// `MoveOut` represents a point in a program that moves out of some
158 /// L-value; i.e., "creates" uninitialized memory.
160 /// With respect to dataflow analysis:
161 /// - Generated by moves and declaration of uninitialized variables.
162 /// - Killed by assignments to the memory.
163 #[derive(Copy, Clone)]
166 pub path: MovePathIndex,
168 pub source: Location,
171 impl fmt::Debug for MoveOut {
172 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
173 write!(fmt, "{:?}@{:?}", self.path, self.source)
177 /// Tables mapping from an l-value to its MovePathIndex.
179 pub struct MovePathLookup<'tcx> {
180 locals: IndexVec<Local, MovePathIndex>,
182 /// projections are made from a base-lvalue and a projection
183 /// elem. The base-lvalue will have a unique MovePathIndex; we use
184 /// the latter as the index into the outer vector (narrowing
185 /// subsequent search so that it is solely relative to that
186 /// base-lvalue). For the remaining lookup, we map the projection
187 /// elem to the associated MovePathIndex.
188 projections: FxHashMap<(MovePathIndex, AbstractElem<'tcx>), MovePathIndex>
191 struct MoveDataBuilder<'a, 'tcx: 'a> {
193 tcx: TyCtxt<'a, 'tcx, 'tcx>,
194 param_env: &'a ParameterEnvironment<'tcx>,
195 data: MoveData<'tcx>,
198 pub enum MovePathError {
200 UnionMove { path: MovePathIndex },
203 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
204 fn new(mir: &'a Mir<'tcx>,
205 tcx: TyCtxt<'a, 'tcx, 'tcx>,
206 param_env: &'a ParameterEnvironment<'tcx>)
208 let mut move_paths = IndexVec::new();
209 let mut path_map = IndexVec::new();
214 param_env: param_env,
216 moves: IndexVec::new(),
217 loc_map: LocationMap::new(mir),
218 rev_lookup: MovePathLookup {
219 locals: mir.local_decls.indices().map(Lvalue::Local).map(|v| {
220 Self::new_move_path(&mut move_paths, &mut path_map, None, v)
222 projections: FxHashMap(),
224 move_paths: move_paths,
230 fn new_move_path(move_paths: &mut IndexVec<MovePathIndex, MovePath<'tcx>>,
231 path_map: &mut IndexVec<MovePathIndex, Vec<MoveOutIndex>>,
232 parent: Option<MovePathIndex>,
233 lvalue: Lvalue<'tcx>)
236 let move_path = move_paths.push(MovePath {
243 if let Some(parent) = parent {
245 mem::replace(&mut move_paths[parent].first_child, Some(move_path));
246 move_paths[move_path].next_sibling = next_sibling;
249 let path_map_ent = path_map.push(vec![]);
250 assert_eq!(path_map_ent, move_path);
254 /// This creates a MovePath for a given lvalue, returning an `MovePathError`
255 /// if that lvalue can't be moved from.
257 /// NOTE: lvalues behind references *do not* get a move path, which is
258 /// problematic for borrowck.
260 /// Maybe we should have seperate "borrowck" and "moveck" modes.
261 fn move_path_for(&mut self, lval: &Lvalue<'tcx>)
262 -> Result<MovePathIndex, MovePathError>
264 debug!("lookup({:?})", lval);
266 Lvalue::Local(local) => Ok(self.data.rev_lookup.locals[local]),
267 // error: can't move out of a static
268 Lvalue::Static(..) => Err(MovePathError::IllegalMove),
269 Lvalue::Projection(ref proj) => {
270 self.move_path_for_projection(lval, proj)
275 fn create_move_path(&mut self, lval: &Lvalue<'tcx>) {
276 // This is an assignment, not a move, so this not being a valid
278 let _ = self.move_path_for(lval);
281 fn move_path_for_projection(&mut self,
283 proj: &LvalueProjection<'tcx>)
284 -> Result<MovePathIndex, MovePathError>
286 let base = try!(self.move_path_for(&proj.base));
287 let lv_ty = proj.base.ty(self.mir, self.tcx).to_ty(self.tcx);
289 // error: can't move out of borrowed content
290 ty::TyRef(..) | ty::TyRawPtr(..) => return Err(MovePathError::IllegalMove),
291 // error: can't move out of struct with destructor
292 ty::TyAdt(adt, _) if adt.has_dtor(self.tcx) && !adt.is_box() =>
293 return Err(MovePathError::IllegalMove),
294 // move out of union - always move the entire union
295 ty::TyAdt(adt, _) if adt.is_union() =>
296 return Err(MovePathError::UnionMove { path: base }),
297 // error: can't move out of a slice
299 return Err(MovePathError::IllegalMove),
300 ty::TyArray(..) => match proj.elem {
301 // error: can't move out of an array
302 ProjectionElem::Index(..) => return Err(MovePathError::IllegalMove),
304 // FIXME: still badly broken
309 match self.data.rev_lookup.projections.entry((base, proj.elem.lift())) {
310 Entry::Occupied(ent) => Ok(*ent.get()),
311 Entry::Vacant(ent) => {
312 let path = Self::new_move_path(
313 &mut self.data.move_paths,
314 &mut self.data.path_map,
324 fn finalize(self) -> MoveData<'tcx> {
326 debug!("moves for {:?}:", self.mir.span);
327 for (j, mo) in self.data.moves.iter_enumerated() {
328 debug!(" {:?} = {:?}", j, mo);
330 debug!("move paths for {:?}:", self.mir.span);
331 for (j, path) in self.data.move_paths.iter_enumerated() {
332 debug!(" {:?} = {:?}", j, path);
340 #[derive(Copy, Clone, Debug)]
341 pub enum LookupResult {
342 Exact(MovePathIndex),
343 Parent(Option<MovePathIndex>)
346 impl<'tcx> MovePathLookup<'tcx> {
347 // Unlike the builder `fn move_path_for` below, this lookup
348 // alternative will *not* create a MovePath on the fly for an
349 // unknown l-value, but will rather return the nearest available
351 pub fn find(&self, lval: &Lvalue<'tcx>) -> LookupResult {
353 Lvalue::Local(local) => LookupResult::Exact(self.locals[local]),
354 Lvalue::Static(..) => LookupResult::Parent(None),
355 Lvalue::Projection(ref proj) => {
356 match self.find(&proj.base) {
357 LookupResult::Exact(base_path) => {
358 match self.projections.get(&(base_path, proj.elem.lift())) {
359 Some(&subpath) => LookupResult::Exact(subpath),
360 None => LookupResult::Parent(Some(base_path))
370 impl<'a, 'tcx> MoveData<'tcx> {
371 pub fn gather_moves(mir: &Mir<'tcx>,
372 tcx: TyCtxt<'a, 'tcx, 'tcx>,
373 param_env: &ParameterEnvironment<'tcx>)
375 gather_moves(mir, tcx, param_env)
379 fn gather_moves<'a, 'tcx>(mir: &Mir<'tcx>,
380 tcx: TyCtxt<'a, 'tcx, 'tcx>,
381 param_env: &ParameterEnvironment<'tcx>)
383 let mut builder = MoveDataBuilder::new(mir, tcx, param_env);
385 for (bb, block) in mir.basic_blocks().iter_enumerated() {
386 for (i, stmt) in block.statements.iter().enumerate() {
387 let source = Location { block: bb, statement_index: i };
388 builder.gather_statement(source, stmt);
391 let terminator_loc = Location {
393 statement_index: block.statements.len()
395 builder.gather_terminator(terminator_loc, block.terminator());
401 impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
402 fn gather_statement(&mut self, loc: Location, stmt: &Statement<'tcx>) {
403 debug!("gather_statement({:?}, {:?})", loc, stmt);
405 StatementKind::Assign(ref lval, ref rval) => {
406 self.create_move_path(lval);
407 self.gather_rvalue(loc, rval);
409 StatementKind::StorageLive(_) |
410 StatementKind::StorageDead(_) => {}
411 StatementKind::SetDiscriminant{ .. } => {
412 span_bug!(stmt.source_info.span,
413 "SetDiscriminant should not exist during borrowck");
415 StatementKind::InlineAsm { .. } |
416 StatementKind::Nop => {}
420 fn gather_rvalue(&mut self, loc: Location, rvalue: &Rvalue<'tcx>) {
422 Rvalue::Use(ref operand) |
423 Rvalue::Repeat(ref operand, _) |
424 Rvalue::Cast(_, ref operand, _) |
425 Rvalue::UnaryOp(_, ref operand) => {
426 self.gather_operand(loc, operand)
428 Rvalue::BinaryOp(ref _binop, ref lhs, ref rhs) |
429 Rvalue::CheckedBinaryOp(ref _binop, ref lhs, ref rhs) => {
430 self.gather_operand(loc, lhs);
431 self.gather_operand(loc, rhs);
433 Rvalue::Aggregate(ref _kind, ref operands) => {
434 for operand in operands {
435 self.gather_operand(loc, operand);
439 Rvalue::Discriminant(..) |
442 // This returns an rvalue with uninitialized contents. We can't
443 // move out of it here because it is an rvalue - assignments always
444 // completely initialize their lvalue.
446 // However, this does not matter - MIR building is careful to
447 // only emit a shallow free for the partially-initialized
450 // In any case, if we want to fix this, we have to register a
451 // special move and change the `statement_effect` functions.
456 fn gather_terminator(&mut self, loc: Location, term: &Terminator<'tcx>) {
457 debug!("gather_terminator({:?}, {:?})", loc, term);
459 TerminatorKind::Goto { target: _ } |
460 TerminatorKind::Resume |
461 TerminatorKind::Unreachable => { }
463 TerminatorKind::Return => {
464 self.gather_move(loc, &Lvalue::Local(RETURN_POINTER));
467 TerminatorKind::Assert { .. } |
468 TerminatorKind::SwitchInt { .. } => {
469 // branching terminators - these don't move anything
472 TerminatorKind::Drop { ref location, target: _, unwind: _ } => {
473 self.gather_move(loc, location);
475 TerminatorKind::DropAndReplace { ref location, ref value, .. } => {
476 self.create_move_path(location);
477 self.gather_operand(loc, value);
479 TerminatorKind::Call { ref func, ref args, ref destination, cleanup: _ } => {
480 self.gather_operand(loc, func);
482 self.gather_operand(loc, arg);
484 if let Some((ref destination, _bb)) = *destination {
485 self.create_move_path(destination);
491 fn gather_operand(&mut self, loc: Location, operand: &Operand<'tcx>) {
493 Operand::Constant(..) => {} // not-a-move
494 Operand::Consume(ref lval) => { // a move
495 self.gather_move(loc, lval);
500 fn gather_move(&mut self, loc: Location, lval: &Lvalue<'tcx>) {
501 debug!("gather_move({:?}, {:?})", loc, lval);
503 let lv_ty = lval.ty(self.mir, self.tcx).to_ty(self.tcx);
504 if !lv_ty.moves_by_default(self.tcx, self.param_env, DUMMY_SP) {
505 debug!("gather_move({:?}, {:?}) - {:?} is Copy. skipping", loc, lval, lv_ty);
509 let path = match self.move_path_for(lval) {
510 Ok(path) | Err(MovePathError::UnionMove { path }) => path,
511 Err(MovePathError::IllegalMove) => {
512 // Moving out of a bad path. Eventually, this should be a MIR
513 // borrowck error instead of a bug.
514 span_bug!(self.mir.span,
515 "Broken MIR: moving out of lvalue {:?}: {:?} at {:?}",
519 let move_out = self.data.moves.push(MoveOut { path: path, source: loc });
521 debug!("gather_move({:?}, {:?}): adding move {:?} of {:?}",
522 loc, lval, move_out, path);
524 self.data.path_map[path].push(move_out);
525 self.data.loc_map[loc].push(move_out);