2022-07-24 14:57:49 +03:00
|
|
|
use crate::move_paths::FxHashMap;
|
2022-06-13 16:37:41 +03:00
|
|
|
use crate::un_derefer::UnDerefer;
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::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
|
|
|
|
|
|
|
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>)>,
|
2022-06-13 16:37:41 +03:00
|
|
|
un_derefer: UnDerefer<'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(),
|
2022-06-13 16:37:41 +03:00
|
|
|
un_derefer: UnDerefer { tcx: tcx, derefer_sidetable: Default::default() },
|
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.
|
2020-03-31 12:19:29 -03:00
|
|
|
fn move_path_for(&mut self, place: Place<'tcx>) -> Result<MovePathIndex, MoveError<'tcx>> {
|
2022-06-13 16:37:41 +03:00
|
|
|
if let Some(new_place) = self.builder.un_derefer.derefer(place.as_ref(), self.builder.body)
|
|
|
|
{
|
|
|
|
return self.move_path_for(new_place);
|
|
|
|
}
|
|
|
|
|
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;
|
2020-08-03 00:49:11 +02:00
|
|
|
match place_ty.kind() {
|
2019-07-30 00:07:28 +02:00
|
|
|
ty::Ref(..) | ty::RawPtr(..) => {
|
|
|
|
let proj = &place.projection[..i + 1];
|
|
|
|
return Err(MoveError::cannot_move_out_of(
|
|
|
|
self.loc,
|
|
|
|
BorrowedContent {
|
|
|
|
target_place: Place {
|
2019-12-11 16:50:03 -03:00
|
|
|
local: place.local,
|
2023-02-17 14:33:08 +11:00
|
|
|
projection: tcx.mk_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,
|
2021-05-13 10:34:41 +05:30
|
|
|
is_index: matches!(elem, ProjectionElem::Index(..)),
|
2019-07-30 00:07:28 +02:00
|
|
|
},
|
|
|
|
));
|
|
|
|
}
|
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() {
|
2020-05-23 12:02:54 +02:00
|
|
|
base = self.add_move_path(base, elem, |tcx| Place {
|
2020-01-22 16:30:15 +01:00
|
|
|
local: place.local,
|
2023-02-17 14:33:08 +11:00
|
|
|
projection: tcx.mk_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,
|
2020-05-23 12:02:54 +02:00
|
|
|
elem: PlaceElem<'tcx>,
|
2019-11-22 22:03:25 +00:00
|
|
|
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;
|
|
|
|
*rev_lookup.projections.entry((base, elem.lift())).or_insert_with(move || {
|
2020-03-22 00:20:58 +01:00
|
|
|
MoveDataBuilder::new_move_path(
|
2019-11-22 22:03:25 +00:00
|
|
|
move_paths,
|
|
|
|
path_map,
|
|
|
|
init_path_map,
|
|
|
|
Some(base),
|
|
|
|
mk_place(*tcx),
|
2020-03-22 00:20:58 +01:00
|
|
|
)
|
2019-11-22 22:03:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-31 12:19:29 -03: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
|
|
|
|
2022-07-28 13:52:49 +03:00
|
|
|
pub type MoveDat<'tcx> = Result<
|
|
|
|
(FxHashMap<Local, Place<'tcx>>, MoveData<'tcx>),
|
|
|
|
(MoveData<'tcx>, Vec<(Place<'tcx>, MoveError<'tcx>)>),
|
|
|
|
>;
|
2022-07-28 12:56:57 +03:00
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
|
2022-07-28 13:52:49 +03:00
|
|
|
fn finalize(self) -> MoveDat<'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
|
|
|
|
2022-07-28 12:56:57 +03:00
|
|
|
if self.errors.is_empty() {
|
2022-07-25 17:08:54 +03:00
|
|
|
Ok((self.un_derefer.derefer_sidetable, self.data))
|
2022-07-28 12:56:57 +03:00
|
|
|
} else {
|
|
|
|
Err((self.data, self.errors))
|
2022-07-24 14:40:43 +03:00
|
|
|
}
|
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>,
|
2022-07-28 13:52:49 +03:00
|
|
|
) -> MoveDat<'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();
|
|
|
|
|
2022-07-05 00:00:00 +00: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>) {
|
2020-03-31 12:19:29 -03:00
|
|
|
match &stmt.kind {
|
2022-06-13 16:37:41 +03:00
|
|
|
StatementKind::Assign(box (place, Rvalue::CopyForDeref(reffed))) => {
|
|
|
|
assert!(place.projection.is_empty());
|
|
|
|
if self.builder.body.local_decls[place.local].is_deref_temp() {
|
|
|
|
self.builder.un_derefer.derefer_sidetable.insert(place.local, *reffed);
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 12:19:29 -03:00
|
|
|
StatementKind::Assign(box (place, rval)) => {
|
|
|
|
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.
|
2020-04-16 00:00:22 +02:00
|
|
|
self.create_move_path(self.builder.tcx.mk_place_deref(*place));
|
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
|
|
|
}
|
2021-03-29 22:48:44 -04:00
|
|
|
StatementKind::FakeRead(box (_, place)) => {
|
|
|
|
self.create_move_path(*place);
|
2018-05-04 12:04:33 +02:00
|
|
|
}
|
2017-11-10 14:11:25 +03:00
|
|
|
StatementKind::StorageLive(_) => {}
|
|
|
|
StatementKind::StorageDead(local) => {
|
2022-06-13 16:37:41 +03:00
|
|
|
// DerefTemp locals (results of CopyForDeref) don't actually move anything.
|
|
|
|
if !self.builder.un_derefer.derefer_sidetable.contains_key(&local) {
|
|
|
|
self.gather_move(Place::from(*local));
|
|
|
|
}
|
2017-11-10 14:11:25 +03:00
|
|
|
}
|
2022-04-05 17:14:59 -04:00
|
|
|
StatementKind::SetDiscriminant { .. } | StatementKind::Deinit(..) => {
|
2019-08-19 15:56:16 +02:00
|
|
|
span_bug!(
|
|
|
|
stmt.source_info.span,
|
2022-04-05 17:14:59 -04:00
|
|
|
"SetDiscriminant/Deinit should not exist during borrowck"
|
2019-08-19 15:56:16 +02:00
|
|
|
);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
2019-08-19 15:56:16 +02:00
|
|
|
StatementKind::Retag { .. }
|
|
|
|
| StatementKind::AscribeUserType(..)
|
2022-09-06 18:41:01 +02:00
|
|
|
| StatementKind::PlaceMention(..)
|
2020-08-15 04:42:13 -07:00
|
|
|
| StatementKind::Coverage(..)
|
2022-07-12 10:05:00 +00:00
|
|
|
| StatementKind::Intrinsic(..)
|
2022-12-20 00:51:17 +00:00
|
|
|
| StatementKind::ConstEvalCounter
|
2019-08-19 15:56:16 +02:00
|
|
|
| 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 {
|
2020-05-02 21:44:25 +02:00
|
|
|
Rvalue::ThreadLocalRef(_) => {} // not-a-move
|
2019-08-19 15:56:16 +02:00
|
|
|
Rvalue::Use(ref operand)
|
|
|
|
| Rvalue::Repeat(ref operand, _)
|
|
|
|
| Rvalue::Cast(_, ref operand, _)
|
2021-09-06 18:33:23 +01:00
|
|
|
| Rvalue::ShallowInitBox(ref operand, _)
|
2019-08-19 15:56:16 +02:00
|
|
|
| Rvalue::UnaryOp(_, ref operand) => self.gather_operand(operand),
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(ref _binop, box (ref lhs, ref rhs))
|
|
|
|
| Rvalue::CheckedBinaryOp(ref _binop, box (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
|
|
|
}
|
|
|
|
}
|
2022-06-13 16:37:41 +03:00
|
|
|
Rvalue::CopyForDeref(..) => unreachable!(),
|
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(..)
|
2022-09-11 00:37:49 -07:00
|
|
|
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {}
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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: _ }
|
2020-10-02 15:40:24 -04:00
|
|
|
| TerminatorKind::FalseEdge { .. }
|
2020-05-08 20:00:32 +01:00
|
|
|
| TerminatorKind::FalseUnwind { .. }
|
|
|
|
// In some sense returning moves the return place into the current
|
|
|
|
// call's destination, however, since there are no statements after
|
|
|
|
// this that could possibly access the return place, this doesn't
|
|
|
|
// need recording.
|
2019-11-17 11:30:48 +00:00
|
|
|
| TerminatorKind::Return
|
2019-08-19 15:56:16 +02:00
|
|
|
| TerminatorKind::Resume
|
2022-10-31 01:01:24 +00:00
|
|
|
| TerminatorKind::Terminate
|
2019-08-19 15:56:16 +02:00
|
|
|
| TerminatorKind::GeneratorDrop
|
2023-01-24 18:48:05 +01:00
|
|
|
| TerminatorKind::Unreachable
|
|
|
|
| TerminatorKind::Drop { .. } => {}
|
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-03-31 12:19:29 -03:00
|
|
|
TerminatorKind::Yield { ref value, resume_arg: 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
|
|
|
}
|
2018-09-29 10:34:12 +01:00
|
|
|
TerminatorKind::Call {
|
|
|
|
ref func,
|
|
|
|
ref args,
|
2022-04-16 09:27:54 -04:00
|
|
|
destination,
|
|
|
|
target,
|
2022-10-08 23:47:59 +01:00
|
|
|
unwind: _,
|
2018-09-29 10:34:12 +01:00
|
|
|
from_hir_call: _,
|
2020-06-09 15:34:23 -04:00
|
|
|
fn_span: _,
|
2018-09-29 10:34:12 +01:00
|
|
|
} => {
|
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
|
|
|
}
|
2022-04-16 09:27:54 -04:00
|
|
|
if let Some(_bb) = target {
|
2017-07-04 15:57:55 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2020-05-26 20:07:59 +01:00
|
|
|
TerminatorKind::InlineAsm {
|
|
|
|
template: _,
|
|
|
|
ref operands,
|
|
|
|
options: _,
|
|
|
|
line_spans: _,
|
2020-06-04 12:25:18 -04:00
|
|
|
destination: _,
|
2022-10-08 23:47:59 +01:00
|
|
|
unwind: _,
|
2020-05-26 20:07:59 +01:00
|
|
|
} => {
|
2020-02-14 18:17:50 +00:00
|
|
|
for op in operands {
|
|
|
|
match *op {
|
|
|
|
InlineAsmOperand::In { reg: _, ref value }
|
2021-04-06 05:50:55 +01:00
|
|
|
=> {
|
2020-02-14 18:17:50 +00:00
|
|
|
self.gather_operand(value);
|
|
|
|
}
|
|
|
|
InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
|
|
|
|
if let Some(place) = place {
|
|
|
|
self.create_move_path(place);
|
|
|
|
self.gather_init(place.as_ref(), InitKind::Deep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InlineAsmOperand::InOut { reg: _, late: _, ref in_value, out_place } => {
|
|
|
|
self.gather_operand(in_value);
|
|
|
|
if let Some(out_place) = out_place {
|
|
|
|
self.create_move_path(out_place);
|
|
|
|
self.gather_init(out_place.as_ref(), InitKind::Deep);
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 05:50:55 +01:00
|
|
|
InlineAsmOperand::Const { value: _ }
|
|
|
|
| InlineAsmOperand::SymFn { value: _ }
|
2020-06-05 15:52:38 +01:00
|
|
|
| InlineAsmOperand::SymStatic { def_id: _ } => {}
|
2020-02-14 18:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
2020-03-31 12:19:29 -03:00
|
|
|
Operand::Move(place) => {
|
2019-08-19 15:56:16 +02:00
|
|
|
// a move
|
2017-12-01 14:39:51 +02:00
|
|
|
self.gather_move(place);
|
2017-07-04 15:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 12:19:29 -03:00
|
|
|
fn gather_move(&mut self, place: Place<'tcx>) {
|
2017-12-01 14:39:51 +02:00
|
|
|
debug!("gather_move({:?}, {:?})", self.loc, place);
|
2022-06-13 16:37:41 +03:00
|
|
|
if let Some(new_place) = self.builder.un_derefer.derefer(place.as_ref(), self.builder.body)
|
|
|
|
{
|
|
|
|
self.gather_move(new_place);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-04 15:57:55 +02:00
|
|
|
|
2019-11-22 22:03:25 +00:00
|
|
|
if let [ref base @ .., ProjectionElem::Subslice { from, to, from_end: false }] =
|
|
|
|
**place.projection
|
|
|
|
{
|
|
|
|
// 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 =
|
2023-02-17 14:33:08 +11:00
|
|
|
Place { local: place.local, projection: self.builder.tcx.mk_place_elems(base) };
|
2020-03-31 12:19:29 -03:00
|
|
|
let base_path = match self.move_path_for(base_place) {
|
2019-11-22 22:03:25 +00:00
|
|
|
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;
|
2020-08-03 00:49:11 +02:00
|
|
|
let len: u64 = match base_ty.kind() {
|
2023-02-14 08:51:19 +00:00
|
|
|
ty::Array(_, size) => {
|
|
|
|
size.eval_target_usize(self.builder.tcx, self.builder.param_env)
|
|
|
|
}
|
2019-11-22 22:03:25 +00:00
|
|
|
_ => bug!("from_end: false slice pattern of non-array type"),
|
|
|
|
};
|
|
|
|
for offset in from..to {
|
|
|
|
let elem =
|
|
|
|
ProjectionElem::ConstantIndex { offset, min_length: len, from_end: false };
|
2020-04-16 00:00:22 +02:00
|
|
|
let path =
|
2020-05-23 12:02:54 +02:00
|
|
|
self.add_move_path(base_path, elem, |tcx| tcx.mk_place_elem(base_place, 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-03-31 12:19:29 -03:00
|
|
|
self.builder.errors.push((place, error));
|
2019-11-22 22:03:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2017-07-04 15:57:55 +02:00
|
|
|
|
2020-03-31 12:19:29 -03: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
|
|
|
|
2022-06-13 16:37:41 +03:00
|
|
|
if let Some(new_place) = self.builder.un_derefer.derefer(place, self.builder.body) {
|
|
|
|
self.gather_init(new_place.as_ref(), kind);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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.
|
2021-01-09 23:33:38 -06:00
|
|
|
if let Some((place_base, ProjectionElem::Field(_, _))) = place.last_projection() {
|
2021-06-02 00:00:00 +00:00
|
|
|
if place_base.ty(self.builder.body, self.builder.tcx).ty.is_union() {
|
|
|
|
place = place_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
|
|
|
}
|