@@ -2235,21 +2235,46 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
22352235        } ; 
22362236
22372237        let  mut  err = report_missing_lifetime_specifiers ( self . tcx . sess ,  span,  lifetime_refs. len ( ) ) ; 
2238+         let  mut  add_label = true ; 
22382239
22392240        if  let  Some ( params)  = error { 
22402241            if  lifetime_refs. len ( )  == 1  { 
2241-                 self . report_elision_failure ( & mut  err,  params) ; 
2242+                 add_label = add_label &&  self . report_elision_failure ( & mut  err,  params,  span ) ; 
22422243            } 
22432244        } 
2245+         if  add_label { 
2246+             add_missing_lifetime_specifiers_label ( & mut  err,  span,  lifetime_refs. len ( ) ) ; 
2247+         } 
22442248
22452249        err. emit ( ) ; 
22462250    } 
22472251
2252+     fn  suggest_lifetime ( & self ,  db :  & mut  DiagnosticBuilder < ' _ > ,  span :  Span ,  msg :  & str )  -> bool  { 
2253+         match  self . tcx . sess . source_map ( ) . span_to_snippet ( span)  { 
2254+             Ok ( ref  snippet)  => { 
2255+                 let  ( sugg,  applicability)  = if  snippet == "&"  { 
2256+                     ( "&'static " . to_owned ( ) ,  Applicability :: MachineApplicable ) 
2257+                 }  else  if  snippet == "'_"  { 
2258+                     ( "'static" . to_owned ( ) ,  Applicability :: MachineApplicable ) 
2259+                 }  else  { 
2260+                     ( format ! ( "{} + 'static" ,  snippet) ,  Applicability :: MaybeIncorrect ) 
2261+                 } ; 
2262+                 db. span_suggestion_with_applicability ( span,  msg,  sugg,  applicability) ; 
2263+                 false 
2264+             } 
2265+             Err ( _)  => { 
2266+                 db. help ( msg) ; 
2267+                 true 
2268+             } 
2269+         } 
2270+     } 
2271+ 
22482272    fn  report_elision_failure ( 
22492273        & mut  self , 
22502274        db :  & mut  DiagnosticBuilder < ' _ > , 
22512275        params :  & [ ElisionFailureInfo ] , 
2252-     )  { 
2276+         span :  Span , 
2277+     )  -> bool  { 
22532278        let  mut  m = String :: new ( ) ; 
22542279        let  len = params. len ( ) ; 
22552280
@@ -2304,33 +2329,32 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
23042329                "this function's return type contains a borrowed value, but \  
23052330                  there is no value for it to be borrowed from"
23062331            ) ; 
2307-             help ! ( db,  "consider giving it a 'static lifetime" ) ; 
2332+             self . suggest_lifetime ( db,  span ,   "consider giving it a 'static lifetime" ) 
23082333        }  else  if  elided_len == 0  { 
23092334            help ! ( 
23102335                db, 
23112336                "this function's return type contains a borrowed value with \  
23122337                  an elided lifetime, but the lifetime cannot be derived from \ 
23132338                  the arguments"
23142339            ) ; 
2315-             help ! ( 
2316-                 db, 
2317-                 "consider giving it an explicit bounded or 'static \  
2318-                   lifetime"
2319-             ) ; 
2340+             let  msg = "consider giving it an explicit bounded or 'static lifetime" ; 
2341+             self . suggest_lifetime ( db,  span,  msg) 
23202342        }  else  if  elided_len == 1  { 
23212343            help ! ( 
23222344                db, 
23232345                "this function's return type contains a borrowed value, but \  
23242346                  the signature does not say which {} it is borrowed from", 
23252347                m
23262348            ) ; 
2349+             true 
23272350        }  else  { 
23282351            help ! ( 
23292352                db, 
23302353                "this function's return type contains a borrowed value, but \  
23312354                  the signature does not say whether it is borrowed from {}", 
23322355                m
23332356            ) ; 
2357+             true 
23342358        } 
23352359    } 
23362360
@@ -2744,26 +2768,28 @@ fn insert_late_bound_lifetimes(
27442768    } 
27452769} 
27462770
2747- pub   fn  report_missing_lifetime_specifiers ( 
2771+ fn  report_missing_lifetime_specifiers ( 
27482772    sess :  & Session , 
27492773    span :  Span , 
27502774    count :  usize , 
27512775)  -> DiagnosticBuilder < ' _ >  { 
2752-     let   mut  err =  struct_span_err ! ( 
2776+     struct_span_err ! ( 
27532777        sess, 
27542778        span, 
27552779        E0106 , 
27562780        "missing lifetime specifier{}" , 
27572781        if  count > 1  {  "s"  }  else {  ""  } 
2758-     ) ; 
2782+     ) 
2783+ } 
27592784
2760-     let  msg:  Cow < ' static ,  str >  = if  count > 1  { 
2761-         format ! ( "expected {} lifetime parameters" ,  count) . into ( ) 
2785+ fn  add_missing_lifetime_specifiers_label ( 
2786+     err :  & mut  DiagnosticBuilder < ' _ > , 
2787+     span :  Span , 
2788+     count :  usize , 
2789+ )  { 
2790+     if  count > 1  { 
2791+         err. span_label ( span,  format ! ( "expected {} lifetime parameters" ,  count) ) ; 
27622792    }  else  { 
2763-         "expected lifetime parameter" . into ( ) 
2793+         err . span_label ( span ,   "expected lifetime parameter" ) ; 
27642794    } ; 
2765- 
2766-     err. span_label ( span,  msg) ; 
2767- 
2768-     err
27692795} 
0 commit comments