@@ -166,59 +166,61 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
166166 if lifetime_sub. name . is_elided ( ) && lifetime_sup. name . is_elided ( ) {
167167 if let Some ( anon_reg) = self . tcx ( ) . is_suitable_region ( sub) {
168168 let hir_id = self . tcx ( ) . hir ( ) . local_def_id_to_hir_id ( anon_reg. def_id ) ;
169- if let hir:: Node :: Item ( & hir:: Item {
170- kind : hir:: ItemKind :: Fn ( _, ref generics, ..) ,
171- ..
172- } ) = self . tcx ( ) . hir ( ) . get ( hir_id)
173- {
174- let ( suggestion_param_name, introduce_new) = generics
175- . params
176- . iter ( )
177- . find ( |p| matches ! ( p. kind, GenericParamKind :: Lifetime { .. } ) )
178- . and_then ( |p| self . tcx ( ) . sess . source_map ( ) . span_to_snippet ( p. span ) . ok ( ) )
179- . map ( |name| ( name, false ) )
180- . unwrap_or_else ( || ( "'a" . to_string ( ) , true ) ) ;
181-
182- let mut suggestions = vec ! [
183- if let hir:: LifetimeName :: Underscore = lifetime_sub. name {
184- ( lifetime_sub. span, suggestion_param_name. clone( ) )
185- } else {
186- (
187- lifetime_sub. span. shrink_to_hi( ) ,
188- suggestion_param_name. clone( ) + " " ,
189- )
190- } ,
191- if let hir:: LifetimeName :: Underscore = lifetime_sup. name {
192- ( lifetime_sup. span, suggestion_param_name. clone( ) )
193- } else {
194- (
195- lifetime_sup. span. shrink_to_hi( ) ,
196- suggestion_param_name. clone( ) + " " ,
197- )
198- } ,
199- ] ;
200-
201- if introduce_new {
202- let new_param_suggestion = match & generics. params {
203- [ ] => ( generics. span , format ! ( "<{}>" , suggestion_param_name) ) ,
204- [ first, ..] => (
205- first. span . shrink_to_lo ( ) ,
206- format ! ( "{}, " , suggestion_param_name) ,
207- ) ,
208- } ;
209-
210- suggestions. push ( new_param_suggestion) ;
211- }
212-
213- err. multipart_suggestion (
214- "consider introducing a named lifetime parameter" ,
215- suggestions,
216- Applicability :: MaybeIncorrect ,
217- ) ;
218- err. note (
219- "each elided lifetime in input position becomes a distinct lifetime" ,
220- ) ;
169+
170+ let node = self . tcx ( ) . hir ( ) . get ( hir_id) ;
171+ let is_impl = matches ! ( & node, hir:: Node :: ImplItem ( _) ) ;
172+ let generics = match node {
173+ hir:: Node :: Item ( & hir:: Item {
174+ kind : hir:: ItemKind :: Fn ( _, ref generics, ..) ,
175+ ..
176+ } )
177+ | hir:: Node :: TraitItem ( & hir:: TraitItem { ref generics, .. } )
178+ | hir:: Node :: ImplItem ( & hir:: ImplItem { ref generics, .. } ) => generics,
179+ _ => return ,
180+ } ;
181+
182+ let ( suggestion_param_name, introduce_new) = generics
183+ . params
184+ . iter ( )
185+ . find ( |p| matches ! ( p. kind, GenericParamKind :: Lifetime { .. } ) )
186+ . and_then ( |p| self . tcx ( ) . sess . source_map ( ) . span_to_snippet ( p. span ) . ok ( ) )
187+ . map ( |name| ( name, false ) )
188+ . unwrap_or_else ( || ( "'a" . to_string ( ) , true ) ) ;
189+
190+ let mut suggestions = vec ! [
191+ if let hir:: LifetimeName :: Underscore = lifetime_sub. name {
192+ ( lifetime_sub. span, suggestion_param_name. clone( ) )
193+ } else {
194+ ( lifetime_sub. span. shrink_to_hi( ) , suggestion_param_name. clone( ) + " " )
195+ } ,
196+ if let hir:: LifetimeName :: Underscore = lifetime_sup. name {
197+ ( lifetime_sup. span, suggestion_param_name. clone( ) )
198+ } else {
199+ ( lifetime_sup. span. shrink_to_hi( ) , suggestion_param_name. clone( ) + " " )
200+ } ,
201+ ] ;
202+
203+ if introduce_new {
204+ let new_param_suggestion = match & generics. params {
205+ [ ] => ( generics. span , format ! ( "<{}>" , suggestion_param_name) ) ,
206+ [ first, ..] => {
207+ ( first. span . shrink_to_lo ( ) , format ! ( "{}, " , suggestion_param_name) )
208+ }
209+ } ;
210+
211+ suggestions. push ( new_param_suggestion) ;
212+ }
213+
214+ let mut sugg = String :: from ( "consider introducing a named lifetime parameter" ) ;
215+ if is_impl {
216+ sugg. push_str ( " and update trait if needed" ) ;
221217 }
218+ err. multipart_suggestion (
219+ sugg. as_str ( ) ,
220+ suggestions,
221+ Applicability :: MaybeIncorrect ,
222+ ) ;
223+ err. note ( "each elided lifetime in input position becomes a distinct lifetime" ) ;
222224 }
223225 }
224226 }
0 commit comments