Skip to main content

rustc_trait_selection/error_reporting/infer/
region.rs

1use std::iter;
2
3use rustc_data_structures::fx::FxIndexSet;
4use rustc_errors::{
5    Applicability, Diag, E0309, E0310, E0311, E0803, Subdiagnostic, msg, struct_span_code_err,
6};
7use rustc_hir::def::DefKind;
8use rustc_hir::def_id::{DefId, LocalDefId};
9use rustc_hir::intravisit::Visitor;
10use rustc_hir::{self as hir, ParamName};
11use rustc_middle::bug;
12use rustc_middle::traits::ObligationCauseCode;
13use rustc_middle::ty::error::TypeError;
14use rustc_middle::ty::{
15    self, IsSuggestable, Region, Ty, TyCtxt, TypeVisitableExt as _, Upcast as _,
16};
17use rustc_span::{BytePos, ErrorGuaranteed, Span, Symbol, kw, sym};
18use tracing::{debug, instrument};
19
20use super::ObligationCauseAsDiagArg;
21use super::nice_region_error::find_anon_type;
22use crate::error_reporting::TypeErrCtxt;
23use crate::error_reporting::infer::ObligationCauseExt;
24use crate::errors::{
25    self, FulfillReqLifetime, LfBoundNotSatisfied, OutlivesBound, OutlivesContent,
26    RefLongerThanData, RegionOriginNote, WhereClauseSuggestions, note_and_explain,
27};
28use crate::infer::region_constraints::GenericKind;
29use crate::infer::{
30    BoundRegionConversionTime, InferCtxt, RegionResolutionError, RegionVariableOrigin,
31    SubregionOrigin,
32};
33
34impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
35    pub fn report_region_errors(
36        &self,
37        generic_param_scope: LocalDefId,
38        errors: &[RegionResolutionError<'tcx>],
39    ) -> ErrorGuaranteed {
40        if !!errors.is_empty() {
    ::core::panicking::panic("assertion failed: !errors.is_empty()")
};assert!(!errors.is_empty());
41
42        if let Some(guaranteed) = self.infcx.tainted_by_errors() {
43            return guaranteed;
44        }
45
46        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:46",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(46u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::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_errors(): {0} errors to start",
                                                    errors.len()) as &dyn Value))])
            });
    } else { ; }
};debug!("report_region_errors(): {} errors to start", errors.len());
47
48        // try to pre-process the errors, which will group some of them
49        // together into a `ProcessedErrors` group:
50        let errors = self.process_errors(errors);
51
52        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:52",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(52u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::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_errors: {0} errors after preprocessing",
                                                    errors.len()) as &dyn Value))])
            });
    } else { ; }
};debug!("report_region_errors: {} errors after preprocessing", errors.len());
53
54        let mut guar = None;
55        for error in errors {
56            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:56",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(56u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::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_errors: error = {0:?}",
                                                    error) as &dyn Value))])
            });
    } else { ; }
};debug!("report_region_errors: error = {:?}", error);
57
58            let e = if let Some(guar) =
59                self.try_report_nice_region_error(generic_param_scope, &error)
60            {
61                guar
62            } else {
63                match error.clone() {
64                    // These errors could indicate all manner of different
65                    // problems with many different solutions. Rather
66                    // than generate a "one size fits all" error, what we
67                    // attempt to do is go through a number of specific
68                    // scenarios and try to find the best way to present
69                    // the error. If all of these fails, we fall back to a rather
70                    // general bit of code that displays the error information
71                    RegionResolutionError::ConcreteFailure(origin, sub, sup) => {
72                        if sub.is_placeholder() || sup.is_placeholder() {
73                            self.report_placeholder_failure(generic_param_scope, origin, sub, sup)
74                                .emit()
75                        } else {
76                            self.report_concrete_failure(generic_param_scope, origin, sub, sup)
77                                .emit()
78                        }
79                    }
80
81                    RegionResolutionError::GenericBoundFailure(origin, param_ty, sub) => self
82                        .report_generic_bound_failure(
83                            generic_param_scope,
84                            origin.span(),
85                            Some(origin),
86                            param_ty,
87                            sub,
88                        ),
89
90                    RegionResolutionError::SubSupConflict(
91                        _,
92                        var_origin,
93                        sub_origin,
94                        sub_r,
95                        sup_origin,
96                        sup_r,
97                        _,
98                    ) => {
99                        if sub_r.is_placeholder() {
100                            self.report_placeholder_failure(
101                                generic_param_scope,
102                                sub_origin,
103                                sub_r,
104                                sup_r,
105                            )
106                            .emit()
107                        } else if sup_r.is_placeholder() {
108                            self.report_placeholder_failure(
109                                generic_param_scope,
110                                sup_origin,
111                                sub_r,
112                                sup_r,
113                            )
114                            .emit()
115                        } else {
116                            self.report_sub_sup_conflict(
117                                generic_param_scope,
118                                var_origin,
119                                sub_origin,
120                                sub_r,
121                                sup_origin,
122                                sup_r,
123                            )
124                        }
125                    }
126
127                    RegionResolutionError::UpperBoundUniverseConflict(
128                        _,
129                        _,
130                        _,
131                        sup_origin,
132                        sup_r,
133                    ) => {
134                        if !sup_r.is_placeholder() {
    ::core::panicking::panic("assertion failed: sup_r.is_placeholder()")
};assert!(sup_r.is_placeholder());
135
136                        // Make a dummy value for the "sub region" --
137                        // this is the initial value of the
138                        // placeholder. In practice, we expect more
139                        // tailored errors that don't really use this
140                        // value.
141                        let sub_r = self.tcx.lifetimes.re_erased;
142
143                        self.report_placeholder_failure(
144                            generic_param_scope,
145                            sup_origin,
146                            sub_r,
147                            sup_r,
148                        )
149                        .emit()
150                    }
151
152                    RegionResolutionError::CannotNormalize(clause, origin) => {
153                        let clause: ty::Clause<'tcx> =
154                            clause.map_bound(ty::ClauseKind::TypeOutlives).upcast(self.tcx);
155                        self.tcx
156                            .dcx()
157                            .struct_span_err(origin.span(), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("cannot normalize `{0}`", clause))
    })format!("cannot normalize `{clause}`"))
158                            .emit()
159                    }
160                }
161            };
162
163            guar = Some(e)
164        }
165
166        guar.unwrap()
167    }
168
169    // This method goes through all the errors and try to group certain types
170    // of error together, for the purpose of suggesting explicit lifetime
171    // parameters to the user. This is done so that we can have a more
172    // complete view of what lifetimes should be the same.
173    // If the return value is an empty vector, it means that processing
174    // failed (so the return value of this method should not be used).
175    //
176    // The method also attempts to weed out messages that seem like
177    // duplicates that will be unhelpful to the end-user. But
178    // obviously it never weeds out ALL errors.
179    fn process_errors(
180        &self,
181        errors: &[RegionResolutionError<'tcx>],
182    ) -> Vec<RegionResolutionError<'tcx>> {
183        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:183",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(183u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::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!("process_errors()")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("process_errors()");
184
185        // We want to avoid reporting generic-bound failures if we can
186        // avoid it: these have a very high rate of being unhelpful in
187        // practice. This is because they are basically secondary
188        // checks that test the state of the region graph after the
189        // rest of inference is done, and the other kinds of errors
190        // indicate that the region constraint graph is internally
191        // inconsistent, so these test results are likely to be
192        // meaningless.
193        //
194        // Therefore, we filter them out of the list unless they are
195        // the only thing in the list.
196
197        let is_bound_failure = |e: &RegionResolutionError<'tcx>| match *e {
198            RegionResolutionError::GenericBoundFailure(..) => true,
199            RegionResolutionError::ConcreteFailure(..)
200            | RegionResolutionError::SubSupConflict(..)
201            | RegionResolutionError::UpperBoundUniverseConflict(..)
202            | RegionResolutionError::CannotNormalize(..) => false,
203        };
204
205        let mut errors = if errors.iter().all(|e| is_bound_failure(e)) {
206            errors.to_owned()
207        } else {
208            errors.iter().filter(|&e| !is_bound_failure(e)).cloned().collect()
209        };
210
211        // sort the errors by span, for better error message stability.
212        errors.sort_by_key(|u| match *u {
213            RegionResolutionError::ConcreteFailure(ref sro, _, _) => sro.span(),
214            RegionResolutionError::GenericBoundFailure(ref sro, _, _) => sro.span(),
215            RegionResolutionError::SubSupConflict(_, ref rvo, _, _, _, _, _) => rvo.span(),
216            RegionResolutionError::UpperBoundUniverseConflict(_, ref rvo, _, _, _) => rvo.span(),
217            RegionResolutionError::CannotNormalize(_, ref sro) => sro.span(),
218        });
219        errors
220    }
221
222    pub(super) fn note_region_origin(&self, err: &mut Diag<'_>, origin: &SubregionOrigin<'tcx>) {
223        match *origin {
224            SubregionOrigin::Subtype(ref trace) => RegionOriginNote::WithRequirement {
225                span: trace.cause.span,
226                requirement: ObligationCauseAsDiagArg(trace.cause.clone()),
227                expected_found: self.values_str(trace.values, &trace.cause, err.long_ty_path()),
228            }
229            .add_to_diag(err),
230            SubregionOrigin::Reborrow(span) => RegionOriginNote::Plain {
231                span,
232                msg: rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("...so that reference does not outlive borrowed content"))msg!("...so that reference does not outlive borrowed content"),
233            }
234            .add_to_diag(err),
235            SubregionOrigin::RelateObjectBound(span) => {
236                RegionOriginNote::Plain {
237                    span,
238                    msg: rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("...so that it can be closed over into an object"))msg!("...so that it can be closed over into an object"),
239                }
240                .add_to_diag(err);
241            }
242            SubregionOrigin::ReferenceOutlivesReferent(ty, span) => {
243                RegionOriginNote::WithName {
244                    span,
245                    msg: rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("...so that the reference type `{$name}` does not outlive the data it points at"))msg!("...so that the reference type `{$name}` does not outlive the data it points at"),
246                    name: &self.ty_to_string(ty),
247                    continues: false,
248                }
249                .add_to_diag(err);
250            }
251            SubregionOrigin::RelateParamBound(span, ty, opt_span) => {
252                RegionOriginNote::WithName {
253                    span,
254                    msg: rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("...so that the type `{$name}` will meet its required lifetime bounds{$continues ->\n                            [true] ...\n                            *[false] {\"\"}\n                        }"))msg!(
255                        "...so that the type `{$name}` will meet its required lifetime bounds{$continues ->
256                            [true] ...
257                            *[false] {\"\"}
258                        }"
259                    ),
260                    name: &self.ty_to_string(ty),
261                    continues: opt_span.is_some(),
262                }
263                .add_to_diag(err);
264                if let Some(span) = opt_span {
265                    RegionOriginNote::Plain {
266                        span,
267                        msg: rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("...that is required by this bound"))msg!("...that is required by this bound"),
268                    }
269                    .add_to_diag(err);
270                }
271            }
272            SubregionOrigin::RelateRegionParamBound(span, _) => {
273                RegionOriginNote::Plain {
274                    span,
275                    msg: rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("...so that the declared lifetime parameter bounds are satisfied"))msg!("...so that the declared lifetime parameter bounds are satisfied"),
276                }
277                .add_to_diag(err);
278            }
279            SubregionOrigin::CompareImplItemObligation { span, .. } => {
280                RegionOriginNote::Plain {
281                    span,
282                    msg: rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("...so that the definition in impl matches the definition from the trait"))msg!(
283                        "...so that the definition in impl matches the definition from the trait"
284                    ),
285                }
286                .add_to_diag(err);
287            }
288            SubregionOrigin::CheckAssociatedTypeBounds { ref parent, .. } => {
289                self.note_region_origin(err, parent);
290            }
291            SubregionOrigin::AscribeUserTypeProvePredicate(span) => {
292                RegionOriginNote::Plain { span, msg: rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("...so that the where clause holds"))msg!("...so that the where clause holds") }
293                    .add_to_diag(err);
294            }
295            SubregionOrigin::SolverRegionConstraint(span) => {
296                RegionOriginNote::Plain {
297                    span,
298                    msg: rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("this diagnostic is currently WIP while -Zassumptions-on-binders is incomplete"))msg!("this diagnostic is currently WIP while -Zassumptions-on-binders is incomplete"),
299                }
300                .add_to_diag(err);
301            }
302        }
303    }
304
305    pub(super) fn report_concrete_failure(
306        &self,
307        generic_param_scope: LocalDefId,
308        origin: SubregionOrigin<'tcx>,
309        sub: Region<'tcx>,
310        sup: Region<'tcx>,
311    ) -> Diag<'a> {
312        let mut err = match origin {
313            SubregionOrigin::Subtype(trace) => {
314                let terr = TypeError::RegionsDoesNotOutlive(sup, sub);
315                let mut err = self.report_and_explain_type_error(
316                    *trace,
317                    self.tcx.param_env(generic_param_scope),
318                    terr,
319                );
320                match (sub.kind(), sup.kind()) {
321                    (ty::RePlaceholder(_), ty::RePlaceholder(_)) => {}
322                    (ty::RePlaceholder(_), _) => {
323                        note_and_explain_region(
324                            self.tcx,
325                            &mut err,
326                            generic_param_scope,
327                            "",
328                            sup,
329                            " doesn't meet the lifetime requirements",
330                            None,
331                        );
332                    }
333                    (_, ty::RePlaceholder(_)) => {
334                        note_and_explain_region(
335                            self.tcx,
336                            &mut err,
337                            generic_param_scope,
338                            "the required lifetime does not necessarily outlive ",
339                            sub,
340                            "",
341                            None,
342                        );
343                    }
344                    _ => {
345                        note_and_explain_region(
346                            self.tcx,
347                            &mut err,
348                            generic_param_scope,
349                            "",
350                            sup,
351                            "...",
352                            None,
353                        );
354                        note_and_explain_region(
355                            self.tcx,
356                            &mut err,
357                            generic_param_scope,
358                            "...does not necessarily outlive ",
359                            sub,
360                            "",
361                            None,
362                        );
363                    }
364                }
365                err
366            }
367            SubregionOrigin::Reborrow(span) => {
368                let reference_valid = note_and_explain::RegionExplanation::new(
369                    self.tcx,
370                    generic_param_scope,
371                    sub,
372                    None,
373                    note_and_explain::PrefixKind::RefValidFor,
374                    note_and_explain::SuffixKind::Continues,
375                );
376                let content_valid = note_and_explain::RegionExplanation::new(
377                    self.tcx,
378                    generic_param_scope,
379                    sup,
380                    None,
381                    note_and_explain::PrefixKind::ContentValidFor,
382                    note_and_explain::SuffixKind::Empty,
383                );
384                self.dcx().create_err(OutlivesContent {
385                    span,
386                    notes: reference_valid.into_iter().chain(content_valid).collect(),
387                })
388            }
389            SubregionOrigin::RelateObjectBound(span) => {
390                let object_valid = note_and_explain::RegionExplanation::new(
391                    self.tcx,
392                    generic_param_scope,
393                    sub,
394                    None,
395                    note_and_explain::PrefixKind::TypeObjValidFor,
396                    note_and_explain::SuffixKind::Empty,
397                );
398                let pointer_valid = note_and_explain::RegionExplanation::new(
399                    self.tcx,
400                    generic_param_scope,
401                    sup,
402                    None,
403                    note_and_explain::PrefixKind::SourcePointerValidFor,
404                    note_and_explain::SuffixKind::Empty,
405                );
406                self.dcx().create_err(OutlivesBound {
407                    span,
408                    notes: object_valid.into_iter().chain(pointer_valid).collect(),
409                })
410            }
411            SubregionOrigin::RelateParamBound(span, ty, opt_span) => {
412                let prefix = match sub.kind() {
413                    ty::ReStatic => note_and_explain::PrefixKind::TypeSatisfy,
414                    _ => note_and_explain::PrefixKind::TypeOutlive,
415                };
416                let suffix = if opt_span.is_some() {
417                    note_and_explain::SuffixKind::ReqByBinding
418                } else {
419                    note_and_explain::SuffixKind::Empty
420                };
421                let note = note_and_explain::RegionExplanation::new(
422                    self.tcx,
423                    generic_param_scope,
424                    sub,
425                    opt_span,
426                    prefix,
427                    suffix,
428                );
429                self.dcx().create_err(FulfillReqLifetime {
430                    span,
431                    ty: self.resolve_vars_if_possible(ty),
432                    note,
433                })
434            }
435            SubregionOrigin::RelateRegionParamBound(span, ty) => {
436                let param_instantiated = note_and_explain::RegionExplanation::new(
437                    self.tcx,
438                    generic_param_scope,
439                    sup,
440                    None,
441                    note_and_explain::PrefixKind::LfParamInstantiatedWith,
442                    note_and_explain::SuffixKind::Empty,
443                );
444                let mut alt_span = None;
445                if let Some(ty) = ty
446                    && sub.is_static()
447                    && let ty::Dynamic(preds, _) = ty.kind()
448                    && let Some(def_id) = preds.principal_def_id()
449                {
450                    for (clause, span) in
451                        self.tcx.predicates_of(def_id).instantiate_identity(self.tcx).into_iter()
452                    {
453                        if let ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(a, b)) =
454                            clause.kind().skip_binder()
455                            && let ty::Param(param) = a.kind()
456                            && param.name == kw::SelfUpper
457                            && b.is_static()
458                        {
459                            // Point at explicit `'static` bound on the trait (`trait T: 'static`).
460                            alt_span = Some(span);
461                        }
462                    }
463                }
464                let param_must_outlive = note_and_explain::RegionExplanation::new(
465                    self.tcx,
466                    generic_param_scope,
467                    sub,
468                    alt_span,
469                    note_and_explain::PrefixKind::LfParamMustOutlive,
470                    note_and_explain::SuffixKind::Empty,
471                );
472                self.dcx().create_err(LfBoundNotSatisfied {
473                    span,
474                    notes: param_instantiated.into_iter().chain(param_must_outlive).collect(),
475                })
476            }
477            SubregionOrigin::ReferenceOutlivesReferent(ty, span) => {
478                let pointer_valid = note_and_explain::RegionExplanation::new(
479                    self.tcx,
480                    generic_param_scope,
481                    sub,
482                    None,
483                    note_and_explain::PrefixKind::PointerValidFor,
484                    note_and_explain::SuffixKind::Empty,
485                );
486                let data_valid = note_and_explain::RegionExplanation::new(
487                    self.tcx,
488                    generic_param_scope,
489                    sup,
490                    None,
491                    note_and_explain::PrefixKind::DataValidFor,
492                    note_and_explain::SuffixKind::Empty,
493                );
494                self.dcx().create_err(RefLongerThanData {
495                    span,
496                    ty: self.resolve_vars_if_possible(ty),
497                    notes: pointer_valid.into_iter().chain(data_valid).collect(),
498                })
499            }
500            SubregionOrigin::CompareImplItemObligation {
501                span,
502                impl_item_def_id,
503                trait_item_def_id,
504            } => {
505                let mut err = self.report_extra_impl_obligation(
506                    span,
507                    impl_item_def_id,
508                    trait_item_def_id,
509                    &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}: {1}`", sup, sub))
    })format!("`{sup}: {sub}`"),
510                );
511                // We should only suggest rewriting the `where` clause if the predicate is within that `where` clause
512                if let Some(generics) = self.tcx.hir_get_generics(impl_item_def_id)
513                    && generics.where_clause_span.contains(span)
514                {
515                    self.suggest_copy_trait_method_bounds(
516                        trait_item_def_id,
517                        impl_item_def_id,
518                        &mut err,
519                    );
520                }
521                err
522            }
523            SubregionOrigin::CheckAssociatedTypeBounds {
524                impl_item_def_id,
525                trait_item_def_id,
526                parent,
527            } => {
528                let mut err = self.report_concrete_failure(generic_param_scope, *parent, sub, sup);
529
530                // Don't mention the item name if it's an RPITIT, since that'll just confuse
531                // folks.
532                if !self.tcx.is_impl_trait_in_trait(impl_item_def_id.to_def_id()) {
533                    let trait_item_span = self.tcx.def_span(trait_item_def_id);
534                    let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
535                    err.span_label(
536                        trait_item_span,
537                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("definition of `{0}` from trait",
                item_name))
    })format!("definition of `{item_name}` from trait"),
538                    );
539                }
540
541                self.suggest_copy_trait_method_bounds(
542                    trait_item_def_id,
543                    impl_item_def_id,
544                    &mut err,
545                );
546                err
547            }
548            SubregionOrigin::AscribeUserTypeProvePredicate(span) => {
549                let instantiated = note_and_explain::RegionExplanation::new(
550                    self.tcx,
551                    generic_param_scope,
552                    sup,
553                    None,
554                    note_and_explain::PrefixKind::LfInstantiatedWith,
555                    note_and_explain::SuffixKind::Empty,
556                );
557                let must_outlive = note_and_explain::RegionExplanation::new(
558                    self.tcx,
559                    generic_param_scope,
560                    sub,
561                    None,
562                    note_and_explain::PrefixKind::LfMustOutlive,
563                    note_and_explain::SuffixKind::Empty,
564                );
565                self.dcx().create_err(LfBoundNotSatisfied {
566                    span,
567                    notes: instantiated.into_iter().chain(must_outlive).collect(),
568                })
569            }
570            SubregionOrigin::SolverRegionConstraint(span) => {
571                let mut d = self.dcx().struct_span_err(
572                    span,
573                    "unsatisfied lifetime constraint from -Zassumptions-on-binders :3",
574                );
575                d.note("meoow :c");
576                d
577            }
578        };
579        if sub.is_error() || sup.is_error() {
580            err.downgrade_to_delayed_bug();
581        }
582        err
583    }
584
585    pub fn suggest_copy_trait_method_bounds(
586        &self,
587        trait_item_def_id: DefId,
588        impl_item_def_id: LocalDefId,
589        err: &mut Diag<'_>,
590    ) {
591        // FIXME(compiler-errors): Right now this is only being used for region
592        // predicate mismatches. Ideally, we'd use it for *all* predicate mismatches,
593        // but right now it's not really very smart when it comes to implicit `Sized`
594        // predicates and bounds on the trait itself.
595
596        let Some(impl_def_id) = self.tcx.trait_impl_of_assoc(impl_item_def_id.to_def_id()) else {
597            return;
598        };
599        let trait_ref = self.tcx.impl_trait_ref(impl_def_id);
600        let trait_args = trait_ref
601            .instantiate_identity()
602            .skip_norm_wip()
603            // Replace the explicit self type with `Self` for better suggestion rendering
604            .with_replaced_self_ty(self.tcx, Ty::new_param(self.tcx, 0, kw::SelfUpper))
605            .args;
606        let trait_item_args = ty::GenericArgs::identity_for_item(self.tcx, impl_item_def_id)
607            .rebase_onto(self.tcx, impl_def_id, trait_args);
608
609        let Ok(trait_predicates) = self
610            .tcx
611            .explicit_predicates_of(trait_item_def_id)
612            .instantiate_own(self.tcx, trait_item_args)
613            .map(|(pred, _)| {
614                let pred = pred.skip_norm_wip();
615                if pred.is_suggestable(self.tcx, false) { Ok(pred.to_string()) } else { Err(()) }
616            })
617            .collect::<Result<Vec<_>, ()>>()
618        else {
619            return;
620        };
621
622        let Some(generics) = self.tcx.hir_get_generics(impl_item_def_id) else {
623            return;
624        };
625
626        let suggestion = if trait_predicates.is_empty() {
627            WhereClauseSuggestions::Remove { span: generics.where_clause_span }
628        } else {
629            let space = if generics.where_clause_span.is_empty() { " " } else { "" };
630            WhereClauseSuggestions::CopyPredicates {
631                span: generics.where_clause_span,
632                space,
633                trait_predicates: trait_predicates.join(", "),
634            }
635        };
636        err.subdiagnostic(suggestion);
637    }
638
639    pub(super) fn report_placeholder_failure(
640        &self,
641        generic_param_scope: LocalDefId,
642        placeholder_origin: SubregionOrigin<'tcx>,
643        sub: Region<'tcx>,
644        sup: Region<'tcx>,
645    ) -> Diag<'a> {
646        // I can't think how to do better than this right now. -nikomatsakis
647        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:647",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(647u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::tracing_core::field::FieldSet::new(&["message",
                                        "placeholder_origin", "sub", "sup"],
                            ::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_placeholder_failure")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&placeholder_origin)
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&sub) as
                                            &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&sup) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?placeholder_origin, ?sub, ?sup, "report_placeholder_failure");
648        match placeholder_origin {
649            SubregionOrigin::Subtype(ref trace)
650                if #[allow(non_exhaustive_omitted_patterns)] match &trace.cause.code().peel_derives()
    {
    ObligationCauseCode::WhereClause(..) |
        ObligationCauseCode::WhereClauseInExpr(..) => true,
    _ => false,
}matches!(
651                    &trace.cause.code().peel_derives(),
652                    ObligationCauseCode::WhereClause(..)
653                        | ObligationCauseCode::WhereClauseInExpr(..)
654                ) =>
655            {
656                // Hack to get around the borrow checker because trace.cause has an `Rc`.
657                if let ObligationCauseCode::WhereClause(_, span)
658                | ObligationCauseCode::WhereClauseInExpr(_, span, ..) =
659                    &trace.cause.code().peel_derives()
660                {
661                    let span = *span;
662                    let mut err = self.report_concrete_failure(
663                        generic_param_scope,
664                        placeholder_origin,
665                        sub,
666                        sup,
667                    );
668                    if !span.is_dummy() {
669                        err =
670                            err.with_span_note(span, "the lifetime requirement is introduced here");
671                    }
672                    err
673                } else {
674                    {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("control flow ensures we have a `BindingObligation` or `WhereClauseInExpr` here...")));
}unreachable!(
675                        "control flow ensures we have a `BindingObligation` or `WhereClauseInExpr` here..."
676                    )
677                }
678            }
679            SubregionOrigin::Subtype(trace) => {
680                let terr = TypeError::RegionsPlaceholderMismatch;
681                return self.report_and_explain_type_error(
682                    *trace,
683                    self.tcx.param_env(generic_param_scope),
684                    terr,
685                );
686            }
687            _ => {
688                return self.report_concrete_failure(
689                    generic_param_scope,
690                    placeholder_origin,
691                    sub,
692                    sup,
693                );
694            }
695        }
696    }
697
698    pub fn report_generic_bound_failure(
699        &self,
700        generic_param_scope: LocalDefId,
701        span: Span,
702        origin: Option<SubregionOrigin<'tcx>>,
703        bound_kind: GenericKind<'tcx>,
704        sub: Region<'tcx>,
705    ) -> ErrorGuaranteed {
706        self.construct_generic_bound_failure(generic_param_scope, span, origin, bound_kind, sub)
707            .emit()
708    }
709
710    pub fn construct_generic_bound_failure(
711        &self,
712        generic_param_scope: LocalDefId,
713        span: Span,
714        origin: Option<SubregionOrigin<'tcx>>,
715        bound_kind: GenericKind<'tcx>,
716        sub: Region<'tcx>,
717    ) -> Diag<'a> {
718        if let Some(SubregionOrigin::CompareImplItemObligation {
719            span,
720            impl_item_def_id,
721            trait_item_def_id,
722        }) = origin
723        {
724            return self.report_extra_impl_obligation(
725                span,
726                impl_item_def_id,
727                trait_item_def_id,
728                &::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}: {1}`", bound_kind, sub))
    })format!("`{bound_kind}: {sub}`"),
729            );
730        }
731
732        let labeled_user_string = match bound_kind {
733            GenericKind::Param(_) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the parameter type `{0}`",
                bound_kind))
    })format!("the parameter type `{bound_kind}`"),
734            GenericKind::Placeholder(_) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the placeholder type `{0}`",
                bound_kind))
    })format!("the placeholder type `{bound_kind}`"),
735            GenericKind::Alias(p) => match p.kind {
736                ty::Projection { .. } | ty::Inherent { .. } => {
737                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the associated type `{0}`",
                bound_kind))
    })format!("the associated type `{bound_kind}`")
738                }
739                ty::Free { .. } => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the type alias `{0}`", bound_kind))
    })format!("the type alias `{bound_kind}`"),
740                ty::Opaque { .. } => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the opaque type `{0}`",
                bound_kind))
    })format!("the opaque type `{bound_kind}`"),
741            },
742        };
743
744        let mut err = self
745            .tcx
746            .dcx()
747            .struct_span_err(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} may not live long enough",
                labeled_user_string))
    })format!("{labeled_user_string} may not live long enough"));
748        err.code(match sub.kind() {
749            ty::ReEarlyParam(_) | ty::ReLateParam(_) if sub.is_named(self.tcx) => E0309,
750            ty::ReStatic => E0310,
751            _ => E0311,
752        });
753
754        '_explain: {
755            let (description, span) = match sub.kind() {
756                ty::ReEarlyParam(_) | ty::ReLateParam(_) | ty::ReStatic => {
757                    msg_span_from_named_region(self.tcx, generic_param_scope, sub, Some(span))
758                }
759                _ => (::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("lifetime `{0}`", sub))
    })format!("lifetime `{sub}`"), Some(span)),
760            };
761            let prefix = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} must be valid for ",
                labeled_user_string))
    })format!("{labeled_user_string} must be valid for ");
762            label_msg_span(&mut err, &prefix, description, span, "...");
763            if let Some(origin) = origin {
764                self.note_region_origin(&mut err, &origin);
765            }
766        }
767
768        'suggestion: {
769            let msg = "consider adding an explicit lifetime bound";
770
771            if (bound_kind, sub).has_infer_regions()
772                || (bound_kind, sub).has_placeholders()
773                || !bound_kind.is_suggestable(self.tcx, false)
774            {
775                let lt_name = sub.get_name_or_anon(self.tcx).to_string();
776                err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} `{1}: {2}`...", msg,
                bound_kind, lt_name))
    })format!("{msg} `{bound_kind}: {lt_name}`..."));
777                break 'suggestion;
778            }
779
780            let mut generic_param_scope = generic_param_scope;
781            while self.tcx.def_kind(generic_param_scope) == DefKind::OpaqueTy {
782                generic_param_scope = self.tcx.local_parent(generic_param_scope);
783            }
784
785            // type_param_sugg_span is (span, has_bounds, needs_parentheses)
786            let (type_scope, type_param_sugg_span) = match bound_kind {
787                GenericKind::Param(param) => {
788                    let generics = self.tcx.generics_of(generic_param_scope);
789                    let type_param = generics.type_param(param, self.tcx);
790                    let def_id = type_param.def_id.expect_local();
791                    let scope = self.tcx.local_def_id_to_hir_id(def_id).owner.def_id;
792                    // Get the `hir::Param` to verify whether it already has any bounds.
793                    // We do this to avoid suggesting code that ends up as `T: 'a'b`,
794                    // instead we suggest `T: 'a + 'b` in that case.
795                    let hir_generics = self.tcx.hir_get_generics(scope).unwrap();
796                    let sugg_span = match hir_generics.bounds_span_for_suggestions(def_id) {
797                        Some((span, open_paren_sp)) => {
798                            Some((span, LifetimeSuggestion::NeedsPlus(open_paren_sp)))
799                        }
800                        // If `param` corresponds to `Self`, no usable suggestion span.
801                        None if generics.has_self && param.index == 0 => None,
802                        None => {
803                            let mut colon_flag = false;
804                            let span = if let Some(param) =
805                                hir_generics.params.iter().find(|param| param.def_id == def_id)
806                                && let ParamName::Plain(ident) = param.name
807                            {
808                                if let Some(sp) = param.colon_span {
809                                    colon_flag = true;
810                                    sp.shrink_to_hi()
811                                } else {
812                                    ident.span.shrink_to_hi()
813                                }
814                            } else {
815                                let span = self.tcx.def_span(def_id);
816                                span.shrink_to_hi()
817                            };
818                            match colon_flag {
819                                true => Some((span, LifetimeSuggestion::HasColon)),
820                                false => Some((span, LifetimeSuggestion::NeedsColon)),
821                            }
822                        }
823                    };
824                    (scope, sugg_span)
825                }
826                _ => (generic_param_scope, None),
827            };
828            let suggestion_scope = {
829                let lifetime_scope = match sub.kind() {
830                    ty::ReStatic => hir::def_id::CRATE_DEF_ID,
831                    _ => match self.tcx.is_suitable_region(generic_param_scope, sub) {
832                        Some(info) => info.scope,
833                        None => generic_param_scope,
834                    },
835                };
836                match self.tcx.is_descendant_of(type_scope.into(), lifetime_scope.into()) {
837                    true => type_scope,
838                    false => lifetime_scope,
839                }
840            };
841
842            let mut suggs = ::alloc::vec::Vec::new()vec![];
843            let lt_name = self.suggest_name_region(generic_param_scope, sub, &mut suggs);
844
845            if let Some((sp, suggestion_type)) = type_param_sugg_span
846                && suggestion_scope == type_scope
847            {
848                match suggestion_type {
849                    LifetimeSuggestion::NeedsPlus(open_paren_sp) => {
850                        let suggestion = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" + {0}", lt_name))
    })format!(" + {lt_name}");
851                        if let Some(open_paren_sp) = open_paren_sp {
852                            suggs.push((open_paren_sp, "(".to_string()));
853                            suggs.push((sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("){0}", suggestion))
    })format!("){suggestion}")));
854                        } else {
855                            suggs.push((sp, suggestion));
856                        }
857                    }
858                    LifetimeSuggestion::NeedsColon => suggs.push((sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(": {0}", lt_name))
    })format!(": {lt_name}"))),
859                    LifetimeSuggestion::HasColon => suggs.push((sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" {0}", lt_name))
    })format!(" {lt_name}"))),
860                }
861            } else if let GenericKind::Alias(ref p) = bound_kind
862                && let ty::Projection { def_id } = p.kind
863                && let DefKind::AssocTy = self.tcx.def_kind(def_id)
864                && let Some(ty::ImplTraitInTraitData::Trait { .. }) =
865                    self.tcx.opt_rpitit_info(def_id)
866            {
867                // The lifetime found in the `impl` is longer than the one on the RPITIT.
868                // Do not suggest `<Type as Trait>::{opaque}: 'static`.
869            } else if let Some(generics) = self.tcx.hir_get_generics(suggestion_scope) {
870                let pred = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: {1}", bound_kind, lt_name))
    })format!("{bound_kind}: {lt_name}");
871                let suggestion = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} {1}",
                generics.add_where_or_trailing_comma(), pred))
    })format!("{} {}", generics.add_where_or_trailing_comma(), pred);
872                suggs.push((generics.tail_span_for_predicate_suggestion(), suggestion))
873            } else {
874                let consider = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} `{1}: {2}`...", msg,
                bound_kind, sub))
    })format!("{msg} `{bound_kind}: {sub}`...");
875                err.help(consider);
876            }
877
878            if !suggs.is_empty() {
879                err.multipart_suggestion(
880                    msg,
881                    suggs,
882                    Applicability::MaybeIncorrect, // Issue #41966
883                );
884            }
885        }
886
887        if sub.kind() == ty::ReStatic
888            && let Some(node) = self.tcx.hir_get_if_local(generic_param_scope.into())
889            && let hir::Node::Item(hir::Item {
890                kind: hir::ItemKind::Fn { sig, body, has_body: true, .. },
891                ..
892            })
893            | hir::Node::TraitItem(hir::TraitItem {
894                kind: hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body)),
895                ..
896            })
897            | hir::Node::ImplItem(hir::ImplItem {
898                kind: hir::ImplItemKind::Fn(sig, body), ..
899            }) = node
900            && let hir::Node::Expr(expr) = self.tcx.hir_node(body.hir_id)
901            && let hir::ExprKind::Block(block, _) = expr.kind
902            && let Some(tail) = block.expr
903            && tail.span == span
904            && let hir::FnRetTy::Return(ty) = sig.decl.output
905            && let hir::TyKind::Path(path) = ty.kind
906            && let hir::QPath::Resolved(None, path) = path
907            && let hir::def::Res::Def(_, def_id) = path.res
908            && Some(def_id) == self.tcx.lang_items().owned_box()
909            && let [segment] = path.segments
910            && let Some(args) = segment.args
911            && let [hir::GenericArg::Type(ty)] = args.args
912            && let hir::TyKind::TraitObject(_, tagged_ref) = ty.kind
913            && let hir::LifetimeKind::ImplicitObjectLifetimeDefault = tagged_ref.pointer().kind
914        {
915            // Explicitly look for `-> Box<dyn Trait>` to point at it as the *likely* source of
916            // the `'static` lifetime requirement.
917            err.span_label(
918                ty.span,
919                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this `dyn Trait` has an implicit `\'static` lifetime bound"))
    })format!("this `dyn Trait` has an implicit `'static` lifetime bound"),
920            );
921        }
922
923        err
924    }
925
926    pub fn suggest_name_region(
927        &self,
928        generic_param_scope: LocalDefId,
929        lifetime: Region<'tcx>,
930        add_lt_suggs: &mut Vec<(Span, String)>,
931    ) -> String {
932        struct LifetimeReplaceVisitor<'a> {
933            needle: hir::LifetimeKind,
934            new_lt: &'a str,
935            add_lt_suggs: &'a mut Vec<(Span, String)>,
936        }
937
938        impl<'hir> hir::intravisit::Visitor<'hir> for LifetimeReplaceVisitor<'_> {
939            fn visit_lifetime(&mut self, lt: &'hir hir::Lifetime) {
940                if lt.kind == self.needle {
941                    self.add_lt_suggs.push(lt.suggestion(self.new_lt));
942                }
943            }
944        }
945
946        let (lifetime_def_id, lifetime_scope) =
947            match self.tcx.is_suitable_region(generic_param_scope, lifetime) {
948                Some(info) if !lifetime.is_named(self.tcx) => {
949                    (info.region_def_id.expect_local(), info.scope)
950                }
951                _ => return lifetime.get_name_or_anon(self.tcx).to_string(),
952            };
953
954        let new_lt = {
955            let generics = self.tcx.generics_of(lifetime_scope);
956            let mut used_names =
957                iter::successors(Some(generics), |g| g.parent.map(|p| self.tcx.generics_of(p)))
958                    .flat_map(|g| &g.own_params)
959                    .filter(|p| #[allow(non_exhaustive_omitted_patterns)] match p.kind {
    ty::GenericParamDefKind::Lifetime => true,
    _ => false,
}matches!(p.kind, ty::GenericParamDefKind::Lifetime))
960                    .map(|p| p.name)
961                    .collect::<Vec<_>>();
962            let hir_id = self.tcx.local_def_id_to_hir_id(lifetime_scope);
963            // consider late-bound lifetimes ...
964            used_names.extend(self.tcx.late_bound_vars(hir_id).into_iter().filter_map(
965                |p| match p {
966                    ty::BoundVariableKind::Region(lt) => lt.get_name(self.tcx),
967                    _ => None,
968                },
969            ));
970            (b'a'..=b'z')
971                .map(|c| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\'{0}", c as char))
    })format!("'{}", c as char))
972                .find(|candidate| !used_names.iter().any(|e| e.as_str() == candidate))
973                .unwrap_or_else(|| "'lt".to_string())
974        };
975
976        let mut visitor = LifetimeReplaceVisitor {
977            needle: hir::LifetimeKind::Param(lifetime_def_id),
978            add_lt_suggs,
979            new_lt: &new_lt,
980        };
981        match self.tcx.expect_hir_owner_node(lifetime_scope) {
982            hir::OwnerNode::Item(i) => visitor.visit_item(i),
983            hir::OwnerNode::ForeignItem(i) => visitor.visit_foreign_item(i),
984            hir::OwnerNode::ImplItem(i) => visitor.visit_impl_item(i),
985            hir::OwnerNode::TraitItem(i) => visitor.visit_trait_item(i),
986            hir::OwnerNode::Crate(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("OwnerNode::Crate doesn\'t not have generics"))bug!("OwnerNode::Crate doesn't not have generics"),
987            hir::OwnerNode::Synthetic => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
988        }
989
990        let ast_generics = self.tcx.hir_get_generics(lifetime_scope).unwrap();
991        let sugg = ast_generics
992            .span_for_lifetime_suggestion()
993            .map(|span| (span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}, ", new_lt))
    })format!("{new_lt}, ")))
994            .unwrap_or_else(|| (ast_generics.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0}>", new_lt))
    })format!("<{new_lt}>")));
995        add_lt_suggs.push(sugg);
996
997        new_lt
998    }
999
1000    fn report_sub_sup_conflict(
1001        &self,
1002        generic_param_scope: LocalDefId,
1003        var_origin: RegionVariableOrigin<'tcx>,
1004        sub_origin: SubregionOrigin<'tcx>,
1005        sub_region: Region<'tcx>,
1006        sup_origin: SubregionOrigin<'tcx>,
1007        sup_region: Region<'tcx>,
1008    ) -> ErrorGuaranteed {
1009        let mut err = self.report_inference_failure(var_origin);
1010
1011        note_and_explain_region(
1012            self.tcx,
1013            &mut err,
1014            generic_param_scope,
1015            "first, the lifetime cannot outlive ",
1016            sup_region,
1017            "...",
1018            None,
1019        );
1020
1021        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:1021",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(1021u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::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_sub_sup_conflict: var_origin={0:?}",
                                                    var_origin) as &dyn Value))])
            });
    } else { ; }
};debug!("report_sub_sup_conflict: var_origin={:?}", var_origin);
1022        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:1022",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(1022u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::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_sub_sup_conflict: sub_region={0:?}",
                                                    sub_region) as &dyn Value))])
            });
    } else { ; }
};debug!("report_sub_sup_conflict: sub_region={:?}", sub_region);
1023        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:1023",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(1023u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::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_sub_sup_conflict: sub_origin={0:?}",
                                                    sub_origin) as &dyn Value))])
            });
    } else { ; }
};debug!("report_sub_sup_conflict: sub_origin={:?}", sub_origin);
1024        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:1024",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(1024u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::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_sub_sup_conflict: sup_region={0:?}",
                                                    sup_region) as &dyn Value))])
            });
    } else { ; }
};debug!("report_sub_sup_conflict: sup_region={:?}", sup_region);
1025        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/region.rs:1025",
                        "rustc_trait_selection::error_reporting::infer::region",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                        ::tracing_core::__macro_support::Option::Some(1025u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                        ::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_sub_sup_conflict: sup_origin={0:?}",
                                                    sup_origin) as &dyn Value))])
            });
    } else { ; }
};debug!("report_sub_sup_conflict: sup_origin={:?}", sup_origin);
1026
1027        if let SubregionOrigin::Subtype(ref sup_trace) = sup_origin
1028            && let SubregionOrigin::Subtype(ref sub_trace) = sub_origin
1029            && let Some((sup_expected, sup_found)) =
1030                self.values_str(sup_trace.values, &sup_trace.cause, err.long_ty_path())
1031            && let Some((sub_expected, sub_found)) =
1032                self.values_str(sub_trace.values, &sub_trace.cause, err.long_ty_path())
1033            && sub_expected == sup_expected
1034            && sub_found == sup_found
1035        {
1036            note_and_explain_region(
1037                self.tcx,
1038                &mut err,
1039                generic_param_scope,
1040                "...but the lifetime must also be valid for ",
1041                sub_region,
1042                "...",
1043                None,
1044            );
1045            err.span_note(
1046                sup_trace.cause.span,
1047                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("...so that the {0}",
                sup_trace.cause.as_requirement_str()))
    })format!("...so that the {}", sup_trace.cause.as_requirement_str()),
1048            );
1049
1050            err.note_expected_found("", sup_expected, "", sup_found);
1051            return if sub_region.is_error() | sup_region.is_error() {
1052                err.delay_as_bug()
1053            } else {
1054                err.emit()
1055            };
1056        }
1057
1058        self.note_region_origin(&mut err, &sup_origin);
1059
1060        note_and_explain_region(
1061            self.tcx,
1062            &mut err,
1063            generic_param_scope,
1064            "but, the lifetime must be valid for ",
1065            sub_region,
1066            "...",
1067            None,
1068        );
1069
1070        self.note_region_origin(&mut err, &sub_origin);
1071        if sub_region.is_error() | sup_region.is_error() { err.delay_as_bug() } else { err.emit() }
1072    }
1073
1074    fn report_inference_failure(&self, var_origin: RegionVariableOrigin<'tcx>) -> Diag<'_> {
1075        let br_string = |br: ty::BoundRegionKind<'tcx>| {
1076            let mut s = match br {
1077                ty::BoundRegionKind::Named(def_id) => self.tcx.item_name(def_id).to_string(),
1078                _ => String::new(),
1079            };
1080            if !s.is_empty() {
1081                s.push(' ');
1082            }
1083            s
1084        };
1085        let var_description = match var_origin {
1086            RegionVariableOrigin::Misc(_) => String::new(),
1087            RegionVariableOrigin::PatternRegion(_) => " for pattern".to_string(),
1088            RegionVariableOrigin::BorrowRegion(_) => " for borrow expression".to_string(),
1089            RegionVariableOrigin::Autoref(_) => " for autoref".to_string(),
1090            RegionVariableOrigin::Coercion(_) => " for automatic coercion".to_string(),
1091            RegionVariableOrigin::BoundRegion(_, br, BoundRegionConversionTime::FnCall) => {
1092                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" for lifetime parameter {0}in function call",
                br_string(br)))
    })format!(" for lifetime parameter {}in function call", br_string(br))
1093            }
1094            RegionVariableOrigin::BoundRegion(
1095                _,
1096                br,
1097                BoundRegionConversionTime::HigherRankedType,
1098            ) => {
1099                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" for lifetime parameter {0}in generic type",
                br_string(br)))
    })format!(" for lifetime parameter {}in generic type", br_string(br))
1100            }
1101            RegionVariableOrigin::BoundRegion(
1102                _,
1103                br,
1104                BoundRegionConversionTime::AssocTypeProjection(def_id),
1105            ) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" for lifetime parameter {0}in trait containing associated type `{1}`",
                br_string(br), self.tcx.associated_item(def_id).name()))
    })format!(
1106                " for lifetime parameter {}in trait containing associated type `{}`",
1107                br_string(br),
1108                self.tcx.associated_item(def_id).name()
1109            ),
1110            RegionVariableOrigin::RegionParameterDefinition(_, name) => {
1111                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" for lifetime parameter `{0}`",
                name))
    })format!(" for lifetime parameter `{name}`")
1112            }
1113            RegionVariableOrigin::UpvarRegion(ref upvar_id, _) => {
1114                let var_name = self.tcx.hir_name(upvar_id.var_path.hir_id);
1115                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" for capture of `{0}` by closure",
                var_name))
    })format!(" for capture of `{var_name}` by closure")
1116            }
1117            RegionVariableOrigin::Nll(..) => ::rustc_middle::util::bug::bug_fmt(format_args!("NLL variable found in lexical phase"))bug!("NLL variable found in lexical phase"),
1118        };
1119
1120        {
    self.dcx().struct_span_err(var_origin.span(),
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("cannot infer an appropriate lifetime{0} due to conflicting requirements",
                            var_description))
                })).with_code(E0803)
}struct_span_code_err!(
1121            self.dcx(),
1122            var_origin.span(),
1123            E0803,
1124            "cannot infer an appropriate lifetime{} due to conflicting requirements",
1125            var_description
1126        )
1127    }
1128}
1129
1130enum LifetimeSuggestion {
1131    NeedsPlus(Option<Span>),
1132    NeedsColon,
1133    HasColon,
1134}
1135
1136pub(super) fn note_and_explain_region<'tcx>(
1137    tcx: TyCtxt<'tcx>,
1138    err: &mut Diag<'_>,
1139    generic_param_scope: LocalDefId,
1140    prefix: &str,
1141    region: ty::Region<'tcx>,
1142    suffix: &str,
1143    alt_span: Option<Span>,
1144) {
1145    let (description, span) = match region.kind() {
1146        ty::ReEarlyParam(_) | ty::ReLateParam(_) | ty::RePlaceholder(_) | ty::ReStatic => {
1147            msg_span_from_named_region(tcx, generic_param_scope, region, alt_span)
1148        }
1149
1150        ty::ReError(_) => return,
1151
1152        // FIXME(#125431): `ReVar` shouldn't reach here.
1153        ty::ReVar(_) => (::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("lifetime `{0}`", region))
    })format!("lifetime `{region}`"), alt_span),
1154
1155        ty::ReBound(..) | ty::ReErased => {
1156            ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected region for note_and_explain_region: {0:?}",
        region));bug!("unexpected region for note_and_explain_region: {:?}", region);
1157        }
1158    };
1159
1160    emit_msg_span(err, prefix, description, span, suffix);
1161}
1162
1163fn explain_free_region<'tcx>(
1164    tcx: TyCtxt<'tcx>,
1165    err: &mut Diag<'_>,
1166    generic_param_scope: LocalDefId,
1167    prefix: &str,
1168    region: ty::Region<'tcx>,
1169    suffix: &str,
1170) {
1171    let (description, span) = msg_span_from_named_region(tcx, generic_param_scope, region, None);
1172
1173    label_msg_span(err, prefix, description, span, suffix);
1174}
1175
1176fn msg_span_from_named_region<'tcx>(
1177    tcx: TyCtxt<'tcx>,
1178    generic_param_scope: LocalDefId,
1179    region: ty::Region<'tcx>,
1180    alt_span: Option<Span>,
1181) -> (String, Option<Span>) {
1182    match region.kind() {
1183        ty::ReEarlyParam(br) => {
1184            let param_def_id = tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id;
1185            let span = tcx.def_span(param_def_id);
1186            let text = if br.is_named() {
1187                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the lifetime `{0}` as defined here",
                br.name))
    })format!("the lifetime `{}` as defined here", br.name)
1188            } else {
1189                "the anonymous lifetime as defined here".to_string()
1190            };
1191            (text, Some(span))
1192        }
1193        ty::ReLateParam(ref fr) => {
1194            if !fr.kind.is_named(tcx)
1195                && let Some((ty, _)) = find_anon_type(tcx, generic_param_scope, region)
1196            {
1197                ("the anonymous lifetime defined here".to_string(), Some(ty.span))
1198            } else {
1199                match fr.kind {
1200                    ty::LateParamRegionKind::Named(param_def_id) => {
1201                        let name = tcx.item_name(param_def_id);
1202                        let span = tcx.def_span(param_def_id);
1203                        let text = if name == kw::UnderscoreLifetime {
1204                            "the anonymous lifetime as defined here".to_string()
1205                        } else {
1206                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the lifetime `{0}` as defined here",
                name))
    })format!("the lifetime `{name}` as defined here")
1207                        };
1208                        (text, Some(span))
1209                    }
1210                    ty::LateParamRegionKind::Anon(_) => (
1211                        "the anonymous lifetime as defined here".to_string(),
1212                        Some(tcx.def_span(generic_param_scope)),
1213                    ),
1214                    _ => (
1215                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the lifetime `{0}` as defined here",
                region))
    })format!("the lifetime `{region}` as defined here"),
1216                        Some(tcx.def_span(generic_param_scope)),
1217                    ),
1218                }
1219            }
1220        }
1221        ty::ReStatic => ("the static lifetime".to_owned(), alt_span),
1222        ty::RePlaceholder(ty::PlaceholderRegion {
1223            bound: ty::BoundRegion { kind: ty::BoundRegionKind::Named(def_id), .. },
1224            ..
1225        }) => (
1226            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the lifetime `{0}` as defined here",
                tcx.item_name(def_id)))
    })format!("the lifetime `{}` as defined here", tcx.item_name(def_id)),
1227            Some(tcx.def_span(def_id)),
1228        ),
1229        ty::RePlaceholder(ty::PlaceholderRegion {
1230            bound: ty::BoundRegion { kind: ty::BoundRegionKind::Anon, .. },
1231            ..
1232        }) => ("an anonymous lifetime".to_owned(), None),
1233        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("{0:?}", region))bug!("{:?}", region),
1234    }
1235}
1236
1237fn emit_msg_span(
1238    err: &mut Diag<'_>,
1239    prefix: &str,
1240    description: String,
1241    span: Option<Span>,
1242    suffix: &str,
1243) {
1244    let message = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}{2}", prefix, description,
                suffix))
    })format!("{prefix}{description}{suffix}");
1245
1246    if let Some(span) = span {
1247        err.span_note(span, message);
1248    } else {
1249        err.note(message);
1250    }
1251}
1252
1253fn label_msg_span(
1254    err: &mut Diag<'_>,
1255    prefix: &str,
1256    description: String,
1257    span: Option<Span>,
1258    suffix: &str,
1259) {
1260    let message = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1}{2}", prefix, description,
                suffix))
    })format!("{prefix}{description}{suffix}");
1261
1262    if let Some(span) = span {
1263        err.span_label(span, message);
1264    } else {
1265        err.note(message);
1266    }
1267}
1268
1269#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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("unexpected_hidden_region_diagnostic",
                                    "rustc_trait_selection::error_reporting::infer::region",
                                    ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/region.rs"),
                                    ::tracing_core::__macro_support::Option::Some(1269u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::region"),
                                    ::tracing_core::field::FieldSet::new(&["generic_param_scope",
                                                    "span", "hidden_ty", "hidden_region", "opaque_ty_key"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::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(&generic_param_scope)
                                                            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(&span)
                                                            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(&hidden_ty)
                                                            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(&hidden_region)
                                                            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(&opaque_ty_key)
                                                            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<'a> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let tcx = infcx.tcx;
            let mut err =
                infcx.dcx().create_err(errors::OpaqueCapturesLifetime {
                        span,
                        opaque_ty: Ty::new_opaque(tcx,
                            opaque_ty_key.def_id.to_def_id(), opaque_ty_key.args),
                        opaque_ty_span: tcx.def_span(opaque_ty_key.def_id),
                    });
            match hidden_region.kind() {
                ty::ReEarlyParam(_) | ty::ReLateParam(_) | ty::ReStatic => {
                    explain_free_region(tcx, &mut err, generic_param_scope,
                        &::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("hidden type `{0}` captures ",
                                            hidden_ty))
                                }), hidden_region, "");
                    if let Some(_) =
                            tcx.is_suitable_region(generic_param_scope, hidden_region) {
                        suggest_precise_capturing(tcx, opaque_ty_key.def_id,
                            hidden_region, &mut err);
                    }
                }
                ty::RePlaceholder(_) => {
                    explain_free_region(tcx, &mut err, generic_param_scope,
                        &::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("hidden type `{0}` captures ",
                                            hidden_ty))
                                }), hidden_region, "");
                }
                ty::ReError(_) => { err.downgrade_to_delayed_bug(); }
                _ => {
                    note_and_explain_region(tcx, &mut err, generic_param_scope,
                        &::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("hidden type `{0}` captures ",
                                            hidden_ty))
                                }), hidden_region, "", None);
                }
            }
            err
        }
    }
}#[instrument(level = "trace", skip(infcx))]
1270pub fn unexpected_hidden_region_diagnostic<'a, 'tcx>(
1271    infcx: &'a InferCtxt<'tcx>,
1272    generic_param_scope: LocalDefId,
1273    span: Span,
1274    hidden_ty: Ty<'tcx>,
1275    hidden_region: ty::Region<'tcx>,
1276    opaque_ty_key: ty::OpaqueTypeKey<'tcx>,
1277) -> Diag<'a> {
1278    let tcx = infcx.tcx;
1279    let mut err = infcx.dcx().create_err(errors::OpaqueCapturesLifetime {
1280        span,
1281        opaque_ty: Ty::new_opaque(tcx, opaque_ty_key.def_id.to_def_id(), opaque_ty_key.args),
1282        opaque_ty_span: tcx.def_span(opaque_ty_key.def_id),
1283    });
1284
1285    // Explain the region we are capturing.
1286    match hidden_region.kind() {
1287        ty::ReEarlyParam(_) | ty::ReLateParam(_) | ty::ReStatic => {
1288            // Assuming regionck succeeded (*), we ought to always be
1289            // capturing *some* region from the fn header, and hence it
1290            // ought to be free. So under normal circumstances, we will go
1291            // down this path which gives a decent human readable
1292            // explanation.
1293            //
1294            // (*) if not, the `tainted_by_errors` field would be set to
1295            // `Some(ErrorGuaranteed)` in any case, so we wouldn't be here at all.
1296            explain_free_region(
1297                tcx,
1298                &mut err,
1299                generic_param_scope,
1300                &format!("hidden type `{hidden_ty}` captures "),
1301                hidden_region,
1302                "",
1303            );
1304            if let Some(_) = tcx.is_suitable_region(generic_param_scope, hidden_region) {
1305                suggest_precise_capturing(tcx, opaque_ty_key.def_id, hidden_region, &mut err);
1306            }
1307        }
1308        ty::RePlaceholder(_) => {
1309            explain_free_region(
1310                tcx,
1311                &mut err,
1312                generic_param_scope,
1313                &format!("hidden type `{}` captures ", hidden_ty),
1314                hidden_region,
1315                "",
1316            );
1317        }
1318        ty::ReError(_) => {
1319            err.downgrade_to_delayed_bug();
1320        }
1321        _ => {
1322            // Ugh. This is a painful case: the hidden region is not one
1323            // that we can easily summarize or explain. This can happen
1324            // in a case like
1325            // `tests/ui/multiple-lifetimes/ordinary-bounds-unsuited.rs`:
1326            //
1327            // ```
1328            // fn upper_bounds<'a, 'b>(a: Ordinary<'a>, b: Ordinary<'b>) -> impl Trait<'a, 'b> {
1329            //   if condition() { a } else { b }
1330            // }
1331            // ```
1332            //
1333            // Here the captured lifetime is the intersection of `'a` and
1334            // `'b`, which we can't quite express.
1335
1336            // We can at least report a really cryptic error for now.
1337            note_and_explain_region(
1338                tcx,
1339                &mut err,
1340                generic_param_scope,
1341                &format!("hidden type `{hidden_ty}` captures "),
1342                hidden_region,
1343                "",
1344                None,
1345            );
1346        }
1347    }
1348
1349    err
1350}
1351
1352fn suggest_precise_capturing<'tcx>(
1353    tcx: TyCtxt<'tcx>,
1354    opaque_def_id: LocalDefId,
1355    captured_lifetime: ty::Region<'tcx>,
1356    diag: &mut Diag<'_>,
1357) {
1358    let hir::OpaqueTy { bounds, origin, .. } =
1359        tcx.hir_node_by_def_id(opaque_def_id).expect_opaque_ty();
1360
1361    let hir::OpaqueTyOrigin::FnReturn { parent: fn_def_id, .. } = *origin else {
1362        return;
1363    };
1364
1365    let new_lifetime = Symbol::intern(&captured_lifetime.to_string());
1366
1367    if let Some((args, span)) = bounds.iter().find_map(|bound| match bound {
1368        hir::GenericBound::Use(args, span) => Some((args, span)),
1369        _ => None,
1370    }) {
1371        let last_lifetime_span = args.iter().rev().find_map(|arg| match arg {
1372            hir::PreciseCapturingArg::Lifetime(lt) => Some(lt.ident.span),
1373            _ => None,
1374        });
1375
1376        let first_param_span = args.iter().find_map(|arg| match arg {
1377            hir::PreciseCapturingArg::Param(p) => Some(p.ident.span),
1378            _ => None,
1379        });
1380
1381        let (span, pre, post) = if let Some(last_lifetime_span) = last_lifetime_span {
1382            (last_lifetime_span.shrink_to_hi(), ", ", "")
1383        } else if let Some(first_param_span) = first_param_span {
1384            (first_param_span.shrink_to_lo(), "", ", ")
1385        } else {
1386            // If we have no args, then have `use<>` and need to fall back to using
1387            // span math. This sucks, but should be reliable due to the construction
1388            // of the `use<>` span.
1389            (span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(), "", "")
1390        };
1391
1392        diag.subdiagnostic(errors::AddPreciseCapturing::Existing { span, new_lifetime, pre, post });
1393    } else {
1394        let mut captured_lifetimes = FxIndexSet::default();
1395        let mut captured_non_lifetimes = FxIndexSet::default();
1396
1397        let variances = tcx.variances_of(opaque_def_id);
1398        let mut generics = tcx.generics_of(opaque_def_id);
1399        let mut synthetics = ::alloc::vec::Vec::new()vec![];
1400        loop {
1401            for param in &generics.own_params {
1402                if variances[param.index as usize] == ty::Bivariant {
1403                    continue;
1404                }
1405
1406                match param.kind {
1407                    ty::GenericParamDefKind::Lifetime => {
1408                        captured_lifetimes.insert(param.name);
1409                    }
1410                    ty::GenericParamDefKind::Type { synthetic: true, .. } => {
1411                        synthetics.push((tcx.def_span(param.def_id), param.name));
1412                    }
1413                    ty::GenericParamDefKind::Type { .. }
1414                    | ty::GenericParamDefKind::Const { .. } => {
1415                        captured_non_lifetimes.insert(param.name);
1416                    }
1417                }
1418            }
1419
1420            if let Some(parent) = generics.parent {
1421                generics = tcx.generics_of(parent);
1422            } else {
1423                break;
1424            }
1425        }
1426
1427        if !captured_lifetimes.insert(new_lifetime) {
1428            // Uh, strange. This lifetime appears to already be captured...
1429            return;
1430        }
1431
1432        if synthetics.is_empty() {
1433            let concatenated_bounds = captured_lifetimes
1434                .into_iter()
1435                .chain(captured_non_lifetimes)
1436                .map(|sym| sym.to_string())
1437                .collect::<Vec<_>>()
1438                .join(", ");
1439
1440            diag.subdiagnostic(errors::AddPreciseCapturing::New {
1441                span: tcx.def_span(opaque_def_id).shrink_to_hi(),
1442                new_lifetime,
1443                concatenated_bounds,
1444            });
1445        } else {
1446            let mut next_fresh_param = || {
1447                ['T', 'U', 'V', 'W', 'X', 'Y', 'A', 'B', 'C']
1448                    .into_iter()
1449                    .map(sym::character)
1450                    .chain((0..).map(|i| Symbol::intern(&::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("T{0}", i)) })format!("T{i}"))))
1451                    .find(|s| captured_non_lifetimes.insert(*s))
1452                    .unwrap()
1453            };
1454
1455            let mut new_params = String::new();
1456            let mut suggs = ::alloc::vec::Vec::new()vec![];
1457            let mut apit_spans = ::alloc::vec::Vec::new()vec![];
1458
1459            for (i, (span, name)) in synthetics.into_iter().enumerate() {
1460                apit_spans.push(span);
1461
1462                let fresh_param = next_fresh_param();
1463
1464                // Suggest renaming.
1465                suggs.push((span, fresh_param.to_string()));
1466
1467                // Super jank. Turn `impl Trait` into `T: Trait`.
1468                //
1469                // This currently involves stripping the `impl` from the name of
1470                // the parameter, since APITs are always named after how they are
1471                // rendered in the AST. This sucks! But to recreate the bound list
1472                // from the APIT itself would be miserable, so we're stuck with
1473                // this for now!
1474                if i > 0 {
1475                    new_params += ", ";
1476                }
1477                let name_as_bounds = name.as_str().trim_start_matches("impl").trim_start();
1478                new_params += fresh_param.as_str();
1479                new_params += ": ";
1480                new_params += name_as_bounds;
1481            }
1482
1483            let Some(generics) = tcx.hir_get_generics(fn_def_id) else {
1484                // This shouldn't happen, but don't ICE.
1485                return;
1486            };
1487
1488            // Add generics or concatenate to the end of the list.
1489            suggs.push(if let Some(params_span) = generics.span_for_param_suggestion() {
1490                (params_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(", {0}", new_params))
    })format!(", {new_params}"))
1491            } else {
1492                (generics.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0}>", new_params))
    })format!("<{new_params}>"))
1493            });
1494
1495            let concatenated_bounds = captured_lifetimes
1496                .into_iter()
1497                .chain(captured_non_lifetimes)
1498                .map(|sym| sym.to_string())
1499                .collect::<Vec<_>>()
1500                .join(", ");
1501
1502            suggs.push((
1503                tcx.def_span(opaque_def_id).shrink_to_hi(),
1504                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" + use<{0}>", concatenated_bounds))
    })format!(" + use<{concatenated_bounds}>"),
1505            ));
1506
1507            diag.subdiagnostic(errors::AddPreciseCapturingAndParams {
1508                suggs,
1509                new_lifetime,
1510                apit_spans,
1511            });
1512        }
1513    }
1514}