]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Auto merge of #96042 - m-ou-se:one-reentrant-mutex, r=Amanieu
[rust.git] / compiler / rustc_borrowck / src / diagnostics / region_errors.rs
1 //! Error reporting machinery for lifetime errors.
2
3 use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
4 use rustc_infer::infer::{
5     error_reporting::nice_region_error::NiceRegionError,
6     error_reporting::unexpected_hidden_region_diagnostic, NllRegionVariableOrigin,
7 };
8 use rustc_middle::hir::place::PlaceBase;
9 use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
10 use rustc_middle::ty::subst::{InternalSubsts, Subst};
11 use rustc_middle::ty::{self, RegionVid, Ty};
12 use rustc_span::symbol::{kw, sym};
13 use rustc_span::{BytePos, Span};
14
15 use crate::borrowck_errors;
16
17 use super::{OutlivesSuggestionBuilder, RegionName};
18 use crate::region_infer::BlameConstraint;
19 use crate::{
20     nll::ConstraintDescription,
21     region_infer::{values::RegionElement, TypeTest},
22     universal_regions::DefiningTy,
23     MirBorrowckCtxt,
24 };
25
26 impl ConstraintDescription for ConstraintCategory {
27     fn description(&self) -> &'static str {
28         // Must end with a space. Allows for empty names to be provided.
29         match self {
30             ConstraintCategory::Assignment => "assignment ",
31             ConstraintCategory::Return(_) => "returning this value ",
32             ConstraintCategory::Yield => "yielding this value ",
33             ConstraintCategory::UseAsConst => "using this value as a constant ",
34             ConstraintCategory::UseAsStatic => "using this value as a static ",
35             ConstraintCategory::Cast => "cast ",
36             ConstraintCategory::CallArgument => "argument ",
37             ConstraintCategory::TypeAnnotation => "type annotation ",
38             ConstraintCategory::ClosureBounds => "closure body ",
39             ConstraintCategory::SizedBound => "proving this value is `Sized` ",
40             ConstraintCategory::CopyBound => "copying this value ",
41             ConstraintCategory::OpaqueType => "opaque type ",
42             ConstraintCategory::ClosureUpvar(_) => "closure capture ",
43             ConstraintCategory::Usage => "this usage ",
44             ConstraintCategory::Predicate(_)
45             | ConstraintCategory::Boring
46             | ConstraintCategory::BoringNoLocation
47             | ConstraintCategory::Internal => "",
48         }
49     }
50 }
51
52 /// A collection of errors encountered during region inference. This is needed to efficiently
53 /// report errors after borrow checking.
54 ///
55 /// Usually we expect this to either be empty or contain a small number of items, so we can avoid
56 /// allocation most of the time.
57 crate type RegionErrors<'tcx> = Vec<RegionErrorKind<'tcx>>;
58
59 #[derive(Clone, Debug)]
60 crate enum RegionErrorKind<'tcx> {
61     /// A generic bound failure for a type test (`T: 'a`).
62     TypeTestError { type_test: TypeTest<'tcx> },
63
64     /// An unexpected hidden region for an opaque type.
65     UnexpectedHiddenRegion {
66         /// The span for the member constraint.
67         span: Span,
68         /// The hidden type.
69         hidden_ty: Ty<'tcx>,
70         /// The unexpected region.
71         member_region: ty::Region<'tcx>,
72     },
73
74     /// Higher-ranked subtyping error.
75     BoundUniversalRegionError {
76         /// The placeholder free region.
77         longer_fr: RegionVid,
78         /// The region element that erroneously must be outlived by `longer_fr`.
79         error_element: RegionElement,
80         /// The placeholder region.
81         placeholder: ty::PlaceholderRegion,
82     },
83
84     /// Any other lifetime error.
85     RegionError {
86         /// The origin of the region.
87         fr_origin: NllRegionVariableOrigin,
88         /// The region that should outlive `shorter_fr`.
89         longer_fr: RegionVid,
90         /// The region that should be shorter, but we can't prove it.
91         shorter_fr: RegionVid,
92         /// Indicates whether this is a reported error. We currently only report the first error
93         /// encountered and leave the rest unreported so as not to overwhelm the user.
94         is_reported: bool,
95     },
96 }
97
98 /// Information about the various region constraints involved in a borrow checker error.
99 #[derive(Clone, Debug)]
100 pub struct ErrorConstraintInfo {
101     // fr: outlived_fr
102     pub(super) fr: RegionVid,
103     pub(super) fr_is_local: bool,
104     pub(super) outlived_fr: RegionVid,
105     pub(super) outlived_fr_is_local: bool,
106
107     // Category and span for best blame constraint
108     pub(super) category: ConstraintCategory,
109     pub(super) span: Span,
110 }
111
112 impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
113     /// Converts a region inference variable into a `ty::Region` that
114     /// we can use for error reporting. If `r` is universally bound,
115     /// then we use the name that we have on record for it. If `r` is
116     /// existentially bound, then we check its inferred value and try
117     /// to find a good name from that. Returns `None` if we can't find
118     /// one (e.g., this is just some random part of the CFG).
119     pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
120         self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
121     }
122
123     /// Returns the `RegionVid` corresponding to the region returned by
124     /// `to_error_region`.
125     pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
126         if self.regioncx.universal_regions().is_universal_region(r) {
127             Some(r)
128         } else {
129             // We just want something nameable, even if it's not
130             // actually an upper bound.
131             let upper_bound = self.regioncx.approx_universal_upper_bound(r);
132
133             if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
134                 self.to_error_region_vid(upper_bound)
135             } else {
136                 None
137             }
138         }
139     }
140
141     /// Returns `true` if a closure is inferred to be an `FnMut` closure.
142     fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
143         if let Some(ty::ReFree(free_region)) = self.to_error_region(fr).as_deref()
144             && let ty::BoundRegionKind::BrEnv = free_region.bound_region
145             && let DefiningTy::Closure(_, substs) = self.regioncx.universal_regions().defining_ty
146         {
147             return substs.as_closure().kind() == ty::ClosureKind::FnMut;
148         }
149
150         false
151     }
152
153     /// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
154     pub(crate) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
155         // Iterate through all the errors, producing a diagnostic for each one. The diagnostics are
156         // buffered in the `MirBorrowckCtxt`.
157
158         let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
159
160         for nll_error in nll_errors.into_iter() {
161             match nll_error {
162                 RegionErrorKind::TypeTestError { type_test } => {
163                     // Try to convert the lower-bound region into something named we can print for the user.
164                     let lower_bound_region = self.to_error_region(type_test.lower_bound);
165
166                     let type_test_span = type_test.locations.span(&self.body);
167
168                     if let Some(lower_bound_region) = lower_bound_region {
169                         self.buffer_error(self.infcx.construct_generic_bound_failure(
170                             type_test_span,
171                             None,
172                             type_test.generic_kind,
173                             lower_bound_region,
174                         ));
175                     } else {
176                         // FIXME. We should handle this case better. It
177                         // indicates that we have e.g., some region variable
178                         // whose value is like `'a+'b` where `'a` and `'b` are
179                         // distinct unrelated universal regions that are not
180                         // known to outlive one another. It'd be nice to have
181                         // some examples where this arises to decide how best
182                         // to report it; we could probably handle it by
183                         // iterating over the universal regions and reporting
184                         // an error that multiple bounds are required.
185                         self.buffer_error(self.infcx.tcx.sess.struct_span_err(
186                             type_test_span,
187                             &format!("`{}` does not live long enough", type_test.generic_kind),
188                         ));
189                     }
190                 }
191
192                 RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, member_region } => {
193                     let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
194                     let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
195                     self.buffer_error(unexpected_hidden_region_diagnostic(
196                         self.infcx.tcx,
197                         span,
198                         named_ty,
199                         named_region,
200                     ));
201                 }
202
203                 RegionErrorKind::BoundUniversalRegionError {
204                     longer_fr,
205                     placeholder,
206                     error_element,
207                 } => {
208                     let error_vid = self.regioncx.region_from_element(longer_fr, &error_element);
209
210                     // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
211                     let (_, cause) = self.regioncx.find_outlives_blame_span(
212                         &self.body,
213                         longer_fr,
214                         NllRegionVariableOrigin::Placeholder(placeholder),
215                         error_vid,
216                     );
217
218                     let universe = placeholder.universe;
219                     let universe_info = self.regioncx.universe_info(universe);
220
221                     universe_info.report_error(self, placeholder, error_element, cause);
222                 }
223
224                 RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
225                     if is_reported {
226                         self.report_region_error(
227                             longer_fr,
228                             fr_origin,
229                             shorter_fr,
230                             &mut outlives_suggestion,
231                         );
232                     } else {
233                         // We only report the first error, so as not to overwhelm the user. See
234                         // `RegRegionErrorKind` docs.
235                         //
236                         // FIXME: currently we do nothing with these, but perhaps we can do better?
237                         // FIXME: try collecting these constraints on the outlives suggestion
238                         // builder. Does it make the suggestions any better?
239                         debug!(
240                             "Unreported region error: can't prove that {:?}: {:?}",
241                             longer_fr, shorter_fr
242                         );
243                     }
244                 }
245             }
246         }
247
248         // Emit one outlives suggestions for each MIR def we borrowck
249         outlives_suggestion.add_suggestion(self);
250     }
251
252     /// Report an error because the universal region `fr` was required to outlive
253     /// `outlived_fr` but it is not known to do so. For example:
254     ///
255     /// ```
256     /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
257     /// ```
258     ///
259     /// Here we would be invoked with `fr = 'a` and `outlived_fr = `'b`.
260     pub(crate) fn report_region_error(
261         &mut self,
262         fr: RegionVid,
263         fr_origin: NllRegionVariableOrigin,
264         outlived_fr: RegionVid,
265         outlives_suggestion: &mut OutlivesSuggestionBuilder,
266     ) {
267         debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
268
269         let BlameConstraint { category, cause, variance_info, from_closure: _ } =
270             self.regioncx.best_blame_constraint(&self.body, fr, fr_origin, |r| {
271                 self.regioncx.provides_universal_region(r, fr, outlived_fr)
272             });
273
274         debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
275         // Check if we can use one of the "nice region errors".
276         if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
277             let nice = NiceRegionError::new_from_span(self.infcx, cause.span, o, f);
278             if let Some(diag) = nice.try_report_from_nll() {
279                 self.buffer_error(diag);
280                 return;
281             }
282         }
283
284         let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
285             self.regioncx.universal_regions().is_local_free_region(fr),
286             self.regioncx.universal_regions().is_local_free_region(outlived_fr),
287         );
288
289         debug!(
290             "report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
291             fr_is_local, outlived_fr_is_local, category
292         );
293
294         let errci = ErrorConstraintInfo {
295             fr,
296             outlived_fr,
297             fr_is_local,
298             outlived_fr_is_local,
299             category,
300             span: cause.span,
301         };
302
303         let mut diag = match (category, fr_is_local, outlived_fr_is_local) {
304             (ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
305                 self.report_fnmut_error(&errci, kind)
306             }
307             (ConstraintCategory::Assignment, true, false)
308             | (ConstraintCategory::CallArgument, true, false) => {
309                 let mut db = self.report_escaping_data_error(&errci);
310
311                 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
312                 outlives_suggestion.collect_constraint(fr, outlived_fr);
313
314                 db
315             }
316             _ => {
317                 let mut db = self.report_general_error(&errci);
318
319                 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
320                 outlives_suggestion.collect_constraint(fr, outlived_fr);
321
322                 db
323             }
324         };
325
326         match variance_info {
327             ty::VarianceDiagInfo::None => {}
328             ty::VarianceDiagInfo::Invariant { ty, param_index } => {
329                 let (desc, note) = match ty.kind() {
330                     ty::RawPtr(ty_mut) => {
331                         assert_eq!(ty_mut.mutbl, rustc_hir::Mutability::Mut);
332                         (
333                             format!("a mutable pointer to `{}`", ty_mut.ty),
334                             "mutable pointers are invariant over their type parameter".to_string(),
335                         )
336                     }
337                     ty::Ref(_, inner_ty, mutbl) => {
338                         assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
339                         (
340                             format!("a mutable reference to `{inner_ty}`"),
341                             "mutable references are invariant over their type parameter"
342                                 .to_string(),
343                         )
344                     }
345                     ty::Adt(adt, substs) => {
346                         let generic_arg = substs[param_index as usize];
347                         let identity_substs =
348                             InternalSubsts::identity_for_item(self.infcx.tcx, adt.did());
349                         let base_ty = self.infcx.tcx.mk_adt(*adt, identity_substs);
350                         let base_generic_arg = identity_substs[param_index as usize];
351                         let adt_desc = adt.descr();
352
353                         let desc = format!(
354                             "the type `{ty}`, which makes the generic argument `{generic_arg}` invariant"
355                         );
356                         let note = format!(
357                             "the {adt_desc} `{base_ty}` is invariant over the parameter `{base_generic_arg}`"
358                         );
359                         (desc, note)
360                     }
361                     ty::FnDef(def_id, _) => {
362                         let name = self.infcx.tcx.item_name(*def_id);
363                         let identity_substs =
364                             InternalSubsts::identity_for_item(self.infcx.tcx, *def_id);
365                         let desc = format!("a function pointer to `{name}`");
366                         let note = format!(
367                             "the function `{name}` is invariant over the parameter `{}`",
368                             identity_substs[param_index as usize]
369                         );
370                         (desc, note)
371                     }
372                     _ => panic!("Unexpected type {:?}", ty),
373                 };
374                 diag.note(&format!("requirement occurs because of {desc}",));
375                 diag.note(&note);
376                 diag.help("see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance");
377             }
378         }
379
380         self.buffer_error(diag);
381     }
382
383     /// Report a specialized error when `FnMut` closures return a reference to a captured variable.
384     /// This function expects `fr` to be local and `outlived_fr` to not be local.
385     ///
386     /// ```text
387     /// error: captured variable cannot escape `FnMut` closure body
388     ///   --> $DIR/issue-53040.rs:15:8
389     ///    |
390     /// LL |     || &mut v;
391     ///    |     -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
392     ///    |     |
393     ///    |     inferred to be a `FnMut` closure
394     ///    |
395     ///    = note: `FnMut` closures only have access to their captured variables while they are
396     ///            executing...
397     ///    = note: ...therefore, returned references to captured variables will escape the closure
398     /// ```
399     fn report_fnmut_error(
400         &self,
401         errci: &ErrorConstraintInfo,
402         kind: ReturnConstraint,
403     ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
404         let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
405
406         let mut diag = self
407             .infcx
408             .tcx
409             .sess
410             .struct_span_err(*span, "captured variable cannot escape `FnMut` closure body");
411
412         let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
413         if let ty::Opaque(def_id, _) = *output_ty.kind() {
414             output_ty = self.infcx.tcx.type_of(def_id)
415         };
416
417         debug!("report_fnmut_error: output_ty={:?}", output_ty);
418
419         let message = match output_ty.kind() {
420             ty::Closure(_, _) => {
421                 "returns a closure that contains a reference to a captured variable, which then \
422                  escapes the closure body"
423             }
424             ty::Adt(def, _) if self.infcx.tcx.is_diagnostic_item(sym::gen_future, def.did()) => {
425                 "returns an `async` block that contains a reference to a captured variable, which then \
426                  escapes the closure body"
427             }
428             _ => "returns a reference to a captured variable which escapes the closure body",
429         };
430
431         diag.span_label(*span, message);
432
433         if let ReturnConstraint::ClosureUpvar(upvar_field) = kind {
434             let def_id = match self.regioncx.universal_regions().defining_ty {
435                 DefiningTy::Closure(def_id, _) => def_id,
436                 ty => bug!("unexpected DefiningTy {:?}", ty),
437             };
438
439             let captured_place = &self.upvars[upvar_field.index()].place;
440             let defined_hir = match captured_place.place.base {
441                 PlaceBase::Local(hirid) => Some(hirid),
442                 PlaceBase::Upvar(upvar) => Some(upvar.var_path.hir_id),
443                 _ => None,
444             };
445
446             if defined_hir.is_some() {
447                 let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap();
448                 let upvar_def_span = self.infcx.tcx.hir().span(defined_hir.unwrap());
449                 let upvar_span = upvars_map.get(&defined_hir.unwrap()).unwrap().span;
450                 diag.span_label(upvar_def_span, "variable defined here");
451                 diag.span_label(upvar_span, "variable captured here");
452             }
453         }
454
455         if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {
456             diag.span_label(fr_span, "inferred to be a `FnMut` closure");
457         }
458
459         diag.note(
460             "`FnMut` closures only have access to their captured variables while they are \
461              executing...",
462         );
463         diag.note("...therefore, they cannot allow references to captured variables to escape");
464
465         diag
466     }
467
468     /// Reports an error specifically for when data is escaping a closure.
469     ///
470     /// ```text
471     /// error: borrowed data escapes outside of function
472     ///   --> $DIR/lifetime-bound-will-change-warning.rs:44:5
473     ///    |
474     /// LL | fn test2<'a>(x: &'a Box<Fn()+'a>) {
475     ///    |              - `x` is a reference that is only valid in the function body
476     /// LL |     // but ref_obj will not, so warn.
477     /// LL |     ref_obj(x)
478     ///    |     ^^^^^^^^^^ `x` escapes the function body here
479     /// ```
480     fn report_escaping_data_error(
481         &self,
482         errci: &ErrorConstraintInfo,
483     ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
484         let ErrorConstraintInfo { span, category, .. } = errci;
485
486         let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
487             self.infcx.tcx,
488             &self.body,
489             &self.local_names,
490             &self.upvars,
491             errci.fr,
492         );
493         let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
494             self.infcx.tcx,
495             &self.body,
496             &self.local_names,
497             &self.upvars,
498             errci.outlived_fr,
499         );
500
501         let (_, escapes_from) = self
502             .infcx
503             .tcx
504             .article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
505
506         // Revert to the normal error in these cases.
507         // Assignments aren't "escapes" in function items.
508         if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
509             || (*category == ConstraintCategory::Assignment
510                 && self.regioncx.universal_regions().defining_ty.is_fn_def())
511             || self.regioncx.universal_regions().defining_ty.is_const()
512         {
513             return self.report_general_error(&ErrorConstraintInfo {
514                 fr_is_local: true,
515                 outlived_fr_is_local: false,
516                 ..*errci
517             });
518         }
519
520         let mut diag =
521             borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
522
523         if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
524             diag.span_label(
525                 outlived_fr_span,
526                 format!("`{outlived_fr_name}` declared here, outside of the {escapes_from} body",),
527             );
528         }
529
530         if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
531             diag.span_label(
532                 fr_span,
533                 format!(
534                     "`{fr_name}` is a reference that is only valid in the {escapes_from} body",
535                 ),
536             );
537
538             diag.span_label(*span, format!("`{fr_name}` escapes the {escapes_from} body here"));
539         }
540
541         // Only show an extra note if we can find an 'error region' for both of the region
542         // variables. This avoids showing a noisy note that just mentions 'synthetic' regions
543         // that don't help the user understand the error.
544         if self.to_error_region(errci.fr).is_some()
545             && self.to_error_region(errci.outlived_fr).is_some()
546         {
547             let fr_region_name = self.give_region_a_name(errci.fr).unwrap();
548             fr_region_name.highlight_region_name(&mut diag);
549             let outlived_fr_region_name = self.give_region_a_name(errci.outlived_fr).unwrap();
550             outlived_fr_region_name.highlight_region_name(&mut diag);
551
552             diag.span_label(
553                 *span,
554                 format!(
555                     "{}requires that `{}` must outlive `{}`",
556                     category.description(),
557                     fr_region_name,
558                     outlived_fr_region_name,
559                 ),
560             );
561         }
562         diag
563     }
564
565     /// Reports a region inference error for the general case with named/synthesized lifetimes to
566     /// explain what is happening.
567     ///
568     /// ```text
569     /// error: unsatisfied lifetime constraints
570     ///   --> $DIR/regions-creating-enums3.rs:17:5
571     ///    |
572     /// LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
573     ///    |                -- -- lifetime `'b` defined here
574     ///    |                |
575     ///    |                lifetime `'a` defined here
576     /// LL |     ast::add(x, y)
577     ///    |     ^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it
578     ///    |                    is returning data with lifetime `'b`
579     /// ```
580     fn report_general_error(
581         &self,
582         errci: &ErrorConstraintInfo,
583     ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
584         let ErrorConstraintInfo {
585             fr,
586             fr_is_local,
587             outlived_fr,
588             outlived_fr_is_local,
589             span,
590             category,
591             ..
592         } = errci;
593
594         let mut diag =
595             self.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
596
597         let (_, mir_def_name) =
598             self.infcx.tcx.article_and_description(self.mir_def_id().to_def_id());
599
600         let fr_name = self.give_region_a_name(*fr).unwrap();
601         fr_name.highlight_region_name(&mut diag);
602         let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
603         outlived_fr_name.highlight_region_name(&mut diag);
604
605         match (category, outlived_fr_is_local, fr_is_local) {
606             (ConstraintCategory::Return(_), true, _) => {
607                 diag.span_label(
608                     *span,
609                     format!(
610                         "{mir_def_name} was supposed to return data with lifetime `{outlived_fr_name}` but it is returning \
611                          data with lifetime `{fr_name}`",
612                     ),
613                 );
614             }
615             _ => {
616                 diag.span_label(
617                     *span,
618                     format!(
619                         "{}requires that `{}` must outlive `{}`",
620                         category.description(),
621                         fr_name,
622                         outlived_fr_name,
623                     ),
624                 );
625             }
626         }
627
628         self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
629
630         diag
631     }
632
633     /// Adds a suggestion to errors where an `impl Trait` is returned.
634     ///
635     /// ```text
636     /// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
637     ///       a constraint
638     ///    |
639     /// LL |     fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
640     ///    |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
641     /// ```
642     fn add_static_impl_trait_suggestion(
643         &self,
644         diag: &mut Diagnostic,
645         fr: RegionVid,
646         // We need to pass `fr_name` - computing it again will label it twice.
647         fr_name: RegionName,
648         outlived_fr: RegionVid,
649     ) {
650         if let (Some(f), Some(ty::ReStatic)) =
651             (self.to_error_region(fr), self.to_error_region(outlived_fr).as_deref())
652         {
653             if let Some(&ty::Opaque(did, substs)) = self
654                 .infcx
655                 .tcx
656                 .is_suitable_region(f)
657                 .map(|r| r.def_id)
658                 .and_then(|id| self.infcx.tcx.return_type_impl_trait(id))
659                 .map(|(ty, _)| ty.kind())
660             {
661                 // Check whether or not the impl trait return type is intended to capture
662                 // data with the static lifetime.
663                 //
664                 // eg. check for `impl Trait + 'static` instead of `impl Trait`.
665                 let has_static_predicate = {
666                     let bounds = self.infcx.tcx.explicit_item_bounds(did);
667
668                     let mut found = false;
669                     for (bound, _) in bounds {
670                         if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) =
671                             bound.kind().skip_binder()
672                         {
673                             let r = r.subst(self.infcx.tcx, substs);
674                             if r.is_static() {
675                                 found = true;
676                                 break;
677                             } else {
678                                 // If there's already a lifetime bound, don't
679                                 // suggest anything.
680                                 return;
681                             }
682                         }
683                     }
684
685                     found
686                 };
687
688                 debug!(
689                     "add_static_impl_trait_suggestion: has_static_predicate={:?}",
690                     has_static_predicate
691                 );
692                 let static_str = kw::StaticLifetime;
693                 // If there is a static predicate, then the only sensible suggestion is to replace
694                 // fr with `'static`.
695                 if has_static_predicate {
696                     diag.help(&format!("consider replacing `{fr_name}` with `{static_str}`"));
697                 } else {
698                     // Otherwise, we should suggest adding a constraint on the return type.
699                     let span = self.infcx.tcx.def_span(did);
700                     if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
701                         let suggestable_fr_name = if fr_name.was_named() {
702                             fr_name.to_string()
703                         } else {
704                             "'_".to_string()
705                         };
706                         let span = if snippet.ends_with(';') {
707                             // `type X = impl Trait;`
708                             span.with_hi(span.hi() - BytePos(1))
709                         } else {
710                             span
711                         };
712                         let suggestion = format!(" + {suggestable_fr_name}");
713                         let span = span.shrink_to_hi();
714                         diag.span_suggestion(
715                             span,
716                             &format!(
717                                 "to allow this `impl Trait` to capture borrowed data with lifetime \
718                                  `{fr_name}`, add `{suggestable_fr_name}` as a bound",
719                             ),
720                             suggestion,
721                             Applicability::MachineApplicable,
722                         );
723                     }
724                 }
725             }
726         }
727     }
728 }