rustc_mir_dataflow/
drop_flag_effects.rs

1use rustc_abi::VariantIdx;
2use rustc_middle::mir::{self, Body, Location, Terminator, TerminatorKind};
3use smallvec::SmallVec;
4use tracing::debug;
5
6use super::move_paths::{InitKind, LookupResult, MoveData, MovePathIndex};
7
8/// The value of an inserted drop flag.
9#[derive(Debug, PartialEq, Eq, Copy, Clone)]
10pub enum DropFlagState {
11    /// The tracked value is initialized and needs to be dropped when leaving its scope.
12    Present,
13
14    /// The tracked value is uninitialized or was moved out of and does not need to be dropped when
15    /// leaving its scope.
16    Absent,
17}
18
19impl DropFlagState {
20    pub fn value(self) -> bool {
21        match self {
22            DropFlagState::Present => true,
23            DropFlagState::Absent => false,
24        }
25    }
26}
27
28pub fn move_path_children_matching<'tcx, F>(
29    move_data: &MoveData<'tcx>,
30    path: MovePathIndex,
31    mut cond: F,
32) -> Option<MovePathIndex>
33where
34    F: FnMut(mir::PlaceElem<'tcx>) -> bool,
35{
36    let mut next_child = move_data.move_paths[path].first_child;
37    while let Some(child_index) = next_child {
38        let move_path_children = &move_data.move_paths[child_index];
39        if let Some(&elem) = move_path_children.place.projection.last() {
40            if cond(elem) {
41                return Some(child_index);
42            }
43        }
44        next_child = move_path_children.next_sibling;
45    }
46
47    None
48}
49
50pub fn on_lookup_result_bits<'tcx, F>(
51    move_data: &MoveData<'tcx>,
52    lookup_result: LookupResult,
53    each_child: F,
54) where
55    F: FnMut(MovePathIndex),
56{
57    match lookup_result {
58        LookupResult::Parent(..) => {
59            // access to untracked value - do not touch children
60        }
61        LookupResult::Exact(e) => on_all_children_bits(move_data, e, each_child),
62    }
63}
64
65pub fn on_all_children_bits<'tcx, F>(
66    move_data: &MoveData<'tcx>,
67    move_path_index: MovePathIndex,
68    mut each_child: F,
69) where
70    F: FnMut(MovePathIndex),
71{
72    fn on_all_children_bits<'tcx, F>(
73        move_data: &MoveData<'tcx>,
74        move_path_index: MovePathIndex,
75        each_child: &mut F,
76    ) where
77        F: FnMut(MovePathIndex),
78    {
79        each_child(move_path_index);
80
81        let mut next_child_index = move_data.move_paths[move_path_index].first_child;
82        while let Some(child_index) = next_child_index {
83            on_all_children_bits(move_data, child_index, each_child);
84            next_child_index = move_data.move_paths[child_index].next_sibling;
85        }
86    }
87    on_all_children_bits(move_data, move_path_index, &mut each_child);
88}
89
90pub fn drop_flag_effects_for_function_entry<'tcx, F>(
91    body: &Body<'tcx>,
92    move_data: &MoveData<'tcx>,
93    mut callback: F,
94) where
95    F: FnMut(MovePathIndex, DropFlagState),
96{
97    for arg in body.args_iter() {
98        let place = mir::Place::from(arg);
99        let lookup_result = move_data.rev_lookup.find(place.as_ref());
100        on_lookup_result_bits(move_data, lookup_result, |mpi| {
101            callback(mpi, DropFlagState::Present)
102        });
103    }
104}
105
106pub fn drop_flag_effects_for_location<'tcx, F>(
107    body: &Body<'tcx>,
108    move_data: &MoveData<'tcx>,
109    loc: Location,
110    mut callback: F,
111) where
112    F: FnMut(MovePathIndex, DropFlagState),
113{
114    debug!("drop_flag_effects_for_location({:?})", loc);
115
116    // first, move out of the RHS
117    for mi in &move_data.loc_map[loc] {
118        let path = mi.move_path_index(move_data);
119        debug!("moving out of path {:?}", move_data.move_paths[path]);
120
121        on_all_children_bits(move_data, path, |mpi| callback(mpi, DropFlagState::Absent))
122    }
123
124    // Drop does not count as a move but we should still consider the variable uninitialized.
125    if let Some(Terminator { kind: TerminatorKind::Drop { place, .. }, .. }) =
126        body.stmt_at(loc).right()
127        && let LookupResult::Exact(mpi) = move_data.rev_lookup.find(place.as_ref())
128    {
129        on_all_children_bits(move_data, mpi, |mpi| callback(mpi, DropFlagState::Absent))
130    }
131
132    debug!("drop_flag_effects: assignment for location({:?})", loc);
133
134    for_location_inits(move_data, loc, |mpi| callback(mpi, DropFlagState::Present));
135}
136
137fn for_location_inits<'tcx, F>(move_data: &MoveData<'tcx>, loc: Location, mut callback: F)
138where
139    F: FnMut(MovePathIndex),
140{
141    for ii in &move_data.init_loc_map[loc] {
142        let init = move_data.inits[*ii];
143        match init.kind {
144            InitKind::Deep => {
145                let path = init.path;
146
147                on_all_children_bits(move_data, path, &mut callback)
148            }
149            InitKind::Shallow => {
150                let mpi = init.path;
151                callback(mpi);
152            }
153            InitKind::NonPanicPathOnly => (),
154        }
155    }
156}
157
158/// Indicates which variants are inactive at a `SwitchInt` edge by listing their `VariantIdx`s or
159/// specifying the single active variant's `VariantIdx`.
160pub(crate) enum InactiveVariants {
161    Inactives(SmallVec<[VariantIdx; 4]>),
162    Active(VariantIdx),
163}
164
165impl InactiveVariants {
166    fn contains(&self, variant_idx: VariantIdx) -> bool {
167        match self {
168            InactiveVariants::Inactives(inactives) => inactives.contains(&variant_idx),
169            InactiveVariants::Active(active) => variant_idx != *active,
170        }
171    }
172}
173
174/// Calls `handle_inactive_variant` for each child move path of `enum_place` corresponding to an
175/// inactive variant at a particular `SwitchInt` edge.
176pub(crate) fn on_all_inactive_variants<'tcx>(
177    move_data: &MoveData<'tcx>,
178    enum_place: mir::Place<'tcx>,
179    inactive_variants: &InactiveVariants,
180    mut handle_inactive_variant: impl FnMut(MovePathIndex),
181) {
182    let LookupResult::Exact(enum_mpi) = move_data.rev_lookup.find(enum_place.as_ref()) else {
183        return;
184    };
185
186    let enum_path = &move_data.move_paths[enum_mpi];
187    for (variant_mpi, variant_path) in enum_path.children(&move_data.move_paths) {
188        // Because of the way we build the `MoveData` tree, each child should have exactly one more
189        // projection than `enum_place`. This additional projection must be a downcast since the
190        // base is an enum.
191        let (downcast, base_proj) = variant_path.place.projection.split_last().unwrap();
192        assert_eq!(enum_place.projection.len(), base_proj.len());
193
194        let mir::ProjectionElem::Downcast(_, variant_idx) = *downcast else {
195            unreachable!();
196        };
197
198        if inactive_variants.contains(variant_idx) {
199            on_all_children_bits(move_data, variant_mpi, |mpi| handle_inactive_variant(mpi));
200        }
201    }
202}