Skip to main content

rustc_borrowck/diagnostics/
region_errors.rs

1//! Error reporting machinery for lifetime errors.
2
3use rustc_data_structures::fx::FxIndexSet;
4use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan, msg};
5use rustc_hir as hir;
6use rustc_hir::GenericBound::Trait;
7use rustc_hir::QPath::Resolved;
8use rustc_hir::WherePredicateKind::BoundPredicate;
9use rustc_hir::def::Res::Def;
10use rustc_hir::def_id::DefId;
11use rustc_hir::intravisit::VisitorExt;
12use rustc_hir::{PolyTraitRef, TyKind, WhereBoundPredicate};
13use rustc_infer::infer::{NllRegionVariableOrigin, SubregionOrigin};
14use rustc_middle::bug;
15use rustc_middle::hir::place::PlaceBase;
16use rustc_middle::mir::{AnnotationSource, ConstraintCategory, ReturnConstraint};
17use rustc_middle::ty::{
18    self, FnSigKind, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitor,
19    fold_regions,
20};
21use rustc_span::{Ident, Span, kw};
22use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
23use rustc_trait_selection::error_reporting::infer::nice_region_error::{
24    self, HirTraitObjectVisitor, NiceRegionError, TraitObjectVisitor, find_anon_type,
25    find_param_with_region, suggest_adding_lifetime_params,
26};
27use rustc_trait_selection::infer::InferCtxtExt;
28use rustc_trait_selection::traits::{Obligation, ObligationCtxt};
29use tracing::{debug, instrument, trace};
30
31use super::{LIMITATION_NOTE, OutlivesSuggestionBuilder, RegionName, RegionNameSource};
32use crate::nll::ConstraintDescription;
33use crate::region_infer::{BlameConstraint, TypeTest};
34use crate::session_diagnostics::{
35    FnMutError, FnMutReturnTypeErr, GenericDoesNotLiveLongEnough, LifetimeOutliveErr,
36    LifetimeReturnCategoryErr, RequireStaticErr, VarHereDenote,
37};
38use crate::universal_regions::DefiningTy;
39use crate::{MirBorrowckCtxt, borrowck_errors};
40
41impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
42    fn description(&self) -> &'static str {
43        // Must end with a space. Allows for empty names to be provided.
44        match self {
45            ConstraintCategory::Assignment => "assignment ",
46            ConstraintCategory::Return(_) => "returning this value ",
47            ConstraintCategory::Yield => "yielding this value ",
48            ConstraintCategory::UseAsConst => "using this value as a constant ",
49            ConstraintCategory::UseAsStatic => "using this value as a static ",
50            ConstraintCategory::Cast { is_implicit_coercion: false, .. } => "cast ",
51            ConstraintCategory::Cast { is_implicit_coercion: true, .. } => "coercion ",
52            ConstraintCategory::CallArgument(_) => "argument ",
53            ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => "generic argument ",
54            ConstraintCategory::TypeAnnotation(_) => "type annotation ",
55            ConstraintCategory::SizedBound => "proving this value is `Sized` ",
56            ConstraintCategory::CopyBound => "copying this value ",
57            ConstraintCategory::OpaqueType => "opaque type ",
58            ConstraintCategory::ClosureUpvar(_) => "closure capture ",
59            ConstraintCategory::Usage => "this usage ",
60            ConstraintCategory::SolverRegionConstraint(_)
61            | ConstraintCategory::Predicate(_)
62            | ConstraintCategory::Boring
63            | ConstraintCategory::BoringNoLocation
64            | ConstraintCategory::Internal
65            | ConstraintCategory::OutlivesUnnameablePlaceholder(..) => "",
66        }
67    }
68}
69
70/// A collection of errors encountered during region inference. This is needed to efficiently
71/// report errors after borrow checking.
72///
73/// Usually we expect this to either be empty or contain a small number of items, so we can avoid
74/// allocation most of the time.
75pub(crate) struct RegionErrors<'tcx>(Vec<(RegionErrorKind<'tcx>, ErrorGuaranteed)>, TyCtxt<'tcx>);
76
77impl<'tcx> RegionErrors<'tcx> {
78    pub(crate) fn new(tcx: TyCtxt<'tcx>) -> Self {
79        Self(::alloc::vec::Vec::new()vec![], tcx)
80    }
81    #[track_caller]
82    pub(crate) fn push(&mut self, val: impl Into<RegionErrorKind<'tcx>>) {
83        let val = val.into();
84        let guar = self.1.sess.dcx().delayed_bug(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0:?}", val))
    })format!("{val:?}"));
85        self.0.push((val, guar));
86    }
87    pub(crate) fn is_empty(&self) -> bool {
88        self.0.is_empty()
89    }
90    pub(crate) fn into_iter(
91        self,
92    ) -> impl Iterator<Item = (RegionErrorKind<'tcx>, ErrorGuaranteed)> {
93        self.0.into_iter()
94    }
95}
96
97impl std::fmt::Debug for RegionErrors<'_> {
98    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        f.debug_tuple("RegionErrors").field(&self.0).finish()
100    }
101}
102
103#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for RegionErrorKind<'tcx> {
    #[inline]
    fn clone(&self) -> RegionErrorKind<'tcx> {
        match self {
            RegionErrorKind::TypeTestError { type_test: __self_0 } =>
                RegionErrorKind::TypeTestError {
                    type_test: ::core::clone::Clone::clone(__self_0),
                },
            RegionErrorKind::PlaceholderOutlivesIllegalRegion {
                longer_fr: __self_0, illegally_outlived_r: __self_1 } =>
                RegionErrorKind::PlaceholderOutlivesIllegalRegion {
                    longer_fr: ::core::clone::Clone::clone(__self_0),
                    illegally_outlived_r: ::core::clone::Clone::clone(__self_1),
                },
            RegionErrorKind::RegionError {
                fr_origin: __self_0,
                longer_fr: __self_1,
                shorter_fr: __self_2,
                is_reported: __self_3 } =>
                RegionErrorKind::RegionError {
                    fr_origin: ::core::clone::Clone::clone(__self_0),
                    longer_fr: ::core::clone::Clone::clone(__self_1),
                    shorter_fr: ::core::clone::Clone::clone(__self_2),
                    is_reported: ::core::clone::Clone::clone(__self_3),
                },
        }
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for RegionErrorKind<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            RegionErrorKind::TypeTestError { type_test: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "TypeTestError", "type_test", &__self_0),
            RegionErrorKind::PlaceholderOutlivesIllegalRegion {
                longer_fr: __self_0, illegally_outlived_r: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "PlaceholderOutlivesIllegalRegion", "longer_fr", __self_0,
                    "illegally_outlived_r", &__self_1),
            RegionErrorKind::RegionError {
                fr_origin: __self_0,
                longer_fr: __self_1,
                shorter_fr: __self_2,
                is_reported: __self_3 } =>
                ::core::fmt::Formatter::debug_struct_field4_finish(f,
                    "RegionError", "fr_origin", __self_0, "longer_fr", __self_1,
                    "shorter_fr", __self_2, "is_reported", &__self_3),
        }
    }
}Debug)]
104pub(crate) enum RegionErrorKind<'tcx> {
105    /// A generic bound failure for a type test (`T: 'a`).
106    TypeTestError { type_test: TypeTest<'tcx> },
107
108    /// 'p outlives 'r, which does not hold. 'p is always a placeholder
109    /// and 'r is some other region.
110    PlaceholderOutlivesIllegalRegion { longer_fr: RegionVid, illegally_outlived_r: RegionVid },
111
112    /// Any other lifetime error.
113    RegionError {
114        /// The origin of the region.
115        fr_origin: NllRegionVariableOrigin<'tcx>,
116        /// The region that should outlive `shorter_fr`.
117        longer_fr: RegionVid,
118        /// The region that should be shorter, but we can't prove it.
119        shorter_fr: RegionVid,
120        /// Indicates whether this is a reported error. We currently only report the first error
121        /// encountered and leave the rest unreported so as not to overwhelm the user.
122        is_reported: bool,
123    },
124}
125
126/// Information about the various region constraints involved in a borrow checker error.
127#[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for ErrorConstraintInfo<'tcx> {
    #[inline]
    fn clone(&self) -> ErrorConstraintInfo<'tcx> {
        ErrorConstraintInfo {
            fr: ::core::clone::Clone::clone(&self.fr),
            outlived_fr: ::core::clone::Clone::clone(&self.outlived_fr),
            category: ::core::clone::Clone::clone(&self.category),
            span: ::core::clone::Clone::clone(&self.span),
        }
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ErrorConstraintInfo<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field4_finish(f,
            "ErrorConstraintInfo", "fr", &self.fr, "outlived_fr",
            &self.outlived_fr, "category", &self.category, "span",
            &&self.span)
    }
}Debug)]
128pub(crate) struct ErrorConstraintInfo<'tcx> {
129    // fr: outlived_fr
130    pub(super) fr: RegionVid,
131    pub(super) outlived_fr: RegionVid,
132
133    // Category and span for best blame constraint
134    pub(super) category: ConstraintCategory<'tcx>,
135    pub(super) span: Span,
136}
137
138impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
139    /// Converts a region inference variable into a `ty::Region` that
140    /// we can use for error reporting. If `r` is universally bound,
141    /// then we use the name that we have on record for it. If `r` is
142    /// existentially bound, then we check its inferred value and try
143    /// to find a good name from that. Returns `None` if we can't find
144    /// one (e.g., this is just some random part of the CFG).
145    pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
146        self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
147    }
148
149    /// Returns the `RegionVid` corresponding to the region returned by
150    /// `to_error_region`.
151    pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
152        if self.regioncx.universal_regions().is_universal_region(r) {
153            Some(r)
154        } else {
155            // We just want something nameable, even if it's not
156            // actually an upper bound.
157            let upper_bound = self.regioncx.approx_universal_upper_bound(r);
158
159            if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
160                self.to_error_region_vid(upper_bound)
161            } else {
162                None
163            }
164        }
165    }
166
167    /// Map the regions in the type to named regions, where possible.
168    fn name_regions<T>(&self, tcx: TyCtxt<'tcx>, ty: T) -> T
169    where
170        T: TypeFoldable<TyCtxt<'tcx>>,
171    {
172        fold_regions(tcx, ty, |region, _| match region.kind() {
173            ty::ReVar(vid) => self.to_error_region(vid).unwrap_or(region),
174            _ => region,
175        })
176    }
177
178    /// Returns `true` if a closure is inferred to be an `FnMut` closure.
179    fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
180        if let Some(r) = self.to_error_region(fr)
181            && let ty::ReLateParam(late_param) = r.kind()
182            && let ty::LateParamRegionKind::ClosureEnv = late_param.kind
183            && let DefiningTy::Closure(_, args) = self.regioncx.universal_regions().defining_ty
184        {
185            return args.as_closure().kind() == ty::ClosureKind::FnMut;
186        }
187
188        false
189    }
190
191    // For generic associated types (GATs) which implied 'static requirement
192    // from higher-ranked trait bounds (HRTB). Try to locate span of the trait
193    // and the span which bounded to the trait for adding 'static lifetime suggestion
194    fn suggest_static_lifetime_for_gat_from_hrtb(
195        &self,
196        diag: &mut Diag<'_>,
197        lower_bound: RegionVid,
198    ) {
199        let tcx = self.infcx.tcx;
200
201        // find generic associated types in the given region 'lower_bound'
202        let gat_id_and_generics = self
203            .regioncx
204            .placeholders_contained_in(lower_bound)
205            .map(|placeholder| {
206                if let Some(id) = placeholder.bound.kind.get_id()
207                    && let Some(placeholder_id) = id.as_local()
208                    && let gat_hir_id = tcx.local_def_id_to_hir_id(placeholder_id)
209                    && let Some(generics_impl) =
210                        tcx.parent_hir_node(tcx.parent_hir_id(gat_hir_id)).generics()
211                {
212                    Some((gat_hir_id, generics_impl))
213                } else {
214                    None
215                }
216            })
217            .collect::<Vec<_>>();
218        {
    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/region_errors.rs:218",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(218u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::tracing_core::field::FieldSet::new(&["gat_id_and_generics"],
                            ::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(&debug(&gat_id_and_generics)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?gat_id_and_generics);
219
220        // Look for the where-bound which introduces the placeholder.
221        // As we're using the HIR, we need to handle both `for<'a> T: Trait<'a>`
222        // and `T: for<'a> Trait`<'a>.
223        let mut hrtb_bounds = ::alloc::vec::Vec::new()vec![];
224        gat_id_and_generics.iter().flatten().for_each(|&(gat_hir_id, generics)| {
225            for pred in generics.predicates {
226                let BoundPredicate(WhereBoundPredicate { bound_generic_params, bounds, .. }) =
227                    pred.kind
228                else {
229                    continue;
230                };
231                if bound_generic_params
232                    .iter()
233                    .rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
234                    .is_some()
235                {
236                    for bound in *bounds {
237                        hrtb_bounds.push(bound);
238                    }
239                } else {
240                    for bound in *bounds {
241                        if let Trait(trait_bound) = bound {
242                            if trait_bound
243                                .bound_generic_params
244                                .iter()
245                                .rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
246                                .is_some()
247                            {
248                                hrtb_bounds.push(bound);
249                                return;
250                            }
251                        }
252                    }
253                }
254            }
255        });
256        {
    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/region_errors.rs:256",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(256u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::tracing_core::field::FieldSet::new(&["hrtb_bounds"],
                            ::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(&debug(&hrtb_bounds)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?hrtb_bounds);
257
258        let mut suggestions = ::alloc::vec::Vec::new()vec![];
259        hrtb_bounds.iter().for_each(|bound| {
260            let Trait(PolyTraitRef { trait_ref, span: trait_span, .. }) = bound else {
261                return;
262            };
263            diag.span_note(*trait_span, LIMITATION_NOTE);
264            let Some(generics_fn) = tcx.hir_get_generics(self.body.source.def_id().expect_local())
265            else {
266                return;
267            };
268            let Def(_, trait_res_defid) = trait_ref.path.res else {
269                return;
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/region_errors.rs:271",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(271u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::tracing_core::field::FieldSet::new(&["generics_fn"],
                            ::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(&debug(&generics_fn)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?generics_fn);
272            generics_fn.predicates.iter().for_each(|predicate| {
273                let BoundPredicate(WhereBoundPredicate { bounded_ty, bounds, .. }) = predicate.kind
274                else {
275                    return;
276                };
277                bounds.iter().for_each(|bd| {
278                    if let Trait(PolyTraitRef { trait_ref: tr_ref, .. }) = bd
279                        && let Def(_, res_defid) = tr_ref.path.res
280                        && res_defid == trait_res_defid // trait id matches
281                        && let TyKind::Path(Resolved(_, path)) = bounded_ty.kind
282                        && let Def(_, defid) = path.res
283                        && generics_fn.params
284                            .iter()
285                            .rfind(|param| param.def_id.to_def_id() == defid)
286                            .is_some()
287                    {
288                        suggestions.push((predicate.span.shrink_to_hi(), " + 'static".to_string()));
289                    }
290                });
291            });
292        });
293        if suggestions.len() > 0 {
294            suggestions.dedup();
295            diag.multipart_suggestion(
296                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("consider restricting the type parameter to the `'static` lifetime"))msg!("consider restricting the type parameter to the `'static` lifetime"),
297                suggestions,
298                Applicability::MaybeIncorrect,
299            );
300        }
301    }
302
303    /// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
304    pub(crate) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
305        // Iterate through all the errors, producing a diagnostic for each one. The diagnostics are
306        // buffered in the `MirBorrowckCtxt`.
307        let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
308        for (nll_error, _) in nll_errors.into_iter() {
309            match nll_error {
310                RegionErrorKind::TypeTestError { type_test } => {
311                    // Try to convert the lower-bound region into something named we can print for
312                    // the user.
313                    let lower_bound_region = self.to_error_region(type_test.lower_bound);
314
315                    let type_test_span = type_test.span;
316
317                    if let Some(lower_bound_region) = lower_bound_region {
318                        let generic_ty = self.name_regions(
319                            self.infcx.tcx,
320                            type_test.generic_kind.to_ty(self.infcx.tcx),
321                        );
322                        let origin =
323                            SubregionOrigin::RelateParamBound(type_test_span, generic_ty, None);
324                        self.buffer_error(self.infcx.err_ctxt().construct_generic_bound_failure(
325                            self.body.source.def_id().expect_local(),
326                            type_test_span,
327                            Some(origin),
328                            self.name_regions(self.infcx.tcx, type_test.generic_kind),
329                            lower_bound_region,
330                        ));
331                    } else {
332                        // FIXME. We should handle this case better. It
333                        // indicates that we have e.g., some region variable
334                        // whose value is like `'a+'b` where `'a` and `'b` are
335                        // distinct unrelated universal regions that are not
336                        // known to outlive one another. It'd be nice to have
337                        // some examples where this arises to decide how best
338                        // to report it; we could probably handle it by
339                        // iterating over the universal regions and reporting
340                        // an error that multiple bounds are required.
341                        let mut diag = self.dcx().create_err(GenericDoesNotLiveLongEnough {
342                            kind: type_test.generic_kind.to_string(),
343                            span: type_test_span,
344                        });
345
346                        // Add notes and suggestions for the case of 'static lifetime
347                        // implied but not specified when a generic associated types
348                        // are from higher-ranked trait bounds
349                        self.suggest_static_lifetime_for_gat_from_hrtb(
350                            &mut diag,
351                            type_test.lower_bound,
352                        );
353
354                        self.buffer_error(diag);
355                    }
356                }
357
358                RegionErrorKind::PlaceholderOutlivesIllegalRegion {
359                    longer_fr,
360                    illegally_outlived_r,
361                } => {
362                    self.report_erroneous_rvid_reaches_placeholder(longer_fr, illegally_outlived_r)
363                }
364
365                RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
366                    if is_reported {
367                        self.report_region_error(
368                            longer_fr,
369                            fr_origin,
370                            shorter_fr,
371                            &mut outlives_suggestion,
372                        );
373                    } else {
374                        // We only report the first error, so as not to overwhelm the user. See
375                        // `RegRegionErrorKind` docs.
376                        //
377                        // FIXME: currently we do nothing with these, but perhaps we can do better?
378                        // FIXME: try collecting these constraints on the outlives suggestion
379                        // builder. Does it make the suggestions any better?
380                        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/diagnostics/region_errors.rs:380",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(380u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::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!("Unreported region error: can\'t prove that {0:?}: {1:?}",
                                                    longer_fr, shorter_fr) as &dyn Value))])
            });
    } else { ; }
};debug!(
381                            "Unreported region error: can't prove that {:?}: {:?}",
382                            longer_fr, shorter_fr
383                        );
384                    }
385                }
386            }
387        }
388
389        // Emit one outlives suggestions for each MIR def we borrowck
390        outlives_suggestion.add_suggestion(self);
391    }
392
393    /// Report that `longer_fr: error_vid`, which doesn't hold,
394    /// where `longer_fr` is a placeholder.
395    fn report_erroneous_rvid_reaches_placeholder(
396        &mut self,
397        longer_fr: RegionVid,
398        error_vid: RegionVid,
399    ) {
400        use NllRegionVariableOrigin::*;
401
402        let origin_longer = self.regioncx.definitions[longer_fr].origin;
403
404        let Placeholder(placeholder) = origin_longer else {
405            ::rustc_middle::util::bug::bug_fmt(format_args!("Expected {0:?} to come from placeholder!",
        longer_fr));bug!("Expected {longer_fr:?} to come from placeholder!");
406        };
407
408        // FIXME: Is throwing away the existential region really the best here?
409        let error_region = match self.regioncx.definitions[error_vid].origin {
410            FreeRegion | Existential { .. } => None,
411            Placeholder(other_placeholder) => Some(other_placeholder),
412        };
413
414        // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
415        let cause =
416            self.regioncx.best_blame_constraint(longer_fr, origin_longer, error_vid).0.cause;
417
418        // FIXME these methods should have better names, and also probably not be this generic.
419        // FIXME note that we *throw away* the error element here! We probably want to
420        // thread it through the computation further down and use it, but there currently isn't
421        // anything there to receive it.
422        self.regioncx.universe_info(placeholder.universe).report_erroneous_element(
423            self,
424            placeholder,
425            error_region,
426            cause,
427        );
428    }
429
430    /// Report an error because the universal region `fr` was required to outlive
431    /// `outlived_fr` but it is not known to do so. For example:
432    ///
433    /// ```compile_fail
434    /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
435    /// ```
436    ///
437    /// Here we would be invoked with `fr = 'a` and `outlived_fr = 'b`.
438    pub(crate) fn report_region_error(
439        &mut self,
440        fr: RegionVid,
441        fr_origin: NllRegionVariableOrigin<'tcx>,
442        outlived_fr: RegionVid,
443        outlives_suggestion: &mut OutlivesSuggestionBuilder,
444    ) {
445        {
    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/region_errors.rs:445",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(445u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::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!("report_region_error(fr={0:?}, outlived_fr={1:?})",
                                                    fr, outlived_fr) as &dyn Value))])
            });
    } else { ; }
};debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
446
447        let (blame_constraint, path) =
448            self.regioncx.best_blame_constraint(fr, fr_origin, outlived_fr);
449        let BlameConstraint { category, cause, variance_info, .. } = blame_constraint;
450
451        {
    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/region_errors.rs:451",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(451u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::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!("report_region_error: category={0:?} {1:?} {2:?}",
                                                    category, cause, variance_info) as &dyn Value))])
            });
    } else { ; }
};debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
452
453        // Check if we can use one of the "nice region errors".
454        if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
455            let infer_err = self.infcx.err_ctxt();
456            let nice =
457                NiceRegionError::new_from_span(&infer_err, self.mir_def_id(), cause.span, o, f);
458            if let Some(diag) = nice.try_report_from_nll() {
459                self.buffer_error(diag);
460                return;
461            }
462        }
463
464        let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
465            self.regioncx.universal_regions().is_local_free_region(fr),
466            self.regioncx.universal_regions().is_local_free_region(outlived_fr),
467        );
468
469        {
    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/region_errors.rs:469",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(469u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::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!("report_region_error: fr_is_local={0:?} outlived_fr_is_local={1:?} category={2:?}",
                                                    fr_is_local, outlived_fr_is_local, category) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(
470            "report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
471            fr_is_local, outlived_fr_is_local, category
472        );
473
474        let errci = ErrorConstraintInfo { fr, outlived_fr, category, span: cause.span };
475
476        let mut diag = match (category, fr_is_local, outlived_fr_is_local) {
477            (ConstraintCategory::SolverRegionConstraint(span), _, _) => {
478                let mut d = self.dcx().struct_span_err(
479                    span,
480                    "unsatisfied lifetime constraint from -Zassumptions-on-binders :3",
481                );
482                d.note("meoow :c");
483                d
484            }
485            (ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
486                self.report_fnmut_error(&errci, kind)
487            }
488            (ConstraintCategory::Assignment, true, false)
489            | (ConstraintCategory::CallArgument(_), true, false) => {
490                let mut db = self.report_escaping_data_error(&errci);
491
492                outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
493                outlives_suggestion.collect_constraint(fr, outlived_fr);
494
495                db
496            }
497            _ => {
498                let mut db = self.report_general_error(&errci);
499
500                outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
501                outlives_suggestion.collect_constraint(fr, outlived_fr);
502
503                db
504            }
505        };
506
507        match variance_info {
508            ty::VarianceDiagInfo::None => {}
509            ty::VarianceDiagInfo::Invariant { ty, param_index } => {
510                let (desc, note) = match ty.kind() {
511                    ty::RawPtr(ty, mutbl) => {
512                        match (&*mutbl, &hir::Mutability::Mut) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(*mutbl, hir::Mutability::Mut);
513                        (
514                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("a mutable pointer to `{0}`", ty))
    })format!("a mutable pointer to `{}`", ty),
515                            "mutable pointers are invariant over their type parameter".to_string(),
516                        )
517                    }
518                    ty::Ref(_, inner_ty, mutbl) => {
519                        match (&*mutbl, &hir::Mutability::Mut) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(*mutbl, hir::Mutability::Mut);
520                        (
521                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("a mutable reference to `{0}`",
                inner_ty))
    })format!("a mutable reference to `{inner_ty}`"),
522                            "mutable references are invariant over their type parameter"
523                                .to_string(),
524                        )
525                    }
526                    ty::Adt(adt, args) => {
527                        let generic_arg = args[param_index as usize];
528                        let identity_args =
529                            GenericArgs::identity_for_item(self.infcx.tcx, adt.did());
530                        let base_ty = Ty::new_adt(self.infcx.tcx, *adt, identity_args);
531                        let base_generic_arg = identity_args[param_index as usize];
532                        let adt_desc = adt.descr();
533
534                        let desc = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the type `{0}`, which makes the generic argument `{1}` invariant",
                ty, generic_arg))
    })format!(
535                            "the type `{ty}`, which makes the generic argument `{generic_arg}` invariant"
536                        );
537                        let note = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the {0} `{1}` is invariant over the parameter `{2}`",
                adt_desc, base_ty, base_generic_arg))
    })format!(
538                            "the {adt_desc} `{base_ty}` is invariant over the parameter `{base_generic_arg}`"
539                        );
540                        (desc, note)
541                    }
542                    ty::FnDef(def_id, _) => {
543                        let name = self.infcx.tcx.item_name(*def_id);
544                        let identity_args = GenericArgs::identity_for_item(self.infcx.tcx, *def_id);
545                        let desc = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the function item type defined by `{0}`",
                name))
    })format!("the function item type defined by `{name}`");
546                        let note = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the function `{1}` is invariant over the parameter `{0}`",
                identity_args[param_index as usize], name))
    })format!(
547                            "the function `{name}` is invariant over the parameter `{}`",
548                            identity_args[param_index as usize]
549                        );
550                        (desc, note)
551                    }
552                    _ => { ::core::panicking::panic_fmt(format_args!("Unexpected type {0:?}", ty)); }panic!("Unexpected type {ty:?}"),
553                };
554                diag.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("requirement occurs because of {0}",
                desc))
    })format!("requirement occurs because of {desc}",));
555                diag.note(note);
556                diag.help("see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance");
557            }
558        }
559
560        self.add_placeholder_from_predicate_note(&mut diag, &path);
561        self.add_sized_or_copy_bound_info(&mut diag, category, &path);
562
563        for constraint in &path {
564            if let ConstraintCategory::Cast { is_raw_ptr_dyn_type_cast: true, .. } =
565                constraint.category
566            {
567                diag.span_note(
568                    constraint.span,
569                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("raw pointer casts of trait objects cannot extend lifetimes"))
    })format!("raw pointer casts of trait objects cannot extend lifetimes"),
570                );
571                diag.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this was previously accepted by the compiler but was changed recently"))
    })format!(
572                    "this was previously accepted by the compiler but was changed recently"
573                ));
574                diag.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("see <https://github.com/rust-lang/rust/issues/141402> for more information"))
    })format!(
575                    "see <https://github.com/rust-lang/rust/issues/141402> for more information"
576                ));
577            }
578        }
579
580        self.buffer_error(diag);
581    }
582
583    /// Report a specialized error when `FnMut` closures return a reference to a captured variable.
584    /// This function expects `fr` to be local and `outlived_fr` to not be local.
585    ///
586    /// ```text
587    /// error: captured variable cannot escape `FnMut` closure body
588    ///   --> $DIR/issue-53040.rs:15:8
589    ///    |
590    /// LL |     || &mut v;
591    ///    |     -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
592    ///    |     |
593    ///    |     inferred to be a `FnMut` closure
594    ///    |
595    ///    = note: `FnMut` closures only have access to their captured variables while they are
596    ///            executing...
597    ///    = note: ...therefore, returned references to captured variables will escape the closure
598    /// ```
599    fn report_fnmut_error(
600        &self,
601        errci: &ErrorConstraintInfo<'tcx>,
602        kind: ReturnConstraint,
603    ) -> Diag<'infcx> {
604        let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
605
606        let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
607        if let ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) = *output_ty.kind() {
608            output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip()
609        };
610
611        {
    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/region_errors.rs:611",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(611u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::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!("report_fnmut_error: output_ty={0:?}",
                                                    output_ty) as &dyn Value))])
            });
    } else { ; }
};debug!("report_fnmut_error: output_ty={:?}", output_ty);
612
613        let err = FnMutError {
614            span: *span,
615            ty_err: match output_ty.kind() {
616                ty::Coroutine(def, ..) if self.infcx.tcx.coroutine_is_async(*def) => {
617                    FnMutReturnTypeErr::ReturnAsyncBlock { span: *span }
618                }
619                _ if output_ty.contains_closure() => {
620                    FnMutReturnTypeErr::ReturnClosure { span: *span }
621                }
622                _ => FnMutReturnTypeErr::ReturnRef { span: *span },
623            },
624        };
625
626        let mut diag = self.dcx().create_err(err);
627
628        if let ReturnConstraint::ClosureUpvar(upvar_field) = kind {
629            let def_id = match self.regioncx.universal_regions().defining_ty {
630                DefiningTy::Closure(def_id, _) => def_id,
631                ty => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected DefiningTy {0:?}",
        ty))bug!("unexpected DefiningTy {:?}", ty),
632            };
633
634            let captured_place = &self.upvars[upvar_field.index()].place;
635            let defined_hir = match captured_place.base {
636                PlaceBase::Local(hirid) => Some(hirid),
637                PlaceBase::Upvar(upvar) => Some(upvar.var_path.hir_id),
638                _ => None,
639            };
640
641            if let Some(def_hir) = defined_hir {
642                let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap();
643                let upvar_def_span = self.infcx.tcx.hir_span(def_hir);
644                let upvar_span = upvars_map.get(&def_hir).unwrap().span;
645                diag.subdiagnostic(VarHereDenote::Defined { span: upvar_def_span });
646                diag.subdiagnostic(VarHereDenote::Captured { span: upvar_span });
647            }
648        }
649
650        if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {
651            diag.subdiagnostic(VarHereDenote::FnMutInferred { span: fr_span });
652        }
653
654        self.suggest_move_on_borrowing_closure(&mut diag);
655
656        diag
657    }
658
659    /// Reports an error specifically for when data is escaping a closure.
660    ///
661    /// ```text
662    /// error: borrowed data escapes outside of function
663    ///   --> $DIR/lifetime-bound-will-change-warning.rs:44:5
664    ///    |
665    /// LL | fn test2<'a>(x: &'a Box<Fn()+'a>) {
666    ///    |              - `x` is a reference that is only valid in the function body
667    /// LL |     // but ref_obj will not, so warn.
668    /// LL |     ref_obj(x)
669    ///    |     ^^^^^^^^^^ `x` escapes the function body here
670    /// ```
671    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("report_escaping_data_error",
                                    "rustc_borrowck::diagnostics::region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(671u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                                    ::tracing_core::field::FieldSet::new(&["errci"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&errci)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: Diag<'infcx> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let ErrorConstraintInfo { span, category, .. } = errci;
            let fr_name_and_span =
                self.regioncx.get_var_name_and_span_for_region(self.infcx.tcx,
                    self.body, &self.local_names(), &self.upvars, errci.fr);
            let outlived_fr_name_and_span =
                self.regioncx.get_var_name_and_span_for_region(self.infcx.tcx,
                    self.body, &self.local_names(), &self.upvars,
                    errci.outlived_fr);
            let escapes_from =
                self.infcx.tcx.def_descr(self.regioncx.universal_regions().defining_ty.def_id());
            if (fr_name_and_span.is_none() &&
                                    outlived_fr_name_and_span.is_none()) ||
                            (*category == ConstraintCategory::Assignment &&
                                    self.regioncx.universal_regions().defining_ty.is_fn_def())
                        || self.regioncx.universal_regions().defining_ty.is_const()
                    ||
                    (fr_name_and_span.is_none() &&
                            self.regioncx.universal_regions().defining_ty.is_fn_def()) {
                return self.report_general_error(errci);
            }
            let mut diag =
                borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx,
                    *span, escapes_from);
            if let Some((Some(outlived_fr_name), outlived_fr_span)) =
                    outlived_fr_name_and_span {
                diag.span_label(outlived_fr_span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("`{0}` declared here, outside of the {1} body",
                                    outlived_fr_name, escapes_from))
                        }));
            }
            if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
                diag.span_label(fr_span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("`{0}` is a reference that is only valid in the {1} body",
                                    fr_name, escapes_from))
                        }));
                diag.span_label(*span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("`{0}` escapes the {1} body here",
                                    fr_name, escapes_from))
                        }));
            } else {
                diag.span_label(*span,
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("a temporary borrow escapes the {0} body here",
                                    escapes_from))
                        }));
                if let Some((Some(outlived_name), _)) =
                        outlived_fr_name_and_span {
                    diag.help(::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!("`{0}` is declared outside the {1}, so any data borrowed inside the {1} cannot be stored into it",
                                        outlived_name, escapes_from))
                            }));
                }
            }
            match (self.to_error_region(errci.fr),
                    self.to_error_region(errci.outlived_fr)) {
                (Some(f), Some(o)) => {
                    self.maybe_suggest_constrain_dyn_trait_impl(&mut diag, f, o,
                        category);
                    let fr_region_name =
                        self.give_region_a_name(errci.fr).unwrap();
                    fr_region_name.highlight_region_name(&mut diag);
                    let outlived_fr_region_name =
                        self.give_region_a_name(errci.outlived_fr).unwrap();
                    outlived_fr_region_name.highlight_region_name(&mut diag);
                    diag.span_label(*span,
                        ::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!("{0}requires that `{1}` must outlive `{2}`",
                                        category.description(), fr_region_name,
                                        outlived_fr_region_name))
                            }));
                }
                _ => {}
            }
            diag
        }
    }
}#[instrument(level = "debug", skip(self))]
672    fn report_escaping_data_error(&self, errci: &ErrorConstraintInfo<'tcx>) -> Diag<'infcx> {
673        let ErrorConstraintInfo { span, category, .. } = errci;
674
675        let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
676            self.infcx.tcx,
677            self.body,
678            &self.local_names(),
679            &self.upvars,
680            errci.fr,
681        );
682        let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
683            self.infcx.tcx,
684            self.body,
685            &self.local_names(),
686            &self.upvars,
687            errci.outlived_fr,
688        );
689
690        let escapes_from =
691            self.infcx.tcx.def_descr(self.regioncx.universal_regions().defining_ty.def_id());
692
693        // Revert to the normal error in these cases.
694        // Assignments aren't "escapes" in function items.
695        if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
696            || (*category == ConstraintCategory::Assignment
697                && self.regioncx.universal_regions().defining_ty.is_fn_def())
698            || self.regioncx.universal_regions().defining_ty.is_const()
699            || (fr_name_and_span.is_none()
700                && self.regioncx.universal_regions().defining_ty.is_fn_def())
701        {
702            return self.report_general_error(errci);
703        }
704
705        let mut diag =
706            borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
707
708        if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
709            diag.span_label(
710                outlived_fr_span,
711                format!("`{outlived_fr_name}` declared here, outside of the {escapes_from} body",),
712            );
713        }
714
715        if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
716            diag.span_label(
717                fr_span,
718                format!(
719                    "`{fr_name}` is a reference that is only valid in the {escapes_from} body",
720                ),
721            );
722
723            diag.span_label(*span, format!("`{fr_name}` escapes the {escapes_from} body here"));
724        } else {
725            diag.span_label(
726                *span,
727                format!("a temporary borrow escapes the {escapes_from} body here"),
728            );
729            if let Some((Some(outlived_name), _)) = outlived_fr_name_and_span {
730                diag.help(format!(
731                    "`{outlived_name}` is declared outside the {escapes_from}, \
732                     so any data borrowed inside the {escapes_from} cannot be stored into it"
733                ));
734            }
735        }
736
737        // Only show an extra note if we can find an 'error region' for both of the region
738        // variables. This avoids showing a noisy note that just mentions 'synthetic' regions
739        // that don't help the user understand the error.
740        match (self.to_error_region(errci.fr), self.to_error_region(errci.outlived_fr)) {
741            (Some(f), Some(o)) => {
742                self.maybe_suggest_constrain_dyn_trait_impl(&mut diag, f, o, category);
743
744                let fr_region_name = self.give_region_a_name(errci.fr).unwrap();
745                fr_region_name.highlight_region_name(&mut diag);
746                let outlived_fr_region_name = self.give_region_a_name(errci.outlived_fr).unwrap();
747                outlived_fr_region_name.highlight_region_name(&mut diag);
748
749                diag.span_label(
750                    *span,
751                    format!(
752                        "{}requires that `{}` must outlive `{}`",
753                        category.description(),
754                        fr_region_name,
755                        outlived_fr_region_name,
756                    ),
757                );
758            }
759            _ => {}
760        }
761
762        diag
763    }
764
765    /// Reports a region inference error for the general case with named/synthesized lifetimes to
766    /// explain what is happening.
767    ///
768    /// ```text
769    /// error: unsatisfied lifetime constraints
770    ///   --> $DIR/regions-creating-enums3.rs:17:5
771    ///    |
772    /// LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
773    ///    |                -- -- lifetime `'b` defined here
774    ///    |                |
775    ///    |                lifetime `'a` defined here
776    /// LL |     ast::add(x, y)
777    ///    |     ^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it
778    ///    |                    is returning data with lifetime `'b`
779    /// ```
780    fn report_general_error(&self, errci: &ErrorConstraintInfo<'tcx>) -> Diag<'infcx> {
781        let ErrorConstraintInfo { fr, outlived_fr, span, category, .. } = errci;
782
783        let mir_def_name = self.infcx.tcx.def_descr(self.mir_def_id().to_def_id());
784
785        let err = LifetimeOutliveErr { span: *span };
786        let mut diag = self.dcx().create_err(err);
787
788        // In certain scenarios, such as the one described in issue #118021,
789        // we might encounter a lifetime that cannot be named.
790        // These situations are bound to result in errors.
791        // To prevent an immediate ICE, we opt to create a dummy name instead.
792        let fr_name = self.give_region_a_name(*fr).unwrap_or(RegionName {
793            name: kw::UnderscoreLifetime,
794            source: RegionNameSource::Static,
795        });
796        fr_name.highlight_region_name(&mut diag);
797        let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
798        outlived_fr_name.highlight_region_name(&mut diag);
799
800        let err_category = if #[allow(non_exhaustive_omitted_patterns)] match category {
    ConstraintCategory::Return(_) => true,
    _ => false,
}matches!(category, ConstraintCategory::Return(_))
801            && self.regioncx.universal_regions().is_local_free_region(*outlived_fr)
802        {
803            LifetimeReturnCategoryErr::WrongReturn {
804                span: *span,
805                mir_def_name,
806                outlived_fr_name,
807                fr_name: &fr_name,
808            }
809        } else {
810            LifetimeReturnCategoryErr::ShortReturn {
811                span: *span,
812                category_desc: category.description(),
813                free_region_name: &fr_name,
814                outlived_fr_name,
815            }
816        };
817
818        diag.subdiagnostic(err_category);
819
820        self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
821        self.suggest_adding_lifetime_params(&mut diag, *fr, *outlived_fr);
822        self.suggest_move_on_borrowing_closure(&mut diag);
823        self.suggest_deref_closure_return(&mut diag);
824
825        diag
826    }
827
828    /// Adds a suggestion to errors where an `impl Trait` is returned.
829    ///
830    /// ```text
831    /// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
832    ///       a constraint
833    ///    |
834    /// LL |     fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
835    ///    |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
836    /// ```
837    fn add_static_impl_trait_suggestion(
838        &self,
839        diag: &mut Diag<'_>,
840        fr: RegionVid,
841        // We need to pass `fr_name` - computing it again will label it twice.
842        fr_name: RegionName,
843        outlived_fr: RegionVid,
844    ) {
845        if let (Some(f), Some(outlived_f)) =
846            (self.to_error_region(fr), self.to_error_region(outlived_fr))
847        {
848            if outlived_f.kind() != ty::ReStatic {
849                return;
850            }
851            let suitable_region = self.infcx.tcx.is_suitable_region(self.mir_def_id(), f);
852            let Some(suitable_region) = suitable_region else {
853                return;
854            };
855
856            let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.scope);
857
858            let Some(param) =
859                find_param_with_region(self.infcx.tcx, self.mir_def_id(), f, outlived_f)
860            else {
861                return;
862            };
863
864            let lifetime =
865                if f.is_named(self.infcx.tcx) { fr_name.name } else { kw::UnderscoreLifetime };
866
867            let arg = match param.param.pat.simple_ident() {
868                Some(simple_ident) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("argument `{0}`", simple_ident))
    })format!("argument `{simple_ident}`"),
869                None => "the argument".to_string(),
870            };
871            let captures = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("captures data from {0}", arg))
    })format!("captures data from {arg}");
872
873            if !fn_returns.is_empty() {
874                nice_region_error::suggest_new_region_bound(
875                    self.infcx.tcx,
876                    diag,
877                    fn_returns,
878                    lifetime.to_string(),
879                    Some(arg),
880                    captures,
881                    Some((param.param_ty_span, param.param_ty.to_string())),
882                    Some(suitable_region.scope),
883                );
884                return;
885            }
886
887            let Some((alias_tys, alias_span, lt_addition_span)) = self
888                .infcx
889                .tcx
890                .return_type_impl_or_dyn_traits_with_type_alias(suitable_region.scope)
891            else {
892                return;
893            };
894
895            // in case the return type of the method is a type alias
896            let mut spans_suggs: Vec<_> = Vec::new();
897            for alias_ty in alias_tys {
898                if alias_ty.span.desugaring_kind().is_some() {
899                    // Skip `async` desugaring `impl Future`.
900                    continue;
901                }
902                if let TyKind::TraitObject(_, lt) = alias_ty.kind {
903                    if lt.kind == hir::LifetimeKind::ImplicitObjectLifetimeDefault {
904                        spans_suggs.push((lt.ident.span.shrink_to_hi(), " + 'a".to_string()));
905                    } else {
906                        spans_suggs.push((lt.ident.span, "'a".to_string()));
907                    }
908                }
909            }
910
911            if let Some(lt_addition_span) = lt_addition_span {
912                spans_suggs.push((lt_addition_span, "'a, ".to_string()));
913            } else {
914                spans_suggs.push((alias_span.shrink_to_hi(), "<'a>".to_string()));
915            }
916
917            diag.multipart_suggestion(
918                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("to declare that the trait object {0}, you can add a lifetime parameter `\'a` in the type alias",
                captures))
    })format!(
919                    "to declare that the trait object {captures}, you can add a lifetime parameter `'a` in the type alias"
920                ),
921                spans_suggs,
922                Applicability::MaybeIncorrect,
923            );
924        }
925    }
926
927    fn maybe_suggest_constrain_dyn_trait_impl(
928        &self,
929        diag: &mut Diag<'_>,
930        f: Region<'tcx>,
931        o: Region<'tcx>,
932        category: &ConstraintCategory<'tcx>,
933    ) {
934        if !o.is_static() {
935            return;
936        }
937
938        let tcx = self.infcx.tcx;
939
940        let ConstraintCategory::CallArgument(Some(func_ty)) = category else { return };
941        let ty::FnDef(fn_did, args) = *func_ty.kind() else { return };
942        {
    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/region_errors.rs:942",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(942u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::tracing_core::field::FieldSet::new(&["fn_did", "args"],
                            ::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(&debug(&fn_did) as
                                            &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&args) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?fn_did, ?args);
943
944        // Only suggest this on function calls, not closures
945        let ty = tcx.type_of(fn_did).instantiate_identity().skip_norm_wip();
946        {
    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/region_errors.rs:946",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(946u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::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!("ty: {0:?}, ty.kind: {1:?}",
                                                    ty, ty.kind()) as &dyn Value))])
            });
    } else { ; }
};debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
947        if let ty::Closure(_, _) = ty.kind() {
948            return;
949        }
950        let Ok(Some(instance)) = ty::Instance::try_resolve(
951            tcx,
952            self.infcx.typing_env(self.infcx.param_env),
953            fn_did,
954            self.infcx.resolve_vars_if_possible(args),
955        ) else {
956            return;
957        };
958
959        let Some(param) = find_param_with_region(tcx, self.mir_def_id(), f, o) else {
960            return;
961        };
962        {
    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/region_errors.rs:962",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(962u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::tracing_core::field::FieldSet::new(&["param"],
                            ::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(&debug(&param) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?param);
963
964        let mut visitor = TraitObjectVisitor(FxIndexSet::default());
965        visitor.visit_ty(param.param_ty);
966
967        let Some((ident, self_ty)) = NiceRegionError::get_impl_ident_and_self_ty_from_trait(
968            tcx,
969            instance.def_id(),
970            &visitor.0,
971        ) else {
972            return;
973        };
974
975        self.suggest_constrain_dyn_trait_in_impl(diag, &visitor.0, ident, self_ty);
976    }
977
978    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("suggest_constrain_dyn_trait_in_impl",
                                    "rustc_borrowck::diagnostics::region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(978u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                                    ::tracing_core::field::FieldSet::new(&["found_dids",
                                                    "ident", "self_ty"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&found_dids)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&ident)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&self_ty)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: bool = loop {};
            return __tracing_attr_fake_return;
        }
        {
            {
                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/region_errors.rs:986",
                                    "rustc_borrowck::diagnostics::region_errors",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                                    ::tracing_core::__macro_support::Option::Some(986u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                                    ::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!("err: {0:#?}",
                                                                err) as &dyn Value))])
                        });
                } else { ; }
            };
            let mut suggested = false;
            for found_did in found_dids {
                let mut traits = ::alloc::vec::Vec::new();
                let mut hir_v =
                    HirTraitObjectVisitor(&mut traits, *found_did);
                hir_v.visit_ty_unambig(self_ty);
                {
                    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/region_errors.rs:992",
                                        "rustc_borrowck::diagnostics::region_errors",
                                        ::tracing::Level::DEBUG,
                                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                                        ::tracing_core::__macro_support::Option::Some(992u32),
                                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                                        ::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!("trait spans found: {0:?}",
                                                                    traits) as &dyn Value))])
                            });
                    } else { ; }
                };
                for span in &traits {
                    let mut multi_span: MultiSpan =
                        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                                    [*span])).into();
                    multi_span.push_span_label(*span,
                        rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("this has an implicit `'static` lifetime requirement")));
                    multi_span.push_span_label(ident.span,
                        rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("calling this method introduces the `impl`'s `'static` requirement")));
                    err.subdiagnostic(RequireStaticErr::UsedImpl {
                            multi_span,
                        });
                    err.span_suggestion_verbose(span.shrink_to_hi(),
                        rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("consider relaxing the implicit `'static` requirement")),
                        " + '_", Applicability::MaybeIncorrect);
                    suggested = true;
                }
            }
            suggested
        }
    }
}#[instrument(skip(self, err), level = "debug")]
979    fn suggest_constrain_dyn_trait_in_impl(
980        &self,
981        err: &mut Diag<'_>,
982        found_dids: &FxIndexSet<DefId>,
983        ident: Ident,
984        self_ty: &hir::Ty<'_>,
985    ) -> bool {
986        debug!("err: {:#?}", err);
987        let mut suggested = false;
988        for found_did in found_dids {
989            let mut traits = vec![];
990            let mut hir_v = HirTraitObjectVisitor(&mut traits, *found_did);
991            hir_v.visit_ty_unambig(self_ty);
992            debug!("trait spans found: {:?}", traits);
993            for span in &traits {
994                let mut multi_span: MultiSpan = vec![*span].into();
995                multi_span.push_span_label(
996                    *span,
997                    msg!("this has an implicit `'static` lifetime requirement"),
998                );
999                multi_span.push_span_label(
1000                    ident.span,
1001                    msg!("calling this method introduces the `impl`'s `'static` requirement"),
1002                );
1003                err.subdiagnostic(RequireStaticErr::UsedImpl { multi_span });
1004                err.span_suggestion_verbose(
1005                    span.shrink_to_hi(),
1006                    msg!("consider relaxing the implicit `'static` requirement"),
1007                    " + '_",
1008                    Applicability::MaybeIncorrect,
1009                );
1010                suggested = true;
1011            }
1012        }
1013        suggested
1014    }
1015
1016    fn suggest_adding_lifetime_params(&self, diag: &mut Diag<'_>, sub: RegionVid, sup: RegionVid) {
1017        let (Some(sub), Some(sup)) = (self.to_error_region(sub), self.to_error_region(sup)) else {
1018            return;
1019        };
1020
1021        let Some((ty_sub, _)) = self
1022            .infcx
1023            .tcx
1024            .is_suitable_region(self.mir_def_id(), sub)
1025            .and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sub))
1026        else {
1027            return;
1028        };
1029
1030        let Some((ty_sup, _)) = self
1031            .infcx
1032            .tcx
1033            .is_suitable_region(self.mir_def_id(), sup)
1034            .and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sup))
1035        else {
1036            return;
1037        };
1038
1039        suggest_adding_lifetime_params(
1040            self.infcx.tcx,
1041            diag,
1042            self.mir_def_id(),
1043            sub,
1044            ty_sup,
1045            ty_sub,
1046        );
1047    }
1048
1049    /// When encountering a lifetime error caused by the return type of a closure, check the
1050    /// corresponding trait bound and see if dereferencing the closure return value would satisfy
1051    /// them. If so, we produce a structured suggestion.
1052    fn suggest_deref_closure_return(&self, diag: &mut Diag<'_>) {
1053        let tcx = self.infcx.tcx;
1054
1055        // Get the closure return value and type.
1056        let closure_def_id = self.mir_def_id();
1057        let hir::Node::Expr(
1058            closure_expr @ hir::Expr {
1059                kind: hir::ExprKind::Closure(hir::Closure { body, .. }), ..
1060            },
1061        ) = tcx.hir_node_by_def_id(closure_def_id)
1062        else {
1063            return;
1064        };
1065        let ty::Closure(_, args) =
1066            *tcx.type_of(closure_def_id).instantiate_identity().skip_norm_wip().kind()
1067        else {
1068            return;
1069        };
1070        let args = args.as_closure();
1071
1072        // Make sure that the parent expression is a method call.
1073        let parent_expr_id = tcx.parent_hir_id(self.mir_hir_id());
1074        let hir::Node::Expr(
1075            parent_expr @ hir::Expr {
1076                kind: hir::ExprKind::MethodCall(_, rcvr, call_args, _), ..
1077            },
1078        ) = tcx.hir_node(parent_expr_id)
1079        else {
1080            return;
1081        };
1082        let typeck_results = tcx.typeck(self.mir_def_id());
1083
1084        // We don't use `ty.peel_refs()` to get the number of `*`s needed to get the root type.
1085        let liberated_sig = tcx.liberate_late_bound_regions(closure_def_id.to_def_id(), args.sig());
1086        let mut peeled_ty = liberated_sig.output();
1087        let mut count = 0;
1088        while let ty::Ref(_, ref_ty, _) = *peeled_ty.kind() {
1089            peeled_ty = ref_ty;
1090            count += 1;
1091        }
1092        if !self.infcx.type_is_copy_modulo_regions(self.infcx.param_env, peeled_ty) {
1093            return;
1094        }
1095
1096        // Build a new closure where the return type is an owned value, instead of a ref.
1097        let fn_sig_kind = FnSigKind::default()
1098            .set_safety(hir::Safety::Safe)
1099            .set_c_variadic(liberated_sig.c_variadic());
1100        let closure_sig_as_fn_ptr_ty = Ty::new_fn_ptr(
1101            tcx,
1102            ty::Binder::dummy(tcx.mk_fn_sig(
1103                liberated_sig.inputs().iter().copied(),
1104                peeled_ty,
1105                fn_sig_kind,
1106            )),
1107        );
1108        let closure_ty = Ty::new_closure(
1109            tcx,
1110            closure_def_id.to_def_id(),
1111            ty::ClosureArgs::new(
1112                tcx,
1113                ty::ClosureArgsParts {
1114                    parent_args: args.parent_args(),
1115                    closure_kind_ty: args.kind_ty(),
1116                    tupled_upvars_ty: args.tupled_upvars_ty(),
1117                    closure_sig_as_fn_ptr_ty,
1118                },
1119            )
1120            .args,
1121        );
1122
1123        let Some((closure_arg_pos, _)) =
1124            call_args.iter().enumerate().find(|(_, arg)| arg.hir_id == closure_expr.hir_id)
1125        else {
1126            return;
1127        };
1128        // Get the type for the parameter corresponding to the argument the closure with the
1129        // lifetime error we had.
1130        let Some(method_def_id) = typeck_results.type_dependent_def_id(parent_expr.hir_id) else {
1131            return;
1132        };
1133        let Some(input_arg) = tcx
1134            .fn_sig(method_def_id)
1135            .skip_binder()
1136            .inputs()
1137            .skip_binder()
1138            // Methods have a `self` arg, so `pos` is actually `+ 1` to match the method call arg.
1139            .get(closure_arg_pos + 1)
1140        else {
1141            return;
1142        };
1143        // If this isn't a param, then we can't substitute a new closure.
1144        let ty::Param(closure_param) = input_arg.kind() else { return };
1145
1146        // Get the arguments for the found method, only specifying that `Self` is the receiver type.
1147        let Some(possible_rcvr_ty) = typeck_results.node_type_opt(rcvr.hir_id) else { return };
1148        let args = GenericArgs::for_item(tcx, method_def_id, |param, _| {
1149            if let ty::GenericParamDefKind::Lifetime = param.kind {
1150                tcx.lifetimes.re_erased.into()
1151            } else if param.index == 0 && param.name == kw::SelfUpper {
1152                possible_rcvr_ty.into()
1153            } else if param.index == closure_param.index {
1154                closure_ty.into()
1155            } else {
1156                self.infcx.var_for_def(parent_expr.span, param)
1157            }
1158        });
1159
1160        let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args);
1161
1162        let ocx = ObligationCtxt::new(&self.infcx);
1163        ocx.register_obligations(preds.iter().map(|(pred, span)| {
1164            {
    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/region_errors.rs:1164",
                        "rustc_borrowck::diagnostics::region_errors",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/diagnostics/region_errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(1164u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_errors"),
                        ::tracing_core::field::FieldSet::new(&["pred"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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(&debug(&pred) as
                                            &dyn Value))])
            });
    } else { ; }
};trace!(?pred);
1165            Obligation::misc(
1166                tcx,
1167                span,
1168                self.mir_def_id(),
1169                self.infcx.param_env,
1170                pred.skip_norm_wip(),
1171            )
1172        }));
1173
1174        if ocx.evaluate_obligations_error_on_ambiguity().is_empty() && count > 0 {
1175            diag.span_suggestion_verbose(
1176                tcx.hir_body(*body).value.peel_blocks().span.shrink_to_lo(),
1177                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("dereference the return value"))msg!("dereference the return value"),
1178                "*".repeat(count),
1179                Applicability::MachineApplicable,
1180            );
1181        }
1182    }
1183
1184    fn suggest_move_on_borrowing_closure(&self, diag: &mut Diag<'_>) {
1185        let body = self.infcx.tcx.hir_body_owned_by(self.mir_def_id());
1186        let expr = &body.value.peel_blocks();
1187        let mut closure_span = None::<rustc_span::Span>;
1188        match expr.kind {
1189            hir::ExprKind::MethodCall(.., args, _) => {
1190                for arg in args {
1191                    if let hir::ExprKind::Closure(hir::Closure {
1192                        capture_clause: hir::CaptureBy::Ref,
1193                        ..
1194                    }) = arg.kind
1195                    {
1196                        closure_span = Some(arg.span.shrink_to_lo());
1197                        break;
1198                    }
1199                }
1200            }
1201            hir::ExprKind::Closure(hir::Closure {
1202                capture_clause: hir::CaptureBy::Ref,
1203                kind,
1204                ..
1205            }) => {
1206                if !#[allow(non_exhaustive_omitted_patterns)] match kind {
    hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async,
        _)) => true,
    _ => false,
}matches!(
1207                    kind,
1208                    hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1209                        hir::CoroutineDesugaring::Async,
1210                        _
1211                    ),)
1212                ) {
1213                    closure_span = Some(expr.span.shrink_to_lo());
1214                }
1215            }
1216            _ => {}
1217        }
1218        if let Some(closure_span) = closure_span {
1219            diag.span_suggestion_verbose(
1220                closure_span,
1221                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("consider adding 'move' keyword before the nested closure"))msg!("consider adding 'move' keyword before the nested closure"),
1222                "move ",
1223                Applicability::MaybeIncorrect,
1224            );
1225        }
1226    }
1227}