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