2021-11-30 15:03:43 -08:00
|
|
|
//! This pass replaces a drop of a type that does not need dropping, with a goto.
|
|
|
|
//!
|
|
|
|
//! When the MIR is built, we check `needs_drop` before emitting a `Drop` for a place. This pass is
|
|
|
|
//! useful because (unlike MIR building) it runs after type checking, so it can make use of
|
2024-11-20 11:59:52 +01:00
|
|
|
//! `TypingMode::PostAnalysis` to provide more precise type information, especially about opaque
|
|
|
|
//! types.
|
2020-09-13 16:04:45 +02:00
|
|
|
|
|
|
|
use rustc_middle::mir::*;
|
2021-01-17 00:00:00 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2024-08-28 15:03:14 +10:00
|
|
|
use tracing::{debug, trace};
|
2020-09-13 16:04:45 +02:00
|
|
|
|
2020-09-19 15:21:39 +02:00
|
|
|
use super::simplify::simplify_cfg;
|
|
|
|
|
2024-08-28 09:39:59 +10:00
|
|
|
pub(super) struct RemoveUnneededDrops;
|
2020-09-13 16:04:45 +02:00
|
|
|
|
2024-09-03 15:45:27 +10:00
|
|
|
impl<'tcx> crate::MirPass<'tcx> for RemoveUnneededDrops {
|
2020-10-04 11:01:38 -07:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
trace!("Running RemoveUnneededDrops on {:?}", body.source);
|
2020-11-18 19:16:23 -05:00
|
|
|
|
2024-11-15 13:53:31 +01:00
|
|
|
let typing_env = body.typing_env(tcx);
|
2021-01-17 00:00:00 +00:00
|
|
|
let mut should_simplify = false;
|
2022-07-04 00:00:00 +00:00
|
|
|
for block in body.basic_blocks.as_mut() {
|
2021-01-17 00:00:00 +00:00
|
|
|
let terminator = block.terminator_mut();
|
|
|
|
if let TerminatorKind::Drop { place, target, .. } = terminator.kind {
|
2022-07-04 00:00:00 +00:00
|
|
|
let ty = place.ty(&body.local_decls, tcx);
|
2024-11-15 13:53:31 +01:00
|
|
|
if ty.ty.needs_drop(tcx, typing_env) {
|
2021-01-17 00:00:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
|
|
|
|
terminator.kind = TerminatorKind::Goto { target };
|
|
|
|
should_simplify = true;
|
|
|
|
}
|
2020-09-13 16:04:45 +02:00
|
|
|
}
|
2020-09-19 15:21:39 +02:00
|
|
|
|
|
|
|
// if we applied optimizations, we potentially have some cfg to cleanup to
|
|
|
|
// make it easier for further passes
|
|
|
|
if should_simplify {
|
2024-01-07 14:59:59 +00:00
|
|
|
simplify_cfg(body);
|
2020-09-19 15:21:39 +02:00
|
|
|
}
|
2020-09-13 16:04:45 +02:00
|
|
|
}
|
2024-12-09 19:34:51 +00:00
|
|
|
|
|
|
|
fn is_required(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2020-09-13 16:04:45 +02:00
|
|
|
}
|