2020-03-29 17:19:48 +02:00
|
|
|
use rustc_index::vec::IndexVec;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::tcx::RvalueInitializationState;
|
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2019-08-19 15:56:16 +02:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2017-07-04 15:57:55 +02:00
|
|
|
|
2019-11-22 22:03:25 +00:00
|
|
|
use std::convert::TryInto;
|
2017-07-04 15:57:55 +02:00
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
use super::abs_domain::Lift;
|
2017-10-04 11:02:26 +02:00
|
|
|
use super::IllegalMoveOriginKind::*;
|
2019-08-19 15:56:16 +02:00
|
|
|
use super::{Init, InitIndex, InitKind, InitLocation, LookupResult, MoveError};
|
|
|
|
use super::{
|
|
|
|
LocationMap, MoveData, MoveOut, MoveOutIndex, MovePath, MovePathIndex, MovePathLookup,
|
|
|
|
};
|
2017-07-04 15:57:55 +02:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
struct MoveDataBuilder<'a, 'tcx> {
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &'a Body<'tcx>,
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-11-22 22:03:25 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2017-07-04 15:57:55 +02:00
|
|
|
data: MoveData<'tcx>,
|
2018-08-07 17:06:21 +02:00
|
|
|
errors: Vec<(Place<'tcx>, MoveError<'tcx>)>,
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
|
2019-11-22 22:03:25 +00:00
|
|
|
fn new(body: &'a Body<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
|
2017-07-04 15:57:55 +02:00
|
|
|
let mut move_paths = IndexVec::new();
|
|
|
|
let mut path_map = IndexVec::new();
|
2017-11-27 08:06:36 +00:00
|
|
|
let mut init_path_map = IndexVec::new();
|
2017-07-04 15:57:55 +02:00
|
|
|
|
|
|
|
MoveDataBuilder {
|
2019-06-03 18:26:48 -04:00
|
|
|
body,
|
2017-07-04 15:57:55 +02:00
|
|
|
tcx,
|
2019-11-22 22:03:25 +00:00
|
|
|
param_env,
|
2017-10-04 11:02:26 +02:00
|
|
|
errors: Vec::new(),
|
2017-07-04 15:57:55 +02:00
|
|
|
data: MoveData {
|
|
|
|
moves: IndexVec::new(),
|
2019-06-03 18:26:48 -04:00
|
|
|
loc_map: LocationMap::new(body),
|
2017-07-04 15:57:55 +02:00
|
|
|
rev_lookup: MovePathLookup {
|
2019-08-19 15:56:16 +02:00
|
|
|
locals: body
|
|
|
|
.local_decls
|
|
|
|
.indices()
|
|
|
|
.map(|i| {
|
|
|
|
Self::new_move_path(
|
|
|
|
&mut move_paths,
|
|
|
|
&mut path_map,
|
|
|
|
&mut init_path_map,
|
|
|
|
None,
|
|
|
|
Place::from(i),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
2018-10-16 16:57:53 +02:00
|
|
|
projections: Default::default(),
|
2017-07-04 15:57:55 +02:00
|
|
|
},
|
|
|
|
move_paths,
|
|
|
|
path_map,
|
2017-11-27 08:06:36 +00:00
|
|
|
inits: IndexVec::new(),
|
2019-06-03 18:26:48 -04:00
|
|
|
init_loc_map: LocationMap::new(body),
|
2017-11-27 08:06:36 +00:00
|
|
|
init_path_map,
|
2019-08-19 15:56:16 +02:00
|
|
|
},
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:56:16 +02:00
|
|
|
fn 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>,
|
|
|
|
) -> MovePathIndex {
|
|
|
|
let move_path =
|
|
|
|
move_paths.push(MovePath { next_sibling: None, first_child: None, parent, place });
|
2017-07-04 15:57:55 +02:00
|
|
|
|
|
|
|
if let Some(parent) = parent {
|
2019-08-19 15:56:16 +02:00
|
|
|
let next_sibling = mem::replace(&mut move_paths[parent].first_child, Some(move_path));
|
2017-07-04 15:57:55 +02:00
|
|
|
move_paths[move_path].next_sibling = next_sibling;
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:34:36 +11:00
|
|
|
let path_map_ent = path_map.push(smallvec![]);
|
2017-07-04 15:57:55 +02:00
|
|
|
assert_eq!(path_map_ent, move_path);
|
2017-11-27 08:06:36 +00:00
|
|
|
|
2018-11-01 15:34:36 +11:00
|
|
|
let init_path_map_ent = init_path_map.push(smallvec![]);
|
2017-11-27 08:06:36 +00:00
|
|
|
assert_eq!(init_path_map_ent, move_path);
|
|
|
|
|
2017-07-04 15:57:55 +02:00
|
|
|
move_path
|
|
|
|
}
|
2017-10-04 12:34:35 +02:00
|
|
|
}
|
2017-07-04 15:57:55 +02:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
|
2017-12-01 14:39:51 +02:00
|
|
|
/// This creates a MovePath for a given place, returning an `MovePathError`
|
|
|
|
/// if that place can't be moved from.
|
2017-07-04 15:57:55 +02:00
|
|
|
///
|
2017-12-01 14:39:51 +02:00
|
|
|
/// NOTE: places behind references *do not* get a move path, which is
|
2017-07-04 15:57:55 +02:00
|
|
|
/// problematic for borrowck.
|
|
|
|
///
|
|
|
|
/// Maybe we should have separate "borrowck" and "moveck" modes.
|
2019-08-19 15:56:16 +02:00
|
|
|
fn move_path_for(&mut self, place: &Place<'tcx>) -> Result<MovePathIndex, MoveError<'tcx>> {
|
2017-12-01 14:39:51 +02:00
|
|
|
debug!("lookup({:?})", place);
|
2019-12-11 16:50:03 -03:00
|
|
|
let mut base = self.builder.data.rev_lookup.locals[place.local];
|
2019-05-24 00:52:10 +02:00
|
|
|
|
2019-12-15 11:08:43 +00:00
|
|
|
// The move path index of the first union that we find. Once this is
|
|
|
|
// some we stop creating child move paths, since moves from unions
|
|
|
|
// move the whole thing.
|
|
|
|
// We continue looking for other move errors though so that moving
|
|
|
|
// from `*(u.f: &_)` isn't allowed.
|
|
|
|
let mut union_path = None;
|
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
for (i, elem) in place.projection.iter().enumerate() {
|
|
|
|
let proj_base = &place.projection[..i];
|
|
|
|
let body = self.builder.body;
|
|
|
|
let tcx = self.builder.tcx;
|
2020-01-14 02:21:42 -03:00
|
|
|
let place_ty = Place::ty_from(place.local, proj_base, body, tcx).ty;
|
2019-09-16 19:08:35 +01:00
|
|
|
match place_ty.kind {
|
2019-07-30 00:07:28 +02:00
|
|
|
ty::Ref(..) | ty::RawPtr(..) => {
|
2019-12-22 17:42:04 -05:00
|
|
|
let proj = &place.projection[..i + 1];
|
2019-07-30 00:07:28 +02:00
|
|
|
return Err(MoveError::cannot_move_out_of(
|
|
|
|
self.loc,
|
|
|
|
BorrowedContent {
|
|
|
|
target_place: Place {
|
2019-12-11 16:50:03 -03:00
|
|
|
local: place.local,
|
2019-10-20 16:11:04 -04:00
|
|
|
projection: tcx.intern_place_elems(proj),
|
2019-08-19 15:56:16 +02:00
|
|
|
},
|
2019-07-30 00:07:28 +02:00
|
|
|
},
|
|
|
|
));
|
|
|
|
}
|
|
|
|
ty::Adt(adt, _) if adt.has_dtor(tcx) && !adt.is_box() => {
|
|
|
|
return Err(MoveError::cannot_move_out_of(
|
|
|
|
self.loc,
|
|
|
|
InteriorOfTypeWithDestructor { container_ty: place_ty },
|
|
|
|
));
|
|
|
|
}
|
|
|
|
ty::Adt(adt, _) if adt.is_union() => {
|
2019-12-15 11:08:43 +00:00
|
|
|
union_path.get_or_insert(base);
|
2019-07-30 00:07:28 +02:00
|
|
|
}
|
|
|
|
ty::Slice(_) => {
|
|
|
|
return Err(MoveError::cannot_move_out_of(
|
|
|
|
self.loc,
|
|
|
|
InteriorOfSliceOrArray {
|
|
|
|
ty: place_ty,
|
|
|
|
is_index: match elem {
|
|
|
|
ProjectionElem::Index(..) => true,
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
));
|
|
|
|
}
|
2020-03-22 13:36:56 +01:00
|
|
|
|
|
|
|
ty::Array(..) => {
|
|
|
|
if let ProjectionElem::Index(..) = elem {
|
2019-08-19 15:56:16 +02:00
|
|
|
return Err(MoveError::cannot_move_out_of(
|
|
|
|
self.loc,
|
2019-07-30 00:07:28 +02:00
|
|
|
InteriorOfSliceOrArray { ty: place_ty, is_index: true },
|
2019-08-19 15:56:16 +02:00
|
|
|
));
|
|
|
|
}
|
2020-03-22 13:36:56 +01:00
|
|
|
}
|
|
|
|
|
2019-07-30 00:07:28 +02:00
|
|
|
_ => {}
|
|
|
|
};
|
2019-05-24 00:52:10 +02:00
|
|
|
|
2019-12-15 11:08:43 +00:00
|
|
|
if union_path.is_none() {
|
2019-12-22 17:42:04 -05:00
|
|
|
base = self.add_move_path(base, elem, |tcx| Place {
|
2020-01-22 16:30:15 +01:00
|
|
|
local: place.local,
|
2019-12-22 17:42:04 -05:00
|
|
|
projection: tcx.intern_place_elems(&place.projection[..i + 1]),
|
2019-12-15 11:08:43 +00:00
|
|
|
});
|
|
|
|
}
|
2019-07-30 00:07:28 +02:00
|
|
|
}
|
2019-05-24 00:52:10 +02:00
|
|
|
|
2019-12-15 11:08:43 +00:00
|
|
|
if let Some(base) = union_path {
|
|
|
|
// Move out of union - always move the entire union.
|
|
|
|
Err(MoveError::UnionMove { path: base })
|
|
|
|
} else {
|
|
|
|
Ok(base)
|
|
|
|
}
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
|
2019-11-22 22:03:25 +00:00
|
|
|
fn add_move_path(
|
|
|
|
&mut self,
|
|
|
|
base: MovePathIndex,
|
|
|
|
elem: &PlaceElem<'tcx>,
|
|
|
|
mk_place: impl FnOnce(TyCtxt<'tcx>) -> Place<'tcx>,
|
|
|
|
) -> MovePathIndex {
|
|
|
|
let MoveDataBuilder {
|
|
|
|
data: MoveData { rev_lookup, move_paths, path_map, init_path_map, .. },
|
|
|
|
tcx,
|
|
|
|
..
|
|
|
|
} = self.builder;
|
2019-12-22 17:42:04 -05:00
|
|
|
*rev_lookup.projections.entry((base, elem.lift())).or_insert_with(move || {
|
2020-03-22 00:20:58 +01:00
|
|
|
MoveDataBuilder::new_move_path(
|
2019-12-22 17:42:04 -05:00
|
|
|
move_paths,
|
|
|
|
path_map,
|
|
|
|
init_path_map,
|
|
|
|
Some(base),
|
|
|
|
mk_place(*tcx),
|
2020-03-22 00:20:58 +01:00
|
|
|
)
|
2019-12-22 17:42:04 -05:00
|
|
|
})
|
2019-11-22 22:03:25 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
fn create_move_path(&mut self, place: &Place<'tcx>) {
|
2018-05-31 12:46:43 +02:00
|
|
|
// This is an non-moving access (such as an overwrite or
|
|
|
|
// drop), so this not being a valid move path is OK.
|
2017-12-01 14:39:51 +02:00
|
|
|
let _ = self.move_path_for(place);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
2017-10-04 12:34:35 +02:00
|
|
|
}
|
2017-07-04 15:57:55 +02:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
|
2018-08-07 17:06:21 +02:00
|
|
|
fn finalize(
|
2019-08-19 15:56:16 +02:00
|
|
|
self,
|
2018-08-07 17:06:21 +02:00
|
|
|
) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
|
2017-07-04 15:57:55 +02:00
|
|
|
debug!("{}", {
|
2019-06-03 18:26:48 -04:00
|
|
|
debug!("moves for {:?}:", self.body.span);
|
2017-07-04 15:57:55 +02:00
|
|
|
for (j, mo) in self.data.moves.iter_enumerated() {
|
|
|
|
debug!(" {:?} = {:?}", j, mo);
|
|
|
|
}
|
2019-06-03 18:26:48 -04:00
|
|
|
debug!("move paths for {:?}:", self.body.span);
|
2017-07-04 15:57:55 +02:00
|
|
|
for (j, path) in self.data.move_paths.iter_enumerated() {
|
|
|
|
debug!(" {:?} = {:?}", j, path);
|
|
|
|
}
|
|
|
|
"done dumping moves"
|
|
|
|
});
|
2017-10-04 11:02:26 +02:00
|
|
|
|
2019-08-19 15:56:16 +02:00
|
|
|
if !self.errors.is_empty() { Err((self.data, self.errors)) } else { Ok(self.data) }
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub(super) fn gather_moves<'tcx>(
|
2019-06-03 18:26:48 -04:00
|
|
|
body: &Body<'tcx>,
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-11-22 22:03:25 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2018-08-07 17:06:21 +02:00
|
|
|
) -> Result<MoveData<'tcx>, (MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>)> {
|
2019-11-22 22:03:25 +00:00
|
|
|
let mut builder = MoveDataBuilder::new(body, tcx, param_env);
|
2017-07-04 15:57:55 +02:00
|
|
|
|
2017-11-27 08:06:36 +00:00
|
|
|
builder.gather_args();
|
|
|
|
|
2019-06-03 18:26:48 -04:00
|
|
|
for (bb, block) in body.basic_blocks().iter_enumerated() {
|
2017-07-04 15:57:55 +02:00
|
|
|
for (i, stmt) in block.statements.iter().enumerate() {
|
|
|
|
let source = Location { block: bb, statement_index: i };
|
|
|
|
builder.gather_statement(source, stmt);
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:56:16 +02:00
|
|
|
let terminator_loc = Location { block: bb, statement_index: block.statements.len() };
|
2017-07-04 15:57:55 +02:00
|
|
|
builder.gather_terminator(terminator_loc, block.terminator());
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.finalize()
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
|
2017-11-27 08:06:36 +00:00
|
|
|
fn gather_args(&mut self) {
|
2019-06-03 18:26:48 -04:00
|
|
|
for arg in self.body.args_iter() {
|
2017-11-27 08:06:36 +00:00
|
|
|
let path = self.data.rev_lookup.locals[arg];
|
|
|
|
|
|
|
|
let init = self.data.inits.push(Init {
|
2019-08-19 15:56:16 +02:00
|
|
|
path,
|
|
|
|
kind: InitKind::Deep,
|
|
|
|
location: InitLocation::Argument(arg),
|
2017-11-27 08:06:36 +00:00
|
|
|
});
|
|
|
|
|
2019-08-19 15:56:16 +02:00
|
|
|
debug!("gather_args: adding init {:?} of {:?} for argument {:?}", init, path, arg);
|
2017-11-27 08:06:36 +00:00
|
|
|
|
|
|
|
self.data.init_path_map[path].push(init);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-04 15:57:55 +02:00
|
|
|
fn gather_statement(&mut self, loc: Location, stmt: &Statement<'tcx>) {
|
|
|
|
debug!("gather_statement({:?}, {:?})", loc, stmt);
|
2017-10-04 12:34:35 +02:00
|
|
|
(Gatherer { builder: self, loc }).gather_statement(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gather_terminator(&mut self, loc: Location, term: &Terminator<'tcx>) {
|
|
|
|
debug!("gather_terminator({:?}, {:?})", loc, term);
|
|
|
|
(Gatherer { builder: self, loc }).gather_terminator(term);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
struct Gatherer<'b, 'a, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
builder: &'b mut MoveDataBuilder<'a, 'tcx>,
|
2017-10-04 12:34:35 +02:00
|
|
|
loc: Location,
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
|
2017-10-04 12:34:35 +02:00
|
|
|
fn gather_statement(&mut self, stmt: &Statement<'tcx>) {
|
2017-07-04 15:57:55 +02:00
|
|
|
match stmt.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
StatementKind::Assign(box (ref place, ref rval)) => {
|
2017-12-01 14:39:51 +02:00
|
|
|
self.create_move_path(place);
|
2017-07-04 15:57:55 +02:00
|
|
|
if let RvalueInitializationState::Shallow = rval.initialization_state() {
|
|
|
|
// Box starts out uninitialized - need to create a separate
|
|
|
|
// move-path for the interior so it will be separate from
|
|
|
|
// the exterior.
|
2019-10-20 21:04:59 -04:00
|
|
|
self.create_move_path(&self.builder.tcx.mk_place_deref(place.clone()));
|
2019-07-21 22:38:30 +02:00
|
|
|
self.gather_init(place.as_ref(), InitKind::Shallow);
|
2017-11-27 08:06:36 +00:00
|
|
|
} else {
|
2019-07-21 22:38:30 +02:00
|
|
|
self.gather_init(place.as_ref(), InitKind::Deep);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
2017-10-04 12:34:35 +02:00
|
|
|
self.gather_rvalue(rval);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
2018-09-14 21:05:31 +02:00
|
|
|
StatementKind::FakeRead(_, ref place) => {
|
2018-05-04 12:04:33 +02:00
|
|
|
self.create_move_path(place);
|
|
|
|
}
|
2020-01-14 13:40:42 +00:00
|
|
|
StatementKind::LlvmInlineAsm(ref asm) => {
|
2019-04-02 20:07:09 +11:00
|
|
|
for (output, kind) in asm.outputs.iter().zip(&asm.asm.outputs) {
|
2017-12-23 23:45:07 +00:00
|
|
|
if !kind.is_indirect {
|
2019-07-21 22:38:30 +02:00
|
|
|
self.gather_init(output.as_ref(), InitKind::Deep);
|
2017-12-23 23:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 20:07:09 +11:00
|
|
|
for (_, input) in asm.inputs.iter() {
|
2017-12-23 23:45:07 +00:00
|
|
|
self.gather_operand(input);
|
|
|
|
}
|
|
|
|
}
|
2017-11-10 14:11:25 +03:00
|
|
|
StatementKind::StorageLive(_) => {}
|
|
|
|
StatementKind::StorageDead(local) => {
|
2019-06-24 17:46:09 +02:00
|
|
|
self.gather_move(&Place::from(local));
|
2017-11-10 14:11:25 +03:00
|
|
|
}
|
2019-08-19 15:56:16 +02:00
|
|
|
StatementKind::SetDiscriminant { .. } => {
|
|
|
|
span_bug!(
|
|
|
|
stmt.source_info.span,
|
|
|
|
"SetDiscriminant should not exist during borrowck"
|
|
|
|
);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
2019-08-19 15:56:16 +02:00
|
|
|
StatementKind::Retag { .. }
|
|
|
|
| StatementKind::AscribeUserType(..)
|
|
|
|
| StatementKind::Nop => {}
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 12:34:35 +02:00
|
|
|
fn gather_rvalue(&mut self, rvalue: &Rvalue<'tcx>) {
|
2017-07-04 15:57:55 +02:00
|
|
|
match *rvalue {
|
2019-08-19 15:56:16 +02:00
|
|
|
Rvalue::Use(ref operand)
|
|
|
|
| Rvalue::Repeat(ref operand, _)
|
|
|
|
| Rvalue::Cast(_, ref operand, _)
|
|
|
|
| Rvalue::UnaryOp(_, ref operand) => self.gather_operand(operand),
|
|
|
|
Rvalue::BinaryOp(ref _binop, ref lhs, ref rhs)
|
|
|
|
| Rvalue::CheckedBinaryOp(ref _binop, ref lhs, ref rhs) => {
|
2017-10-04 12:34:35 +02:00
|
|
|
self.gather_operand(lhs);
|
|
|
|
self.gather_operand(rhs);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
Rvalue::Aggregate(ref _kind, ref operands) => {
|
|
|
|
for operand in operands {
|
2017-10-04 12:34:35 +02:00
|
|
|
self.gather_operand(operand);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-19 15:56:16 +02:00
|
|
|
Rvalue::Ref(..)
|
2018-12-23 19:00:58 +00:00
|
|
|
| Rvalue::AddressOf(..)
|
2019-08-19 15:56:16 +02:00
|
|
|
| Rvalue::Discriminant(..)
|
|
|
|
| Rvalue::Len(..)
|
|
|
|
| Rvalue::NullaryOp(NullOp::SizeOf, _)
|
|
|
|
| Rvalue::NullaryOp(NullOp::Box, _) => {
|
2017-07-04 15:57:55 +02:00
|
|
|
// This returns an rvalue with uninitialized contents. We can't
|
|
|
|
// move out of it here because it is an rvalue - assignments always
|
2017-12-01 14:39:51 +02:00
|
|
|
// completely initialize their place.
|
2017-07-04 15:57:55 +02:00
|
|
|
//
|
|
|
|
// However, this does not matter - MIR building is careful to
|
|
|
|
// only emit a shallow free for the partially-initialized
|
|
|
|
// temporary.
|
|
|
|
//
|
|
|
|
// In any case, if we want to fix this, we have to register a
|
|
|
|
// special move and change the `statement_effect` functions.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 12:34:35 +02:00
|
|
|
fn gather_terminator(&mut self, term: &Terminator<'tcx>) {
|
2017-07-04 15:57:55 +02:00
|
|
|
match term.kind {
|
2019-08-19 15:56:16 +02:00
|
|
|
TerminatorKind::Goto { target: _ }
|
|
|
|
| TerminatorKind::Resume
|
|
|
|
| TerminatorKind::Abort
|
|
|
|
| TerminatorKind::GeneratorDrop
|
|
|
|
| TerminatorKind::FalseEdges { .. }
|
|
|
|
| TerminatorKind::FalseUnwind { .. }
|
|
|
|
| TerminatorKind::Unreachable => {}
|
2017-07-04 15:57:55 +02:00
|
|
|
|
|
|
|
TerminatorKind::Return => {
|
2019-07-30 00:07:28 +02:00
|
|
|
self.gather_move(&Place::return_place());
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
|
2018-02-15 15:04:43 +01:00
|
|
|
TerminatorKind::Assert { ref cond, .. } => {
|
|
|
|
self.gather_operand(cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
TerminatorKind::SwitchInt { ref discr, .. } => {
|
|
|
|
self.gather_operand(discr);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
|
2020-01-25 02:30:46 +01:00
|
|
|
TerminatorKind::Yield { ref value, resume_arg: ref place, .. } => {
|
2020-02-17 21:20:48 +01:00
|
|
|
self.gather_operand(value);
|
2020-01-25 02:30:46 +01:00
|
|
|
self.create_move_path(place);
|
|
|
|
self.gather_init(place.as_ref(), InitKind::Deep);
|
2017-08-16 11:33:10 -07:00
|
|
|
}
|
|
|
|
|
2017-07-04 15:57:55 +02:00
|
|
|
TerminatorKind::Drop { ref location, target: _, unwind: _ } => {
|
2017-11-17 17:19:57 +02:00
|
|
|
self.gather_move(location);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
TerminatorKind::DropAndReplace { ref location, ref value, .. } => {
|
|
|
|
self.create_move_path(location);
|
2017-10-04 12:34:35 +02:00
|
|
|
self.gather_operand(value);
|
2019-07-21 22:38:30 +02:00
|
|
|
self.gather_init(location.as_ref(), InitKind::Deep);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
2018-09-29 10:34:12 +01:00
|
|
|
TerminatorKind::Call {
|
|
|
|
ref func,
|
|
|
|
ref args,
|
|
|
|
ref destination,
|
|
|
|
cleanup: _,
|
|
|
|
from_hir_call: _,
|
|
|
|
} => {
|
2017-10-04 12:34:35 +02:00
|
|
|
self.gather_operand(func);
|
2017-07-04 15:57:55 +02:00
|
|
|
for arg in args {
|
2017-10-04 12:34:35 +02:00
|
|
|
self.gather_operand(arg);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
if let Some((ref destination, _bb)) = *destination {
|
|
|
|
self.create_move_path(destination);
|
2019-07-21 22:38:30 +02:00
|
|
|
self.gather_init(destination.as_ref(), InitKind::NonPanicPathOnly);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 12:34:35 +02:00
|
|
|
fn gather_operand(&mut self, operand: &Operand<'tcx>) {
|
2017-07-04 15:57:55 +02:00
|
|
|
match *operand {
|
2019-08-19 15:56:16 +02:00
|
|
|
Operand::Constant(..) | Operand::Copy(..) => {} // not-a-move
|
|
|
|
Operand::Move(ref place) => {
|
|
|
|
// a move
|
2017-12-01 14:39:51 +02:00
|
|
|
self.gather_move(place);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
fn gather_move(&mut self, place: &Place<'tcx>) {
|
|
|
|
debug!("gather_move({:?}, {:?})", self.loc, place);
|
2017-07-04 15:57:55 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
if let [ref base @ .., ProjectionElem::Subslice { from, to, from_end: false }] =
|
|
|
|
**place.projection
|
|
|
|
{
|
2019-11-22 22:03:25 +00:00
|
|
|
// Split `Subslice` patterns into the corresponding list of
|
|
|
|
// `ConstIndex` patterns. This is done to ensure that all move paths
|
|
|
|
// are disjoint, which is expected by drop elaboration.
|
2020-01-22 16:30:15 +01:00
|
|
|
let base_place =
|
|
|
|
Place { local: place.local, projection: self.builder.tcx.intern_place_elems(base) };
|
2019-11-22 22:03:25 +00:00
|
|
|
let base_path = match self.move_path_for(&base_place) {
|
|
|
|
Ok(path) => path,
|
|
|
|
Err(MoveError::UnionMove { path }) => {
|
|
|
|
self.record_move(place, path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Err(error @ MoveError::IllegalMove { .. }) => {
|
|
|
|
self.builder.errors.push((base_place, error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let base_ty = base_place.ty(self.builder.body, self.builder.tcx).ty;
|
|
|
|
let len: u32 = match base_ty.kind {
|
|
|
|
ty::Array(_, size) => {
|
|
|
|
let length = size.eval_usize(self.builder.tcx, self.builder.param_env);
|
2019-12-22 17:42:04 -05:00
|
|
|
length
|
|
|
|
.try_into()
|
|
|
|
.expect("slice pattern of array with more than u32::MAX elements")
|
2019-11-22 22:03:25 +00:00
|
|
|
}
|
|
|
|
_ => bug!("from_end: false slice pattern of non-array type"),
|
|
|
|
};
|
|
|
|
for offset in from..to {
|
2019-12-22 17:42:04 -05:00
|
|
|
let elem =
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length: len, from_end: false };
|
|
|
|
let path = self.add_move_path(base_path, &elem, |tcx| {
|
|
|
|
tcx.mk_place_elem(base_place.clone(), elem)
|
|
|
|
});
|
2019-11-22 22:03:25 +00:00
|
|
|
self.record_move(place, path);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
2019-11-22 22:03:25 +00:00
|
|
|
} else {
|
|
|
|
match self.move_path_for(place) {
|
|
|
|
Ok(path) | Err(MoveError::UnionMove { path }) => self.record_move(place, path),
|
|
|
|
Err(error @ MoveError::IllegalMove { .. }) => {
|
2020-01-22 16:30:15 +01:00
|
|
|
self.builder.errors.push((*place, error));
|
2019-11-22 22:03:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2017-07-04 15:57:55 +02:00
|
|
|
|
2019-11-22 22:03:25 +00:00
|
|
|
fn record_move(&mut self, place: &Place<'tcx>, path: MovePathIndex) {
|
2020-03-06 19:28:44 +01:00
|
|
|
let move_out = self.builder.data.moves.push(MoveOut { path, source: self.loc });
|
2019-08-19 15:56:16 +02:00
|
|
|
debug!(
|
|
|
|
"gather_move({:?}, {:?}): adding move {:?} of {:?}",
|
|
|
|
self.loc, place, move_out, path
|
|
|
|
);
|
2017-10-04 12:34:35 +02:00
|
|
|
self.builder.data.path_map[path].push(move_out);
|
|
|
|
self.builder.data.loc_map[self.loc].push(move_out);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
2017-11-27 08:06:36 +00:00
|
|
|
|
2020-03-04 18:25:03 -03:00
|
|
|
fn gather_init(&mut self, place: PlaceRef<'tcx>, kind: InitKind) {
|
2017-12-01 14:39:51 +02:00
|
|
|
debug!("gather_init({:?}, {:?})", self.loc, place);
|
2017-11-27 08:06:36 +00:00
|
|
|
|
2019-07-19 21:58:03 +02:00
|
|
|
let mut place = place;
|
|
|
|
|
|
|
|
// Check if we are assigning into a field of a union, if so, lookup the place
|
|
|
|
// of the union so it is marked as initialized again.
|
2019-08-24 20:53:20 -04:00
|
|
|
if let [proj_base @ .., ProjectionElem::Field(_, _)] = place.projection {
|
2019-07-19 21:58:03 +02:00
|
|
|
if let ty::Adt(def, _) =
|
2020-01-14 02:21:42 -03:00
|
|
|
Place::ty_from(place.local, proj_base, self.builder.body, self.builder.tcx).ty.kind
|
2019-07-19 21:58:03 +02:00
|
|
|
{
|
|
|
|
if def.is_union() {
|
2019-12-11 16:50:03 -03:00
|
|
|
place = PlaceRef { local: place.local, projection: proj_base }
|
2019-04-30 18:58:24 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-19 21:58:03 +02:00
|
|
|
}
|
2018-11-03 16:54:35 +01:00
|
|
|
|
2019-07-19 21:49:34 +02:00
|
|
|
if let LookupResult::Exact(path) = self.builder.data.rev_lookup.find(place) {
|
2017-11-27 08:06:36 +00:00
|
|
|
let init = self.builder.data.inits.push(Init {
|
2018-08-30 18:14:54 +02:00
|
|
|
location: InitLocation::Statement(self.loc),
|
2017-11-27 08:06:36 +00:00
|
|
|
path,
|
|
|
|
kind,
|
|
|
|
});
|
|
|
|
|
2019-08-19 15:56:16 +02:00
|
|
|
debug!(
|
|
|
|
"gather_init({:?}, {:?}): adding init {:?} of {:?}",
|
|
|
|
self.loc, place, init, path
|
|
|
|
);
|
2017-11-27 08:06:36 +00:00
|
|
|
|
|
|
|
self.builder.data.init_path_map[path].push(init);
|
|
|
|
self.builder.data.init_loc_map[self.loc].push(init);
|
|
|
|
}
|
|
|
|
}
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|