rustc_borrowck/diagnostics/
region_errors.rs

1//! Error reporting machinery for lifetime errors.
2
3use rustc_data_structures::fx::FxIndexSet;
4use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan};
5use rustc_hir as hir;
6use rustc_hir::GenericBound::Trait;
7use rustc_hir::QPath::Resolved;
8use rustc_hir::WherePredicateKind::BoundPredicate;
9use rustc_hir::def::Res::Def;
10use rustc_hir::def_id::DefId;
11use rustc_hir::intravisit::VisitorExt;
12use rustc_hir::{PolyTraitRef, TyKind, WhereBoundPredicate};
13use rustc_infer::infer::{NllRegionVariableOrigin, SubregionOrigin};
14use rustc_middle::bug;
15use rustc_middle::hir::place::PlaceBase;
16use rustc_middle::mir::{AnnotationSource, ConstraintCategory, ReturnConstraint};
17use rustc_middle::ty::{
18    self, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitor, fold_regions,
19};
20use rustc_span::{Ident, Span, kw};
21use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
22use rustc_trait_selection::error_reporting::infer::nice_region_error::{
23    self, HirTraitObjectVisitor, NiceRegionError, TraitObjectVisitor, find_anon_type,
24    find_param_with_region, suggest_adding_lifetime_params,
25};
26use rustc_trait_selection::infer::InferCtxtExt;
27use rustc_trait_selection::traits::{Obligation, ObligationCtxt};
28use tracing::{debug, instrument, trace};
29
30use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource};
31use crate::nll::ConstraintDescription;
32use crate::region_infer::values::RegionElement;
33use crate::region_infer::{BlameConstraint, TypeTest};
34use crate::session_diagnostics::{
35    FnMutError, FnMutReturnTypeErr, GenericDoesNotLiveLongEnough, LifetimeOutliveErr,
36    LifetimeReturnCategoryErr, RequireStaticErr, VarHereDenote,
37};
38use crate::universal_regions::DefiningTy;
39use crate::{MirBorrowckCtxt, borrowck_errors, fluent_generated as fluent};
40
41impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
42    fn description(&self) -> &'static str {
43        // Must end with a space. Allows for empty names to be provided.
44        match self {
45            ConstraintCategory::Assignment => "assignment ",
46            ConstraintCategory::Return(_) => "returning this value ",
47            ConstraintCategory::Yield => "yielding this value ",
48            ConstraintCategory::UseAsConst => "using this value as a constant ",
49            ConstraintCategory::UseAsStatic => "using this value as a static ",
50            ConstraintCategory::Cast { is_implicit_coercion: false, .. } => "cast ",
51            ConstraintCategory::Cast { is_implicit_coercion: true, .. } => "coercion ",
52            ConstraintCategory::CallArgument(_) => "argument ",
53            ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => "generic argument ",
54            ConstraintCategory::TypeAnnotation(_) => "type annotation ",
55            ConstraintCategory::SizedBound => "proving this value is `Sized` ",
56            ConstraintCategory::CopyBound => "copying this value ",
57            ConstraintCategory::OpaqueType => "opaque type ",
58            ConstraintCategory::ClosureUpvar(_) => "closure capture ",
59            ConstraintCategory::Usage => "this usage ",
60            ConstraintCategory::Predicate(_)
61            | ConstraintCategory::Boring
62            | ConstraintCategory::BoringNoLocation
63            | ConstraintCategory::Internal
64            | ConstraintCategory::OutlivesUnnameablePlaceholder(..) => "",
65        }
66    }
67}
68
69/// A collection of errors encountered during region inference. This is needed to efficiently
70/// report errors after borrow checking.
71///
72/// Usually we expect this to either be empty or contain a small number of items, so we can avoid
73/// allocation most of the time.
74pub(crate) struct RegionErrors<'tcx>(Vec<(RegionErrorKind<'tcx>, ErrorGuaranteed)>, TyCtxt<'tcx>);
75
76impl<'tcx> RegionErrors<'tcx> {
77    pub(crate) fn new(tcx: TyCtxt<'tcx>) -> Self {
78        Self(vec![], tcx)
79    }
80    #[track_caller]
81    pub(crate) fn push(&mut self, val: impl Into<RegionErrorKind<'tcx>>) {
82        let val = val.into();
83        let guar = self.1.sess.dcx().delayed_bug(format!("{val:?}"));
84        self.0.push((val, guar));
85    }
86    pub(crate) fn is_empty(&self) -> bool {
87        self.0.is_empty()
88    }
89    pub(crate) fn into_iter(
90        self,
91    ) -> impl Iterator<Item = (RegionErrorKind<'tcx>, ErrorGuaranteed)> {
92        self.0.into_iter()
93    }
94}
95
96impl std::fmt::Debug for RegionErrors<'_> {
97    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98        f.debug_tuple("RegionErrors").field(&self.0).finish()
99    }
100}
101
102#[derive(Clone, Debug)]
103pub(crate) enum RegionErrorKind<'tcx> {
104    /// A generic bound failure for a type test (`T: 'a`).
105    TypeTestError { type_test: TypeTest<'tcx> },
106
107    /// Higher-ranked subtyping error.
108    BoundUniversalRegionError {
109        /// The placeholder free region.
110        longer_fr: RegionVid,
111        /// The region element that erroneously must be outlived by `longer_fr`.
112        error_element: RegionElement<'tcx>,
113        /// The placeholder region.
114        placeholder: ty::PlaceholderRegion<'tcx>,
115    },
116
117    /// Any other lifetime error.
118    RegionError {
119        /// The origin of the region.
120        fr_origin: NllRegionVariableOrigin<'tcx>,
121        /// The region that should outlive `shorter_fr`.
122        longer_fr: RegionVid,
123        /// The region that should be shorter, but we can't prove it.
124        shorter_fr: RegionVid,
125        /// Indicates whether this is a reported error. We currently only report the first error
126        /// encountered and leave the rest unreported so as not to overwhelm the user.
127        is_reported: bool,
128    },
129}
130
131/// Information about the various region constraints involved in a borrow checker error.
132#[derive(Clone, Debug)]
133pub(crate) struct ErrorConstraintInfo<'tcx> {
134    // fr: outlived_fr
135    pub(super) fr: RegionVid,
136    pub(super) outlived_fr: RegionVid,
137
138    // Category and span for best blame constraint
139    pub(super) category: ConstraintCategory<'tcx>,
140    pub(super) span: Span,
141}
142
143impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
144    /// Converts a region inference variable into a `ty::Region` that
145    /// we can use for error reporting. If `r` is universally bound,
146    /// then we use the name that we have on record for it. If `r` is
147    /// existentially bound, then we check its inferred value and try
148    /// to find a good name from that. Returns `None` if we can't find
149    /// one (e.g., this is just some random part of the CFG).
150    pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
151        self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
152    }
153
154    /// Returns the `RegionVid` corresponding to the region returned by
155    /// `to_error_region`.
156    pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
157        if self.regioncx.universal_regions().is_universal_region(r) {
158            Some(r)
159        } else {
160            // We just want something nameable, even if it's not
161            // actually an upper bound.
162            let upper_bound = self.regioncx.approx_universal_upper_bound(r);
163
164            if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
165                self.to_error_region_vid(upper_bound)
166            } else {
167                None
168            }
169        }
170    }
171
172    /// Map the regions in the type to named regions, where possible.
173    fn name_regions<T>(&self, tcx: TyCtxt<'tcx>, ty: T) -> T
174    where
175        T: TypeFoldable<TyCtxt<'tcx>>,
176    {
177        fold_regions(tcx, ty, |region, _| match region.kind() {
178            ty::ReVar(vid) => self.to_error_region(vid).unwrap_or(region),
179            _ => region,
180        })
181    }
182
183    /// Returns `true` if a closure is inferred to be an `FnMut` closure.
184    fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
185        if let Some(r) = self.to_error_region(fr)
186            && let ty::ReLateParam(late_param) = r.kind()
187            && let ty::LateParamRegionKind::ClosureEnv = late_param.kind
188            && let DefiningTy::Closure(_, args) = self.regioncx.universal_regions().defining_ty
189        {
190            return args.as_closure().kind() == ty::ClosureKind::FnMut;
191        }
192
193        false
194    }
195
196    // For generic associated types (GATs) which implied 'static requirement
197    // from higher-ranked trait bounds (HRTB). Try to locate span of the trait
198    // and the span which bounded to the trait for adding 'static lifetime suggestion
199    #[allow(rustc::diagnostic_outside_of_impl)]
200    fn suggest_static_lifetime_for_gat_from_hrtb(
201        &self,
202        diag: &mut Diag<'_>,
203        lower_bound: RegionVid,
204    ) {
205        let tcx = self.infcx.tcx;
206
207        // find generic associated types in the given region 'lower_bound'
208        let gat_id_and_generics = self
209            .regioncx
210            .placeholders_contained_in(lower_bound)
211            .map(|placeholder| {
212                if let Some(id) = placeholder.bound.kind.get_id()
213                    && let Some(placeholder_id) = id.as_local()
214                    && let gat_hir_id = tcx.local_def_id_to_hir_id(placeholder_id)
215                    && let Some(generics_impl) =
216                        tcx.parent_hir_node(tcx.parent_hir_id(gat_hir_id)).generics()
217                {
218                    Some((gat_hir_id, generics_impl))
219                } else {
220                    None
221                }
222            })
223            .collect::<Vec<_>>();
224        debug!(?gat_id_and_generics);
225
226        // Look for the where-bound which introduces the placeholder.
227        // As we're using the HIR, we need to handle both `for<'a> T: Trait<'a>`
228        // and `T: for<'a> Trait`<'a>.
229        let mut hrtb_bounds = vec![];
230        gat_id_and_generics.iter().flatten().for_each(|&(gat_hir_id, generics)| {
231            for pred in generics.predicates {
232                let BoundPredicate(WhereBoundPredicate { bound_generic_params, bounds, .. }) =
233                    pred.kind
234                else {
235                    continue;
236                };
237                if bound_generic_params
238                    .iter()
239                    .rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
240                    .is_some()
241                {
242                    for bound in *bounds {
243                        hrtb_bounds.push(bound);
244                    }
245                } else {
246                    for bound in *bounds {
247                        if let Trait(trait_bound) = bound {
248                            if trait_bound
249                                .bound_generic_params
250                                .iter()
251                                .rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
252                                .is_some()
253                            {
254                                hrtb_bounds.push(bound);
255                                return;
256                            }
257                        }
258                    }
259                }
260            }
261        });
262        debug!(?hrtb_bounds);
263
264        let mut suggestions = vec![];
265        hrtb_bounds.iter().for_each(|bound| {
266            let Trait(PolyTraitRef { trait_ref, span: trait_span, .. }) = bound else {
267                return;
268            };
269            diag.span_note(*trait_span, fluent::borrowck_limitations_implies_static);
270            let Some(generics_fn) = tcx.hir_get_generics(self.body.source.def_id().expect_local())
271            else {
272                return;
273            };
274            let Def(_, trait_res_defid) = trait_ref.path.res else {
275                return;
276            };
277            debug!(?generics_fn);
278            generics_fn.predicates.iter().for_each(|predicate| {
279                let BoundPredicate(WhereBoundPredicate { bounded_ty, bounds, .. }) = predicate.kind
280                else {
281                    return;
282                };
283                bounds.iter().for_each(|bd| {
284                    if let Trait(PolyTraitRef { trait_ref: tr_ref, .. }) = bd
285                        && let Def(_, res_defid) = tr_ref.path.res
286                        && res_defid == trait_res_defid // trait id matches
287                        && let TyKind::Path(Resolved(_, path)) = bounded_ty.kind
288                        && let Def(_, defid) = path.res
289                        && generics_fn.params
290                            .iter()
291                            .rfind(|param| param.def_id.to_def_id() == defid)
292                            .is_some()
293                    {
294                        suggestions.push((predicate.span.shrink_to_hi(), " + 'static".to_string()));
295                    }
296                });
297            });
298        });
299        if suggestions.len() > 0 {
300            suggestions.dedup();
301            diag.multipart_suggestion_verbose(
302                fluent::borrowck_restrict_to_static,
303                suggestions,
304                Applicability::MaybeIncorrect,
305            );
306        }
307    }
308
309    /// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
310    pub(crate) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
311        // Iterate through all the errors, producing a diagnostic for each one. The diagnostics are
312        // buffered in the `MirBorrowckCtxt`.
313        let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
314        for (nll_error, _) in nll_errors.into_iter() {
315            match nll_error {
316                RegionErrorKind::TypeTestError { type_test } => {
317                    // Try to convert the lower-bound region into something named we can print for
318                    // the user.
319                    let lower_bound_region = self.to_error_region(type_test.lower_bound);
320
321                    let type_test_span = type_test.span;
322
323                    if let Some(lower_bound_region) = lower_bound_region {
324                        let generic_ty = self.name_regions(
325                            self.infcx.tcx,
326                            type_test.generic_kind.to_ty(self.infcx.tcx),
327                        );
328                        let origin =
329                            SubregionOrigin::RelateParamBound(type_test_span, generic_ty, None);
330                        self.buffer_error(self.infcx.err_ctxt().construct_generic_bound_failure(
331                            self.body.source.def_id().expect_local(),
332                            type_test_span,
333                            Some(origin),
334                            self.name_regions(self.infcx.tcx, type_test.generic_kind),
335                            lower_bound_region,
336                        ));
337                    } else {
338                        // FIXME. We should handle this case better. It
339                        // indicates that we have e.g., some region variable
340                        // whose value is like `'a+'b` where `'a` and `'b` are
341                        // distinct unrelated universal regions that are not
342                        // known to outlive one another. It'd be nice to have
343                        // some examples where this arises to decide how best
344                        // to report it; we could probably handle it by
345                        // iterating over the universal regions and reporting
346                        // an error that multiple bounds are required.
347                        let mut diag = self.dcx().create_err(GenericDoesNotLiveLongEnough {
348                            kind: type_test.generic_kind.to_string(),
349                            span: type_test_span,
350                        });
351
352                        // Add notes and suggestions for the case of 'static lifetime
353                        // implied but not specified when a generic associated types
354                        // are from higher-ranked trait bounds
355                        self.suggest_static_lifetime_for_gat_from_hrtb(
356                            &mut diag,
357                            type_test.lower_bound,
358                        );
359
360                        self.buffer_error(diag);
361                    }
362                }
363
364                RegionErrorKind::BoundUniversalRegionError {
365                    longer_fr,
366                    placeholder,
367                    error_element,
368                } => {
369                    let error_vid = self.regioncx.region_from_element(longer_fr, &error_element);
370
371                    // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
372                    let cause = self
373                        .regioncx
374                        .best_blame_constraint(
375                            longer_fr,
376                            NllRegionVariableOrigin::Placeholder(placeholder),
377                            error_vid,
378                        )
379                        .0
380                        .cause;
381
382                    let universe = placeholder.universe;
383                    let universe_info = self.regioncx.universe_info(universe);
384
385                    universe_info.report_erroneous_element(self, placeholder, error_element, cause);
386                }
387
388                RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
389                    if is_reported {
390                        self.report_region_error(
391                            longer_fr,
392                            fr_origin,
393                            shorter_fr,
394                            &mut outlives_suggestion,
395                        );
396                    } else {
397                        // We only report the first error, so as not to overwhelm the user. See
398                        // `RegRegionErrorKind` docs.
399                        //
400                        // FIXME: currently we do nothing with these, but perhaps we can do better?
401                        // FIXME: try collecting these constraints on the outlives suggestion
402                        // builder. Does it make the suggestions any better?
403                        debug!(
404                            "Unreported region error: can't prove that {:?}: {:?}",
405                            longer_fr, shorter_fr
406                        );
407                    }
408                }
409            }
410        }
411
412        // Emit one outlives suggestions for each MIR def we borrowck
413        outlives_suggestion.add_suggestion(self);
414    }
415
416    /// Report an error because the universal region `fr` was required to outlive
417    /// `outlived_fr` but it is not known to do so. For example:
418    ///
419    /// ```compile_fail
420    /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
421    /// ```
422    ///
423    /// Here we would be invoked with `fr = 'a` and `outlived_fr = 'b`.
424    // FIXME: make this translatable
425    #[allow(rustc::diagnostic_outside_of_impl)]
426    #[allow(rustc::untranslatable_diagnostic)]
427    pub(crate) fn report_region_error(
428        &mut self,
429        fr: RegionVid,
430        fr_origin: NllRegionVariableOrigin<'tcx>,
431        outlived_fr: RegionVid,
432        outlives_suggestion: &mut OutlivesSuggestionBuilder,
433    ) {
434        debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
435
436        let (blame_constraint, path) =
437            self.regioncx.best_blame_constraint(fr, fr_origin, outlived_fr);
438        let BlameConstraint { category, cause, variance_info, .. } = blame_constraint;
439
440        debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
441
442        // Check if we can use one of the "nice region errors".
443        if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
444            let infer_err = self.infcx.err_ctxt();
445            let nice =
446                NiceRegionError::new_from_span(&infer_err, self.mir_def_id(), cause.span, o, f);
447            if let Some(diag) = nice.try_report_from_nll() {
448                self.buffer_error(diag);
449                return;
450            }
451        }
452
453        let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
454            self.regioncx.universal_regions().is_local_free_region(fr),
455            self.regioncx.universal_regions().is_local_free_region(outlived_fr),
456        );
457
458        debug!(
459            "report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
460            fr_is_local, outlived_fr_is_local, category
461        );
462
463        let errci = ErrorConstraintInfo { fr, outlived_fr, category, span: cause.span };
464
465        let mut diag = match (category, fr_is_local, outlived_fr_is_local) {
466            (ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
467                self.report_fnmut_error(&errci, kind)
468            }
469            (ConstraintCategory::Assignment, true, false)
470            | (ConstraintCategory::CallArgument(_), true, false) => {
471                let mut db = self.report_escaping_data_error(&errci);
472
473                outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
474                outlives_suggestion.collect_constraint(fr, outlived_fr);
475
476                db
477            }
478            _ => {
479                let mut db = self.report_general_error(&errci);
480
481                outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
482                outlives_suggestion.collect_constraint(fr, outlived_fr);
483
484                db
485            }
486        };
487
488        match variance_info {
489            ty::VarianceDiagInfo::None => {}
490            ty::VarianceDiagInfo::Invariant { ty, param_index } => {
491                let (desc, note) = match ty.kind() {
492                    ty::RawPtr(ty, mutbl) => {
493                        assert_eq!(*mutbl, hir::Mutability::Mut);
494                        (
495                            format!("a mutable pointer to `{}`", ty),
496                            "mutable pointers are invariant over their type parameter".to_string(),
497                        )
498                    }
499                    ty::Ref(_, inner_ty, mutbl) => {
500                        assert_eq!(*mutbl, hir::Mutability::Mut);
501                        (
502                            format!("a mutable reference to `{inner_ty}`"),
503                            "mutable references are invariant over their type parameter"
504                                .to_string(),
505                        )
506                    }
507                    ty::Adt(adt, args) => {
508                        let generic_arg = args[param_index as usize];
509                        let identity_args =
510                            GenericArgs::identity_for_item(self.infcx.tcx, adt.did());
511                        let base_ty = Ty::new_adt(self.infcx.tcx, *adt, identity_args);
512                        let base_generic_arg = identity_args[param_index as usize];
513                        let adt_desc = adt.descr();
514
515                        let desc = format!(
516                            "the type `{ty}`, which makes the generic argument `{generic_arg}` invariant"
517                        );
518                        let note = format!(
519                            "the {adt_desc} `{base_ty}` is invariant over the parameter `{base_generic_arg}`"
520                        );
521                        (desc, note)
522                    }
523                    ty::FnDef(def_id, _) => {
524                        let name = self.infcx.tcx.item_name(*def_id);
525                        let identity_args = GenericArgs::identity_for_item(self.infcx.tcx, *def_id);
526                        let desc = format!("a function pointer to `{name}`");
527                        let note = format!(
528                            "the function `{name}` is invariant over the parameter `{}`",
529                            identity_args[param_index as usize]
530                        );
531                        (desc, note)
532                    }
533                    _ => panic!("Unexpected type {ty:?}"),
534                };
535                diag.note(format!("requirement occurs because of {desc}",));
536                diag.note(note);
537                diag.help("see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance");
538            }
539        }
540
541        self.add_placeholder_from_predicate_note(&mut diag, &path);
542        self.add_sized_or_copy_bound_info(&mut diag, category, &path);
543
544        for constraint in &path {
545            if let ConstraintCategory::Cast { is_raw_ptr_dyn_type_cast: true, .. } =
546                constraint.category
547            {
548                diag.span_note(
549                    constraint.span,
550                    format!("raw pointer casts of trait objects cannot extend lifetimes"),
551                );
552                diag.note(format!(
553                    "this was previously accepted by the compiler but was changed recently"
554                ));
555                diag.help(format!(
556                    "see <https://github.com/rust-lang/rust/issues/141402> for more information"
557                ));
558            }
559        }
560
561        self.buffer_error(diag);
562    }
563
564    /// Report a specialized error when `FnMut` closures return a reference to a captured variable.
565    /// This function expects `fr` to be local and `outlived_fr` to not be local.
566    ///
567    /// ```text
568    /// error: captured variable cannot escape `FnMut` closure body
569    ///   --> $DIR/issue-53040.rs:15:8
570    ///    |
571    /// LL |     || &mut v;
572    ///    |     -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
573    ///    |     |
574    ///    |     inferred to be a `FnMut` closure
575    ///    |
576    ///    = note: `FnMut` closures only have access to their captured variables while they are
577    ///            executing...
578    ///    = note: ...therefore, returned references to captured variables will escape the closure
579    /// ```
580    #[allow(rustc::diagnostic_outside_of_impl)] // FIXME
581    fn report_fnmut_error(
582        &self,
583        errci: &ErrorConstraintInfo<'tcx>,
584        kind: ReturnConstraint,
585    ) -> Diag<'infcx> {
586        let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
587
588        let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
589        if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *output_ty.kind() {
590            output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity()
591        };
592
593        debug!("report_fnmut_error: output_ty={:?}", output_ty);
594
595        let err = FnMutError {
596            span: *span,
597            ty_err: match output_ty.kind() {
598                ty::Coroutine(def, ..) if self.infcx.tcx.coroutine_is_async(*def) => {
599                    FnMutReturnTypeErr::ReturnAsyncBlock { span: *span }
600                }
601                _ if output_ty.contains_closure() => {
602                    FnMutReturnTypeErr::ReturnClosure { span: *span }
603                }
604                _ => FnMutReturnTypeErr::ReturnRef { span: *span },
605            },
606        };
607
608        let mut diag = self.dcx().create_err(err);
609
610        if let ReturnConstraint::ClosureUpvar(upvar_field) = kind {
611            let def_id = match self.regioncx.universal_regions().defining_ty {
612                DefiningTy::Closure(def_id, _) => def_id,
613                ty => bug!("unexpected DefiningTy {:?}", ty),
614            };
615
616            let captured_place = &self.upvars[upvar_field.index()].place;
617            let defined_hir = match captured_place.base {
618                PlaceBase::Local(hirid) => Some(hirid),
619                PlaceBase::Upvar(upvar) => Some(upvar.var_path.hir_id),
620                _ => None,
621            };
622
623            if let Some(def_hir) = defined_hir {
624                let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap();
625                let upvar_def_span = self.infcx.tcx.hir_span(def_hir);
626                let upvar_span = upvars_map.get(&def_hir).unwrap().span;
627                diag.subdiagnostic(VarHereDenote::Defined { span: upvar_def_span });
628                diag.subdiagnostic(VarHereDenote::Captured { span: upvar_span });
629            }
630        }
631
632        if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {
633            diag.subdiagnostic(VarHereDenote::FnMutInferred { span: fr_span });
634        }
635
636        self.suggest_move_on_borrowing_closure(&mut diag);
637
638        diag
639    }
640
641    /// Reports an error specifically for when data is escaping a closure.
642    ///
643    /// ```text
644    /// error: borrowed data escapes outside of function
645    ///   --> $DIR/lifetime-bound-will-change-warning.rs:44:5
646    ///    |
647    /// LL | fn test2<'a>(x: &'a Box<Fn()+'a>) {
648    ///    |              - `x` is a reference that is only valid in the function body
649    /// LL |     // but ref_obj will not, so warn.
650    /// LL |     ref_obj(x)
651    ///    |     ^^^^^^^^^^ `x` escapes the function body here
652    /// ```
653    #[instrument(level = "debug", skip(self))]
654    fn report_escaping_data_error(&self, errci: &ErrorConstraintInfo<'tcx>) -> Diag<'infcx> {
655        let ErrorConstraintInfo { span, category, .. } = errci;
656
657        let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
658            self.infcx.tcx,
659            self.body,
660            &self.local_names(),
661            &self.upvars,
662            errci.fr,
663        );
664        let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
665            self.infcx.tcx,
666            self.body,
667            &self.local_names(),
668            &self.upvars,
669            errci.outlived_fr,
670        );
671
672        let escapes_from =
673            self.infcx.tcx.def_descr(self.regioncx.universal_regions().defining_ty.def_id());
674
675        // Revert to the normal error in these cases.
676        // Assignments aren't "escapes" in function items.
677        if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
678            || (*category == ConstraintCategory::Assignment
679                && self.regioncx.universal_regions().defining_ty.is_fn_def())
680            || self.regioncx.universal_regions().defining_ty.is_const()
681        {
682            return self.report_general_error(errci);
683        }
684
685        let mut diag =
686            borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
687
688        if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
689            // FIXME: make this translatable
690            #[allow(rustc::diagnostic_outside_of_impl)]
691            #[allow(rustc::untranslatable_diagnostic)]
692            diag.span_label(
693                outlived_fr_span,
694                format!("`{outlived_fr_name}` declared here, outside of the {escapes_from} body",),
695            );
696        }
697
698        // FIXME: make this translatable
699        #[allow(rustc::diagnostic_outside_of_impl)]
700        #[allow(rustc::untranslatable_diagnostic)]
701        if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
702            diag.span_label(
703                fr_span,
704                format!(
705                    "`{fr_name}` is a reference that is only valid in the {escapes_from} body",
706                ),
707            );
708
709            diag.span_label(*span, format!("`{fr_name}` escapes the {escapes_from} body here"));
710        } else {
711            diag.span_label(
712                *span,
713                format!("a temporary borrow escapes the {escapes_from} body here"),
714            );
715            if let Some((Some(outlived_name), _)) = outlived_fr_name_and_span {
716                diag.help(format!(
717                    "`{outlived_name}` is declared outside the {escapes_from}, \
718                     so any data borrowed inside the {escapes_from} cannot be stored into it"
719                ));
720            }
721        }
722
723        // Only show an extra note if we can find an 'error region' for both of the region
724        // variables. This avoids showing a noisy note that just mentions 'synthetic' regions
725        // that don't help the user understand the error.
726        match (self.to_error_region(errci.fr), self.to_error_region(errci.outlived_fr)) {
727            (Some(f), Some(o)) => {
728                self.maybe_suggest_constrain_dyn_trait_impl(&mut diag, f, o, category);
729
730                let fr_region_name = self.give_region_a_name(errci.fr).unwrap();
731                fr_region_name.highlight_region_name(&mut diag);
732                let outlived_fr_region_name = self.give_region_a_name(errci.outlived_fr).unwrap();
733                outlived_fr_region_name.highlight_region_name(&mut diag);
734
735                // FIXME: make this translatable
736                #[allow(rustc::diagnostic_outside_of_impl)]
737                #[allow(rustc::untranslatable_diagnostic)]
738                diag.span_label(
739                    *span,
740                    format!(
741                        "{}requires that `{}` must outlive `{}`",
742                        category.description(),
743                        fr_region_name,
744                        outlived_fr_region_name,
745                    ),
746                );
747            }
748            _ => {}
749        }
750
751        diag
752    }
753
754    /// Reports a region inference error for the general case with named/synthesized lifetimes to
755    /// explain what is happening.
756    ///
757    /// ```text
758    /// error: unsatisfied lifetime constraints
759    ///   --> $DIR/regions-creating-enums3.rs:17:5
760    ///    |
761    /// LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
762    ///    |                -- -- lifetime `'b` defined here
763    ///    |                |
764    ///    |                lifetime `'a` defined here
765    /// LL |     ast::add(x, y)
766    ///    |     ^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it
767    ///    |                    is returning data with lifetime `'b`
768    /// ```
769    #[allow(rustc::diagnostic_outside_of_impl)] // FIXME
770    fn report_general_error(&self, errci: &ErrorConstraintInfo<'tcx>) -> Diag<'infcx> {
771        let ErrorConstraintInfo { fr, outlived_fr, span, category, .. } = errci;
772
773        let mir_def_name = self.infcx.tcx.def_descr(self.mir_def_id().to_def_id());
774
775        let err = LifetimeOutliveErr { span: *span };
776        let mut diag = self.dcx().create_err(err);
777
778        // In certain scenarios, such as the one described in issue #118021,
779        // we might encounter a lifetime that cannot be named.
780        // These situations are bound to result in errors.
781        // To prevent an immediate ICE, we opt to create a dummy name instead.
782        let fr_name = self.give_region_a_name(*fr).unwrap_or(RegionName {
783            name: kw::UnderscoreLifetime,
784            source: RegionNameSource::Static,
785        });
786        fr_name.highlight_region_name(&mut diag);
787        let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
788        outlived_fr_name.highlight_region_name(&mut diag);
789
790        let err_category = if matches!(category, ConstraintCategory::Return(_))
791            && self.regioncx.universal_regions().is_local_free_region(*outlived_fr)
792        {
793            LifetimeReturnCategoryErr::WrongReturn {
794                span: *span,
795                mir_def_name,
796                outlived_fr_name,
797                fr_name: &fr_name,
798            }
799        } else {
800            LifetimeReturnCategoryErr::ShortReturn {
801                span: *span,
802                category_desc: category.description(),
803                free_region_name: &fr_name,
804                outlived_fr_name,
805            }
806        };
807
808        diag.subdiagnostic(err_category);
809
810        self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
811        self.suggest_adding_lifetime_params(&mut diag, *fr, *outlived_fr);
812        self.suggest_move_on_borrowing_closure(&mut diag);
813        self.suggest_deref_closure_return(&mut diag);
814
815        diag
816    }
817
818    /// Adds a suggestion to errors where an `impl Trait` is returned.
819    ///
820    /// ```text
821    /// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
822    ///       a constraint
823    ///    |
824    /// LL |     fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
825    ///    |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
826    /// ```
827    #[allow(rustc::diagnostic_outside_of_impl)]
828    #[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
829    fn add_static_impl_trait_suggestion(
830        &self,
831        diag: &mut Diag<'_>,
832        fr: RegionVid,
833        // We need to pass `fr_name` - computing it again will label it twice.
834        fr_name: RegionName,
835        outlived_fr: RegionVid,
836    ) {
837        if let (Some(f), Some(outlived_f)) =
838            (self.to_error_region(fr), self.to_error_region(outlived_fr))
839        {
840            if outlived_f.kind() != ty::ReStatic {
841                return;
842            }
843            let suitable_region = self.infcx.tcx.is_suitable_region(self.mir_def_id(), f);
844            let Some(suitable_region) = suitable_region else {
845                return;
846            };
847
848            let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.scope);
849
850            let Some(param) =
851                find_param_with_region(self.infcx.tcx, self.mir_def_id(), f, outlived_f)
852            else {
853                return;
854            };
855
856            let lifetime =
857                if f.is_named(self.infcx.tcx) { fr_name.name } else { kw::UnderscoreLifetime };
858
859            let arg = match param.param.pat.simple_ident() {
860                Some(simple_ident) => format!("argument `{simple_ident}`"),
861                None => "the argument".to_string(),
862            };
863            let captures = format!("captures data from {arg}");
864
865            if !fn_returns.is_empty() {
866                nice_region_error::suggest_new_region_bound(
867                    self.infcx.tcx,
868                    diag,
869                    fn_returns,
870                    lifetime.to_string(),
871                    Some(arg),
872                    captures,
873                    Some((param.param_ty_span, param.param_ty.to_string())),
874                    Some(suitable_region.scope),
875                );
876                return;
877            }
878
879            let Some((alias_tys, alias_span, lt_addition_span)) = self
880                .infcx
881                .tcx
882                .return_type_impl_or_dyn_traits_with_type_alias(suitable_region.scope)
883            else {
884                return;
885            };
886
887            // in case the return type of the method is a type alias
888            let mut spans_suggs: Vec<_> = Vec::new();
889            for alias_ty in alias_tys {
890                if alias_ty.span.desugaring_kind().is_some() {
891                    // Skip `async` desugaring `impl Future`.
892                }
893                if let TyKind::TraitObject(_, lt) = alias_ty.kind {
894                    if lt.kind == hir::LifetimeKind::ImplicitObjectLifetimeDefault {
895                        spans_suggs.push((lt.ident.span.shrink_to_hi(), " + 'a".to_string()));
896                    } else {
897                        spans_suggs.push((lt.ident.span, "'a".to_string()));
898                    }
899                }
900            }
901
902            if let Some(lt_addition_span) = lt_addition_span {
903                spans_suggs.push((lt_addition_span, "'a, ".to_string()));
904            } else {
905                spans_suggs.push((alias_span.shrink_to_hi(), "<'a>".to_string()));
906            }
907
908            diag.multipart_suggestion_verbose(
909                format!(
910                    "to declare that the trait object {captures}, you can add a lifetime parameter `'a` in the type alias"
911                ),
912                spans_suggs,
913                Applicability::MaybeIncorrect,
914            );
915        }
916    }
917
918    fn maybe_suggest_constrain_dyn_trait_impl(
919        &self,
920        diag: &mut Diag<'_>,
921        f: Region<'tcx>,
922        o: Region<'tcx>,
923        category: &ConstraintCategory<'tcx>,
924    ) {
925        if !o.is_static() {
926            return;
927        }
928
929        let tcx = self.infcx.tcx;
930
931        let ConstraintCategory::CallArgument(Some(func_ty)) = category else { return };
932        let ty::FnDef(fn_did, args) = func_ty.kind() else { return };
933        debug!(?fn_did, ?args);
934
935        // Only suggest this on function calls, not closures
936        let ty = tcx.type_of(fn_did).instantiate_identity();
937        debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
938        if let ty::Closure(_, _) = ty.kind() {
939            return;
940        }
941        let Ok(Some(instance)) = ty::Instance::try_resolve(
942            tcx,
943            self.infcx.typing_env(self.infcx.param_env),
944            *fn_did,
945            self.infcx.resolve_vars_if_possible(args),
946        ) else {
947            return;
948        };
949
950        let Some(param) = find_param_with_region(tcx, self.mir_def_id(), f, o) else {
951            return;
952        };
953        debug!(?param);
954
955        let mut visitor = TraitObjectVisitor(FxIndexSet::default());
956        visitor.visit_ty(param.param_ty);
957
958        let Some((ident, self_ty)) = NiceRegionError::get_impl_ident_and_self_ty_from_trait(
959            tcx,
960            instance.def_id(),
961            &visitor.0,
962        ) else {
963            return;
964        };
965
966        self.suggest_constrain_dyn_trait_in_impl(diag, &visitor.0, ident, self_ty);
967    }
968
969    #[allow(rustc::diagnostic_outside_of_impl)]
970    #[instrument(skip(self, err), level = "debug")]
971    fn suggest_constrain_dyn_trait_in_impl(
972        &self,
973        err: &mut Diag<'_>,
974        found_dids: &FxIndexSet<DefId>,
975        ident: Ident,
976        self_ty: &hir::Ty<'_>,
977    ) -> bool {
978        debug!("err: {:#?}", err);
979        let mut suggested = false;
980        for found_did in found_dids {
981            let mut traits = vec![];
982            let mut hir_v = HirTraitObjectVisitor(&mut traits, *found_did);
983            hir_v.visit_ty_unambig(self_ty);
984            debug!("trait spans found: {:?}", traits);
985            for span in &traits {
986                let mut multi_span: MultiSpan = vec![*span].into();
987                multi_span.push_span_label(*span, fluent::borrowck_implicit_static);
988                multi_span.push_span_label(ident.span, fluent::borrowck_implicit_static_introduced);
989                err.subdiagnostic(RequireStaticErr::UsedImpl { multi_span });
990                err.span_suggestion_verbose(
991                    span.shrink_to_hi(),
992                    fluent::borrowck_implicit_static_relax,
993                    " + '_",
994                    Applicability::MaybeIncorrect,
995                );
996                suggested = true;
997            }
998        }
999        suggested
1000    }
1001
1002    fn suggest_adding_lifetime_params(&self, diag: &mut Diag<'_>, sub: RegionVid, sup: RegionVid) {
1003        let (Some(sub), Some(sup)) = (self.to_error_region(sub), self.to_error_region(sup)) else {
1004            return;
1005        };
1006
1007        let Some((ty_sub, _)) = self
1008            .infcx
1009            .tcx
1010            .is_suitable_region(self.mir_def_id(), sub)
1011            .and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sub))
1012        else {
1013            return;
1014        };
1015
1016        let Some((ty_sup, _)) = self
1017            .infcx
1018            .tcx
1019            .is_suitable_region(self.mir_def_id(), sup)
1020            .and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sup))
1021        else {
1022            return;
1023        };
1024
1025        suggest_adding_lifetime_params(
1026            self.infcx.tcx,
1027            diag,
1028            self.mir_def_id(),
1029            sub,
1030            ty_sup,
1031            ty_sub,
1032        );
1033    }
1034
1035    #[allow(rustc::diagnostic_outside_of_impl)]
1036    /// When encountering a lifetime error caused by the return type of a closure, check the
1037    /// corresponding trait bound and see if dereferencing the closure return value would satisfy
1038    /// them. If so, we produce a structured suggestion.
1039    fn suggest_deref_closure_return(&self, diag: &mut Diag<'_>) {
1040        let tcx = self.infcx.tcx;
1041
1042        // Get the closure return value and type.
1043        let closure_def_id = self.mir_def_id();
1044        let hir::Node::Expr(
1045            closure_expr @ hir::Expr {
1046                kind: hir::ExprKind::Closure(hir::Closure { body, .. }), ..
1047            },
1048        ) = tcx.hir_node_by_def_id(closure_def_id)
1049        else {
1050            return;
1051        };
1052        let ty::Closure(_, args) = *tcx.type_of(closure_def_id).instantiate_identity().kind()
1053        else {
1054            return;
1055        };
1056        let args = args.as_closure();
1057
1058        // Make sure that the parent expression is a method call.
1059        let parent_expr_id = tcx.parent_hir_id(self.mir_hir_id());
1060        let hir::Node::Expr(
1061            parent_expr @ hir::Expr {
1062                kind: hir::ExprKind::MethodCall(_, rcvr, call_args, _), ..
1063            },
1064        ) = tcx.hir_node(parent_expr_id)
1065        else {
1066            return;
1067        };
1068        let typeck_results = tcx.typeck(self.mir_def_id());
1069
1070        // We don't use `ty.peel_refs()` to get the number of `*`s needed to get the root type.
1071        let liberated_sig = tcx.liberate_late_bound_regions(closure_def_id.to_def_id(), args.sig());
1072        let mut peeled_ty = liberated_sig.output();
1073        let mut count = 0;
1074        while let ty::Ref(_, ref_ty, _) = *peeled_ty.kind() {
1075            peeled_ty = ref_ty;
1076            count += 1;
1077        }
1078        if !self.infcx.type_is_copy_modulo_regions(self.infcx.param_env, peeled_ty) {
1079            return;
1080        }
1081
1082        // Build a new closure where the return type is an owned value, instead of a ref.
1083        let closure_sig_as_fn_ptr_ty = Ty::new_fn_ptr(
1084            tcx,
1085            ty::Binder::dummy(tcx.mk_fn_sig(
1086                liberated_sig.inputs().iter().copied(),
1087                peeled_ty,
1088                liberated_sig.c_variadic,
1089                hir::Safety::Safe,
1090                rustc_abi::ExternAbi::Rust,
1091            )),
1092        );
1093        let closure_ty = Ty::new_closure(
1094            tcx,
1095            closure_def_id.to_def_id(),
1096            ty::ClosureArgs::new(
1097                tcx,
1098                ty::ClosureArgsParts {
1099                    parent_args: args.parent_args(),
1100                    closure_kind_ty: args.kind_ty(),
1101                    tupled_upvars_ty: args.tupled_upvars_ty(),
1102                    closure_sig_as_fn_ptr_ty,
1103                },
1104            )
1105            .args,
1106        );
1107
1108        let Some((closure_arg_pos, _)) =
1109            call_args.iter().enumerate().find(|(_, arg)| arg.hir_id == closure_expr.hir_id)
1110        else {
1111            return;
1112        };
1113        // Get the type for the parameter corresponding to the argument the closure with the
1114        // lifetime error we had.
1115        let Some(method_def_id) = typeck_results.type_dependent_def_id(parent_expr.hir_id) else {
1116            return;
1117        };
1118        let Some(input_arg) = tcx
1119            .fn_sig(method_def_id)
1120            .skip_binder()
1121            .inputs()
1122            .skip_binder()
1123            // Methods have a `self` arg, so `pos` is actually `+ 1` to match the method call arg.
1124            .get(closure_arg_pos + 1)
1125        else {
1126            return;
1127        };
1128        // If this isn't a param, then we can't substitute a new closure.
1129        let ty::Param(closure_param) = input_arg.kind() else { return };
1130
1131        // Get the arguments for the found method, only specifying that `Self` is the receiver type.
1132        let Some(possible_rcvr_ty) = typeck_results.node_type_opt(rcvr.hir_id) else { return };
1133        let args = GenericArgs::for_item(tcx, method_def_id, |param, _| {
1134            if let ty::GenericParamDefKind::Lifetime = param.kind {
1135                tcx.lifetimes.re_erased.into()
1136            } else if param.index == 0 && param.name == kw::SelfUpper {
1137                possible_rcvr_ty.into()
1138            } else if param.index == closure_param.index {
1139                closure_ty.into()
1140            } else {
1141                self.infcx.var_for_def(parent_expr.span, param)
1142            }
1143        });
1144
1145        let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args);
1146
1147        let ocx = ObligationCtxt::new(&self.infcx);
1148        ocx.register_obligations(preds.iter().map(|(pred, span)| {
1149            trace!(?pred);
1150            Obligation::misc(tcx, span, self.mir_def_id(), self.infcx.param_env, pred)
1151        }));
1152
1153        if ocx.evaluate_obligations_error_on_ambiguity().is_empty() && count > 0 {
1154            diag.span_suggestion_verbose(
1155                tcx.hir_body(*body).value.peel_blocks().span.shrink_to_lo(),
1156                fluent::borrowck_dereference_suggestion,
1157                "*".repeat(count),
1158                Applicability::MachineApplicable,
1159            );
1160        }
1161    }
1162
1163    #[allow(rustc::diagnostic_outside_of_impl)]
1164    fn suggest_move_on_borrowing_closure(&self, diag: &mut Diag<'_>) {
1165        let body = self.infcx.tcx.hir_body_owned_by(self.mir_def_id());
1166        let expr = &body.value.peel_blocks();
1167        let mut closure_span = None::<rustc_span::Span>;
1168        match expr.kind {
1169            hir::ExprKind::MethodCall(.., args, _) => {
1170                for arg in args {
1171                    if let hir::ExprKind::Closure(hir::Closure {
1172                        capture_clause: hir::CaptureBy::Ref,
1173                        ..
1174                    }) = arg.kind
1175                    {
1176                        closure_span = Some(arg.span.shrink_to_lo());
1177                        break;
1178                    }
1179                }
1180            }
1181            hir::ExprKind::Closure(hir::Closure {
1182                capture_clause: hir::CaptureBy::Ref,
1183                kind,
1184                ..
1185            }) => {
1186                if !matches!(
1187                    kind,
1188                    hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1189                        hir::CoroutineDesugaring::Async,
1190                        _
1191                    ),)
1192                ) {
1193                    closure_span = Some(expr.span.shrink_to_lo());
1194                }
1195            }
1196            _ => {}
1197        }
1198        if let Some(closure_span) = closure_span {
1199            diag.span_suggestion_verbose(
1200                closure_span,
1201                fluent::borrowck_move_closure_suggestion,
1202                "move ",
1203                Applicability::MaybeIncorrect,
1204            );
1205        }
1206    }
1207}