rustc_mir_transform/
remove_uninit_drops.rs

1use rustc_abi::FieldIdx;
2use rustc_index::bit_set::MixedBitSet;
3use rustc_middle::mir::{Body, TerminatorKind};
4use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, VariantDef};
5use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
6use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
7use rustc_mir_dataflow::{Analysis, MaybeReachable, move_path_children_matching};
8
9/// Removes `Drop` terminators whose target is known to be uninitialized at
10/// that point.
11///
12/// This is redundant with drop elaboration, but we need to do it prior to const-checking, and
13/// running const-checking after drop elaboration makes it optimization dependent, causing issues
14/// like [#90770].
15///
16/// [#90770]: https://github.com/rust-lang/rust/issues/90770
17pub(super) struct RemoveUninitDrops;
18
19impl<'tcx> crate::MirPass<'tcx> for RemoveUninitDrops {
20    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
21        let typing_env = body.typing_env(tcx);
22        let move_data = MoveData::gather_moves(body, tcx, |ty| ty.needs_drop(tcx, typing_env));
23
24        let mut maybe_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
25            .iterate_to_fixpoint(tcx, body, Some("remove_uninit_drops"))
26            .into_results_cursor(body);
27
28        let mut to_remove = vec![];
29        for (bb, block) in body.basic_blocks.iter_enumerated() {
30            let terminator = block.terminator();
31            let TerminatorKind::Drop { place, .. } = &terminator.kind else { continue };
32
33            maybe_inits.seek_before_primary_effect(body.terminator_loc(bb));
34            let MaybeReachable::Reachable(maybe_inits) = maybe_inits.get() else { continue };
35
36            // If there's no move path for the dropped place, it's probably a `Deref`. Let it alone.
37            let LookupResult::Exact(mpi) = move_data.rev_lookup.find(place.as_ref()) else {
38                continue;
39            };
40
41            let should_keep = is_needs_drop_and_init(
42                tcx,
43                typing_env,
44                maybe_inits,
45                &move_data,
46                place.ty(body, tcx).ty,
47                mpi,
48            );
49            if !should_keep {
50                to_remove.push(bb)
51            }
52        }
53
54        for bb in to_remove {
55            let block = &mut body.basic_blocks_mut()[bb];
56
57            let TerminatorKind::Drop { target, .. } = &block.terminator().kind else {
58                unreachable!()
59            };
60
61            // Replace block terminator with `Goto`.
62            block.terminator_mut().kind = TerminatorKind::Goto { target: *target };
63        }
64    }
65
66    fn is_required(&self) -> bool {
67        true
68    }
69}
70
71fn is_needs_drop_and_init<'tcx>(
72    tcx: TyCtxt<'tcx>,
73    typing_env: ty::TypingEnv<'tcx>,
74    maybe_inits: &MixedBitSet<MovePathIndex>,
75    move_data: &MoveData<'tcx>,
76    ty: Ty<'tcx>,
77    mpi: MovePathIndex,
78) -> bool {
79    // No need to look deeper if the root is definitely uninit or if it has no `Drop` impl.
80    if !maybe_inits.contains(mpi) || !ty.needs_drop(tcx, typing_env) {
81        return false;
82    }
83
84    let field_needs_drop_and_init = |(f, f_ty, mpi)| {
85        let child = move_path_children_matching(move_data, mpi, |x| x.is_field_to(f));
86        let Some(mpi) = child else {
87            return Ty::needs_drop(f_ty, tcx, typing_env);
88        };
89
90        is_needs_drop_and_init(tcx, typing_env, maybe_inits, move_data, f_ty, mpi)
91    };
92
93    // This pass is only needed for const-checking, so it doesn't handle as many cases as
94    // `DropCtxt::open_drop`, since they aren't relevant in a const-context.
95    match ty.kind() {
96        ty::Adt(adt, args) => {
97            let dont_elaborate = adt.is_union() || adt.is_manually_drop() || adt.has_dtor(tcx);
98            if dont_elaborate {
99                return true;
100            }
101
102            // Look at all our fields, or if we are an enum all our variants and their fields.
103            //
104            // If a field's projection *is not* present in `MoveData`, it has the same
105            // initializedness as its parent (maybe init).
106            //
107            // If its projection *is* present in `MoveData`, then the field may have been moved
108            // from separate from its parent. Recurse.
109            adt.variants().iter_enumerated().any(|(vid, variant)| {
110                // Enums have multiple variants, which are discriminated with a `Downcast`
111                // projection. Structs have a single variant, and don't use a `Downcast`
112                // projection.
113                let mpi = if adt.is_enum() {
114                    let downcast =
115                        move_path_children_matching(move_data, mpi, |x| x.is_downcast_to(vid));
116                    let Some(dc_mpi) = downcast else {
117                        return variant_needs_drop(tcx, typing_env, args, variant);
118                    };
119
120                    dc_mpi
121                } else {
122                    mpi
123                };
124
125                variant
126                    .fields
127                    .iter()
128                    .enumerate()
129                    .map(|(f, field)| (FieldIdx::from_usize(f), field.ty(tcx, args), mpi))
130                    .any(field_needs_drop_and_init)
131            })
132        }
133
134        ty::Tuple(fields) => fields
135            .iter()
136            .enumerate()
137            .map(|(f, f_ty)| (FieldIdx::from_usize(f), f_ty, mpi))
138            .any(field_needs_drop_and_init),
139
140        _ => true,
141    }
142}
143
144fn variant_needs_drop<'tcx>(
145    tcx: TyCtxt<'tcx>,
146    typing_env: ty::TypingEnv<'tcx>,
147    args: GenericArgsRef<'tcx>,
148    variant: &VariantDef,
149) -> bool {
150    variant.fields.iter().any(|field| {
151        let f_ty = field.ty(tcx, args);
152        f_ty.needs_drop(tcx, typing_env)
153    })
154}