@@ -23,16 +23,17 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
23
23
pub ( super ) fn try_report_static_impl_trait ( & self ) -> Option < ErrorReported > {
24
24
debug ! ( "try_report_static_impl_trait(error={:?})" , self . error) ;
25
25
let tcx = self . tcx ( ) ;
26
- let ( var_origin, sub_origin, sub_r, sup_origin, sup_r) = match self . error . as_ref ( ) ? {
26
+ let ( var_origin, sub_origin, sub_r, sup_origin, sup_r, spans ) = match self . error . as_ref ( ) ? {
27
27
RegionResolutionError :: SubSupConflict (
28
28
_,
29
29
var_origin,
30
30
sub_origin,
31
31
sub_r,
32
32
sup_origin,
33
33
sup_r,
34
+ spans,
34
35
) if * * sub_r == RegionKind :: ReStatic => {
35
- ( var_origin, sub_origin, sub_r, sup_origin, sup_r)
36
+ ( var_origin, sub_origin, sub_r, sup_origin, sup_r, spans )
36
37
}
37
38
RegionResolutionError :: ConcreteFailure (
38
39
SubregionOrigin :: Subtype ( box TypeTrace { cause, .. } ) ,
@@ -123,15 +124,31 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
123
124
param_name,
124
125
lifetime,
125
126
) ;
126
- err. span_label ( param. param_ty_span , & format ! ( "this data with {}..." , lifetime) ) ;
127
+
128
+ let ( mention_capture, capture_point) = if sup_origin. span ( ) . overlaps ( param. param_ty_span ) {
129
+ // Account for `async fn` like in `async-await/issues/issue-62097.rs`.
130
+ // The desugaring of `async `fn`s causes `sup_origin` and `param` to point at the same
131
+ // place (but with different `ctxt`, hence `overlaps` instead of `==` above).
132
+ //
133
+ // This avoids the following:
134
+ //
135
+ // LL | pub async fn run_dummy_fn(&self) {
136
+ // | ^^^^^
137
+ // | |
138
+ // | this data with an anonymous lifetime `'_`...
139
+ // | ...is captured here...
140
+ ( false , sup_origin. span ( ) )
141
+ } else {
142
+ ( true , param. param_ty_span )
143
+ } ;
144
+ err. span_label ( capture_point, & format ! ( "this data with {}..." , lifetime) ) ;
145
+
127
146
debug ! ( "try_report_static_impl_trait: param_info={:?}" , param) ;
128
147
129
148
// We try to make the output have fewer overlapping spans if possible.
130
149
if ( sp == sup_origin. span ( ) || !return_sp. overlaps ( sup_origin. span ( ) ) )
131
150
&& sup_origin. span ( ) != return_sp
132
151
{
133
- // FIXME: account for `async fn` like in `async-await/issues/issue-62097.rs`
134
-
135
152
// Customize the spans and labels depending on their relative order so
136
153
// that split sentences flow correctly.
137
154
if sup_origin. span ( ) . overlaps ( return_sp) && sp == sup_origin. span ( ) {
@@ -152,29 +169,61 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
152
169
// | ---- ^
153
170
err. span_label (
154
171
sup_origin. span ( ) ,
155
- "...is captured here, requiring it to live as long as `'static`" ,
172
+ & format ! (
173
+ "...is captured here, requiring it to live as long as `'static`{}" ,
174
+ if spans. is_empty( ) { "" } else { "..." } ,
175
+ ) ,
156
176
) ;
157
177
} else {
158
- err . span_label ( sup_origin. span ( ) , "...is captured here..." ) ;
159
- if return_sp < sup_origin. span ( ) {
178
+ if return_sp < sup_origin. span ( ) && mention_capture {
179
+ err . span_label ( sup_origin. span ( ) , "...is captured here..." ) ;
160
180
err. span_note (
161
181
return_sp,
162
182
"...and is required to live as long as `'static` here" ,
163
183
) ;
164
184
} else {
165
185
err. span_label (
166
186
return_sp,
167
- "...and is required to live as long as `'static` here" ,
187
+ & format ! (
188
+ "...is required to live as long as `'static` here{}" ,
189
+ if spans. is_empty( ) { "" } else { "..." } ,
190
+ ) ,
168
191
) ;
192
+ if mention_capture {
193
+ let span = sup_origin. span ( ) ;
194
+ let msg = if spans. iter ( ) . any ( |sp| * sp > span) {
195
+ "...is captured here..."
196
+ } else {
197
+ "...and is captured here"
198
+ } ;
199
+ err. span_label ( span, msg) ;
200
+ }
169
201
}
170
202
}
171
203
} else {
172
204
err. span_label (
173
205
return_sp,
174
- "...is captured and required to live as long as `'static` here" ,
206
+ & format ! (
207
+ "...is captured and required to live as long as `'static` here{}" ,
208
+ if spans. is_empty( ) { "" } else { "..." } ,
209
+ ) ,
175
210
) ;
176
211
}
177
212
213
+ for span in spans {
214
+ let msg =
215
+ format ! ( "...and is captured here{}" , if mention_capture { " too" } else { "" } ) ;
216
+ if span. overlaps ( return_sp) {
217
+ err. span_note ( * span, & msg) ;
218
+ } else {
219
+ err. span_label ( * span, & msg) ;
220
+ }
221
+ }
222
+
223
+ if let SubregionOrigin :: RelateParamBound ( _, _, Some ( bound) ) = sub_origin {
224
+ err. span_note ( * bound, "`'static` lifetime requirement introduced by this trait bound" ) ;
225
+ }
226
+
178
227
let fn_returns = tcx. return_type_impl_or_dyn_traits ( anon_reg_sup. def_id ) ;
179
228
180
229
let mut override_error_code = None ;
0 commit comments