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