@@ -12,6 +12,7 @@ use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorGuaranteed};
12
12
use rustc_hir as hir;
13
13
use rustc_hir:: { GenericParamKind , Ty } ;
14
14
use rustc_middle:: ty:: Region ;
15
+ use rustc_span:: symbol:: kw;
15
16
16
17
impl < ' a , ' tcx > NiceRegionError < ' a , ' tcx > {
17
18
/// Print the error message for lifetime errors when both the concerned regions are anonymous.
@@ -169,7 +170,7 @@ pub fn suggest_adding_lifetime_params<'tcx>(
169
170
return false ;
170
171
} ;
171
172
172
- if !lifetime_sub. name . is_elided ( ) || !lifetime_sup. name . is_elided ( ) {
173
+ if !lifetime_sub. name . is_anonymous ( ) || !lifetime_sup. name . is_anonymous ( ) {
173
174
return false ;
174
175
} ;
175
176
@@ -188,32 +189,37 @@ pub fn suggest_adding_lifetime_params<'tcx>(
188
189
_ => return false ,
189
190
} ;
190
191
191
- let ( suggestion_param_name, introduce_new ) = generics
192
+ let suggestion_param_name = generics
192
193
. params
193
194
. iter ( )
194
- . find ( |p| matches ! ( p. kind, GenericParamKind :: Lifetime { .. } ) )
195
- . and_then ( |p| tcx. sess . source_map ( ) . span_to_snippet ( p. span ) . ok ( ) )
196
- . map ( |name| ( name, false ) )
197
- . unwrap_or_else ( || ( "'a" . to_string ( ) , true ) ) ;
198
-
199
- let mut suggestions = vec ! [
200
- if let hir:: LifetimeName :: Underscore = lifetime_sub. name {
201
- ( lifetime_sub. span, suggestion_param_name. clone( ) )
195
+ . filter ( |p| matches ! ( p. kind, GenericParamKind :: Lifetime { .. } ) )
196
+ . map ( |p| p. name . ident ( ) . name )
197
+ . find ( |i| * i != kw:: UnderscoreLifetime ) ;
198
+ let introduce_new = suggestion_param_name. is_none ( ) ;
199
+ let suggestion_param_name =
200
+ suggestion_param_name. map ( |n| n. to_string ( ) ) . unwrap_or_else ( || "'a" . to_owned ( ) ) ;
201
+
202
+ debug ! ( ?lifetime_sup. span) ;
203
+ debug ! ( ?lifetime_sub. span) ;
204
+ let make_suggestion = |span : rustc_span:: Span | {
205
+ if span. is_empty ( ) {
206
+ ( span, format ! ( "{}, " , suggestion_param_name) )
207
+ } else if let Ok ( "&" ) = tcx. sess . source_map ( ) . span_to_snippet ( span) . as_deref ( ) {
208
+ ( span. shrink_to_hi ( ) , format ! ( "{} " , suggestion_param_name) )
202
209
} else {
203
- ( lifetime_sub. span. shrink_to_hi( ) , suggestion_param_name. clone( ) + " " )
204
- } ,
205
- if let hir:: LifetimeName :: Underscore = lifetime_sup. name {
206
- ( lifetime_sup. span, suggestion_param_name. clone( ) )
207
- } else {
208
- ( lifetime_sup. span. shrink_to_hi( ) , suggestion_param_name. clone( ) + " " )
209
- } ,
210
- ] ;
210
+ ( span, suggestion_param_name. clone ( ) )
211
+ }
212
+ } ;
213
+ let mut suggestions =
214
+ vec ! [ make_suggestion( lifetime_sub. span) , make_suggestion( lifetime_sup. span) ] ;
211
215
212
216
if introduce_new {
213
- let new_param_suggestion = match & generics. params {
214
- [ ] => ( generics. span , format ! ( "<{}>" , suggestion_param_name) ) ,
215
- [ first, ..] => ( first. span . shrink_to_lo ( ) , format ! ( "{}, " , suggestion_param_name) ) ,
216
- } ;
217
+ let new_param_suggestion =
218
+ if let Some ( first) = generics. params . iter ( ) . find ( |p| !p. name . ident ( ) . span . is_empty ( ) ) {
219
+ ( first. span . shrink_to_lo ( ) , format ! ( "{}, " , suggestion_param_name) )
220
+ } else {
221
+ ( generics. span , format ! ( "<{}>" , suggestion_param_name) )
222
+ } ;
217
223
218
224
suggestions. push ( new_param_suggestion) ;
219
225
}
0 commit comments