@@ -2235,21 +2235,46 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2235
2235
} ;
2236
2236
2237
2237
let mut err = report_missing_lifetime_specifiers ( self . tcx . sess , span, lifetime_refs. len ( ) ) ;
2238
+ let mut add_label = true ;
2238
2239
2239
2240
if let Some ( params) = error {
2240
2241
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 ) ;
2242
2243
}
2243
2244
}
2245
+ if add_label {
2246
+ add_missing_lifetime_specifiers_label ( & mut err, span, lifetime_refs. len ( ) ) ;
2247
+ }
2244
2248
2245
2249
err. emit ( ) ;
2246
2250
}
2247
2251
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
+
2248
2272
fn report_elision_failure (
2249
2273
& mut self ,
2250
2274
db : & mut DiagnosticBuilder < ' _ > ,
2251
2275
params : & [ ElisionFailureInfo ] ,
2252
- ) {
2276
+ span : Span ,
2277
+ ) -> bool {
2253
2278
let mut m = String :: new ( ) ;
2254
2279
let len = params. len ( ) ;
2255
2280
@@ -2304,33 +2329,32 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
2304
2329
"this function's return type contains a borrowed value, but \
2305
2330
there is no value for it to be borrowed from"
2306
2331
) ;
2307
- help ! ( db, "consider giving it a 'static lifetime" ) ;
2332
+ self . suggest_lifetime ( db, span , "consider giving it a 'static lifetime" )
2308
2333
} else if elided_len == 0 {
2309
2334
help ! (
2310
2335
db,
2311
2336
"this function's return type contains a borrowed value with \
2312
2337
an elided lifetime, but the lifetime cannot be derived from \
2313
2338
the arguments"
2314
2339
) ;
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)
2320
2342
} else if elided_len == 1 {
2321
2343
help ! (
2322
2344
db,
2323
2345
"this function's return type contains a borrowed value, but \
2324
2346
the signature does not say which {} it is borrowed from",
2325
2347
m
2326
2348
) ;
2349
+ true
2327
2350
} else {
2328
2351
help ! (
2329
2352
db,
2330
2353
"this function's return type contains a borrowed value, but \
2331
2354
the signature does not say whether it is borrowed from {}",
2332
2355
m
2333
2356
) ;
2357
+ true
2334
2358
}
2335
2359
}
2336
2360
@@ -2744,26 +2768,28 @@ fn insert_late_bound_lifetimes(
2744
2768
}
2745
2769
}
2746
2770
2747
- pub fn report_missing_lifetime_specifiers (
2771
+ fn report_missing_lifetime_specifiers (
2748
2772
sess : & Session ,
2749
2773
span : Span ,
2750
2774
count : usize ,
2751
2775
) -> DiagnosticBuilder < ' _ > {
2752
- let mut err = struct_span_err ! (
2776
+ struct_span_err ! (
2753
2777
sess,
2754
2778
span,
2755
2779
E0106 ,
2756
2780
"missing lifetime specifier{}" ,
2757
2781
if count > 1 { "s" } else { "" }
2758
- ) ;
2782
+ )
2783
+ }
2759
2784
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) ) ;
2762
2792
} else {
2763
- "expected lifetime parameter" . into ( )
2793
+ err . span_label ( span , "expected lifetime parameter" ) ;
2764
2794
} ;
2765
-
2766
- err. span_label ( span, msg) ;
2767
-
2768
- err
2769
2795
}
0 commit comments