@@ -45,13 +45,31 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
45
45
let mut err = struct_span_err ! (
46
46
tcx. sess,
47
47
cause. span,
48
- E0759 ,
49
- "cannot infer an appropriate lifetime"
48
+ E0767 ,
49
+ "{} has {} but calling `{}` introduces an implicit `'static` lifetime \
50
+ requirement",
51
+ param
52
+ . param
53
+ . pat
54
+ . simple_ident( )
55
+ . map( |s| format!( "`{}`" , s) )
56
+ . unwrap_or_else( || "`fn` parameter" . to_string( ) ) ,
57
+ lifetime,
58
+ assoc. ident,
50
59
) ;
51
60
err. span_label ( param. param_ty_span , & format ! ( "this data with {}..." , lifetime) ) ;
52
61
err. span_label (
53
62
cause. span ,
54
- "...is captured and required to live as long as `'static` here" ,
63
+ & format ! (
64
+ "...is captured and required to live as long as `'static` here \
65
+ because of an implicit lifetime bound on the {}",
66
+ match assoc. container {
67
+ AssocItemContainer :: TraitContainer ( id) =>
68
+ format!( "`impl` of `{}`" , tcx. def_path_str( id) ) ,
69
+ AssocItemContainer :: ImplContainer ( _) =>
70
+ "inherent `impl`" . to_string( ) ,
71
+ } ,
72
+ ) ,
55
73
) ;
56
74
if self . find_impl_on_dyn_trait ( & mut err, param. param_ty , assoc) {
57
75
err. emit ( ) ;
@@ -78,10 +96,49 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
78
96
} else {
79
97
( "'_" . to_owned ( ) , "an anonymous lifetime `'_`" . to_string ( ) )
80
98
} ;
81
- let mut err = struct_span_err ! ( tcx. sess, sp, E0759 , "cannot infer an appropriate lifetime" ) ;
99
+ let param_name = param
100
+ . param
101
+ . pat
102
+ . simple_ident ( )
103
+ . map ( |s| format ! ( "`{}`" , s) )
104
+ . unwrap_or_else ( || "`fn` parameter" . to_string ( ) ) ;
105
+ let mut err = struct_span_err ! (
106
+ tcx. sess,
107
+ sp,
108
+ E0759 ,
109
+ "{} has {} but it needs to satisfy a `'static` lifetime requirement" ,
110
+ param_name,
111
+ lifetime,
112
+ ) ;
82
113
err. span_label ( param. param_ty_span , & format ! ( "this data with {}..." , lifetime) ) ;
83
114
debug ! ( "try_report_static_impl_trait: param_info={:?}" , param) ;
84
115
116
+ let fn_returns = tcx. return_type_impl_or_dyn_traits ( anon_reg_sup. def_id ) ;
117
+
118
+ let mut postfix = String :: new ( ) ;
119
+ if let SubregionOrigin :: Subtype ( box TypeTrace { cause, .. } ) = & sup_origin {
120
+ if let ObligationCauseCode :: UnifyReceiver ( assoc) = & cause. code {
121
+ if self . find_impl_on_dyn_trait ( & mut err, param. param_ty , assoc)
122
+ && fn_returns. is_empty ( )
123
+ {
124
+ err. code ( rustc_errors:: error_code!( E0767 ) ) ;
125
+ err. set_primary_message ( & format ! (
126
+ "{} has {} but calling `{}` introduces an implicit `'static` lifetime \
127
+ requirement",
128
+ param_name, lifetime, assoc. ident,
129
+ ) ) ;
130
+ postfix = format ! (
131
+ " because of an implicit lifetime on the {}" ,
132
+ match assoc. container {
133
+ AssocItemContainer :: TraitContainer ( id) =>
134
+ format!( "`impl` of `{}`" , tcx. def_path_str( id) ) ,
135
+ AssocItemContainer :: ImplContainer ( _) => "inherent `impl`" . to_string( ) ,
136
+ } ,
137
+ ) ;
138
+ }
139
+ }
140
+ }
141
+
85
142
// We try to make the output have fewer overlapping spans if possible.
86
143
if ( sp == sup_origin. span ( ) || !return_sp. overlaps ( sup_origin. span ( ) ) )
87
144
&& sup_origin. span ( ) != return_sp
@@ -108,36 +165,35 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
108
165
// | ---- ^
109
166
err. span_label (
110
167
sup_origin. span ( ) ,
111
- "...is captured here, requiring it to live as long as `'static`" ,
168
+ & format ! (
169
+ "...is captured here, requiring it to live as long as `'static`{}" ,
170
+ postfix
171
+ ) ,
112
172
) ;
113
173
} else {
114
174
err. span_label ( sup_origin. span ( ) , "...is captured here..." ) ;
115
175
if return_sp < sup_origin. span ( ) {
116
176
err. span_note (
117
177
return_sp,
118
- "...and is required to live as long as `'static` here" ,
178
+ & format ! ( "...and is required to live as long as `'static` here{}" , postfix ) ,
119
179
) ;
120
180
} else {
121
181
err. span_label (
122
182
return_sp,
123
- "...and is required to live as long as `'static` here" ,
183
+ & format ! ( "...and is required to live as long as `'static` here{}" , postfix ) ,
124
184
) ;
125
185
}
126
186
}
127
187
} else {
128
188
err. span_label (
129
189
return_sp,
130
- "...is captured and required to live as long as `'static` here" ,
190
+ & format ! (
191
+ "...is captured and required to live as long as `'static` here{}" ,
192
+ postfix
193
+ ) ,
131
194
) ;
132
195
}
133
196
134
- if let SubregionOrigin :: Subtype ( box TypeTrace { cause, .. } ) = & sup_origin {
135
- if let ObligationCauseCode :: UnifyReceiver ( assoc) = & cause. code {
136
- self . find_impl_on_dyn_trait ( & mut err, param. param_ty , assoc) ;
137
- }
138
- }
139
-
140
- let fn_returns = tcx. return_type_impl_or_dyn_traits ( anon_reg_sup. def_id ) ;
141
197
debug ! ( "try_report_static_impl_trait: fn_return={:?}" , fn_returns) ;
142
198
// FIXME: account for the need of parens in `&(dyn Trait + '_)`
143
199
let consider = "consider changing the" ;
@@ -295,20 +351,17 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
295
351
let mut multi_span: MultiSpan = vec ! [ * span] . into ( ) ;
296
352
multi_span. push_span_label (
297
353
* span,
298
- "this trait object has an implicit `'static` lifetime requirement"
299
- . to_string ( ) ,
354
+ "this has an implicit `'static` lifetime requirement" . to_string ( ) ,
300
355
) ;
301
356
multi_span. push_span_label (
302
357
assoc. ident . span ,
303
- "the `'static` requirement is introduced when calling this method"
358
+ "`'static` requirement is introduced when calling this method"
304
359
. to_string ( ) ,
305
360
) ;
306
361
err. span_note (
307
362
multi_span,
308
363
& format ! (
309
- "when using method `{}` on `{}`, an implicit `'static` \
310
- requirement is introduced",
311
- assoc. ident,
364
+ "`{}`'s inherent `impl` has a `'static` requirement" ,
312
365
tcx. def_path_str( * found_did) ,
313
366
) ,
314
367
) ;
@@ -363,22 +416,18 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
363
416
let mut multi_span: MultiSpan = vec ! [ * span] . into ( ) ;
364
417
multi_span. push_span_label (
365
418
* span,
366
- "this trait object has an implicit `'static` lifetime requirement"
367
- . to_string ( ) ,
419
+ "this has an implicit `'static` lifetime requirement" . to_string ( ) ,
368
420
) ;
369
421
multi_span. push_span_label (
370
422
method. span ,
371
- "the `'static` requirement is introduced when calling this method"
372
- . to_string ( ) ,
423
+ "`'static` requirement is introduced when calling this method" . to_string ( ) ,
373
424
) ;
374
425
err. span_note (
375
426
multi_span,
376
427
& format ! (
377
- "when using method `{}` of trait `{}` on `{}`, an implicit `'static` \
378
- requirement is introduced",
379
- method,
380
- tcx. def_path_str( container_id) ,
428
+ "`{}`'s `impl` of `{}` has an implicit `'static` requirement" ,
381
429
tcx. def_path_str( * found_did) ,
430
+ tcx. def_path_str( container_id) ,
382
431
) ,
383
432
) ;
384
433
err. span_suggestion_verbose (
0 commit comments