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