Skip to main content

rustc_mir_transform/
remove_unneeded_drops.rs

1//! This pass replaces a drop of a type that does not need dropping, with a goto.
2//!
3//! When the MIR is built, we check `needs_drop` before emitting a `Drop` for a place. This pass is
4//! useful because (unlike MIR building) it runs after type checking, so it can make use of
5//! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque
6//! types.
7
8use rustc_middle::mir::*;
9use rustc_middle::ty::TyCtxt;
10use tracing::{debug, trace};
11
12use super::simplify::simplify_cfg;
13
14pub(super) struct RemoveUnneededDrops;
15
16impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
17    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
18        trace!("Running RemoveUnneededDrops on {:?}", body.source);
19
20        let typing_env = body.typing_env(tcx);
21        let mut should_simplify = false;
22        for block in body.basic_blocks.as_mut() {
23            let terminator = block.terminator_mut();
24            let TerminatorKind::Drop { place, target, .. } = terminator.kind else { continue };
25            let ty = place.ty(&body.local_decls, tcx).ty;
26
27            if ty.needs_drop(tcx, typing_env) {
28                continue;
29            }
30            debug!("SUCCESS: replacing `drop` with goto({:?})", target);
31            terminator.kind = TerminatorKind::Goto { target };
32            should_simplify = true;
33        }
34
35        // if we applied optimizations, we potentially have some cfg to cleanup to
36        // make it easier for further passes
37        if should_simplify {
38            simplify_cfg(tcx, body);
39        }
40    }
41
42    fn is_required(&self) -> bool {
43        true
44    }
45}