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