@@ -12,6 +12,7 @@ use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorGuaranteed};
1212use rustc_hir as hir;
1313use rustc_hir:: { GenericParamKind , Ty } ;
1414use rustc_middle:: ty:: Region ;
15+ use rustc_span:: symbol:: kw;
1516
1617impl < ' a , ' tcx > NiceRegionError < ' a , ' tcx > {
1718 /// 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>(
169170 return false ;
170171 } ;
171172
172- if !lifetime_sub. name . is_elided ( ) || !lifetime_sup. name . is_elided ( ) {
173+ if !lifetime_sub. name . is_anonymous ( ) || !lifetime_sup. name . is_anonymous ( ) {
173174 return false ;
174175 } ;
175176
@@ -188,32 +189,37 @@ pub fn suggest_adding_lifetime_params<'tcx>(
188189 _ => return false ,
189190 } ;
190191
191- let ( suggestion_param_name, introduce_new ) = generics
192+ let suggestion_param_name = generics
192193 . params
193194 . 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) )
202209 } 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) ] ;
211215
212216 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+ } ;
217223
218224 suggestions. push ( new_param_suggestion) ;
219225 }
0 commit comments