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