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