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