Skip to main content

rustc_borrowck/diagnostics/
mod.rs

1//! Borrow checker diagnostics.
2
3use std::collections::BTreeMap;
4
5use rustc_abi::{FieldIdx, VariantIdx};
6use rustc_data_structures::fx::FxIndexMap;
7use rustc_errors::formatting::DiagMessageAddArg;
8use rustc_errors::{Applicability, Diag, DiagMessage, EmissionGuarantee, MultiSpan, listify, msg};
9use rustc_hir::def::{CtorKind, Namespace};
10use rustc_hir::{
11    self as hir, CoroutineKind, GenericBound, LangItem, WhereBoundPredicate, WherePredicateKind,
12};
13use rustc_index::{IndexSlice, IndexVec};
14use rustc_infer::infer::{BoundRegionConversionTime, NllRegionVariableOrigin};
15use rustc_infer::traits::SelectionError;
16use rustc_middle::mir::{
17    AggregateKind, CallSource, ConstOperand, ConstraintCategory, FakeReadCause, Local, LocalInfo,
18    LocalKind, Location, Operand, Place, PlaceRef, PlaceTy, ProjectionElem, Rvalue, Statement,
19    StatementKind, Terminator, TerminatorKind, VarDebugInfoContents, find_self_call,
20};
21use rustc_middle::ty::print::Print;
22use rustc_middle::ty::{self, Ty, TyCtxt};
23use rustc_middle::{bug, span_bug};
24use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveOutIndex};
25use rustc_span::def_id::LocalDefId;
26use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Spanned, Symbol, sym};
27use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
28use rustc_trait_selection::error_reporting::traits::call_kind::{CallDesugaringKind, call_kind};
29use rustc_trait_selection::infer::InferCtxtExt;
30use rustc_trait_selection::traits::{
31    FulfillmentError, FulfillmentErrorCode, type_known_to_meet_bound_modulo_regions,
32};
33use tracing::debug;
34
35use super::MirBorrowckCtxt;
36use super::borrow_set::BorrowData;
37use crate::constraints::OutlivesConstraint;
38use crate::nll::ConstraintDescription;
39use crate::session_diagnostics::{
40    CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
41    CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
42};
43
44mod find_all_local_uses;
45mod find_use;
46mod outlives_suggestion;
47mod region_name;
48mod var_name;
49
50mod bound_region_errors;
51mod conflict_errors;
52mod explain_borrow;
53mod move_errors;
54mod mutability_errors;
55mod opaque_types;
56mod region_errors;
57
58pub(crate) use bound_region_errors::{ToUniverseInfo, UniverseInfo};
59pub(crate) use move_errors::{IllegalMoveOriginKind, MoveError};
60pub(crate) use mutability_errors::AccessKind;
61pub(crate) use outlives_suggestion::OutlivesSuggestionBuilder;
62pub(crate) use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
63pub(crate) use region_name::{RegionName, RegionNameSource};
64pub(crate) use rustc_trait_selection::error_reporting::traits::call_kind::CallKind;
65
66pub(super) struct DescribePlaceOpt {
67    including_downcast: bool,
68
69    /// Enable/Disable tuple fields.
70    /// For example `x` tuple. if it's `true` `x.0`. Otherwise `x`
71    including_tuple_field: bool,
72}
73
74pub(super) struct IncludingTupleField(pub(super) bool);
75
76enum BufferedDiag<'infcx> {
77    Error(Diag<'infcx>),
78    NonError(Diag<'infcx, ()>),
79}
80
81impl<'infcx> BufferedDiag<'infcx> {
82    fn sort_span(&self) -> Span {
83        match self {
84            BufferedDiag::Error(diag) => diag.sort_span,
85            BufferedDiag::NonError(diag) => diag.sort_span,
86        }
87    }
88}
89
90#[derive(#[automatically_derived]
impl<'infcx, 'tcx> ::core::default::Default for
    BorrowckDiagnosticsBuffer<'infcx, 'tcx> {
    #[inline]
    fn default() -> BorrowckDiagnosticsBuffer<'infcx, 'tcx> {
        BorrowckDiagnosticsBuffer {
            buffered_move_errors: ::core::default::Default::default(),
            buffered_mut_errors: ::core::default::Default::default(),
            buffered_diags: ::core::default::Default::default(),
        }
    }
}Default)]
91pub(crate) struct BorrowckDiagnosticsBuffer<'infcx, 'tcx> {
92    /// This field keeps track of move errors that are to be reported for given move indices.
93    ///
94    /// There are situations where many errors can be reported for a single move out (see
95    /// #53807) and we want only the best of those errors.
96    ///
97    /// The `report_use_of_moved_or_uninitialized` function checks this map and replaces the
98    /// diagnostic (if there is one) if the `Place` of the error being reported is a prefix of
99    /// the `Place` of the previous most diagnostic. This happens instead of buffering the
100    /// error. Once all move errors have been reported, any diagnostics in this map are added
101    /// to the buffer to be emitted.
102    ///
103    /// `BTreeMap` is used to preserve the order of insertions when iterating. This is necessary
104    /// when errors in the map are being re-added to the error buffer so that errors with the
105    /// same primary span come out in a consistent order.
106    buffered_move_errors: BTreeMap<Vec<MoveOutIndex>, (PlaceRef<'tcx>, Diag<'infcx>)>,
107
108    buffered_mut_errors: FxIndexMap<Span, (Diag<'infcx>, usize)>,
109
110    /// Buffer of diagnostics to be reported. A mixture of error and non-error diagnostics.
111    buffered_diags: Vec<BufferedDiag<'infcx>>,
112}
113
114impl<'infcx, 'tcx> BorrowckDiagnosticsBuffer<'infcx, 'tcx> {
115    pub(crate) fn buffer_non_error(&mut self, diag: Diag<'infcx, ()>) {
116        self.buffered_diags.push(BufferedDiag::NonError(diag));
117    }
118}
119
120impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
121    pub(crate) fn buffer_error(&mut self, diag: Diag<'infcx>) {
122        self.diags_buffer.buffered_diags.push(BufferedDiag::Error(diag));
123    }
124
125    pub(crate) fn buffer_non_error(&mut self, diag: Diag<'infcx, ()>) {
126        self.diags_buffer.buffer_non_error(diag);
127    }
128
129    pub(crate) fn buffer_move_error(
130        &mut self,
131        move_out_indices: Vec<MoveOutIndex>,
132        place_and_err: (PlaceRef<'tcx>, Diag<'infcx>),
133    ) -> bool {
134        if let Some((_, diag)) =
135            self.diags_buffer.buffered_move_errors.insert(move_out_indices, place_and_err)
136        {
137            // Cancel the old diagnostic so we don't ICE
138            diag.cancel();
139            false
140        } else {
141            true
142        }
143    }
144
145    pub(crate) fn get_buffered_mut_error(&mut self, span: Span) -> Option<(Diag<'infcx>, usize)> {
146        // FIXME(#120456) - is `swap_remove` correct?
147        self.diags_buffer.buffered_mut_errors.swap_remove(&span)
148    }
149
150    pub(crate) fn buffer_mut_error(&mut self, span: Span, diag: Diag<'infcx>, count: usize) {
151        self.diags_buffer.buffered_mut_errors.insert(span, (diag, count));
152    }
153
154    pub(crate) fn emit_errors(&mut self) -> Option<ErrorGuaranteed> {
155        let mut res = self.infcx.tainted_by_errors();
156
157        // Buffer any move errors that we collected and de-duplicated.
158        for (_, (_, diag)) in std::mem::take(&mut self.diags_buffer.buffered_move_errors) {
159            // We have already set tainted for this error, so just buffer it.
160            self.buffer_error(diag);
161        }
162        for (_, (mut diag, count)) in std::mem::take(&mut self.diags_buffer.buffered_mut_errors) {
163            if count > 10 {
164                diag.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("...and {0} other attempted mutable borrows",
                count - 10))
    })format!("...and {} other attempted mutable borrows", count - 10));
165            }
166            self.buffer_error(diag);
167        }
168
169        if !self.diags_buffer.buffered_diags.is_empty() {
170            self.diags_buffer.buffered_diags.sort_by_key(|buffered_diag| buffered_diag.sort_span());
171            for buffered_diag in self.diags_buffer.buffered_diags.drain(..) {
172                match buffered_diag {
173                    BufferedDiag::Error(diag) => res = Some(diag.emit()),
174                    BufferedDiag::NonError(diag) => diag.emit(),
175                }
176            }
177        }
178
179        res
180    }
181
182    pub(crate) fn has_buffered_diags(&self) -> bool {
183        self.diags_buffer.buffered_diags.is_empty()
184    }
185
186    pub(crate) fn has_move_error(
187        &self,
188        move_out_indices: &[MoveOutIndex],
189    ) -> Option<&(PlaceRef<'tcx>, Diag<'infcx>)> {
190        self.diags_buffer.buffered_move_errors.get(move_out_indices)
191    }
192
193    /// Uses `body.var_debug_info` to find the symbol
194    fn local_name(&self, index: Local) -> Option<Symbol> {
195        *self.local_names().get(index)?
196    }
197
198    fn local_names(&self) -> &IndexSlice<Local, Option<Symbol>> {
199        self.local_names.get_or_init(|| {
200            let mut local_names = IndexVec::from_elem(None, &self.body.local_decls);
201            for var_debug_info in &self.body.var_debug_info {
202                if let VarDebugInfoContents::Place(place) = var_debug_info.value {
203                    if let Some(local) = place.as_local() {
204                        if let Some(prev_name) = local_names[local]
205                            && var_debug_info.name != prev_name
206                        {
207                            ::rustc_middle::util::bug::span_bug_fmt(var_debug_info.source_info.span,
    format_args!("local {0:?} has many names (`{1}` vs `{2}`)", local,
        prev_name, var_debug_info.name));span_bug!(
208                                var_debug_info.source_info.span,
209                                "local {:?} has many names (`{}` vs `{}`)",
210                                local,
211                                prev_name,
212                                var_debug_info.name
213                            );
214                        }
215                        local_names[local] = Some(var_debug_info.name);
216                    }
217                }
218            }
219            local_names
220        })
221    }
222}
223
224impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
225    /// Adds a suggestion when a closure is invoked twice with a moved variable or when a closure
226    /// is moved after being invoked.
227    ///
228    /// ```text
229    /// note: closure cannot be invoked more than once because it moves the variable `dict` out of
230    ///       its environment
231    ///   --> $DIR/issue-42065.rs:16:29
232    ///    |
233    /// LL |         for (key, value) in dict {
234    ///    |                             ^^^^
235    /// ```
236    pub(super) fn add_moved_or_invoked_closure_note(
237        &self,
238        location: Location,
239        place: PlaceRef<'tcx>,
240        diag: &mut Diag<'infcx>,
241    ) -> bool {
242        {
    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/diagnostics/mod.rs:242",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(242u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("add_moved_or_invoked_closure_note: location={0:?} place={1:?}",
                                                    location, place) as &dyn Value))])
            });
    } else { ; }
};debug!("add_moved_or_invoked_closure_note: location={:?} place={:?}", location, place);
243        let mut target = place.local_or_deref_local();
244        for stmt in &self.body[location.block].statements[location.statement_index..] {
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/diagnostics/mod.rs:245",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(245u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("add_moved_or_invoked_closure_note: stmt={0:?} target={1:?}",
                                                    stmt, target) as &dyn Value))])
            });
    } else { ; }
};debug!("add_moved_or_invoked_closure_note: stmt={:?} target={:?}", stmt, target);
246            if let StatementKind::Assign(box (into, Rvalue::Use(from))) = &stmt.kind {
247                {
    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/diagnostics/mod.rs:247",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(247u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("add_fnonce_closure_note: into={0:?} from={1:?}",
                                                    into, from) as &dyn Value))])
            });
    } else { ; }
};debug!("add_fnonce_closure_note: into={:?} from={:?}", into, from);
248                match from {
249                    Operand::Copy(place) | Operand::Move(place)
250                        if target == place.local_or_deref_local() =>
251                    {
252                        target = into.local_or_deref_local()
253                    }
254                    _ => {}
255                }
256            }
257        }
258
259        // Check if we are attempting to call a closure after it has been invoked.
260        let terminator = self.body[location.block].terminator();
261        {
    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/diagnostics/mod.rs:261",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(261u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("add_moved_or_invoked_closure_note: terminator={0:?}",
                                                    terminator) as &dyn Value))])
            });
    } else { ; }
};debug!("add_moved_or_invoked_closure_note: terminator={:?}", terminator);
262        if let TerminatorKind::Call {
263            func: Operand::Constant(box ConstOperand { const_, .. }),
264            args,
265            ..
266        } = &terminator.kind
267            && let ty::FnDef(id, _) = *const_.ty().kind()
268        {
269            {
    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/diagnostics/mod.rs:269",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(269u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("add_moved_or_invoked_closure_note: id={0:?}",
                                                    id) as &dyn Value))])
            });
    } else { ; }
};debug!("add_moved_or_invoked_closure_note: id={:?}", id);
270            if self.infcx.tcx.is_lang_item(self.infcx.tcx.parent(id), LangItem::FnOnce) {
271                let closure = match args.first() {
272                    Some(Spanned { node: Operand::Copy(place) | Operand::Move(place), .. })
273                        if target == place.local_or_deref_local() =>
274                    {
275                        place.local_or_deref_local().unwrap()
276                    }
277                    _ => return false,
278                };
279
280                {
    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/diagnostics/mod.rs:280",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(280u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("add_moved_or_invoked_closure_note: closure={0:?}",
                                                    closure) as &dyn Value))])
            });
    } else { ; }
};debug!("add_moved_or_invoked_closure_note: closure={:?}", closure);
281                if let ty::Closure(did, _) = self.body.local_decls[closure].ty.kind() {
282                    let did = did.expect_local();
283                    if let Some((span, hir_place)) = self.infcx.tcx.closure_kind_origin(did) {
284                        diag.subdiagnostic(OnClosureNote::InvokedTwice {
285                            place_name: &ty::place_to_string_for_capture(self.infcx.tcx, hir_place),
286                            span: *span,
287                        });
288                        return true;
289                    }
290                }
291            }
292        }
293
294        // Check if we are just moving a closure after it has been invoked.
295        if let Some(target) = target
296            && let ty::Closure(did, _) = self.body.local_decls[target].ty.kind()
297        {
298            let did = did.expect_local();
299            if let Some((span, hir_place)) = self.infcx.tcx.closure_kind_origin(did) {
300                diag.subdiagnostic(OnClosureNote::MovedTwice {
301                    place_name: &ty::place_to_string_for_capture(self.infcx.tcx, hir_place),
302                    span: *span,
303                });
304                return true;
305            }
306        }
307        false
308    }
309
310    /// End-user visible description of `place` if one can be found.
311    /// If the place is a temporary for instance, `"value"` will be returned.
312    pub(super) fn describe_any_place(&self, place_ref: PlaceRef<'tcx>) -> String {
313        match self.describe_place(place_ref) {
314            Some(mut descr) => {
315                // Surround descr with `backticks`.
316                descr.reserve(2);
317                descr.insert(0, '`');
318                descr.push('`');
319                descr
320            }
321            None => "value".to_string(),
322        }
323    }
324
325    /// End-user visible description of `place` if one can be found.
326    /// If the place is a temporary for instance, `None` will be returned.
327    pub(super) fn describe_place(&self, place_ref: PlaceRef<'tcx>) -> Option<String> {
328        self.describe_place_with_options(
329            place_ref,
330            DescribePlaceOpt { including_downcast: false, including_tuple_field: true },
331        )
332    }
333
334    /// End-user visible description of `place` if one can be found. If the place is a temporary
335    /// for instance, `None` will be returned.
336    /// `IncludingDowncast` parameter makes the function return `None` if `ProjectionElem` is
337    /// `Downcast` and `IncludingDowncast` is true
338    pub(super) fn describe_place_with_options(
339        &self,
340        place: PlaceRef<'tcx>,
341        opt: DescribePlaceOpt,
342    ) -> Option<String> {
343        let local = place.local;
344        if self.body.local_decls[local]
345            .source_info
346            .span
347            .in_external_macro(self.infcx.tcx.sess.source_map())
348        {
349            return None;
350        }
351
352        let mut autoderef_index = None;
353        let mut buf = String::new();
354        let mut ok = self.append_local_to_string(local, &mut buf);
355
356        for (index, elem) in place.projection.into_iter().enumerate() {
357            match elem {
358                ProjectionElem::Deref => {
359                    if index == 0 {
360                        if self.body.local_decls[local].is_ref_for_guard() {
361                            continue;
362                        }
363                        if let LocalInfo::StaticRef { def_id, .. } =
364                            *self.body.local_decls[local].local_info()
365                        {
366                            buf.push_str(self.infcx.tcx.item_name(def_id).as_str());
367                            ok = Ok(());
368                            continue;
369                        }
370                    }
371                    if let Some(field) = self.is_upvar_field_projection(PlaceRef {
372                        local,
373                        projection: place.projection.split_at(index + 1).0,
374                    }) {
375                        let var_index = field.index();
376                        buf = self.upvars[var_index].to_string(self.infcx.tcx);
377                        ok = Ok(());
378                        if !self.upvars[var_index].is_by_ref() {
379                            buf.insert(0, '*');
380                        }
381                    } else {
382                        if autoderef_index.is_none() {
383                            autoderef_index = match place.projection.iter().rposition(|elem| {
384                                !#[allow(non_exhaustive_omitted_patterns)] match elem {
    ProjectionElem::Deref | ProjectionElem::Downcast(..) => true,
    _ => false,
}matches!(
385                                    elem,
386                                    ProjectionElem::Deref | ProjectionElem::Downcast(..)
387                                )
388                            }) {
389                                Some(index) => Some(index + 1),
390                                None => Some(0),
391                            };
392                        }
393                        if index >= autoderef_index.unwrap() {
394                            buf.insert(0, '*');
395                        }
396                    }
397                }
398                ProjectionElem::Downcast(..) if opt.including_downcast => return None,
399                ProjectionElem::Downcast(..) => (),
400                ProjectionElem::OpaqueCast(..) => (),
401                ProjectionElem::UnwrapUnsafeBinder(_) => (),
402                ProjectionElem::Field(field, _ty) => {
403                    // FIXME(project-rfc_2229#36): print capture precisely here.
404                    if let Some(field) = self.is_upvar_field_projection(PlaceRef {
405                        local,
406                        projection: place.projection.split_at(index + 1).0,
407                    }) {
408                        buf = self.upvars[field.index()].to_string(self.infcx.tcx);
409                        ok = Ok(());
410                    } else {
411                        let field_name = self.describe_field(
412                            PlaceRef { local, projection: place.projection.split_at(index).0 },
413                            *field,
414                            IncludingTupleField(opt.including_tuple_field),
415                        );
416                        if let Some(field_name_str) = field_name {
417                            buf.push('.');
418                            buf.push_str(&field_name_str);
419                        }
420                    }
421                }
422                ProjectionElem::Index(index) => {
423                    buf.push('[');
424                    if self.append_local_to_string(*index, &mut buf).is_err() {
425                        buf.push('_');
426                    }
427                    buf.push(']');
428                }
429                ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
430                    // Since it isn't possible to borrow an element on a particular index and
431                    // then use another while the borrow is held, don't output indices details
432                    // to avoid confusing the end-user
433                    buf.push_str("[..]");
434                }
435            }
436        }
437        ok.ok().map(|_| buf)
438    }
439
440    fn describe_name(&self, place: PlaceRef<'tcx>) -> Option<Symbol> {
441        for elem in place.projection.into_iter() {
442            match elem {
443                ProjectionElem::Downcast(Some(name), _) => {
444                    return Some(*name);
445                }
446                _ => {}
447            }
448        }
449        None
450    }
451
452    /// Appends end-user visible description of the `local` place to `buf`. If `local` doesn't have
453    /// a name, or its name was generated by the compiler, then `Err` is returned
454    fn append_local_to_string(&self, local: Local, buf: &mut String) -> Result<(), ()> {
455        let decl = &self.body.local_decls[local];
456        match self.local_name(local) {
457            Some(name) if !decl.from_compiler_desugaring() => {
458                buf.push_str(name.as_str());
459                Ok(())
460            }
461            _ => Err(()),
462        }
463    }
464
465    /// End-user visible description of the `field`nth field of `base`
466    fn describe_field(
467        &self,
468        place: PlaceRef<'tcx>,
469        field: FieldIdx,
470        including_tuple_field: IncludingTupleField,
471    ) -> Option<String> {
472        let place_ty = match place {
473            PlaceRef { local, projection: [] } => PlaceTy::from_ty(self.body.local_decls[local].ty),
474            PlaceRef { local, projection: [proj_base @ .., elem] } => match elem {
475                ProjectionElem::Deref
476                | ProjectionElem::Index(..)
477                | ProjectionElem::ConstantIndex { .. }
478                | ProjectionElem::Subslice { .. } => {
479                    PlaceRef { local, projection: proj_base }.ty(self.body, self.infcx.tcx)
480                }
481                ProjectionElem::Downcast(..) => place.ty(self.body, self.infcx.tcx),
482                ProjectionElem::OpaqueCast(ty) | ProjectionElem::UnwrapUnsafeBinder(ty) => {
483                    PlaceTy::from_ty(*ty)
484                }
485                ProjectionElem::Field(_, field_type) => PlaceTy::from_ty(*field_type),
486            },
487        };
488        self.describe_field_from_ty(
489            place_ty.ty,
490            field,
491            place_ty.variant_index,
492            including_tuple_field,
493        )
494    }
495
496    /// End-user visible description of the `field_index`nth field of `ty`
497    fn describe_field_from_ty(
498        &self,
499        ty: Ty<'_>,
500        field: FieldIdx,
501        variant_index: Option<VariantIdx>,
502        including_tuple_field: IncludingTupleField,
503    ) -> Option<String> {
504        if let Some(boxed_ty) = ty.boxed_ty() {
505            // If the type is a box, the field is described from the boxed type
506            self.describe_field_from_ty(boxed_ty, field, variant_index, including_tuple_field)
507        } else {
508            match *ty.kind() {
509                ty::Adt(def, _) => {
510                    let variant = if let Some(idx) = variant_index {
511                        if !def.is_enum() {
    ::core::panicking::panic("assertion failed: def.is_enum()")
};assert!(def.is_enum());
512                        def.variant(idx)
513                    } else {
514                        def.non_enum_variant()
515                    };
516                    if !including_tuple_field.0 && variant.ctor_kind() == Some(CtorKind::Fn) {
517                        return None;
518                    }
519                    Some(variant.fields[field].name.to_string())
520                }
521                ty::Tuple(_) => Some(field.index().to_string()),
522                ty::Ref(_, ty, _) | ty::RawPtr(ty, _) => {
523                    self.describe_field_from_ty(ty, field, variant_index, including_tuple_field)
524                }
525                ty::Array(ty, _) | ty::Slice(ty) => {
526                    self.describe_field_from_ty(ty, field, variant_index, including_tuple_field)
527                }
528                ty::Closure(def_id, _) | ty::Coroutine(def_id, _) => {
529                    // We won't be borrowck'ing here if the closure came from another crate,
530                    // so it's safe to call `expect_local`.
531                    //
532                    // We know the field exists so it's safe to call operator[] and `unwrap` here.
533                    let def_id = def_id.expect_local();
534                    let var_id =
535                        self.infcx.tcx.closure_captures(def_id)[field.index()].get_root_variable();
536
537                    Some(self.infcx.tcx.hir_name(var_id).to_string())
538                }
539                _ => {
540                    // Might need a revision when the fields in trait RFC is implemented
541                    // (https://github.com/rust-lang/rfcs/pull/1546)
542                    ::rustc_middle::util::bug::bug_fmt(format_args!("End-user description not implemented for field access on `{0:?}`",
        ty));bug!("End-user description not implemented for field access on `{:?}`", ty);
543                }
544            }
545        }
546    }
547
548    pub(super) fn borrowed_content_source(
549        &self,
550        deref_base: PlaceRef<'tcx>,
551    ) -> BorrowedContentSource<'tcx> {
552        let tcx = self.infcx.tcx;
553
554        // Look up the provided place and work out the move path index for it,
555        // we'll use this to check whether it was originally from an overloaded
556        // operator.
557        match self.move_data.rev_lookup.find(deref_base) {
558            LookupResult::Exact(mpi) | LookupResult::Parent(Some(mpi)) => {
559                {
    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/diagnostics/mod.rs:559",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(559u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("borrowed_content_source: mpi={0:?}",
                                                    mpi) as &dyn Value))])
            });
    } else { ; }
};debug!("borrowed_content_source: mpi={:?}", mpi);
560
561                for i in &self.move_data.init_path_map[mpi] {
562                    let init = &self.move_data.inits[*i];
563                    {
    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/diagnostics/mod.rs:563",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(563u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("borrowed_content_source: init={0:?}",
                                                    init) as &dyn Value))])
            });
    } else { ; }
};debug!("borrowed_content_source: init={:?}", init);
564                    // We're only interested in statements that initialized a value, not the
565                    // initializations from arguments.
566                    let InitLocation::Statement(loc) = init.location else { continue };
567
568                    let bbd = &self.body[loc.block];
569                    let is_terminator = bbd.statements.len() == loc.statement_index;
570                    {
    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/diagnostics/mod.rs:570",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(570u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("borrowed_content_source: loc={0:?} is_terminator={1:?}",
                                                    loc, is_terminator) as &dyn Value))])
            });
    } else { ; }
};debug!(
571                        "borrowed_content_source: loc={:?} is_terminator={:?}",
572                        loc, is_terminator,
573                    );
574                    if !is_terminator {
575                        continue;
576                    } else if let Some(Terminator {
577                        kind:
578                            TerminatorKind::Call {
579                                func,
580                                call_source: CallSource::OverloadedOperator,
581                                ..
582                            },
583                        ..
584                    }) = &bbd.terminator
585                    {
586                        if let Some(source) =
587                            BorrowedContentSource::from_call(func.ty(self.body, tcx), tcx)
588                        {
589                            return source;
590                        }
591                    }
592                }
593            }
594            // Base is a `static` so won't be from an overloaded operator
595            _ => (),
596        };
597
598        // If we didn't find an overloaded deref or index, then assume it's a
599        // built in deref and check the type of the base.
600        let base_ty = deref_base.ty(self.body, tcx).ty;
601        if base_ty.is_raw_ptr() {
602            BorrowedContentSource::DerefRawPointer
603        } else if base_ty.is_mutable_ptr() {
604            BorrowedContentSource::DerefMutableRef
605        } else if base_ty.is_ref() {
606            BorrowedContentSource::DerefSharedRef
607        } else {
608            // Custom type implementing `Deref` (e.g. `MyBox<T>`, `Rc<T>`, `Arc<T>`)
609            // that wasn't detected via the MIR init trace above. This can happen
610            // when the deref base is initialized by a regular statement rather than
611            // a `TerminatorKind::Call` with `CallSource::OverloadedOperator`.
612            BorrowedContentSource::OverloadedDeref(base_ty)
613        }
614    }
615
616    /// Return the name of the provided `Ty` (that must be a reference) with a synthesized lifetime
617    /// name where required.
618    pub(super) fn get_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
619        let mut p = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);
620
621        // We need to add synthesized lifetimes where appropriate. We do
622        // this by hooking into the pretty printer and telling it to label the
623        // lifetimes without names with the value `'0`.
624        if let ty::Ref(region, ..) = ty.kind() {
625            match region.kind() {
626                ty::ReBound(_, ty::BoundRegion { kind: br, .. })
627                | ty::RePlaceholder(ty::PlaceholderRegion {
628                    bound: ty::BoundRegion { kind: br, .. },
629                    ..
630                }) => p.region_highlight_mode.highlighting_bound_region(br, counter),
631                _ => {}
632            }
633        }
634
635        ty.print(&mut p).unwrap();
636        p.into_buffer()
637    }
638
639    /// Returns the name of the provided `Ty` (that must be a reference)'s region with a
640    /// synthesized lifetime name where required.
641    pub(super) fn get_region_name_for_ty(&self, ty: Ty<'tcx>, counter: usize) -> String {
642        let mut p = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);
643
644        let region = if let ty::Ref(region, ..) = ty.kind() {
645            match region.kind() {
646                ty::ReBound(_, ty::BoundRegion { kind: br, .. })
647                | ty::RePlaceholder(ty::PlaceholderRegion {
648                    bound: ty::BoundRegion { kind: br, .. },
649                    ..
650                }) => p.region_highlight_mode.highlighting_bound_region(br, counter),
651                _ => {}
652            }
653            region
654        } else {
655            ::rustc_middle::util::bug::bug_fmt(format_args!("ty for annotation of borrow region is not a reference"));bug!("ty for annotation of borrow region is not a reference");
656        };
657
658        region.print(&mut p).unwrap();
659        p.into_buffer()
660    }
661
662    /// Add a note to region errors and borrow explanations when higher-ranked regions in predicates
663    /// implicitly introduce an "outlives `'static`" constraint.
664    ///
665    /// This is very similar to `fn suggest_static_lifetime_for_gat_from_hrtb` which handles this
666    /// note for failed type tests instead of outlives errors.
667    fn add_placeholder_from_predicate_note<G: EmissionGuarantee>(
668        &self,
669        diag: &mut Diag<'_, G>,
670        path: &[OutlivesConstraint<'tcx>],
671    ) {
672        let tcx = self.infcx.tcx;
673        let Some((gat_hir_id, generics)) = path.iter().find_map(|constraint| {
674            let outlived = constraint.sub;
675            if let Some(origin) = self.regioncx.definitions.get(outlived)
676                && let NllRegionVariableOrigin::Placeholder(placeholder) = origin.origin
677                && let Some(id) = placeholder.bound.kind.get_id()
678                && let Some(placeholder_id) = id.as_local()
679                && let gat_hir_id = tcx.local_def_id_to_hir_id(placeholder_id)
680                && let Some(generics_impl) =
681                    tcx.parent_hir_node(tcx.parent_hir_id(gat_hir_id)).generics()
682            {
683                Some((gat_hir_id, generics_impl))
684            } else {
685                None
686            }
687        }) else {
688            return;
689        };
690
691        // Look for the where-bound which introduces the placeholder.
692        // As we're using the HIR, we need to handle both `for<'a> T: Trait<'a>`
693        // and `T: for<'a> Trait`<'a>.
694        for pred in generics.predicates {
695            let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
696                bound_generic_params,
697                bounds,
698                ..
699            }) = pred.kind
700            else {
701                continue;
702            };
703            if bound_generic_params
704                .iter()
705                .rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
706                .is_some()
707            {
708                diag.span_note(pred.span, LIMITATION_NOTE);
709                return;
710            }
711            for bound in bounds.iter() {
712                if let GenericBound::Trait(bound) = bound {
713                    if bound
714                        .bound_generic_params
715                        .iter()
716                        .rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
717                        .is_some()
718                    {
719                        diag.span_note(bound.span, LIMITATION_NOTE);
720                        return;
721                    }
722                }
723            }
724        }
725    }
726
727    /// Add a label to region errors and borrow explanations when outlives constraints arise from
728    /// proving a type implements `Sized` or `Copy`.
729    fn add_sized_or_copy_bound_info<G: EmissionGuarantee>(
730        &self,
731        err: &mut Diag<'_, G>,
732        blamed_category: ConstraintCategory<'tcx>,
733        path: &[OutlivesConstraint<'tcx>],
734    ) {
735        for sought_category in [ConstraintCategory::SizedBound, ConstraintCategory::CopyBound] {
736            if sought_category != blamed_category
737                && let Some(sought_constraint) = path.iter().find(|c| c.category == sought_category)
738            {
739                let label = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("requirement occurs due to {0}",
                sought_category.description().trim_end()))
    })format!(
740                    "requirement occurs due to {}",
741                    sought_category.description().trim_end()
742                );
743                err.span_label(sought_constraint.span, label);
744            }
745        }
746    }
747}
748
749/// The span(s) associated to a use of a place.
750#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for UseSpans<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for UseSpans<'tcx> {
    #[inline]
    fn clone(&self) -> UseSpans<'tcx> {
        let _: ::core::clone::AssertParamIsClone<hir::ClosureKind>;
        let _: ::core::clone::AssertParamIsClone<Span>;
        let _: ::core::clone::AssertParamIsClone<CallKind<'tcx>>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for UseSpans<'tcx> {
    #[inline]
    fn eq(&self, other: &UseSpans<'tcx>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (UseSpans::ClosureUse {
                    closure_kind: __self_0,
                    args_span: __self_1,
                    capture_kind_span: __self_2,
                    path_span: __self_3 }, UseSpans::ClosureUse {
                    closure_kind: __arg1_0,
                    args_span: __arg1_1,
                    capture_kind_span: __arg1_2,
                    path_span: __arg1_3 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                            __self_2 == __arg1_2 && __self_3 == __arg1_3,
                (UseSpans::FnSelfUse {
                    var_span: __self_0,
                    fn_call_span: __self_1,
                    fn_span: __self_2,
                    kind: __self_3 }, UseSpans::FnSelfUse {
                    var_span: __arg1_0,
                    fn_call_span: __arg1_1,
                    fn_span: __arg1_2,
                    kind: __arg1_3 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                            __self_2 == __arg1_2 && __self_3 == __arg1_3,
                (UseSpans::PatUse(__self_0), UseSpans::PatUse(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (UseSpans::OtherUse(__self_0), UseSpans::OtherUse(__arg1_0))
                    => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for UseSpans<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<hir::ClosureKind>;
        let _: ::core::cmp::AssertParamIsEq<Span>;
        let _: ::core::cmp::AssertParamIsEq<CallKind<'tcx>>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for UseSpans<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            UseSpans::ClosureUse {
                closure_kind: __self_0,
                args_span: __self_1,
                capture_kind_span: __self_2,
                path_span: __self_3 } =>
                ::core::fmt::Formatter::debug_struct_field4_finish(f,
                    "ClosureUse", "closure_kind", __self_0, "args_span",
                    __self_1, "capture_kind_span", __self_2, "path_span",
                    &__self_3),
            UseSpans::FnSelfUse {
                var_span: __self_0,
                fn_call_span: __self_1,
                fn_span: __self_2,
                kind: __self_3 } =>
                ::core::fmt::Formatter::debug_struct_field4_finish(f,
                    "FnSelfUse", "var_span", __self_0, "fn_call_span", __self_1,
                    "fn_span", __self_2, "kind", &__self_3),
            UseSpans::PatUse(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "PatUse",
                    &__self_0),
            UseSpans::OtherUse(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "OtherUse", &__self_0),
        }
    }
}Debug)]
751pub(super) enum UseSpans<'tcx> {
752    /// The access is caused by capturing a variable for a closure.
753    ClosureUse {
754        /// This is true if the captured variable was from a coroutine.
755        closure_kind: hir::ClosureKind,
756        /// The span of the args of the closure, including the `move` keyword if
757        /// it's present.
758        args_span: Span,
759        /// The span of the use resulting in capture kind
760        /// Check `ty::CaptureInfo` for more details
761        capture_kind_span: Span,
762        /// The span of the use resulting in the captured path
763        /// Check `ty::CaptureInfo` for more details
764        path_span: Span,
765    },
766    /// The access is caused by using a variable as the receiver of a method
767    /// that takes 'self'
768    FnSelfUse {
769        /// The span of the variable being moved
770        var_span: Span,
771        /// The span of the method call on the variable
772        fn_call_span: Span,
773        /// The definition span of the method being called
774        fn_span: Span,
775        kind: CallKind<'tcx>,
776    },
777    /// This access is caused by a `match` or `if let` pattern.
778    PatUse(Span),
779    /// This access has a single span associated to it: common case.
780    OtherUse(Span),
781}
782
783impl UseSpans<'_> {
784    pub(super) fn args_or_use(self) -> Span {
785        match self {
786            UseSpans::ClosureUse { args_span: span, .. }
787            | UseSpans::PatUse(span)
788            | UseSpans::OtherUse(span) => span,
789            UseSpans::FnSelfUse { var_span, .. } => var_span,
790        }
791    }
792
793    /// Returns the span of `self`, in the case of a `ClosureUse` returns the `path_span`
794    pub(super) fn var_or_use_path_span(self) -> Span {
795        match self {
796            UseSpans::ClosureUse { path_span: span, .. }
797            | UseSpans::PatUse(span)
798            | UseSpans::OtherUse(span) => span,
799            UseSpans::FnSelfUse { var_span, .. } => var_span,
800        }
801    }
802
803    /// Returns the span of `self`, in the case of a `ClosureUse` returns the `capture_kind_span`
804    pub(super) fn var_or_use(self) -> Span {
805        match self {
806            UseSpans::ClosureUse { capture_kind_span: span, .. }
807            | UseSpans::PatUse(span)
808            | UseSpans::OtherUse(span) => span,
809            UseSpans::FnSelfUse { var_span, .. } => var_span,
810        }
811    }
812
813    // FIXME(coroutines): Make this just return the `ClosureKind` directly?
814    pub(super) fn coroutine_kind(self) -> Option<CoroutineKind> {
815        match self {
816            UseSpans::ClosureUse {
817                closure_kind: hir::ClosureKind::Coroutine(coroutine_kind),
818                ..
819            } => Some(coroutine_kind),
820            _ => None,
821        }
822    }
823
824    /// Add a span label to the arguments of the closure, if it exists.
825    pub(super) fn args_subdiag(self, err: &mut Diag<'_>, f: impl FnOnce(Span) -> CaptureArgLabel) {
826        if let UseSpans::ClosureUse { args_span, .. } = self {
827            err.subdiagnostic(f(args_span));
828        }
829    }
830
831    /// Add a span label to the use of the captured variable, if it exists.
832    /// only adds label to the `path_span`
833    pub(super) fn var_path_only_subdiag(
834        self,
835        err: &mut Diag<'_>,
836        action: crate::InitializationRequiringAction,
837    ) {
838        use CaptureVarPathUseCause::*;
839
840        use crate::InitializationRequiringAction::*;
841        if let UseSpans::ClosureUse { closure_kind, path_span, .. } = self {
842            match closure_kind {
843                hir::ClosureKind::Coroutine(_) => {
844                    err.subdiagnostic(match action {
845                        Borrow => BorrowInCoroutine { path_span },
846                        MatchOn | Use => UseInCoroutine { path_span },
847                        Assignment => AssignInCoroutine { path_span },
848                        PartialAssignment => AssignPartInCoroutine { path_span },
849                    });
850                }
851                hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
852                    err.subdiagnostic(match action {
853                        Borrow => BorrowInClosure { path_span },
854                        MatchOn | Use => UseInClosure { path_span },
855                        Assignment => AssignInClosure { path_span },
856                        PartialAssignment => AssignPartInClosure { path_span },
857                    });
858                }
859            }
860        }
861    }
862
863    /// Add a subdiagnostic to the use of the captured variable, if it exists.
864    pub(super) fn var_subdiag(
865        self,
866        err: &mut Diag<'_>,
867        kind: Option<rustc_middle::mir::BorrowKind>,
868        f: impl FnOnce(hir::ClosureKind, Span) -> CaptureVarCause,
869    ) {
870        if let UseSpans::ClosureUse { closure_kind, capture_kind_span, path_span, .. } = self {
871            if capture_kind_span != path_span {
872                err.subdiagnostic(match kind {
873                    Some(kd) => match kd {
874                        rustc_middle::mir::BorrowKind::Shared
875                        | rustc_middle::mir::BorrowKind::Fake(_) => {
876                            CaptureVarKind::Immut { kind_span: capture_kind_span }
877                        }
878
879                        rustc_middle::mir::BorrowKind::Mut { .. } => {
880                            CaptureVarKind::Mut { kind_span: capture_kind_span }
881                        }
882                    },
883                    None => CaptureVarKind::Move { kind_span: capture_kind_span },
884                });
885            };
886            let diag = f(closure_kind, path_span);
887            err.subdiagnostic(diag);
888        }
889    }
890
891    /// Returns `false` if this place is not used in a closure.
892    pub(super) fn for_closure(&self) -> bool {
893        match *self {
894            UseSpans::ClosureUse { closure_kind, .. } => {
895                #[allow(non_exhaustive_omitted_patterns)] match closure_kind {
    hir::ClosureKind::Closure => true,
    _ => false,
}matches!(closure_kind, hir::ClosureKind::Closure)
896            }
897            _ => false,
898        }
899    }
900
901    /// Returns `false` if this place is not used in a coroutine.
902    pub(super) fn for_coroutine(&self) -> bool {
903        match *self {
904            // FIXME(coroutines): Do we want this to apply to synthetic coroutines?
905            UseSpans::ClosureUse { closure_kind, .. } => {
906                #[allow(non_exhaustive_omitted_patterns)] match closure_kind {
    hir::ClosureKind::Coroutine(..) => true,
    _ => false,
}matches!(closure_kind, hir::ClosureKind::Coroutine(..))
907            }
908            _ => false,
909        }
910    }
911
912    pub(super) fn or_else<F>(self, if_other: F) -> Self
913    where
914        F: FnOnce() -> Self,
915    {
916        match self {
917            closure @ UseSpans::ClosureUse { .. } => closure,
918            UseSpans::PatUse(_) | UseSpans::OtherUse(_) => if_other(),
919            fn_self @ UseSpans::FnSelfUse { .. } => fn_self,
920        }
921    }
922}
923
924pub(super) enum BorrowedContentSource<'tcx> {
925    DerefRawPointer,
926    DerefMutableRef,
927    DerefSharedRef,
928    OverloadedDeref(Ty<'tcx>),
929    OverloadedIndex(Ty<'tcx>),
930}
931
932impl<'tcx> BorrowedContentSource<'tcx> {
933    pub(super) fn describe_for_unnamed_place(&self, tcx: TyCtxt<'_>) -> String {
934        match *self {
935            BorrowedContentSource::DerefRawPointer => "a raw pointer".to_string(),
936            BorrowedContentSource::DerefSharedRef => "a shared reference".to_string(),
937            BorrowedContentSource::DerefMutableRef => "a mutable reference".to_string(),
938            BorrowedContentSource::OverloadedDeref(ty) => ty
939                .ty_adt_def()
940                .and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
941                    name @ (sym::Rc | sym::Arc) => Some(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("an `{0}`", name))
    })format!("an `{name}`")),
942                    _ => None,
943                })
944                .unwrap_or_else(|| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("dereference of `{0}`", ty))
    })format!("dereference of `{ty}`")),
945            BorrowedContentSource::OverloadedIndex(ty) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("index of `{0}`", ty))
    })format!("index of `{ty}`"),
946        }
947    }
948
949    pub(super) fn describe_for_named_place(&self) -> Option<&'static str> {
950        match *self {
951            BorrowedContentSource::DerefRawPointer => Some("raw pointer"),
952            BorrowedContentSource::DerefSharedRef => Some("shared reference"),
953            BorrowedContentSource::DerefMutableRef => Some("mutable reference"),
954            // Overloaded deref and index operators should be evaluated into a
955            // temporary. So we don't need a description here.
956            BorrowedContentSource::OverloadedDeref(_)
957            | BorrowedContentSource::OverloadedIndex(_) => None,
958        }
959    }
960
961    pub(super) fn describe_for_immutable_place(&self, tcx: TyCtxt<'_>) -> String {
962        match *self {
963            BorrowedContentSource::DerefRawPointer => "a `*const` pointer".to_string(),
964            BorrowedContentSource::DerefSharedRef => "a `&` reference".to_string(),
965            BorrowedContentSource::DerefMutableRef => {
966                ::rustc_middle::util::bug::bug_fmt(format_args!("describe_for_immutable_place: DerefMutableRef isn\'t immutable"))bug!("describe_for_immutable_place: DerefMutableRef isn't immutable")
967            }
968            BorrowedContentSource::OverloadedDeref(ty) => ty
969                .ty_adt_def()
970                .and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
971                    name @ (sym::Rc | sym::Arc) => Some(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("an `{0}`", name))
    })format!("an `{name}`")),
972                    _ => None,
973                })
974                .unwrap_or_else(|| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("dereference of `{0}`", ty))
    })format!("dereference of `{ty}`")),
975            BorrowedContentSource::OverloadedIndex(ty) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("an index of `{0}`", ty))
    })format!("an index of `{ty}`"),
976        }
977    }
978
979    fn from_call(func: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<Self> {
980        match *func.kind() {
981            ty::FnDef(def_id, args) => {
982                let trait_id = tcx.trait_of_assoc(def_id)?;
983
984                if tcx.is_lang_item(trait_id, LangItem::Deref)
985                    || tcx.is_lang_item(trait_id, LangItem::DerefMut)
986                {
987                    Some(BorrowedContentSource::OverloadedDeref(args.type_at(0)))
988                } else if tcx.is_lang_item(trait_id, LangItem::Index)
989                    || tcx.is_lang_item(trait_id, LangItem::IndexMut)
990                {
991                    Some(BorrowedContentSource::OverloadedIndex(args.type_at(0)))
992                } else {
993                    None
994                }
995            }
996            _ => None,
997        }
998    }
999}
1000
1001/// Helper struct for `explain_captures`.
1002struct CapturedMessageOpt {
1003    is_partial_move: bool,
1004    is_loop_message: bool,
1005    is_move_msg: bool,
1006    is_loop_move: bool,
1007    has_suggest_reborrow: bool,
1008    maybe_reinitialized_locations_is_empty: bool,
1009}
1010
1011/// Tracks whether [`MirBorrowckCtxt::explain_captures`] emitted a clone
1012/// suggestion, so callers can avoid emitting redundant suggestions downstream.
1013#[derive(#[automatically_derived]
impl ::core::marker::Copy for CloneSuggestion { }Copy, #[automatically_derived]
impl ::core::clone::Clone for CloneSuggestion {
    #[inline]
    fn clone(&self) -> CloneSuggestion { *self }
}Clone, #[automatically_derived]
impl ::core::cmp::PartialEq for CloneSuggestion {
    #[inline]
    fn eq(&self, other: &CloneSuggestion) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for CloneSuggestion {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq)]
1014pub(super) enum CloneSuggestion {
1015    Emitted,
1016    NotEmitted,
1017}
1018
1019impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
1020    /// Finds the spans associated to a move or copy of move_place at location.
1021    pub(super) fn move_spans(
1022        &self,
1023        moved_place: PlaceRef<'tcx>, // Could also be an upvar.
1024        location: Location,
1025    ) -> UseSpans<'tcx> {
1026        use self::UseSpans::*;
1027
1028        let Some(stmt) = self.body[location.block].statements.get(location.statement_index) else {
1029            return OtherUse(self.body.source_info(location).span);
1030        };
1031
1032        {
    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/diagnostics/mod.rs:1032",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1032u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("move_spans: moved_place={0:?} location={1:?} stmt={2:?}",
                                                    moved_place, location, stmt) as &dyn Value))])
            });
    } else { ; }
};debug!("move_spans: moved_place={:?} location={:?} stmt={:?}", moved_place, location, stmt);
1033        if let StatementKind::Assign(box (_, Rvalue::Aggregate(kind, places))) = &stmt.kind
1034            && let AggregateKind::Closure(def_id, _) | AggregateKind::Coroutine(def_id, _) = **kind
1035        {
1036            {
    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/diagnostics/mod.rs:1036",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1036u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("move_spans: def_id={0:?} places={1:?}",
                                                    def_id, places) as &dyn Value))])
            });
    } else { ; }
};debug!("move_spans: def_id={:?} places={:?}", def_id, places);
1037            let def_id = def_id.expect_local();
1038            if let Some((args_span, closure_kind, capture_kind_span, path_span)) =
1039                self.closure_span(def_id, moved_place, places)
1040            {
1041                return ClosureUse { closure_kind, args_span, capture_kind_span, path_span };
1042            }
1043        }
1044
1045        // StatementKind::FakeRead only contains a def_id if they are introduced as a result
1046        // of pattern matching within a closure.
1047        if let StatementKind::FakeRead(box (cause, place)) = stmt.kind {
1048            match cause {
1049                FakeReadCause::ForMatchedPlace(Some(closure_def_id))
1050                | FakeReadCause::ForLet(Some(closure_def_id)) => {
1051                    {
    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/diagnostics/mod.rs:1051",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1051u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("move_spans: def_id={0:?} place={1:?}",
                                                    closure_def_id, place) as &dyn Value))])
            });
    } else { ; }
};debug!("move_spans: def_id={:?} place={:?}", closure_def_id, place);
1052                    let places = &[Operand::Move(place)];
1053                    if let Some((args_span, closure_kind, capture_kind_span, path_span)) =
1054                        self.closure_span(closure_def_id, moved_place, IndexSlice::from_raw(places))
1055                    {
1056                        return ClosureUse {
1057                            closure_kind,
1058                            args_span,
1059                            capture_kind_span,
1060                            path_span,
1061                        };
1062                    }
1063                }
1064                _ => {}
1065            }
1066        }
1067
1068        let normal_ret =
1069            if moved_place.projection.iter().any(|p| #[allow(non_exhaustive_omitted_patterns)] match p {
    ProjectionElem::Downcast(..) => true,
    _ => false,
}matches!(p, ProjectionElem::Downcast(..))) {
1070                PatUse(stmt.source_info.span)
1071            } else {
1072                OtherUse(stmt.source_info.span)
1073            };
1074
1075        // We are trying to find MIR of the form:
1076        // ```
1077        // _temp = _moved_val;
1078        // ...
1079        // FnSelfCall(_temp, ...)
1080        // ```
1081        //
1082        // where `_moved_val` is the place we generated the move error for,
1083        // `_temp` is some other local, and `FnSelfCall` is a function
1084        // that has a `self` parameter.
1085
1086        let target_temp = match stmt.kind {
1087            StatementKind::Assign(box (temp, _)) if temp.as_local().is_some() => {
1088                temp.as_local().unwrap()
1089            }
1090            _ => return normal_ret,
1091        };
1092
1093        {
    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/diagnostics/mod.rs:1093",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1093u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("move_spans: target_temp = {0:?}",
                                                    target_temp) as &dyn Value))])
            });
    } else { ; }
};debug!("move_spans: target_temp = {:?}", target_temp);
1094
1095        if let Some(Terminator {
1096            kind: TerminatorKind::Call { fn_span, call_source, .. }, ..
1097        }) = &self.body[location.block].terminator
1098        {
1099            let Some((method_did, method_args)) =
1100                find_self_call(self.infcx.tcx, self.body, target_temp, location.block)
1101            else {
1102                return normal_ret;
1103            };
1104
1105            let kind = call_kind(
1106                self.infcx.tcx,
1107                self.infcx.typing_env(self.infcx.param_env),
1108                method_did,
1109                method_args,
1110                *fn_span,
1111                call_source.from_hir_call(),
1112                self.infcx.tcx.fn_arg_idents(method_did)[0],
1113            );
1114
1115            return FnSelfUse {
1116                var_span: stmt.source_info.span,
1117                fn_call_span: *fn_span,
1118                fn_span: self.infcx.tcx.def_span(method_did),
1119                kind,
1120            };
1121        }
1122
1123        normal_ret
1124    }
1125
1126    /// Finds the span of arguments of a closure (within `maybe_closure_span`)
1127    /// and its usage of the local assigned at `location`.
1128    /// This is done by searching in statements succeeding `location`
1129    /// and originating from `maybe_closure_span`.
1130    pub(super) fn borrow_spans(&self, use_span: Span, location: Location) -> UseSpans<'tcx> {
1131        use self::UseSpans::*;
1132        {
    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/diagnostics/mod.rs:1132",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1132u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("borrow_spans: use_span={0:?} location={1:?}",
                                                    use_span, location) as &dyn Value))])
            });
    } else { ; }
};debug!("borrow_spans: use_span={:?} location={:?}", use_span, location);
1133
1134        let Some(Statement { kind: StatementKind::Assign(box (place, _)), .. }) =
1135            self.body[location.block].statements.get(location.statement_index)
1136        else {
1137            return OtherUse(use_span);
1138        };
1139        let Some(target) = place.as_local() else { return OtherUse(use_span) };
1140
1141        if self.body.local_kind(target) != LocalKind::Temp {
1142            // operands are always temporaries.
1143            return OtherUse(use_span);
1144        }
1145
1146        // drop and replace might have moved the assignment to the next block
1147        let maybe_additional_statement =
1148            if let TerminatorKind::Drop { target: drop_target, .. } =
1149                self.body[location.block].terminator().kind
1150            {
1151                self.body[drop_target].statements.first()
1152            } else {
1153                None
1154            };
1155
1156        let statements =
1157            self.body[location.block].statements[location.statement_index + 1..].iter();
1158
1159        for stmt in statements.chain(maybe_additional_statement) {
1160            if let StatementKind::Assign(box (_, Rvalue::Aggregate(kind, places))) = &stmt.kind {
1161                let (&def_id, is_coroutine) = match kind {
1162                    box AggregateKind::Closure(def_id, _) => (def_id, false),
1163                    box AggregateKind::Coroutine(def_id, _) => (def_id, true),
1164                    _ => continue,
1165                };
1166                let def_id = def_id.expect_local();
1167
1168                {
    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/diagnostics/mod.rs:1168",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1168u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("borrow_spans: def_id={0:?} is_coroutine={1:?} places={2:?}",
                                                    def_id, is_coroutine, places) as &dyn Value))])
            });
    } else { ; }
};debug!(
1169                    "borrow_spans: def_id={:?} is_coroutine={:?} places={:?}",
1170                    def_id, is_coroutine, places
1171                );
1172                if let Some((args_span, closure_kind, capture_kind_span, path_span)) =
1173                    self.closure_span(def_id, Place::from(target).as_ref(), places)
1174                {
1175                    return ClosureUse { closure_kind, args_span, capture_kind_span, path_span };
1176                } else {
1177                    return OtherUse(use_span);
1178                }
1179            }
1180
1181            if use_span != stmt.source_info.span {
1182                break;
1183            }
1184        }
1185
1186        OtherUse(use_span)
1187    }
1188
1189    /// Finds the spans of a captured place within a closure or coroutine.
1190    /// The first span is the location of the use resulting in the capture kind of the capture
1191    /// The second span is the location the use resulting in the captured path of the capture
1192    fn closure_span(
1193        &self,
1194        def_id: LocalDefId,
1195        target_place: PlaceRef<'tcx>,
1196        places: &IndexSlice<FieldIdx, Operand<'tcx>>,
1197    ) -> Option<(Span, hir::ClosureKind, Span, Span)> {
1198        {
    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/diagnostics/mod.rs:1198",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1198u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("closure_span: def_id={0:?} target_place={1:?} places={2:?}",
                                                    def_id, target_place, places) as &dyn Value))])
            });
    } else { ; }
};debug!(
1199            "closure_span: def_id={:?} target_place={:?} places={:?}",
1200            def_id, target_place, places
1201        );
1202        let hir_id = self.infcx.tcx.local_def_id_to_hir_id(def_id);
1203        let expr = &self.infcx.tcx.hir_expect_expr(hir_id).kind;
1204        {
    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/diagnostics/mod.rs:1204",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1204u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("closure_span: hir_id={0:?} expr={1:?}",
                                                    hir_id, expr) as &dyn Value))])
            });
    } else { ; }
};debug!("closure_span: hir_id={:?} expr={:?}", hir_id, expr);
1205        if let &hir::ExprKind::Closure(&hir::Closure { kind, fn_decl_span, .. }) = expr {
1206            for (captured_place, place) in
1207                self.infcx.tcx.closure_captures(def_id).iter().zip(places)
1208            {
1209                match place {
1210                    Operand::Copy(place) | Operand::Move(place)
1211                        if target_place == place.as_ref() =>
1212                    {
1213                        {
    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/diagnostics/mod.rs:1213",
                        "rustc_borrowck::diagnostics", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/mod.rs"),
                        ::tracing_core::__macro_support::Option::Some(1213u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics"),
                        ::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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("closure_span: found captured local {0:?}",
                                                    place) as &dyn Value))])
            });
    } else { ; }
};debug!("closure_span: found captured local {:?}", place);
1214                        return Some((
1215                            fn_decl_span,
1216                            kind,
1217                            captured_place.get_capture_kind_span(self.infcx.tcx),
1218                            captured_place.get_path_span(self.infcx.tcx),
1219                        ));
1220                    }
1221                    _ => {}
1222                }
1223            }
1224        }
1225        None
1226    }
1227
1228    /// Helper to retrieve span(s) of given borrow from the current MIR
1229    /// representation
1230    pub(super) fn retrieve_borrow_spans(&self, borrow: &BorrowData<'_>) -> UseSpans<'tcx> {
1231        let span = self.body.source_info(borrow.reserve_location).span;
1232        self.borrow_spans(span, borrow.reserve_location)
1233    }
1234
1235    fn explain_captures(
1236        &mut self,
1237        err: &mut Diag<'infcx>,
1238        span: Span,
1239        move_span: Span,
1240        move_spans: UseSpans<'tcx>,
1241        moved_place: Place<'tcx>,
1242        msg_opt: CapturedMessageOpt,
1243    ) -> CloneSuggestion {
1244        let CapturedMessageOpt {
1245            is_partial_move: is_partial,
1246            is_loop_message,
1247            is_move_msg,
1248            is_loop_move,
1249            has_suggest_reborrow,
1250            maybe_reinitialized_locations_is_empty,
1251        } = msg_opt;
1252        let mut suggested_cloning = false;
1253        if let UseSpans::FnSelfUse { var_span, fn_call_span, fn_span, kind } = move_spans {
1254            let place_name = self
1255                .describe_place(moved_place.as_ref())
1256                .map(|n| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", n))
    })format!("`{n}`"))
1257                .unwrap_or_else(|| "value".to_owned());
1258            match kind {
1259                CallKind::FnCall { fn_trait_id, self_ty }
1260                    if self.infcx.tcx.is_lang_item(fn_trait_id, LangItem::FnOnce) =>
1261                {
1262                    err.subdiagnostic(CaptureReasonLabel::Call {
1263                        fn_call_span,
1264                        place_name: &place_name,
1265                        is_partial,
1266                        is_loop_message,
1267                    });
1268                    // Check if the move occurs on a value because of a call on a closure that comes
1269                    // from a type parameter `F: FnOnce()`. If so, we provide a targeted `note`:
1270                    // ```
1271                    // error[E0382]: use of moved value: `blk`
1272                    //   --> $DIR/once-cant-call-twice-on-heap.rs:8:5
1273                    //    |
1274                    // LL | fn foo<F:FnOnce()>(blk: F) {
1275                    //    |                    --- move occurs because `blk` has type `F`, which does not implement the `Copy` trait
1276                    // LL | blk();
1277                    //    | ----- `blk` moved due to this call
1278                    // LL | blk();
1279                    //    | ^^^ value used here after move
1280                    //    |
1281                    // note: `FnOnce` closures can only be called once
1282                    //   --> $DIR/once-cant-call-twice-on-heap.rs:6:10
1283                    //    |
1284                    // LL | fn foo<F:FnOnce()>(blk: F) {
1285                    //    |        ^^^^^^^^ `F` is made to be an `FnOnce` closure here
1286                    // LL | blk();
1287                    //    | ----- this value implements `FnOnce`, which causes it to be moved when called
1288                    // ```
1289                    if let ty::Param(param_ty) = *self_ty.kind()
1290                        && let generics = self.infcx.tcx.generics_of(self.mir_def_id())
1291                        && let param = generics.type_param(param_ty, self.infcx.tcx)
1292                        && let Some(hir_generics) = self.infcx.tcx.hir_get_generics(
1293                            self.infcx.tcx.typeck_root_def_id_local(self.mir_def_id()),
1294                        )
1295                        && let spans = hir_generics
1296                            .predicates
1297                            .iter()
1298                            .filter_map(|pred| match pred.kind {
1299                                hir::WherePredicateKind::BoundPredicate(pred) => Some(pred),
1300                                _ => None,
1301                            })
1302                            .filter(|pred| {
1303                                if let Some((id, _)) = pred.bounded_ty.as_generic_param() {
1304                                    id == param.def_id
1305                                } else {
1306                                    false
1307                                }
1308                            })
1309                            .flat_map(|pred| pred.bounds)
1310                            .filter_map(|bound| {
1311                                if let Some(trait_ref) = bound.trait_ref()
1312                                    && let Some(trait_def_id) = trait_ref.trait_def_id()
1313                                    && trait_def_id == fn_trait_id
1314                                {
1315                                    Some(bound.span())
1316                                } else {
1317                                    None
1318                                }
1319                            })
1320                            .collect::<Vec<Span>>()
1321                        && !spans.is_empty()
1322                    {
1323                        let mut span: MultiSpan = spans.clone().into();
1324                        let msg = rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`{$ty}` is made to be an `FnOnce` closure here"))msg!("`{$ty}` is made to be an `FnOnce` closure here")
1325                            .arg("ty", param_ty.to_string())
1326                            .format();
1327                        for sp in spans {
1328                            span.push_span_label(sp, msg.clone());
1329                        }
1330                        span.push_span_label(
1331                            fn_call_span,
1332                            rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("this value implements `FnOnce`, which causes it to be moved when called"))msg!("this value implements `FnOnce`, which causes it to be moved when called"),
1333                        );
1334                        err.span_note(span, rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("`FnOnce` closures can only be called once"))msg!("`FnOnce` closures can only be called once"));
1335                    } else {
1336                        err.subdiagnostic(CaptureReasonNote::FnOnceMoveInCall { var_span });
1337                    }
1338                }
1339                CallKind::Operator { self_arg, trait_id, .. } => {
1340                    let self_arg = self_arg.unwrap();
1341                    err.subdiagnostic(CaptureReasonLabel::OperatorUse {
1342                        fn_call_span,
1343                        place_name: &place_name,
1344                        is_partial,
1345                        is_loop_message,
1346                    });
1347                    if self.fn_self_span_reported.insert(fn_span) {
1348                        let lang = self.infcx.tcx.lang_items();
1349                        err.subdiagnostic(
1350                            if [lang.not_trait(), lang.deref_trait(), lang.neg_trait()]
1351                                .contains(&Some(trait_id))
1352                            {
1353                                CaptureReasonNote::UnOpMoveByOperator { span: self_arg.span }
1354                            } else {
1355                                CaptureReasonNote::LhsMoveByOperator { span: self_arg.span }
1356                            },
1357                        );
1358                    }
1359                }
1360                CallKind::Normal { self_arg, desugaring, method_did, method_args } => {
1361                    let self_arg = self_arg.unwrap();
1362                    let mut has_sugg = false;
1363                    let tcx = self.infcx.tcx;
1364                    // Avoid pointing to the same function in multiple different
1365                    // error messages.
1366                    if span != DUMMY_SP && self.fn_self_span_reported.insert(self_arg.span) {
1367                        self.explain_iterator_advancement_in_for_loop_if_applicable(
1368                            err,
1369                            span,
1370                            &move_spans,
1371                        );
1372
1373                        let func = tcx.def_path_str(method_did);
1374                        err.subdiagnostic(CaptureReasonNote::FuncTakeSelf {
1375                            func,
1376                            place_name: place_name.clone(),
1377                            span: self_arg.span,
1378                        });
1379                    }
1380                    let parent_did = tcx.parent(method_did);
1381                    let parent_self_ty =
1382                        #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(parent_did) {
    rustc_hir::def::DefKind::Impl { .. } => true,
    _ => false,
}matches!(tcx.def_kind(parent_did), rustc_hir::def::DefKind::Impl { .. })
1383                            .then_some(parent_did)
1384                            .and_then(|did| {
1385                                match tcx.type_of(did).instantiate_identity().skip_norm_wip().kind()
1386                                {
1387                                    ty::Adt(def, ..) => Some(def.did()),
1388                                    _ => None,
1389                                }
1390                            });
1391                    let is_option_or_result = parent_self_ty.is_some_and(|def_id| {
1392                        #[allow(non_exhaustive_omitted_patterns)] match tcx.get_diagnostic_name(def_id)
    {
    Some(sym::Option | sym::Result) => true,
    _ => false,
}matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
1393                    });
1394                    if is_option_or_result && maybe_reinitialized_locations_is_empty {
1395                        err.subdiagnostic(CaptureReasonLabel::BorrowContent {
1396                            var_span: var_span.shrink_to_hi(),
1397                        });
1398                    }
1399                    if let Some((CallDesugaringKind::ForLoopIntoIter, _)) = desugaring {
1400                        let ty = moved_place.ty(self.body, tcx).ty;
1401                        let suggest = match tcx.get_diagnostic_item(sym::IntoIterator) {
1402                            Some(def_id) => type_known_to_meet_bound_modulo_regions(
1403                                self.infcx,
1404                                self.infcx.param_env,
1405                                Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, ty),
1406                                def_id,
1407                            ),
1408                            _ => false,
1409                        };
1410                        if suggest {
1411                            err.subdiagnostic(CaptureReasonSuggest::IterateSlice {
1412                                ty,
1413                                span: move_span.shrink_to_lo(),
1414                            });
1415                        }
1416
1417                        err.subdiagnostic(CaptureReasonLabel::ImplicitCall {
1418                            fn_call_span,
1419                            place_name: &place_name,
1420                            is_partial,
1421                            is_loop_message,
1422                        });
1423                        // If the moved place was a `&mut` ref, then we can
1424                        // suggest to reborrow it where it was moved, so it
1425                        // will still be valid by the time we get to the usage.
1426                        if let ty::Ref(_, _, hir::Mutability::Mut) =
1427                            moved_place.ty(self.body, self.infcx.tcx).ty.kind()
1428                        {
1429                            // Suggest `reborrow` in other place for following situations:
1430                            // 1. If we are in a loop this will be suggested later.
1431                            // 2. If the moved value is a mut reference, it is used in a
1432                            // generic function and the corresponding arg's type is generic param.
1433                            if !is_loop_move && !has_suggest_reborrow {
1434                                self.suggest_reborrow(
1435                                    err,
1436                                    move_span.shrink_to_lo(),
1437                                    moved_place.as_ref(),
1438                                );
1439                            }
1440                        }
1441                    } else {
1442                        if let Some((CallDesugaringKind::Await, _)) = desugaring {
1443                            err.subdiagnostic(CaptureReasonLabel::Await {
1444                                fn_call_span,
1445                                place_name: &place_name,
1446                                is_partial,
1447                                is_loop_message,
1448                            });
1449                        } else {
1450                            err.subdiagnostic(CaptureReasonLabel::MethodCall {
1451                                fn_call_span,
1452                                place_name: &place_name,
1453                                is_partial,
1454                                is_loop_message,
1455                            });
1456                        }
1457                        // Erase and shadow everything that could be passed to the new infcx.
1458                        let ty = moved_place.ty(self.body, tcx).ty;
1459
1460                        if let ty::Adt(def, args) = ty.peel_refs().kind()
1461                            && tcx.is_lang_item(def.did(), LangItem::Pin)
1462                            && let ty::Ref(_, _, hir::Mutability::Mut) = args.type_at(0).kind()
1463                            && let self_ty = self.infcx.instantiate_binder_with_fresh_vars(
1464                                fn_call_span,
1465                                BoundRegionConversionTime::FnCall,
1466                                tcx.fn_sig(method_did)
1467                                    .instantiate(tcx, method_args)
1468                                    .skip_norm_wip()
1469                                    .input(0),
1470                            )
1471                            && self.infcx.can_eq(self.infcx.param_env, ty, self_ty)
1472                        {
1473                            err.subdiagnostic(CaptureReasonSuggest::FreshReborrow {
1474                                span: move_span.shrink_to_hi(),
1475                            });
1476                            has_sugg = true;
1477                        }
1478                        if let Some(clone_trait) = tcx.lang_items().clone_trait() {
1479                            // Check whether the deref is from a custom Deref impl
1480                            // (e.g. Rc, Box) or a built-in reference deref.
1481                            // For built-in derefs with Clone fully satisfied, we skip
1482                            // the UFCS suggestion here and let `suggest_cloning`
1483                            // downstream emit a simpler `.clone()` suggestion instead.
1484                            let has_overloaded_deref =
1485                                moved_place.iter_projections().any(|(place, elem)| {
1486                                    #[allow(non_exhaustive_omitted_patterns)] match elem {
    ProjectionElem::Deref => true,
    _ => false,
}matches!(elem, ProjectionElem::Deref)
1487                                        && #[allow(non_exhaustive_omitted_patterns)] match self.borrowed_content_source(place)
    {
    BorrowedContentSource::OverloadedDeref(_) |
        BorrowedContentSource::OverloadedIndex(_) => true,
    _ => false,
}matches!(
1488                                            self.borrowed_content_source(place),
1489                                            BorrowedContentSource::OverloadedDeref(_)
1490                                                | BorrowedContentSource::OverloadedIndex(_)
1491                                        )
1492                                });
1493
1494                            let has_deref = moved_place
1495                                .iter_projections()
1496                                .any(|(_, elem)| #[allow(non_exhaustive_omitted_patterns)] match elem {
    ProjectionElem::Deref => true,
    _ => false,
}matches!(elem, ProjectionElem::Deref));
1497
1498                            let sugg = if has_deref {
1499                                let (start, end) = if let Some(expr) = self.find_expr(move_span)
1500                                    && let Some(_) = self.clone_on_reference(expr)
1501                                    && let hir::ExprKind::MethodCall(_, rcvr, _, _) = expr.kind
1502                                {
1503                                    (move_span.shrink_to_lo(), move_span.with_lo(rcvr.span.hi()))
1504                                } else {
1505                                    (move_span.shrink_to_lo(), move_span.shrink_to_hi())
1506                                };
1507                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(start,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("<{0} as Clone>::clone(&",
                                    ty))
                        })), (end, ")".to_string())]))vec![
1508                                    // We use the fully-qualified path because `.clone()` can
1509                                    // sometimes choose `<&T as Clone>` instead of `<T as Clone>`
1510                                    // when going through auto-deref, so this ensures that doesn't
1511                                    // happen, causing suggestions for `.clone().clone()`.
1512                                    (start, format!("<{ty} as Clone>::clone(&")),
1513                                    (end, ")".to_string()),
1514                                ]
1515                            } else {
1516                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(move_span.shrink_to_hi(), ".clone()".to_string())]))vec![(move_span.shrink_to_hi(), ".clone()".to_string())]
1517                            };
1518                            if let Some(errors) = self.infcx.type_implements_trait_shallow(
1519                                clone_trait,
1520                                ty,
1521                                self.infcx.param_env,
1522                            ) && !has_sugg
1523                            {
1524                                let skip_for_simple_clone =
1525                                    has_deref && !has_overloaded_deref && errors.is_empty();
1526                                if !skip_for_simple_clone {
1527                                    let msg = match &errors[..] {
1528                                        [] => "you can `clone` the value and consume it, but \
1529                                               this might not be your desired behavior"
1530                                            .to_string(),
1531                                        [error] => {
1532                                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you could `clone` the value and consume it, if the `{0}` trait bound could be satisfied",
                error.obligation.predicate))
    })format!(
1533                                                "you could `clone` the value and consume it, if \
1534                                                 the `{}` trait bound could be satisfied",
1535                                                error.obligation.predicate,
1536                                            )
1537                                        }
1538                                        _ => {
1539                                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you could `clone` the value and consume it, if the following trait bounds could be satisfied: {0}",
                listify(&errors,
                        |e: &FulfillmentError<'tcx>|
                            ::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("`{0}`",
                                            e.obligation.predicate))
                                })).unwrap()))
    })format!(
1540                                                "you could `clone` the value and consume it, if \
1541                                                 the following trait bounds could be satisfied: \
1542                                                 {}",
1543                                                listify(
1544                                                    &errors,
1545                                                    |e: &FulfillmentError<'tcx>| format!(
1546                                                        "`{}`",
1547                                                        e.obligation.predicate
1548                                                    )
1549                                                )
1550                                                .unwrap(),
1551                                            )
1552                                        }
1553                                    };
1554                                    err.multipart_suggestion(
1555                                        msg,
1556                                        sugg,
1557                                        Applicability::MaybeIncorrect,
1558                                    );
1559
1560                                    suggested_cloning = errors.is_empty();
1561
1562                                    for error in errors {
1563                                        if let FulfillmentErrorCode::Select(
1564                                            SelectionError::Unimplemented,
1565                                        ) = error.code
1566                                            && let ty::PredicateKind::Clause(ty::ClauseKind::Trait(
1567                                                pred,
1568                                            )) = error.obligation.predicate.kind().skip_binder()
1569                                        {
1570                                            self.infcx.err_ctxt().suggest_derive(
1571                                                &error.obligation,
1572                                                err,
1573                                                error.obligation.predicate.kind().rebind(pred),
1574                                            );
1575                                        }
1576                                    }
1577                                }
1578                            }
1579                        }
1580                    }
1581                }
1582                // Other desugarings takes &self, which cannot cause a move
1583                _ => {}
1584            }
1585        } else {
1586            if move_span != span || is_loop_message {
1587                err.subdiagnostic(CaptureReasonLabel::MovedHere {
1588                    move_span,
1589                    is_partial,
1590                    is_move_msg,
1591                    is_loop_message,
1592                });
1593            }
1594            // If the move error occurs due to a loop, don't show
1595            // another message for the same span
1596            if !is_loop_message {
1597                move_spans.var_subdiag(err, None, |kind, var_span| match kind {
1598                    hir::ClosureKind::Coroutine(_) => {
1599                        CaptureVarCause::PartialMoveUseInCoroutine { var_span, is_partial }
1600                    }
1601                    hir::ClosureKind::Closure | hir::ClosureKind::CoroutineClosure(_) => {
1602                        CaptureVarCause::PartialMoveUseInClosure { var_span, is_partial }
1603                    }
1604                })
1605            }
1606        }
1607        if suggested_cloning { CloneSuggestion::Emitted } else { CloneSuggestion::NotEmitted }
1608    }
1609
1610    /// Skip over locals that begin with an underscore or have no name
1611    pub(crate) fn local_excluded_from_unused_mut_lint(&self, index: Local) -> bool {
1612        self.local_name(index).is_none_or(|name| name.as_str().starts_with('_'))
1613    }
1614}
1615
1616const LIMITATION_NOTE: DiagMessage =
1617    rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("due to a current limitation of the type system, this implies a `'static` lifetime"))msg!("due to a current limitation of the type system, this implies a `'static` lifetime");