Skip to main content

rustc_borrowck/type_check/liveness/
trace.rs

1use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2use rustc_index::bit_set::DenseBitSet;
3use rustc_index::interval::IntervalSet;
4use rustc_infer::infer::canonical::QueryRegionConstraints;
5use rustc_infer::traits::TraitErrors;
6use rustc_middle::mir::{BasicBlock, Body, ConstraintCategory, HasLocalDecls, Local, Location};
7use rustc_middle::traits::query::DropckOutlivesResult;
8use rustc_middle::ty::relate::Relate;
9use rustc_middle::ty::{Ty, TyCtxt, TypeVisitable, TypeVisitableExt};
10use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
11use rustc_mir_dataflow::move_paths::{HasMoveData, MoveData, MovePathIndex};
12use rustc_mir_dataflow::points::{DenseLocationMap, PointIndex};
13use rustc_mir_dataflow::{Analysis, ResultsCursor};
14use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
15use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
16use rustc_trait_selection::traits::ObligationCtxt;
17use rustc_trait_selection::traits::outlives_for_liveness::FreeRegionsVisitor;
18use rustc_trait_selection::traits::query::dropck_outlives;
19use rustc_trait_selection::traits::query::type_op::{DropckOutlives, TypeOpOutput};
20use tracing::debug;
21
22use crate::polonius;
23use crate::region_infer::values;
24use crate::type_check::liveness::local_use_map::LocalUseMap;
25use crate::type_check::{NormalizeLocation, TypeChecker};
26
27/// This is the heart of the liveness computation. For each variable X
28/// that requires a liveness computation, it walks over all the uses
29/// of X and does a reverse depth-first search ("trace") through the
30/// MIR. This search stops when we find a definition of that variable.
31/// The points visited in this search is the USE-LIVE set for the variable;
32/// of those points is added to all the regions that appear in the variable's
33/// type.
34///
35/// We then also walks through each *drop* of those variables and does
36/// another search, stopping when we reach a use or definition. This
37/// is the DROP-LIVE set of points. Each of the points in the
38/// DROP-LIVE set are to the liveness sets for regions found in the
39/// `dropck_outlives` result of the variable's type (in particular,
40/// this respects `#[may_dangle]` annotations).
41pub(super) fn trace<'tcx>(
42    typeck: &mut TypeChecker<'_, 'tcx>,
43    location_map: &DenseLocationMap,
44    move_data: &MoveData<'tcx>,
45    relevant_live_locals: Vec<Local>,
46    boring_locals: Vec<Local>,
47) {
48    let _timer = typeck.tcx().prof.generic_activity("borrowck_liveness_trace");
49
50    let local_use_map = &LocalUseMap::build(&relevant_live_locals, location_map, typeck.body);
51    let cx = LivenessContext {
52        typeck,
53        flow_inits: None,
54        location_map,
55        local_use_map,
56        move_data,
57        drop_data: FxIndexMap::default(),
58    };
59
60    let mut results = LivenessResults::new(cx);
61
62    results.add_extra_drop_facts(&relevant_live_locals);
63
64    results.compute_for_all_locals(relevant_live_locals);
65
66    results.dropck_boring_locals(boring_locals);
67}
68
69/// Contextual state for the type-liveness coroutine.
70struct LivenessContext<'a, 'typeck, 'tcx> {
71    /// Current type-checker, giving us our inference context etc.
72    ///
73    /// This also stores the body we're currently analyzing.
74    typeck: &'a mut TypeChecker<'typeck, 'tcx>,
75
76    /// Defines the `PointIndex` mapping
77    location_map: &'a DenseLocationMap,
78
79    /// Mapping to/from the various indices used for initialization tracking.
80    move_data: &'a MoveData<'tcx>,
81
82    /// Cache for the results of `dropck_outlives` query.
83    drop_data: FxIndexMap<Ty<'tcx>, DropData<'tcx>>,
84
85    /// Results of dataflow tracking which variables (and paths) have been
86    /// initialized. Computed lazily when needed by drop-liveness.
87    flow_inits: Option<ResultsCursor<'a, 'tcx, MaybeInitializedPlaces<'a, 'tcx>>>,
88
89    /// Index indicating where each variable is assigned, used, or
90    /// dropped.
91    local_use_map: &'a LocalUseMap,
92}
93
94struct DropData<'tcx> {
95    dropck_result: DropckOutlivesResult<'tcx>,
96    region_constraint_data: Option<&'tcx QueryRegionConstraints<'tcx>>,
97}
98
99struct LivenessResults<'a, 'typeck, 'tcx> {
100    cx: LivenessContext<'a, 'typeck, 'tcx>,
101
102    /// Set of points that define the current local.
103    defs: DenseBitSet<PointIndex>,
104
105    /// Points where the current variable is "use live" -- meaning
106    /// that there is a future "full use" that may use its value.
107    use_live_at: IntervalSet<PointIndex>,
108
109    /// Points where the current variable is "drop live" -- meaning
110    /// that there is no future "full use" that may use its value, but
111    /// there is a future drop.
112    drop_live_at: IntervalSet<PointIndex>,
113
114    /// Locations where drops may occur.
115    drop_locations: Vec<Location>,
116
117    /// Stack used when doing (reverse) DFS.
118    stack: Vec<PointIndex>,
119}
120
121impl<'a, 'typeck, 'tcx> LivenessResults<'a, 'typeck, 'tcx> {
122    fn new(cx: LivenessContext<'a, 'typeck, 'tcx>) -> Self {
123        let num_points = cx.location_map.num_points();
124        LivenessResults {
125            cx,
126            defs: DenseBitSet::new_empty(num_points),
127            use_live_at: IntervalSet::new(num_points),
128            drop_live_at: IntervalSet::new(num_points),
129            drop_locations: ::alloc::vec::Vec::new()vec![],
130            stack: ::alloc::vec::Vec::new()vec![],
131        }
132    }
133
134    fn compute_for_all_locals(&mut self, relevant_live_locals: Vec<Local>) {
135        for local in relevant_live_locals {
136            self.reset_local_state();
137            self.add_defs_for(local);
138            self.compute_use_live_points_for(local);
139            self.compute_drop_live_points_for(local);
140
141            let local_ty = self.cx.body().local_decls[local].ty;
142
143            if !self.use_live_at.is_empty() {
144                self.cx.add_use_live_facts_for(local_ty, &self.use_live_at);
145            }
146
147            if !self.drop_live_at.is_empty() {
148                self.cx.add_drop_live_facts_for(
149                    local,
150                    local_ty,
151                    &self.drop_locations,
152                    &self.drop_live_at,
153                );
154            }
155        }
156    }
157
158    /// Runs dropck for locals whose liveness isn't relevant. This is
159    /// necessary to eagerly detect unbound recursion during drop glue computation.
160    ///
161    /// These are all the locals which do not potentially reference a region local
162    /// to this body. Locals which only reference free regions are always drop-live
163    /// and can therefore safely be dropped.
164    fn dropck_boring_locals(&mut self, boring_locals: Vec<Local>) {
165        for local in boring_locals {
166            let local_ty = self.cx.body().local_decls[local].ty;
167            let local_span = self.cx.body().local_decls[local].source_info.span;
168            let drop_data = self.cx.drop_data.entry(local_ty).or_insert_with({
169                let typeck = &self.cx.typeck;
170                move || LivenessContext::compute_drop_data(typeck, local_ty, local_span)
171            });
172
173            drop_data.dropck_result.report_overflows(
174                self.cx.typeck.infcx.tcx,
175                self.cx.typeck.body.local_decls[local].source_info.span,
176                local_ty,
177            );
178        }
179    }
180
181    /// Add extra drop facts needed for Polonius.
182    ///
183    /// Add facts for all locals with free regions, since regions may outlive
184    /// the function body only at certain nodes in the CFG.
185    fn add_extra_drop_facts(&mut self, relevant_live_locals: &[Local]) {
186        // This collect is more necessary than immediately apparent
187        // because these facts go into `add_drop_live_facts_for()`,
188        // which also writes to `polonius_facts`, and so this is genuinely
189        // a simultaneous overlapping mutable borrow.
190        // FIXME for future hackers: investigate whether this is
191        // actually necessary; these facts come from Polonius
192        // and probably maybe plausibly does not need to go back in.
193        // It may be necessary to just pick out the parts of
194        // `add_drop_live_facts_for()` that make sense.
195        let Some(facts) = self.cx.typeck.polonius_facts.as_ref() else { return };
196        let facts_to_add: Vec<_> = {
197            let relevant_live_locals: FxIndexSet<_> =
198                relevant_live_locals.iter().copied().collect();
199
200            facts
201                .var_dropped_at
202                .iter()
203                .filter_map(|&(local, location_index)| {
204                    let local_ty = self.cx.body().local_decls[local].ty;
205                    if relevant_live_locals.contains(&local) || !local_ty.has_free_regions() {
206                        return None;
207                    }
208
209                    let location = self.cx.typeck.location_table.to_location(location_index);
210                    Some((local, local_ty, location))
211                })
212                .collect()
213        };
214
215        let live_at = IntervalSet::new(self.cx.location_map.num_points());
216        for (local, local_ty, location) in facts_to_add {
217            self.cx.add_drop_live_facts_for(local, local_ty, &[location], &live_at);
218        }
219    }
220
221    /// Clear the value of fields that are "per local variable".
222    fn reset_local_state(&mut self) {
223        self.defs.clear();
224        self.use_live_at.clear();
225        self.drop_live_at.clear();
226        self.drop_locations.clear();
227        if !self.stack.is_empty() {
    ::core::panicking::panic("assertion failed: self.stack.is_empty()")
};assert!(self.stack.is_empty());
228    }
229
230    /// Adds the definitions of `local` into `self.defs`.
231    fn add_defs_for(&mut self, local: Local) {
232        for def in self.cx.local_use_map.defs(local) {
233            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:233",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(233u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("- defined at {0:?}",
                                                    def) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("- defined at {:?}", def);
234            self.defs.insert(def);
235        }
236    }
237
238    /// Computes all points where local is "use live" -- meaning its
239    /// current value may be used later (except by a drop). This is
240    /// done by walking backwards from each use of `local` until we
241    /// find a `def` of local.
242    ///
243    /// Requires `add_defs_for(local)` to have been executed.
244    fn compute_use_live_points_for(&mut self, local: Local) {
245        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:245",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(245u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_use_live_points_for(local={0:?})",
                                                    local) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_use_live_points_for(local={:?})", local);
246
247        self.stack.extend(self.cx.local_use_map.uses(local));
248        while let Some(p) = self.stack.pop() {
249            // We are live in this block from the closest to us of:
250            //
251            //  * Inclusively, the block start
252            //  * Exclusively, the previous definition (if it's in this block)
253            //  * Exclusively, the previous live_at setting (an optimization)
254            let block_start = self.cx.location_map.to_block_start(p);
255            let previous_defs = self.defs.last_set_in(block_start..=p);
256            let previous_live_at = self.use_live_at.last_set_in(block_start..=p);
257
258            let exclusive_start = match (previous_defs, previous_live_at) {
259                (Some(a), Some(b)) => Some(std::cmp::max(a, b)),
260                (Some(a), None) | (None, Some(a)) => Some(a),
261                (None, None) => None,
262            };
263
264            if let Some(exclusive) = exclusive_start {
265                self.use_live_at.insert_range(exclusive + 1..=p);
266
267                // If we have a bound after the start of the block, we should
268                // not add the predecessors for this block.
269                continue;
270            } else {
271                // Add all the elements of this block.
272                self.use_live_at.insert_range(block_start..=p);
273
274                // Then add the predecessors for this block, which are the
275                // terminators of predecessor basic blocks. Push those onto the
276                // stack so that the next iteration(s) will process them.
277
278                let block = self.cx.location_map.to_location(block_start).block;
279                self.stack.extend(
280                    self.cx.body().basic_blocks.predecessors()[block]
281                        .iter()
282                        .map(|&pred_bb| self.cx.body().terminator_loc(pred_bb))
283                        .map(|pred_loc| self.cx.location_map.point_from_location(pred_loc)),
284                );
285            }
286        }
287    }
288
289    /// Computes all points where local is "drop live" -- meaning its
290    /// current value may be dropped later (but not used). This is
291    /// done by iterating over the drops of `local` where `local` (or
292    /// some subpart of `local`) is initialized. For each such drop,
293    /// we walk backwards until we find a point where `local` is
294    /// either defined or use-live.
295    ///
296    /// Requires `compute_use_live_points_for` and `add_defs_for` to
297    /// have been executed.
298    fn compute_drop_live_points_for(&mut self, local: Local) {
299        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:299",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(299u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for(local={0:?})",
                                                    local) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for(local={:?})", local);
300
301        let Some(mpi) = self.cx.move_data.rev_lookup.find_local(local) else { return };
302        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:302",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(302u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for: mpi = {0:?}",
                                                    mpi) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for: mpi = {:?}", mpi);
303
304        // Find the drops where `local` is initialized.
305        for drop_point in self.cx.local_use_map.drops(local) {
306            let location = self.cx.location_map.to_location(drop_point);
307            if true {
    {
        match (&self.cx.body().terminator_loc(location.block), &location) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(self.cx.body().terminator_loc(location.block), location,);
308
309            if self.cx.initialized_at_terminator(location.block, mpi)
310                && self.drop_live_at.insert(drop_point)
311            {
312                self.drop_locations.push(location);
313                self.stack.push(drop_point);
314            }
315        }
316
317        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:317",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(317u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for: drop_locations={0:?}",
                                                    self.drop_locations) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for: drop_locations={:?}", self.drop_locations);
318
319        // Reverse DFS. But for drops, we do it a bit differently.
320        // The stack only ever stores *terminators of blocks*. Within
321        // a block, we walk back the statements in an inner loop.
322        while let Some(term_point) = self.stack.pop() {
323            self.compute_drop_live_points_for_block(mpi, term_point);
324        }
325    }
326
327    /// Executes one iteration of the drop-live analysis loop.
328    ///
329    /// The parameter `mpi` is the `MovePathIndex` of the local variable
330    /// we are currently analyzing.
331    ///
332    /// The point `term_point` represents some terminator in the MIR,
333    /// where the local `mpi` is drop-live on entry to that terminator.
334    ///
335    /// This method adds all drop-live points within the block and --
336    /// where applicable -- pushes the terminators of preceding blocks
337    /// onto `self.stack`.
338    fn compute_drop_live_points_for_block(&mut self, mpi: MovePathIndex, term_point: PointIndex) {
339        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:339",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(339u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block(mpi={0:?}, term_point={1:?})",
                                                    self.cx.move_data.move_paths[mpi].place,
                                                    self.cx.location_map.to_location(term_point)) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
340            "compute_drop_live_points_for_block(mpi={:?}, term_point={:?})",
341            self.cx.move_data.move_paths[mpi].place,
342            self.cx.location_map.to_location(term_point),
343        );
344
345        // We are only invoked with terminators where `mpi` is
346        // drop-live on entry.
347        if true {
    if !self.drop_live_at.contains(term_point) {
        ::core::panicking::panic("assertion failed: self.drop_live_at.contains(term_point)")
    };
};debug_assert!(self.drop_live_at.contains(term_point));
348
349        // Otherwise, scan backwards through the statements in the
350        // block. One of them may be either a definition or use
351        // live point.
352        let term_location = self.cx.location_map.to_location(term_point);
353        if true {
    {
        match (&self.cx.body().terminator_loc(term_location.block),
                &term_location) {
            (left_val, right_val) => {
                if !(*left_val == *right_val) {
                    let kind = ::core::panicking::AssertKind::Eq;
                    ::core::panicking::assert_failed(kind, &*left_val,
                        &*right_val, ::core::option::Option::None);
                }
            }
        }
    };
};debug_assert_eq!(self.cx.body().terminator_loc(term_location.block), term_location,);
354        let block = term_location.block;
355        let entry_point = self.cx.location_map.entry_point(term_location.block);
356        for p in (entry_point..term_point).rev() {
357            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:357",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(357u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block: p = {0:?}",
                                                    self.cx.location_map.to_location(p)) as
                                            &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
358                "compute_drop_live_points_for_block: p = {:?}",
359                self.cx.location_map.to_location(p)
360            );
361
362            if self.defs.contains(p) {
363                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:363",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(363u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block: def site")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for_block: def site");
364                return;
365            }
366
367            if self.use_live_at.contains(p) {
368                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:368",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(368u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block: use-live at {0:?}",
                                                    p) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for_block: use-live at {:?}", p);
369                return;
370            }
371
372            if !self.drop_live_at.insert(p) {
373                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:373",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(373u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block: already drop-live")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for_block: already drop-live");
374                return;
375            }
376        }
377
378        let body = self.cx.typeck.body;
379        for &pred_block in body.basic_blocks.predecessors()[block].iter() {
380            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:380",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(380u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block: pred_block = {0:?}",
                                                    pred_block) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for_block: pred_block = {:?}", pred_block,);
381
382            // Check whether the variable is (at least partially)
383            // initialized at the exit of this predecessor. If so, we
384            // want to enqueue it on our list. If not, go check the
385            // next block.
386            //
387            // Note that we only need to check whether `live_local`
388            // became de-initialized at basic block boundaries. If it
389            // were to become de-initialized within the block, that
390            // would have been a "use-live" transition in the earlier
391            // loop, and we'd have returned already.
392            //
393            // NB. It's possible that the pred-block ends in a call
394            // which stores to the variable; in that case, the
395            // variable may be uninitialized "at exit" because this
396            // call only considers the *unconditional effects* of the
397            // terminator. *But*, in that case, the terminator is also
398            // a *definition* of the variable, in which case we want
399            // to stop the search anyhow. (But see Note 1 below.)
400            if !self.cx.initialized_at_exit(pred_block, mpi) {
401                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:401",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(401u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block: not initialized")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for_block: not initialized");
402                continue;
403            }
404
405            let pred_term_loc = self.cx.body().terminator_loc(pred_block);
406            let pred_term_point = self.cx.location_map.point_from_location(pred_term_loc);
407
408            // If the terminator of this predecessor either *assigns*
409            // our value or is a "normal use", then stop.
410            if self.defs.contains(pred_term_point) {
411                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:411",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(411u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block: defined at {0:?}",
                                                    pred_term_loc) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for_block: defined at {:?}", pred_term_loc);
412                continue;
413            }
414
415            if self.use_live_at.contains(pred_term_point) {
416                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:416",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(416u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block: use-live at {0:?}",
                                                    pred_term_loc) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for_block: use-live at {:?}", pred_term_loc);
417                continue;
418            }
419
420            // Otherwise, we are drop-live on entry to the terminator,
421            // so walk it.
422            if self.drop_live_at.insert(pred_term_point) {
423                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:423",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(423u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_live_points_for_block: pushed to stack")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_live_points_for_block: pushed to stack");
424                self.stack.push(pred_term_point);
425            }
426        }
427
428        // Note 1. There is a weird scenario that you might imagine
429        // being problematic here, but which actually cannot happen.
430        // The problem would be if we had a variable that *is* initialized
431        // (but dead) on entry to the terminator, and where the current value
432        // will be dropped in the case of unwind. In that case, we ought to
433        // consider `X` to be drop-live in between the last use and call.
434        // Here is the example:
435        //
436        // ```
437        // BB0 {
438        //   X = ...
439        //   use(X); // last use
440        //   ...     // <-- X ought to be drop-live here
441        //   X = call() goto BB1 unwind BB2
442        // }
443        //
444        // BB1 {
445        //   DROP(X)
446        // }
447        //
448        // BB2 {
449        //   DROP(X)
450        // }
451        // ```
452        //
453        // However, the current code would, when walking back from BB2,
454        // simply stop and never explore BB0. This seems bad! But it turns
455        // out this code is flawed anyway -- note that the existing value of
456        // `X` would leak in the case where unwinding did *not* occur.
457        //
458        // What we *actually* generate is a store to a temporary
459        // for the call (`TMP = call()...`) and then a
460        // `Drop(X)` followed by `X = TMP`  to swap that with `X`.
461    }
462}
463
464impl<'a, 'typeck, 'tcx> LivenessContext<'a, 'typeck, 'tcx> {
465    /// Computes the `MaybeInitializedPlaces` dataflow analysis if it hasn't been done already.
466    ///
467    /// In practice, the results of this dataflow analysis are rarely needed but can be expensive to
468    /// compute on big functions, so we compute them lazily as a fast path when:
469    /// - there are relevant live locals
470    /// - there are drop points for these relevant live locals.
471    ///
472    /// This happens as part of the drop-liveness computation: it's the only place checking for
473    /// maybe-initializedness of `MovePathIndex`es.
474    fn flow_inits(&mut self) -> &mut ResultsCursor<'a, 'tcx, MaybeInitializedPlaces<'a, 'tcx>> {
475        self.flow_inits.get_or_insert_with(|| {
476            let tcx = self.typeck.tcx();
477            let body = self.typeck.body;
478            // FIXME: reduce the `MaybeInitializedPlaces` domain to the useful `MovePath`s.
479            //
480            // This dataflow analysis computes maybe-initializedness of all move paths, which
481            // explains why it can be expensive on big functions. But this data is only used in
482            // drop-liveness. Therefore, most of the move paths computed here are ultimately unused,
483            // even if the results are computed lazily and "no relevant live locals with drop
484            // points" is the common case.
485            //
486            // So we only need the ones for 1) relevant live locals 2) that have drop points. That's
487            // a much, much smaller domain: in our benchmarks, when it's not zero (the most likely
488            // case), there are a few dozens compared to e.g. thousands or tens of thousands of
489            // locals and move paths.
490            let _timer = tcx.prof.generic_activity("borrowck_dataflow_maybe_inits");
491            let flow_inits = MaybeInitializedPlaces::new(tcx, body, self.move_data)
492                .iterate_to_fixpoint(tcx, body, Some("borrowck"))
493                .into_results_cursor(body);
494            flow_inits
495        })
496    }
497}
498
499impl<'tcx> LivenessContext<'_, '_, 'tcx> {
500    fn body(&self) -> &Body<'tcx> {
501        self.typeck.body
502    }
503
504    /// Returns `true` if the local variable (or some part of it) is initialized at the current
505    /// cursor position. Callers should call one of the `seek` methods immediately before to point
506    /// the cursor to the desired location.
507    fn initialized_at_curr_loc(&mut self, mpi: MovePathIndex) -> bool {
508        let flow_inits = self.flow_inits();
509        let state = flow_inits.get();
510        if state.contains(mpi) {
511            return true;
512        }
513
514        let move_paths = &flow_inits.analysis().move_data().move_paths;
515        move_paths[mpi].find_descendant(move_paths, |mpi| state.contains(mpi)).is_some()
516    }
517
518    /// Returns `true` if the local variable (or some part of it) is initialized in
519    /// the terminator of `block`. We need to check this to determine if a
520    /// DROP of some local variable will have an effect -- note that
521    /// drops, as they may unwind, are always terminators.
522    fn initialized_at_terminator(&mut self, block: BasicBlock, mpi: MovePathIndex) -> bool {
523        let terminator_location = self.body().terminator_loc(block);
524        self.flow_inits().seek_before_primary_effect(terminator_location);
525        self.initialized_at_curr_loc(mpi)
526    }
527
528    /// Returns `true` if the path `mpi` (or some part of it) is initialized at
529    /// the exit of `block`.
530    ///
531    /// **Warning:** Does not account for the result of `Call`
532    /// instructions.
533    fn initialized_at_exit(&mut self, block: BasicBlock, mpi: MovePathIndex) -> bool {
534        let terminator_location = self.body().terminator_loc(block);
535        self.flow_inits().seek_after_primary_effect(terminator_location);
536        self.initialized_at_curr_loc(mpi)
537    }
538
539    /// Stores the result that all regions in `value` are live for the
540    /// points `live_at`.
541    fn add_use_live_facts_for(&mut self, value: Ty<'tcx>, live_at: &IntervalSet<PointIndex>) {
542        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:542",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(542u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("add_use_live_facts_for(value={0:?})",
                                                    value) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("add_use_live_facts_for(value={:?})", value);
543        Self::make_all_regions_live(self.location_map, self.typeck, value, live_at);
544    }
545
546    /// Some variable with type `live_ty` is "drop live" at `location`
547    /// -- i.e., it may be dropped later. This means that *some* of
548    /// the regions in its type must be live at `location`. The
549    /// precise set will depend on the dropck constraints, and in
550    /// particular this takes `#[may_dangle]` into account.
551    fn add_drop_live_facts_for(
552        &mut self,
553        dropped_local: Local,
554        dropped_ty: Ty<'tcx>,
555        drop_locations: &[Location],
556        live_at: &IntervalSet<PointIndex>,
557    ) {
558        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:558",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(558u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("add_drop_live_constraint(dropped_local={0:?}, dropped_ty={1:?}, drop_locations={2:?}, live_at={3:?})",
                                                    dropped_local, dropped_ty, drop_locations,
                                                    values::pretty_print_points(self.location_map,
                                                        live_at.iter())) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
559            "add_drop_live_constraint(\
560             dropped_local={:?}, \
561             dropped_ty={:?}, \
562             drop_locations={:?}, \
563             live_at={:?})",
564            dropped_local,
565            dropped_ty,
566            drop_locations,
567            values::pretty_print_points(self.location_map, live_at.iter()),
568        );
569
570        let local_span = self.body().local_decls()[dropped_local].source_info.span;
571        let drop_data = self.drop_data.entry(dropped_ty).or_insert_with({
572            let typeck = &self.typeck;
573            move || Self::compute_drop_data(typeck, dropped_ty, local_span)
574        });
575
576        if let Some(data) = &drop_data.region_constraint_data {
577            for &drop_location in drop_locations {
578                self.typeck.push_region_constraints(
579                    drop_location.to_locations(),
580                    ConstraintCategory::Boring,
581                    data,
582                );
583            }
584        }
585
586        drop_data.dropck_result.report_overflows(
587            self.typeck.infcx.tcx,
588            self.typeck.body.source_info(*drop_locations.first().unwrap()).span,
589            dropped_ty,
590        );
591
592        // All things in the `outlives` array may be touched by
593        // the destructor and must be live at this point.
594        for &kind in &drop_data.dropck_result.kinds {
595            Self::make_all_regions_live(self.location_map, self.typeck, kind, live_at);
596            polonius::legacy::emit_drop_facts(
597                self.typeck.tcx(),
598                dropped_local,
599                &kind,
600                self.typeck.universal_regions,
601                self.typeck.polonius_facts,
602            );
603        }
604    }
605
606    fn make_all_regions_live(
607        location_map: &DenseLocationMap,
608        typeck: &mut TypeChecker<'_, 'tcx>,
609        value: impl TypeVisitable<TyCtxt<'tcx>> + Relate<TyCtxt<'tcx>>,
610        live_at: &IntervalSet<PointIndex>,
611    ) {
612        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:612",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(612u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("make_all_regions_live(value={0:?})",
                                                    value) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("make_all_regions_live(value={:?})", value);
613        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:613",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(613u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("make_all_regions_live: live_at={0}",
                                                    values::pretty_print_points(location_map, live_at.iter()))
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
614            "make_all_regions_live: live_at={}",
615            values::pretty_print_points(location_map, live_at.iter()),
616        );
617
618        value.visit_with(&mut FreeRegionsVisitor {
619            tcx: typeck.tcx(),
620            param_env: typeck.infcx.param_env,
621            op: |r| {
622                let live_region_vid = typeck.universal_regions.to_region_vid(r);
623
624                typeck.constraints.liveness_constraints.add_points(live_region_vid, live_at);
625            },
626        });
627
628        // When using `-Zpolonius=next`, we record the variance of each live region.
629        if let Some(polonius_context) = typeck.polonius_context.as_mut() {
630            polonius_context.record_live_region_variance(
631                typeck.infcx.tcx,
632                typeck.universal_regions,
633                value,
634            );
635        }
636    }
637
638    fn compute_drop_data(
639        typeck: &TypeChecker<'_, 'tcx>,
640        dropped_ty: Ty<'tcx>,
641        span: Span,
642    ) -> DropData<'tcx> {
643        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/type_check/liveness/trace.rs:643",
                        "rustc_borrowck::type_check::liveness::trace",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/type_check/liveness/trace.rs"),
                        ::tracing_core::__macro_support::Option::Some(643u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::type_check::liveness::trace"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("compute_drop_data(dropped_ty={0:?})",
                                                    dropped_ty) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("compute_drop_data(dropped_ty={:?})", dropped_ty);
644
645        let goal = DropckOutlives { dropped_ty };
646
647        match typeck.infcx.fully_perform(goal, DUMMY_SP) {
648            Ok(TypeOpOutput { output, constraints, .. }) => {
649                DropData { dropck_result: output, region_constraint_data: constraints }
650            }
651            Err(ErrorGuaranteed { .. }) => {
652                // We don't run dropck on HIR, and dropck looks inside fields of
653                // types, so there's no guarantee that it succeeds. We also
654                // can't rely on the `ErrorGuaranteed` from `fully_perform` here
655                // because it comes from delay_span_bug.
656                //
657                // Do this inside of a probe because we don't particularly care (or want)
658                // any region side-effects of this operation in our infcx.
659                typeck.infcx.probe(|_| {
660                    let ocx = ObligationCtxt::new_with_diagnostics(&typeck.infcx);
661                    let errors = match dropck_outlives::compute_dropck_outlives_with_errors(
662                        &ocx,
663                        typeck.infcx.param_env.and(goal),
664                        span,
665                    ) {
666                        Ok(_) => ocx.evaluate_obligations_error_on_ambiguity(),
667                        Err(e) => TraitErrors::HasErrors(e),
668                    };
669
670                    // Could have no errors if a type lowering error, say, caused the query
671                    // to fail.
672                    if let TraitErrors::HasErrors(errors) = errors {
673                        typeck.infcx.err_ctxt().report_fulfillment_errors(errors);
674                    }
675                });
676                DropData { dropck_result: Default::default(), region_constraint_data: None }
677            }
678        }
679    }
680}