2016-05-17 02:26:18 +03:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-06-26 14:57:26 +02:00
|
|
|
use dataflow::move_paths::{HasMoveData, MoveData, MovePathIndex, LookupResult};
|
|
|
|
use dataflow::{MaybeInitializedLvals, MaybeUninitializedLvals};
|
|
|
|
use dataflow::{DataflowResults};
|
|
|
|
use dataflow::{on_all_children_bits, on_all_drop_children_bits};
|
|
|
|
use dataflow::{drop_flag_effects_for_location, on_lookup_result_bits};
|
|
|
|
use dataflow::MoveDataParamEnv;
|
|
|
|
use dataflow;
|
2017-11-10 19:20:35 +02:00
|
|
|
use rustc::hir;
|
2017-03-09 20:10:05 +02:00
|
|
|
use rustc::ty::{self, TyCtxt};
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::*;
|
2017-02-05 07:01:48 +02:00
|
|
|
use rustc::middle::const_val::ConstVal;
|
2016-11-08 14:02:55 +11:00
|
|
|
use rustc::util::nodemap::FxHashMap;
|
2016-10-05 22:43:27 -04:00
|
|
|
use rustc_data_structures::indexed_set::IdxSetBuf;
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2017-11-10 19:20:35 +02:00
|
|
|
use transform::{MirPass, MirSource};
|
2017-06-26 14:57:26 +02:00
|
|
|
use util::patch::MirPatch;
|
|
|
|
use util::elaborate_drops::{DropFlagState, Unwind, elaborate_drop};
|
|
|
|
use util::elaborate_drops::{DropElaborator, DropStyle, DropFlagMode};
|
2017-04-07 01:00:53 +03:00
|
|
|
use syntax::ast;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2016-05-17 02:26:18 +03:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
pub struct ElaborateDrops;
|
|
|
|
|
2017-04-25 18:23:33 -04:00
|
|
|
impl MirPass for ElaborateDrops {
|
|
|
|
fn run_pass<'a, 'tcx>(&self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
src: MirSource,
|
|
|
|
mir: &mut Mir<'tcx>)
|
2016-05-17 02:26:18 +03:00
|
|
|
{
|
|
|
|
debug!("elaborate_drops({:?} @ {:?})", src, mir.span);
|
2017-11-10 19:20:35 +02:00
|
|
|
|
|
|
|
// Don't run on constant MIR, because trans might not be able to
|
|
|
|
// evaluate the modified MIR.
|
|
|
|
// FIXME(eddyb) Remove check after miri is merged.
|
|
|
|
let id = tcx.hir.as_local_node_id(src.def_id).unwrap();
|
|
|
|
match (tcx.hir.body_owner_kind(id), src.promoted) {
|
|
|
|
(hir::BodyOwnerKind::Fn, None) => {},
|
2016-05-17 02:26:18 +03:00
|
|
|
_ => return
|
|
|
|
}
|
2017-11-10 19:20:35 +02:00
|
|
|
let param_env = tcx.param_env(src.def_id);
|
2017-10-04 11:02:26 +02:00
|
|
|
let move_data = MoveData::gather_moves(mir, tcx, param_env).unwrap();
|
2016-05-17 02:26:18 +03:00
|
|
|
let elaborate_patch = {
|
|
|
|
let mir = &*mir;
|
2016-05-27 15:07:08 +03:00
|
|
|
let env = MoveDataParamEnv {
|
2017-08-06 22:54:09 -07:00
|
|
|
move_data,
|
|
|
|
param_env,
|
2016-05-27 15:07:08 +03:00
|
|
|
};
|
2017-04-07 01:00:53 +03:00
|
|
|
let dead_unwinds = find_dead_unwinds(tcx, mir, id, &env);
|
2016-05-27 15:07:08 +03:00
|
|
|
let flow_inits =
|
2017-06-26 14:57:26 +02:00
|
|
|
dataflow::do_dataflow(tcx, mir, id, &[], &dead_unwinds,
|
|
|
|
MaybeInitializedLvals::new(tcx, mir, &env),
|
|
|
|
|bd, p| &bd.move_data().move_paths[p]);
|
2016-05-27 15:07:08 +03:00
|
|
|
let flow_uninits =
|
2017-06-26 14:57:26 +02:00
|
|
|
dataflow::do_dataflow(tcx, mir, id, &[], &dead_unwinds,
|
|
|
|
MaybeUninitializedLvals::new(tcx, mir, &env),
|
|
|
|
|bd, p| &bd.move_data().move_paths[p]);
|
2016-05-27 15:07:08 +03:00
|
|
|
|
|
|
|
ElaborateDropsCtxt {
|
2017-08-06 22:54:09 -07:00
|
|
|
tcx,
|
|
|
|
mir,
|
2016-05-27 15:07:08 +03:00
|
|
|
env: &env,
|
2017-08-06 22:54:09 -07:00
|
|
|
flow_inits,
|
|
|
|
flow_uninits,
|
2016-11-08 14:02:55 +11:00
|
|
|
drop_flags: FxHashMap(),
|
2016-05-27 15:07:08 +03:00
|
|
|
patch: MirPatch::new(mir),
|
|
|
|
}.elaborate()
|
2016-05-17 02:26:18 +03:00
|
|
|
};
|
|
|
|
elaborate_patch.apply(mir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-07 01:00:53 +03:00
|
|
|
/// Return the set of basic blocks whose unwind edges are known
|
|
|
|
/// to not be reachable, because they are `drop` terminators
|
|
|
|
/// that can't drop anything.
|
|
|
|
fn find_dead_unwinds<'a, 'tcx>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mir: &Mir<'tcx>,
|
|
|
|
id: ast::NodeId,
|
2017-10-30 05:50:39 -04:00
|
|
|
env: &MoveDataParamEnv<'tcx, 'tcx>)
|
2017-04-07 01:00:53 +03:00
|
|
|
-> IdxSetBuf<BasicBlock>
|
|
|
|
{
|
|
|
|
debug!("find_dead_unwinds({:?})", mir.span);
|
|
|
|
// We only need to do this pass once, because unwind edges can only
|
|
|
|
// reach cleanup blocks, which can't have unwind edges themselves.
|
|
|
|
let mut dead_unwinds = IdxSetBuf::new_empty(mir.basic_blocks().len());
|
|
|
|
let flow_inits =
|
2017-06-26 14:57:26 +02:00
|
|
|
dataflow::do_dataflow(tcx, mir, id, &[], &dead_unwinds,
|
2017-04-07 01:00:53 +03:00
|
|
|
MaybeInitializedLvals::new(tcx, mir, &env),
|
|
|
|
|bd, p| &bd.move_data().move_paths[p]);
|
|
|
|
for (bb, bb_data) in mir.basic_blocks().iter_enumerated() {
|
2016-12-26 14:34:03 +01:00
|
|
|
let location = match bb_data.terminator().kind {
|
2017-04-07 01:00:53 +03:00
|
|
|
TerminatorKind::Drop { ref location, unwind: Some(_), .. } |
|
2016-12-26 14:34:03 +01:00
|
|
|
TerminatorKind::DropAndReplace { ref location, unwind: Some(_), .. } => location,
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
2017-08-25 07:16:24 -07:00
|
|
|
let mut init_data = InitializationData {
|
|
|
|
live: flow_inits.sets().on_entry_set_for(bb.index()).to_owned(),
|
|
|
|
dead: IdxSetBuf::new_empty(env.move_data.move_paths.len()),
|
|
|
|
};
|
|
|
|
debug!("find_dead_unwinds @ {:?}: {:?}; init_data={:?}",
|
|
|
|
bb, bb_data, init_data.live);
|
|
|
|
for stmt in 0..bb_data.statements.len() {
|
|
|
|
let loc = Location { block: bb, statement_index: stmt };
|
|
|
|
init_data.apply_location(tcx, mir, env, loc);
|
|
|
|
}
|
2017-04-07 01:00:53 +03:00
|
|
|
|
2017-08-25 07:16:24 -07:00
|
|
|
let path = match env.move_data.rev_lookup.find(location) {
|
|
|
|
LookupResult::Exact(e) => e,
|
|
|
|
LookupResult::Parent(..) => {
|
|
|
|
debug!("find_dead_unwinds: has parent; skipping");
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
};
|
2017-04-07 01:00:53 +03:00
|
|
|
|
2017-08-25 07:16:24 -07:00
|
|
|
debug!("find_dead_unwinds @ {:?}: path({:?})={:?}", bb, location, path);
|
2017-04-07 01:00:53 +03:00
|
|
|
|
2017-08-25 07:16:24 -07:00
|
|
|
let mut maybe_live = false;
|
|
|
|
on_all_drop_children_bits(tcx, mir, &env, path, |child| {
|
|
|
|
let (child_maybe_live, _) = init_data.state(child);
|
|
|
|
maybe_live |= child_maybe_live;
|
|
|
|
});
|
2017-04-07 01:00:53 +03:00
|
|
|
|
2017-08-25 07:16:24 -07:00
|
|
|
debug!("find_dead_unwinds @ {:?}: maybe_live={}", bb, maybe_live);
|
|
|
|
if !maybe_live {
|
|
|
|
dead_unwinds.add(&bb);
|
|
|
|
}
|
2017-04-07 01:00:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dead_unwinds
|
|
|
|
}
|
|
|
|
|
2016-05-17 02:26:18 +03:00
|
|
|
struct InitializationData {
|
2016-05-27 15:07:08 +03:00
|
|
|
live: IdxSetBuf<MovePathIndex>,
|
|
|
|
dead: IdxSetBuf<MovePathIndex>
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InitializationData {
|
|
|
|
fn apply_location<'a,'tcx>(&mut self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mir: &Mir<'tcx>,
|
2017-10-30 05:50:39 -04:00
|
|
|
env: &MoveDataParamEnv<'tcx, 'tcx>,
|
2016-05-17 02:26:18 +03:00
|
|
|
loc: Location)
|
|
|
|
{
|
2016-05-27 15:07:08 +03:00
|
|
|
drop_flag_effects_for_location(tcx, mir, env, loc, |path, df| {
|
2016-05-17 02:26:18 +03:00
|
|
|
debug!("at location {:?}: setting {:?} to {:?}",
|
|
|
|
loc, path, df);
|
|
|
|
match df {
|
2016-05-27 15:07:08 +03:00
|
|
|
DropFlagState::Present => {
|
|
|
|
self.live.add(&path);
|
|
|
|
self.dead.remove(&path);
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
2016-05-27 15:07:08 +03:00
|
|
|
DropFlagState::Absent => {
|
|
|
|
self.dead.add(&path);
|
|
|
|
self.live.remove(&path);
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn state(&self, path: MovePathIndex) -> (bool, bool) {
|
2016-05-27 15:07:08 +03:00
|
|
|
(self.live.contains(&path), self.dead.contains(&path))
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 20:10:05 +02:00
|
|
|
struct Elaborator<'a, 'b: 'a, 'tcx: 'b> {
|
|
|
|
init_data: &'a InitializationData,
|
|
|
|
ctxt: &'a mut ElaborateDropsCtxt<'b, 'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b, 'tcx> fmt::Debug for Elaborator<'a, 'b, 'tcx> {
|
2016-05-17 02:26:18 +03:00
|
|
|
fn fmt(&self, _f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 20:10:05 +02:00
|
|
|
impl<'a, 'b, 'tcx> DropElaborator<'a, 'tcx> for Elaborator<'a, 'b, 'tcx> {
|
|
|
|
type Path = MovePathIndex;
|
|
|
|
|
|
|
|
fn patch(&mut self) -> &mut MirPatch<'tcx> {
|
|
|
|
&mut self.ctxt.patch
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mir(&self) -> &'a Mir<'tcx> {
|
|
|
|
self.ctxt.mir
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:13:36 -04:00
|
|
|
fn tcx(&self) -> TyCtxt<'a, 'tcx, 'tcx> {
|
2017-03-09 20:10:05 +02:00
|
|
|
self.ctxt.tcx
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:57:30 -04:00
|
|
|
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
2017-03-09 20:10:05 +02:00
|
|
|
self.ctxt.param_env()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle {
|
|
|
|
let ((maybe_live, maybe_dead), multipart) = match mode {
|
|
|
|
DropFlagMode::Shallow => (self.init_data.state(path), false),
|
|
|
|
DropFlagMode::Deep => {
|
|
|
|
let mut some_live = false;
|
|
|
|
let mut some_dead = false;
|
|
|
|
let mut children_count = 0;
|
2017-04-07 01:00:53 +03:00
|
|
|
on_all_drop_children_bits(
|
|
|
|
self.tcx(), self.mir(), self.ctxt.env, path, |child| {
|
|
|
|
let (live, dead) = self.init_data.state(child);
|
|
|
|
debug!("elaborate_drop: state({:?}) = {:?}",
|
|
|
|
child, (live, dead));
|
|
|
|
some_live |= live;
|
|
|
|
some_dead |= dead;
|
|
|
|
children_count += 1;
|
2017-03-09 20:10:05 +02:00
|
|
|
});
|
|
|
|
((some_live, some_dead), children_count != 1)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match (maybe_live, maybe_dead, multipart) {
|
|
|
|
(false, _, _) => DropStyle::Dead,
|
|
|
|
(true, false, _) => DropStyle::Static,
|
|
|
|
(true, true, false) => DropStyle::Conditional,
|
|
|
|
(true, true, true) => DropStyle::Open,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_drop_flag(&mut self, loc: Location, path: Self::Path, mode: DropFlagMode) {
|
|
|
|
match mode {
|
|
|
|
DropFlagMode::Shallow => {
|
|
|
|
self.ctxt.set_drop_flag(loc, path, DropFlagState::Absent);
|
|
|
|
}
|
|
|
|
DropFlagMode::Deep => {
|
|
|
|
on_all_children_bits(
|
|
|
|
self.tcx(), self.mir(), self.ctxt.move_data(), path,
|
|
|
|
|child| self.ctxt.set_drop_flag(loc, child, DropFlagState::Absent)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn field_subpath(&self, path: Self::Path, field: Field) -> Option<Self::Path> {
|
2017-06-26 14:57:26 +02:00
|
|
|
dataflow::move_path_children_matching(self.ctxt.move_data(), path, |p| {
|
2017-03-09 20:10:05 +02:00
|
|
|
match p {
|
|
|
|
&Projection {
|
|
|
|
elem: ProjectionElem::Field(idx, _), ..
|
|
|
|
} => idx == field,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_subpath(&self, path: Self::Path) -> Option<Self::Path> {
|
2017-06-26 14:57:26 +02:00
|
|
|
dataflow::move_path_children_matching(self.ctxt.move_data(), path, |p| {
|
2017-03-09 20:10:05 +02:00
|
|
|
match p {
|
|
|
|
&Projection { elem: ProjectionElem::Deref, .. } => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn downcast_subpath(&self, path: Self::Path, variant: usize) -> Option<Self::Path> {
|
2017-06-26 14:57:26 +02:00
|
|
|
dataflow::move_path_children_matching(self.ctxt.move_data(), path, |p| {
|
2017-03-09 20:10:05 +02:00
|
|
|
match p {
|
|
|
|
&Projection {
|
|
|
|
elem: ProjectionElem::Downcast(_, idx), ..
|
|
|
|
} => idx == variant,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>> {
|
|
|
|
self.ctxt.drop_flag(path).map(Operand::Consume)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 02:26:18 +03:00
|
|
|
struct ElaborateDropsCtxt<'a, 'tcx: 'a> {
|
2016-05-27 15:07:08 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
mir: &'a Mir<'tcx>,
|
2017-10-30 05:50:39 -04:00
|
|
|
env: &'a MoveDataParamEnv<'tcx, 'tcx>,
|
|
|
|
flow_inits: DataflowResults<MaybeInitializedLvals<'a, 'tcx, 'tcx>>,
|
|
|
|
flow_uninits: DataflowResults<MaybeUninitializedLvals<'a, 'tcx, 'tcx>>,
|
2016-11-08 14:02:55 +11:00
|
|
|
drop_flags: FxHashMap<MovePathIndex, Local>,
|
2016-05-17 02:26:18 +03:00
|
|
|
patch: MirPatch<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
|
2016-05-27 15:07:08 +03:00
|
|
|
fn move_data(&self) -> &'b MoveData<'tcx> { &self.env.move_data }
|
2017-05-10 10:28:06 -04:00
|
|
|
|
2017-05-15 17:57:30 -04:00
|
|
|
fn param_env(&self) -> ty::ParamEnv<'tcx> {
|
2017-05-10 10:28:06 -04:00
|
|
|
self.env.param_env
|
2016-05-27 15:07:08 +03:00
|
|
|
}
|
2016-05-17 02:26:18 +03:00
|
|
|
|
|
|
|
fn initialization_data_at(&self, loc: Location) -> InitializationData {
|
|
|
|
let mut data = InitializationData {
|
|
|
|
live: self.flow_inits.sets().on_entry_set_for(loc.block.index())
|
|
|
|
.to_owned(),
|
|
|
|
dead: self.flow_uninits.sets().on_entry_set_for(loc.block.index())
|
|
|
|
.to_owned(),
|
|
|
|
};
|
2016-08-08 18:46:06 -07:00
|
|
|
for stmt in 0..loc.statement_index {
|
2016-05-27 15:07:08 +03:00
|
|
|
data.apply_location(self.tcx, self.mir, self.env,
|
2016-08-08 18:46:06 -07:00
|
|
|
Location { block: loc.block, statement_index: stmt });
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
data
|
|
|
|
}
|
|
|
|
|
2017-04-11 23:52:51 +03:00
|
|
|
fn create_drop_flag(&mut self, index: MovePathIndex, span: Span) {
|
2016-05-27 15:07:08 +03:00
|
|
|
let tcx = self.tcx;
|
2016-05-17 02:26:18 +03:00
|
|
|
let patch = &mut self.patch;
|
2017-02-26 16:21:26 +02:00
|
|
|
debug!("create_drop_flag({:?})", self.mir.span);
|
2016-05-17 02:26:18 +03:00
|
|
|
self.drop_flags.entry(index).or_insert_with(|| {
|
2017-07-15 22:41:33 +02:00
|
|
|
patch.new_internal(tcx.types.bool, span)
|
2016-05-17 02:26:18 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drop_flag(&mut self, index: MovePathIndex) -> Option<Lvalue<'tcx>> {
|
2016-09-25 01:38:27 +02:00
|
|
|
self.drop_flags.get(&index).map(|t| Lvalue::Local(*t))
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// create a patch that elaborates all drops in the input
|
|
|
|
/// MIR.
|
|
|
|
fn elaborate(mut self) -> MirPatch<'tcx>
|
|
|
|
{
|
|
|
|
self.collect_drop_flags();
|
|
|
|
|
|
|
|
self.elaborate_drops();
|
|
|
|
|
|
|
|
self.drop_flags_on_init();
|
|
|
|
self.drop_flags_for_fn_rets();
|
|
|
|
self.drop_flags_for_args();
|
|
|
|
self.drop_flags_for_locs();
|
|
|
|
|
|
|
|
self.patch
|
|
|
|
}
|
|
|
|
|
|
|
|
fn collect_drop_flags(&mut self)
|
|
|
|
{
|
2016-06-07 21:20:50 +03:00
|
|
|
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
|
2016-05-17 02:26:18 +03:00
|
|
|
let terminator = data.terminator();
|
|
|
|
let location = match terminator.kind {
|
|
|
|
TerminatorKind::Drop { ref location, .. } |
|
|
|
|
TerminatorKind::DropAndReplace { ref location, .. } => location,
|
|
|
|
_ => continue
|
|
|
|
};
|
|
|
|
|
|
|
|
let init_data = self.initialization_data_at(Location {
|
|
|
|
block: bb,
|
2016-08-08 18:46:06 -07:00
|
|
|
statement_index: data.statements.len()
|
2016-05-17 02:26:18 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
let path = self.move_data().rev_lookup.find(location);
|
2016-06-11 23:47:28 +03:00
|
|
|
debug!("collect_drop_flags: {:?}, lv {:?} ({:?})",
|
2016-05-17 02:26:18 +03:00
|
|
|
bb, location, path);
|
|
|
|
|
2016-06-11 23:47:28 +03:00
|
|
|
let path = match path {
|
|
|
|
LookupResult::Exact(e) => e,
|
|
|
|
LookupResult::Parent(None) => continue,
|
|
|
|
LookupResult::Parent(Some(parent)) => {
|
|
|
|
let (_maybe_live, maybe_dead) = init_data.state(parent);
|
|
|
|
if maybe_dead {
|
|
|
|
span_bug!(terminator.source_info.span,
|
|
|
|
"drop of untracked, uninitialized value {:?}, lv {:?} ({:?})",
|
|
|
|
bb, location, path);
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-07 01:00:53 +03:00
|
|
|
on_all_drop_children_bits(self.tcx, self.mir, self.env, path, |child| {
|
|
|
|
let (maybe_live, maybe_dead) = init_data.state(child);
|
|
|
|
debug!("collect_drop_flags: collecting {:?} from {:?}@{:?} - {:?}",
|
|
|
|
child, location, path, (maybe_live, maybe_dead));
|
|
|
|
if maybe_live && maybe_dead {
|
2017-04-11 23:52:51 +03:00
|
|
|
self.create_drop_flag(child, terminator.source_info.span)
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn elaborate_drops(&mut self)
|
|
|
|
{
|
2016-06-07 21:20:50 +03:00
|
|
|
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
|
2016-08-08 18:46:06 -07:00
|
|
|
let loc = Location { block: bb, statement_index: data.statements.len() };
|
2016-05-17 02:26:18 +03:00
|
|
|
let terminator = data.terminator();
|
|
|
|
|
|
|
|
let resume_block = self.patch.resume_block();
|
|
|
|
match terminator.kind {
|
|
|
|
TerminatorKind::Drop { ref location, target, unwind } => {
|
|
|
|
let init_data = self.initialization_data_at(loc);
|
2016-06-11 23:47:28 +03:00
|
|
|
match self.move_data().rev_lookup.find(location) {
|
|
|
|
LookupResult::Exact(path) => {
|
2017-03-09 20:10:05 +02:00
|
|
|
elaborate_drop(
|
|
|
|
&mut Elaborator {
|
|
|
|
init_data: &init_data,
|
|
|
|
ctxt: self
|
|
|
|
},
|
|
|
|
terminator.source_info,
|
|
|
|
location,
|
|
|
|
path,
|
|
|
|
target,
|
|
|
|
if data.is_cleanup {
|
2017-05-12 15:00:36 +03:00
|
|
|
Unwind::InCleanup
|
2016-06-11 23:47:28 +03:00
|
|
|
} else {
|
2017-05-12 15:00:36 +03:00
|
|
|
Unwind::To(Option::unwrap_or(unwind, resume_block))
|
2017-03-09 20:10:05 +02:00
|
|
|
},
|
|
|
|
bb)
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
2016-06-11 23:47:28 +03:00
|
|
|
LookupResult::Parent(..) => {
|
|
|
|
span_bug!(terminator.source_info.span,
|
|
|
|
"drop of untracked value {:?}", bb);
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
TerminatorKind::DropAndReplace { ref location, ref value,
|
|
|
|
target, unwind } =>
|
|
|
|
{
|
|
|
|
assert!(!data.is_cleanup);
|
|
|
|
|
|
|
|
self.elaborate_replace(
|
|
|
|
loc,
|
|
|
|
location, value,
|
|
|
|
target, unwind
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Elaborate a MIR `replace` terminator. This instruction
|
|
|
|
/// is not directly handled by translation, and therefore
|
|
|
|
/// must be desugared.
|
|
|
|
///
|
|
|
|
/// The desugaring drops the location if needed, and then writes
|
|
|
|
/// the value (including setting the drop flag) over it in *both* arms.
|
|
|
|
///
|
|
|
|
/// The `replace` terminator can also be called on lvalues that
|
|
|
|
/// are not tracked by elaboration (for example,
|
|
|
|
/// `replace x[i] <- tmp0`). The borrow checker requires that
|
|
|
|
/// these locations are initialized before the assignment,
|
|
|
|
/// so we just generate an unconditional drop.
|
|
|
|
fn elaborate_replace(
|
|
|
|
&mut self,
|
|
|
|
loc: Location,
|
|
|
|
location: &Lvalue<'tcx>,
|
|
|
|
value: &Operand<'tcx>,
|
|
|
|
target: BasicBlock,
|
|
|
|
unwind: Option<BasicBlock>)
|
|
|
|
{
|
|
|
|
let bb = loc.block;
|
2016-06-07 21:20:50 +03:00
|
|
|
let data = &self.mir[bb];
|
2016-05-17 02:26:18 +03:00
|
|
|
let terminator = data.terminator();
|
2017-05-12 15:00:36 +03:00
|
|
|
assert!(!data.is_cleanup, "DropAndReplace in unwind path not supported");
|
2016-05-17 02:26:18 +03:00
|
|
|
|
2016-06-05 09:00:17 +03:00
|
|
|
let assign = Statement {
|
|
|
|
kind: StatementKind::Assign(location.clone(), Rvalue::Use(value.clone())),
|
2016-06-07 19:21:56 +03:00
|
|
|
source_info: terminator.source_info
|
2016-06-05 09:00:17 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let unwind = unwind.unwrap_or(self.patch.resume_block());
|
|
|
|
let unwind = self.patch.new_block(BasicBlockData {
|
|
|
|
statements: vec![assign.clone()],
|
|
|
|
terminator: Some(Terminator {
|
|
|
|
kind: TerminatorKind::Goto { target: unwind },
|
|
|
|
..*terminator
|
|
|
|
}),
|
|
|
|
is_cleanup: true
|
|
|
|
});
|
|
|
|
|
|
|
|
let target = self.patch.new_block(BasicBlockData {
|
|
|
|
statements: vec![assign],
|
|
|
|
terminator: Some(Terminator {
|
|
|
|
kind: TerminatorKind::Goto { target: target },
|
|
|
|
..*terminator
|
|
|
|
}),
|
2017-05-12 15:00:36 +03:00
|
|
|
is_cleanup: false,
|
2016-06-05 09:00:17 +03:00
|
|
|
});
|
2016-05-17 02:26:18 +03:00
|
|
|
|
2016-06-11 23:47:28 +03:00
|
|
|
match self.move_data().rev_lookup.find(location) {
|
|
|
|
LookupResult::Exact(path) => {
|
|
|
|
debug!("elaborate_drop_and_replace({:?}) - tracked {:?}", terminator, path);
|
|
|
|
let init_data = self.initialization_data_at(loc);
|
|
|
|
|
2017-03-09 20:10:05 +02:00
|
|
|
elaborate_drop(
|
|
|
|
&mut Elaborator {
|
|
|
|
init_data: &init_data,
|
|
|
|
ctxt: self
|
|
|
|
},
|
|
|
|
terminator.source_info,
|
|
|
|
location,
|
|
|
|
path,
|
|
|
|
target,
|
2017-05-12 15:00:36 +03:00
|
|
|
Unwind::To(unwind),
|
2017-03-09 20:10:05 +02:00
|
|
|
bb);
|
2016-06-11 23:47:28 +03:00
|
|
|
on_all_children_bits(self.tcx, self.mir, self.move_data(), path, |child| {
|
|
|
|
self.set_drop_flag(Location { block: target, statement_index: 0 },
|
|
|
|
child, DropFlagState::Present);
|
|
|
|
self.set_drop_flag(Location { block: unwind, statement_index: 0 },
|
|
|
|
child, DropFlagState::Present);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
LookupResult::Parent(parent) => {
|
|
|
|
// drop and replace behind a pointer/array/whatever. The location
|
|
|
|
// must be initialized.
|
|
|
|
debug!("elaborate_drop_and_replace({:?}) - untracked {:?}", terminator, parent);
|
|
|
|
self.patch.patch_terminator(bb, TerminatorKind::Drop {
|
|
|
|
location: location.clone(),
|
2017-08-06 22:54:09 -07:00
|
|
|
target,
|
2016-06-11 23:47:28 +03:00
|
|
|
unwind: Some(unwind)
|
|
|
|
});
|
|
|
|
}
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn constant_bool(&self, span: Span, val: bool) -> Rvalue<'tcx> {
|
2017-05-12 01:38:26 +03:00
|
|
|
Rvalue::Use(Operand::Constant(Box::new(Constant {
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2016-05-27 15:07:08 +03:00
|
|
|
ty: self.tcx.types.bool,
|
2017-08-04 00:41:44 +03:00
|
|
|
literal: Literal::Value {
|
2017-08-04 11:25:13 +03:00
|
|
|
value: self.tcx.mk_const(ty::Const {
|
|
|
|
val: ConstVal::Bool(val),
|
|
|
|
ty: self.tcx.types.bool
|
|
|
|
})
|
2017-08-04 00:41:44 +03:00
|
|
|
}
|
2017-05-12 01:38:26 +03:00
|
|
|
})))
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn set_drop_flag(&mut self, loc: Location, path: MovePathIndex, val: DropFlagState) {
|
|
|
|
if let Some(&flag) = self.drop_flags.get(&path) {
|
2016-06-07 19:21:56 +03:00
|
|
|
let span = self.patch.source_info_for_location(self.mir, loc).span;
|
2016-05-17 02:26:18 +03:00
|
|
|
let val = self.constant_bool(span, val.value());
|
2016-09-25 01:38:27 +02:00
|
|
|
self.patch.add_assign(loc, Lvalue::Local(flag), val);
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drop_flags_on_init(&mut self) {
|
2016-08-08 18:46:06 -07:00
|
|
|
let loc = Location { block: START_BLOCK, statement_index: 0 };
|
2016-06-07 19:21:56 +03:00
|
|
|
let span = self.patch.source_info_for_location(self.mir, loc).span;
|
2016-05-17 02:26:18 +03:00
|
|
|
let false_ = self.constant_bool(span, false);
|
|
|
|
for flag in self.drop_flags.values() {
|
2016-09-25 01:38:27 +02:00
|
|
|
self.patch.add_assign(loc, Lvalue::Local(*flag), false_.clone());
|
2016-05-17 02:26:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drop_flags_for_fn_rets(&mut self) {
|
2016-06-07 21:20:50 +03:00
|
|
|
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
|
2016-05-17 02:26:18 +03:00
|
|
|
if let TerminatorKind::Call {
|
|
|
|
destination: Some((ref lv, tgt)), cleanup: Some(_), ..
|
|
|
|
} = data.terminator().kind {
|
|
|
|
assert!(!self.patch.is_patched(bb));
|
|
|
|
|
2016-08-08 18:46:06 -07:00
|
|
|
let loc = Location { block: tgt, statement_index: 0 };
|
2016-05-17 02:26:18 +03:00
|
|
|
let path = self.move_data().rev_lookup.find(lv);
|
2016-06-11 23:47:28 +03:00
|
|
|
on_lookup_result_bits(
|
2016-05-27 15:07:08 +03:00
|
|
|
self.tcx, self.mir, self.move_data(), path,
|
|
|
|
|child| self.set_drop_flag(loc, child, DropFlagState::Present)
|
2016-05-17 02:26:18 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drop_flags_for_args(&mut self) {
|
2016-08-08 18:46:06 -07:00
|
|
|
let loc = Location { block: START_BLOCK, statement_index: 0 };
|
2017-06-26 14:57:26 +02:00
|
|
|
dataflow::drop_flag_effects_for_function_entry(
|
2016-05-27 15:07:08 +03:00
|
|
|
self.tcx, self.mir, self.env, |path, ds| {
|
2016-05-17 02:26:18 +03:00
|
|
|
self.set_drop_flag(loc, path, ds);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drop_flags_for_locs(&mut self) {
|
|
|
|
// We intentionally iterate only over the *old* basic blocks.
|
|
|
|
//
|
|
|
|
// Basic blocks created by drop elaboration update their
|
|
|
|
// drop flags by themselves, to avoid the drop flags being
|
|
|
|
// clobbered before they are read.
|
|
|
|
|
2016-06-07 21:20:50 +03:00
|
|
|
for (bb, data) in self.mir.basic_blocks().iter_enumerated() {
|
2016-05-17 02:26:18 +03:00
|
|
|
debug!("drop_flags_for_locs({:?})", data);
|
|
|
|
for i in 0..(data.statements.len()+1) {
|
|
|
|
debug!("drop_flag_for_locs: stmt {}", i);
|
|
|
|
let mut allow_initializations = true;
|
|
|
|
if i == data.statements.len() {
|
|
|
|
match data.terminator().kind {
|
|
|
|
TerminatorKind::Drop { .. } => {
|
|
|
|
// drop elaboration should handle that by itself
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
TerminatorKind::DropAndReplace { .. } => {
|
2016-05-29 22:01:06 +03:00
|
|
|
// this contains the move of the source and
|
2016-05-17 02:26:18 +03:00
|
|
|
// the initialization of the destination. We
|
2016-05-29 22:01:06 +03:00
|
|
|
// only want the former - the latter is handled
|
|
|
|
// by the elaboration code and must be done
|
|
|
|
// *after* the destination is dropped.
|
2016-05-17 02:26:18 +03:00
|
|
|
assert!(self.patch.is_patched(bb));
|
|
|
|
allow_initializations = false;
|
|
|
|
}
|
2017-06-02 12:52:09 +02:00
|
|
|
TerminatorKind::Resume => {
|
|
|
|
// It is possible for `Resume` to be patched
|
|
|
|
// (in particular it can be patched to be replaced with
|
|
|
|
// a Goto; see `MirPatch::new`).
|
|
|
|
}
|
2016-05-17 02:26:18 +03:00
|
|
|
_ => {
|
|
|
|
assert!(!self.patch.is_patched(bb));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-08 18:46:06 -07:00
|
|
|
let loc = Location { block: bb, statement_index: i };
|
2017-06-26 14:57:26 +02:00
|
|
|
dataflow::drop_flag_effects_for_location(
|
2016-05-27 15:07:08 +03:00
|
|
|
self.tcx, self.mir, self.env, loc, |path, ds| {
|
|
|
|
if ds == DropFlagState::Absent || allow_initializations {
|
2016-05-17 02:26:18 +03:00
|
|
|
self.set_drop_flag(loc, path, ds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// There may be a critical edge after this call,
|
|
|
|
// so mark the return as initialized *before* the
|
|
|
|
// call.
|
|
|
|
if let TerminatorKind::Call {
|
|
|
|
destination: Some((ref lv, _)), cleanup: None, ..
|
|
|
|
} = data.terminator().kind {
|
|
|
|
assert!(!self.patch.is_patched(bb));
|
|
|
|
|
2016-08-08 18:46:06 -07:00
|
|
|
let loc = Location { block: bb, statement_index: data.statements.len() };
|
2016-05-17 02:26:18 +03:00
|
|
|
let path = self.move_data().rev_lookup.find(lv);
|
2016-06-11 23:47:28 +03:00
|
|
|
on_lookup_result_bits(
|
2016-05-27 15:07:08 +03:00
|
|
|
self.tcx, self.mir, self.move_data(), path,
|
|
|
|
|child| self.set_drop_flag(loc, child, DropFlagState::Present)
|
2016-05-17 02:26:18 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|