1 //! Removes assignments to ZST places.
4 use rustc_middle::mir::{Body, StatementKind};
5 use rustc_middle::ty::{self, Ty, TyCtxt};
9 impl<'tcx> MirPass<'tcx> for RemoveZsts {
10 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
11 sess.mir_opt_level() > 0
14 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
15 // Avoid query cycles (generators require optimized MIR for layout).
16 if tcx.type_of(body.source.def_id()).is_generator() {
19 let param_env = tcx.param_env(body.source.def_id());
20 let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
21 let local_decls = &body.local_decls;
22 for block in basic_blocks {
23 for statement in block.statements.iter_mut() {
24 if let StatementKind::Assign(box (place, _)) | StatementKind::Deinit(box place) =
27 let place_ty = place.ty(local_decls, tcx).ty;
28 if !maybe_zst(place_ty) {
31 let Ok(layout) = tcx.layout_of(param_env.and(place_ty)) else {
37 if tcx.consider_optimizing(|| {
39 "RemoveZsts - Place: {:?} SourceInfo: {:?}",
40 place, statement.source_info
51 /// A cheap, approximate check to avoid unnecessary `layout_of` calls.
52 fn maybe_zst(ty: Ty<'_>) -> bool {
54 // maybe ZST (could be more precise)
59 | ty::Alias(ty::Opaque, ..) => true,
61 ty::FnDef(..) | ty::Never => true,
62 // unreachable or can't be ZST