1 use crate::dataflow::move_paths::{HasMoveData, LookupResult, MoveData, MovePathIndex};
2 use crate::dataflow::DataflowResults;
3 use crate::dataflow::MoveDataParamEnv;
4 use crate::dataflow::{self, do_dataflow, DebugFormatted};
5 use crate::dataflow::{drop_flag_effects_for_location, on_lookup_result_bits};
6 use crate::dataflow::{on_all_children_bits, on_all_drop_children_bits};
7 use crate::dataflow::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
8 use crate::transform::{MirPass, MirSource};
9 use crate::util::elaborate_drops::{elaborate_drop, DropFlagState, Unwind};
10 use crate::util::elaborate_drops::{DropElaborator, DropFlagMode, DropStyle};
11 use crate::util::patch::MirPatch;
13 use rustc::ty::layout::VariantIdx;
14 use rustc::ty::{self, TyCtxt};
15 use rustc_data_structures::fx::FxHashMap;
17 use rustc_index::bit_set::BitSet;
21 pub struct ElaborateDrops;
23 impl<'tcx> MirPass<'tcx> for ElaborateDrops {
24 fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut BodyAndCache<'tcx>) {
25 debug!("elaborate_drops({:?} @ {:?})", src, body.span);
27 let def_id = src.def_id();
28 let param_env = tcx.param_env(src.def_id()).with_reveal_all();
29 let move_data = match MoveData::gather_moves(body, tcx, param_env) {
30 Ok(move_data) => move_data,
31 Err(_) => bug!("No `move_errors` should be allowed in MIR borrowck"),
33 let elaborate_patch = {
35 let env = MoveDataParamEnv { move_data, param_env };
36 let dead_unwinds = find_dead_unwinds(tcx, body, def_id, &env);
37 let flow_inits = do_dataflow(
43 MaybeInitializedPlaces::new(tcx, body, &env),
44 |bd, p| DebugFormatted::new(&bd.move_data().move_paths[p]),
46 let flow_uninits = do_dataflow(
52 MaybeUninitializedPlaces::new(tcx, body, &env),
53 |bd, p| DebugFormatted::new(&bd.move_data().move_paths[p]),
62 drop_flags: Default::default(),
63 patch: MirPatch::new(body),
67 elaborate_patch.apply(body);
71 /// Returns the set of basic blocks whose unwind edges are known
72 /// to not be reachable, because they are `drop` terminators
73 /// that can't drop anything.
74 fn find_dead_unwinds<'tcx>(
77 def_id: hir::def_id::DefId,
78 env: &MoveDataParamEnv<'tcx>,
79 ) -> BitSet<BasicBlock> {
80 debug!("find_dead_unwinds({:?})", body.span);
81 // We only need to do this pass once, because unwind edges can only
82 // reach cleanup blocks, which can't have unwind edges themselves.
83 let mut dead_unwinds = BitSet::new_empty(body.basic_blocks().len());
84 let flow_inits = do_dataflow(
90 MaybeInitializedPlaces::new(tcx, body, &env),
91 |bd, p| DebugFormatted::new(&bd.move_data().move_paths[p]),
93 for (bb, bb_data) in body.basic_blocks().iter_enumerated() {
94 let location = match bb_data.terminator().kind {
95 TerminatorKind::Drop { ref location, unwind: Some(_), .. }
96 | TerminatorKind::DropAndReplace { ref location, unwind: Some(_), .. } => location,
100 let mut init_data = InitializationData {
101 live: flow_inits.sets().entry_set_for(bb.index()).to_owned(),
102 dead: BitSet::new_empty(env.move_data.move_paths.len()),
104 debug!("find_dead_unwinds @ {:?}: {:?}; init_data={:?}", bb, bb_data, init_data.live);
105 for stmt in 0..bb_data.statements.len() {
106 let loc = Location { block: bb, statement_index: stmt };
107 init_data.apply_location(tcx, body, env, loc);
110 let path = match env.move_data.rev_lookup.find(location.as_ref()) {
111 LookupResult::Exact(e) => e,
112 LookupResult::Parent(..) => {
113 debug!("find_dead_unwinds: has parent; skipping");
118 debug!("find_dead_unwinds @ {:?}: path({:?})={:?}", bb, location, path);
120 let mut maybe_live = false;
121 on_all_drop_children_bits(tcx, body, &env, path, |child| {
122 let (child_maybe_live, _) = init_data.state(child);
123 maybe_live |= child_maybe_live;
126 debug!("find_dead_unwinds @ {:?}: maybe_live={}", bb, maybe_live);
128 dead_unwinds.insert(bb);
135 struct InitializationData {
136 live: BitSet<MovePathIndex>,
137 dead: BitSet<MovePathIndex>,
140 impl InitializationData {
141 fn apply_location<'tcx>(
145 env: &MoveDataParamEnv<'tcx>,
148 drop_flag_effects_for_location(tcx, body, env, loc, |path, df| {
149 debug!("at location {:?}: setting {:?} to {:?}", loc, path, df);
151 DropFlagState::Present => {
152 self.live.insert(path);
153 self.dead.remove(path);
155 DropFlagState::Absent => {
156 self.dead.insert(path);
157 self.live.remove(path);
163 fn state(&self, path: MovePathIndex) -> (bool, bool) {
164 (self.live.contains(path), self.dead.contains(path))
168 struct Elaborator<'a, 'b, 'tcx> {
169 init_data: &'a InitializationData,
170 ctxt: &'a mut ElaborateDropsCtxt<'b, 'tcx>,
173 impl<'a, 'b, 'tcx> fmt::Debug for Elaborator<'a, 'b, 'tcx> {
174 fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
179 impl<'a, 'b, 'tcx> DropElaborator<'a, 'tcx> for Elaborator<'a, 'b, 'tcx> {
180 type Path = MovePathIndex;
182 fn patch(&mut self) -> &mut MirPatch<'tcx> {
186 fn body(&self) -> &'a Body<'tcx> {
190 fn tcx(&self) -> TyCtxt<'tcx> {
194 fn param_env(&self) -> ty::ParamEnv<'tcx> {
195 self.ctxt.param_env()
198 fn drop_style(&self, path: Self::Path, mode: DropFlagMode) -> DropStyle {
199 let ((maybe_live, maybe_dead), multipart) = match mode {
200 DropFlagMode::Shallow => (self.init_data.state(path), false),
201 DropFlagMode::Deep => {
202 let mut some_live = false;
203 let mut some_dead = false;
204 let mut children_count = 0;
205 on_all_drop_children_bits(self.tcx(), self.body(), self.ctxt.env, path, |child| {
206 let (live, dead) = self.init_data.state(child);
207 debug!("elaborate_drop: state({:?}) = {:?}", child, (live, dead));
212 ((some_live, some_dead), children_count != 1)
215 match (maybe_live, maybe_dead, multipart) {
216 (false, _, _) => DropStyle::Dead,
217 (true, false, _) => DropStyle::Static,
218 (true, true, false) => DropStyle::Conditional,
219 (true, true, true) => DropStyle::Open,
223 fn clear_drop_flag(&mut self, loc: Location, path: Self::Path, mode: DropFlagMode) {
225 DropFlagMode::Shallow => {
226 self.ctxt.set_drop_flag(loc, path, DropFlagState::Absent);
228 DropFlagMode::Deep => {
229 on_all_children_bits(
232 self.ctxt.move_data(),
234 |child| self.ctxt.set_drop_flag(loc, child, DropFlagState::Absent),
240 fn field_subpath(&self, path: Self::Path, field: Field) -> Option<Self::Path> {
241 dataflow::move_path_children_matching(self.ctxt.move_data(), path, |e| match e {
242 ProjectionElem::Field(idx, _) => *idx == field,
247 fn array_subpath(&self, path: Self::Path, index: u32, size: u32) -> Option<Self::Path> {
248 dataflow::move_path_children_matching(self.ctxt.move_data(), path, |e| match e {
249 ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
250 debug_assert!(size == *min_length, "min_length should be exact for arrays");
251 assert!(!from_end, "from_end should not be used for array element ConstantIndex");
258 fn deref_subpath(&self, path: Self::Path) -> Option<Self::Path> {
259 dataflow::move_path_children_matching(self.ctxt.move_data(), path, |e| {
260 *e == ProjectionElem::Deref
264 fn downcast_subpath(&self, path: Self::Path, variant: VariantIdx) -> Option<Self::Path> {
265 dataflow::move_path_children_matching(self.ctxt.move_data(), path, |e| match e {
266 ProjectionElem::Downcast(_, idx) => *idx == variant,
271 fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>> {
272 self.ctxt.drop_flag(path).map(Operand::Copy)
276 struct ElaborateDropsCtxt<'a, 'tcx> {
278 body: &'a Body<'tcx>,
279 env: &'a MoveDataParamEnv<'tcx>,
280 flow_inits: DataflowResults<'tcx, MaybeInitializedPlaces<'a, 'tcx>>,
281 flow_uninits: DataflowResults<'tcx, MaybeUninitializedPlaces<'a, 'tcx>>,
282 drop_flags: FxHashMap<MovePathIndex, Local>,
283 patch: MirPatch<'tcx>,
286 impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
287 fn move_data(&self) -> &'b MoveData<'tcx> {
291 fn param_env(&self) -> ty::ParamEnv<'tcx> {
295 fn initialization_data_at(&self, loc: Location) -> InitializationData {
296 let mut data = InitializationData {
297 live: self.flow_inits.sets().entry_set_for(loc.block.index()).to_owned(),
298 dead: self.flow_uninits.sets().entry_set_for(loc.block.index()).to_owned(),
300 for stmt in 0..loc.statement_index {
305 Location { block: loc.block, statement_index: stmt },
311 fn create_drop_flag(&mut self, index: MovePathIndex, span: Span) {
313 let patch = &mut self.patch;
314 debug!("create_drop_flag({:?})", self.body.span);
315 self.drop_flags.entry(index).or_insert_with(|| patch.new_internal(tcx.types.bool, span));
318 fn drop_flag(&mut self, index: MovePathIndex) -> Option<Place<'tcx>> {
319 self.drop_flags.get(&index).map(|t| Place::from(*t))
322 /// create a patch that elaborates all drops in the input
324 fn elaborate(mut self) -> MirPatch<'tcx> {
325 self.collect_drop_flags();
327 self.elaborate_drops();
329 self.drop_flags_on_init();
330 self.drop_flags_for_fn_rets();
331 self.drop_flags_for_args();
332 self.drop_flags_for_locs();
337 fn collect_drop_flags(&mut self) {
338 for (bb, data) in self.body.basic_blocks().iter_enumerated() {
339 let terminator = data.terminator();
340 let location = match terminator.kind {
341 TerminatorKind::Drop { ref location, .. }
342 | TerminatorKind::DropAndReplace { ref location, .. } => location,
346 let init_data = self.initialization_data_at(Location {
348 statement_index: data.statements.len(),
351 let path = self.move_data().rev_lookup.find(location.as_ref());
352 debug!("collect_drop_flags: {:?}, place {:?} ({:?})", bb, location, path);
354 let path = match path {
355 LookupResult::Exact(e) => e,
356 LookupResult::Parent(None) => continue,
357 LookupResult::Parent(Some(parent)) => {
358 let (_maybe_live, maybe_dead) = init_data.state(parent);
361 terminator.source_info.span,
362 "drop of untracked, uninitialized value {:?}, place {:?} ({:?})",
372 on_all_drop_children_bits(self.tcx, self.body, self.env, path, |child| {
373 let (maybe_live, maybe_dead) = init_data.state(child);
375 "collect_drop_flags: collecting {:?} from {:?}@{:?} - {:?}",
379 (maybe_live, maybe_dead)
381 if maybe_live && maybe_dead {
382 self.create_drop_flag(child, terminator.source_info.span)
388 fn elaborate_drops(&mut self) {
389 for (bb, data) in self.body.basic_blocks().iter_enumerated() {
390 let loc = Location { block: bb, statement_index: data.statements.len() };
391 let terminator = data.terminator();
393 let resume_block = self.patch.resume_block();
394 match terminator.kind {
395 TerminatorKind::Drop { ref location, target, unwind } => {
396 let init_data = self.initialization_data_at(loc);
397 match self.move_data().rev_lookup.find(location.as_ref()) {
398 LookupResult::Exact(path) => elaborate_drop(
399 &mut Elaborator { init_data: &init_data, ctxt: self },
400 terminator.source_info,
407 Unwind::To(Option::unwrap_or(unwind, resume_block))
411 LookupResult::Parent(..) => {
413 terminator.source_info.span,
414 "drop of untracked value {:?}",
420 TerminatorKind::DropAndReplace { ref location, ref value, target, unwind } => {
421 assert!(!data.is_cleanup);
423 self.elaborate_replace(loc, location, value, target, unwind);
430 /// Elaborate a MIR `replace` terminator. This instruction
431 /// is not directly handled by codegen, and therefore
432 /// must be desugared.
434 /// The desugaring drops the location if needed, and then writes
435 /// the value (including setting the drop flag) over it in *both* arms.
437 /// The `replace` terminator can also be called on places that
438 /// are not tracked by elaboration (for example,
439 /// `replace x[i] <- tmp0`). The borrow checker requires that
440 /// these locations are initialized before the assignment,
441 /// so we just generate an unconditional drop.
442 fn elaborate_replace(
445 location: &Place<'tcx>,
446 value: &Operand<'tcx>,
448 unwind: Option<BasicBlock>,
451 let data = &self.body[bb];
452 let terminator = data.terminator();
453 assert!(!data.is_cleanup, "DropAndReplace in unwind path not supported");
455 let assign = Statement {
456 kind: StatementKind::Assign(box (location.clone(), Rvalue::Use(value.clone()))),
457 source_info: terminator.source_info,
460 let unwind = unwind.unwrap_or_else(|| self.patch.resume_block());
461 let unwind = self.patch.new_block(BasicBlockData {
462 statements: vec![assign.clone()],
463 terminator: Some(Terminator {
464 kind: TerminatorKind::Goto { target: unwind },
470 let target = self.patch.new_block(BasicBlockData {
471 statements: vec![assign],
472 terminator: Some(Terminator { kind: TerminatorKind::Goto { target }, ..*terminator }),
476 match self.move_data().rev_lookup.find(location.as_ref()) {
477 LookupResult::Exact(path) => {
478 debug!("elaborate_drop_and_replace({:?}) - tracked {:?}", terminator, path);
479 let init_data = self.initialization_data_at(loc);
482 &mut Elaborator { init_data: &init_data, ctxt: self },
483 terminator.source_info,
490 on_all_children_bits(self.tcx, self.body, self.move_data(), path, |child| {
492 Location { block: target, statement_index: 0 },
494 DropFlagState::Present,
497 Location { block: unwind, statement_index: 0 },
499 DropFlagState::Present,
503 LookupResult::Parent(parent) => {
504 // drop and replace behind a pointer/array/whatever. The location
505 // must be initialized.
506 debug!("elaborate_drop_and_replace({:?}) - untracked {:?}", terminator, parent);
507 self.patch.patch_terminator(
509 TerminatorKind::Drop {
510 location: location.clone(),
512 unwind: Some(unwind),
519 fn constant_bool(&self, span: Span, val: bool) -> Rvalue<'tcx> {
520 Rvalue::Use(Operand::Constant(Box::new(Constant {
523 literal: ty::Const::from_bool(self.tcx, val),
527 fn set_drop_flag(&mut self, loc: Location, path: MovePathIndex, val: DropFlagState) {
528 if let Some(&flag) = self.drop_flags.get(&path) {
529 let span = self.patch.source_info_for_location(self.body, loc).span;
530 let val = self.constant_bool(span, val.value());
531 self.patch.add_assign(loc, Place::from(flag), val);
535 fn drop_flags_on_init(&mut self) {
536 let loc = Location::START;
537 let span = self.patch.source_info_for_location(self.body, loc).span;
538 let false_ = self.constant_bool(span, false);
539 for flag in self.drop_flags.values() {
540 self.patch.add_assign(loc, Place::from(*flag), false_.clone());
544 fn drop_flags_for_fn_rets(&mut self) {
545 for (bb, data) in self.body.basic_blocks().iter_enumerated() {
546 if let TerminatorKind::Call {
547 destination: Some((ref place, tgt)),
550 } = data.terminator().kind
552 assert!(!self.patch.is_patched(bb));
554 let loc = Location { block: tgt, statement_index: 0 };
555 let path = self.move_data().rev_lookup.find(place.as_ref());
556 on_lookup_result_bits(self.tcx, self.body, self.move_data(), path, |child| {
557 self.set_drop_flag(loc, child, DropFlagState::Present)
563 fn drop_flags_for_args(&mut self) {
564 let loc = Location::START;
565 dataflow::drop_flag_effects_for_function_entry(self.tcx, self.body, self.env, |path, ds| {
566 self.set_drop_flag(loc, path, ds);
570 fn drop_flags_for_locs(&mut self) {
571 // We intentionally iterate only over the *old* basic blocks.
573 // Basic blocks created by drop elaboration update their
574 // drop flags by themselves, to avoid the drop flags being
575 // clobbered before they are read.
577 for (bb, data) in self.body.basic_blocks().iter_enumerated() {
578 debug!("drop_flags_for_locs({:?})", data);
579 for i in 0..(data.statements.len() + 1) {
580 debug!("drop_flag_for_locs: stmt {}", i);
581 let mut allow_initializations = true;
582 if i == data.statements.len() {
583 match data.terminator().kind {
584 TerminatorKind::Drop { .. } => {
585 // drop elaboration should handle that by itself
588 TerminatorKind::DropAndReplace { .. } => {
589 // this contains the move of the source and
590 // the initialization of the destination. We
591 // only want the former - the latter is handled
592 // by the elaboration code and must be done
593 // *after* the destination is dropped.
594 assert!(self.patch.is_patched(bb));
595 allow_initializations = false;
597 TerminatorKind::Resume => {
598 // It is possible for `Resume` to be patched
599 // (in particular it can be patched to be replaced with
600 // a Goto; see `MirPatch::new`).
603 assert!(!self.patch.is_patched(bb));
607 let loc = Location { block: bb, statement_index: i };
608 dataflow::drop_flag_effects_for_location(
614 if ds == DropFlagState::Absent || allow_initializations {
615 self.set_drop_flag(loc, path, ds)
621 // There may be a critical edge after this call,
622 // so mark the return as initialized *before* the
624 if let TerminatorKind::Call {
625 destination: Some((ref place, _)), cleanup: None, ..
626 } = data.terminator().kind
628 assert!(!self.patch.is_patched(bb));
630 let loc = Location { block: bb, statement_index: data.statements.len() };
631 let path = self.move_data().rev_lookup.find(place.as_ref());
632 on_lookup_result_bits(self.tcx, self.body, self.move_data(), path, |child| {
633 self.set_drop_flag(loc, child, DropFlagState::Present)