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