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