@@ -239,7 +239,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
239239 let mut v = TraitObjectVisitor ( FxIndexSet :: default ( ) ) ;
240240 v. visit_ty ( param. param_ty ) ;
241241 if let Some ( ( ident, self_ty) ) =
242- self . get_impl_ident_and_self_ty_from_trait ( item_def_id, & v. 0 )
242+ NiceRegionError :: get_impl_ident_and_self_ty_from_trait ( tcx , item_def_id, & v. 0 )
243243 && self . suggest_constrain_dyn_trait_in_impl ( & mut err, & v. 0 , ident, self_ty)
244244 {
245245 override_error_code = Some ( ident. name ) ;
@@ -309,19 +309,12 @@ pub fn suggest_new_region_bound(
309309 let did = item_id. owner_id . to_def_id ( ) ;
310310 let ty = tcx. mk_opaque ( did, ty:: InternalSubsts :: identity_for_item ( tcx, did) ) ;
311311
312- if let Some ( span) = opaque
313- . bounds
314- . iter ( )
315- . filter_map ( |arg| match arg {
316- GenericBound :: Outlives ( Lifetime {
317- res : LifetimeName :: Static ,
318- ident,
319- ..
320- } ) => Some ( ident. span ) ,
321- _ => None ,
322- } )
323- . next ( )
324- {
312+ if let Some ( span) = opaque. bounds . iter ( ) . find_map ( |arg| match arg {
313+ GenericBound :: Outlives ( Lifetime {
314+ res : LifetimeName :: Static , ident, ..
315+ } ) => Some ( ident. span ) ,
316+ _ => None ,
317+ } ) {
325318 if let Some ( explicit_static) = & explicit_static {
326319 err. span_suggestion_verbose (
327320 span,
@@ -338,20 +331,14 @@ pub fn suggest_new_region_bound(
338331 Applicability :: MaybeIncorrect ,
339332 ) ;
340333 }
341- } else if opaque
342- . bounds
343- . iter ( )
344- . filter_map ( |arg| match arg {
345- GenericBound :: Outlives ( Lifetime { ident, .. } )
346- if ident. name . to_string ( ) == lifetime_name =>
347- {
348- Some ( ident. span )
349- }
350- _ => None ,
351- } )
352- . next ( )
353- . is_some ( )
354- {
334+ } else if opaque. bounds . iter ( ) . any ( |arg| match arg {
335+ GenericBound :: Outlives ( Lifetime { ident, .. } )
336+ if ident. name . to_string ( ) == lifetime_name =>
337+ {
338+ true
339+ }
340+ _ => false ,
341+ } ) {
355342 } else {
356343 err. span_suggestion_verbose (
357344 fn_return. span . shrink_to_hi ( ) ,
@@ -403,66 +390,54 @@ pub fn suggest_new_region_bound(
403390}
404391
405392impl < ' a , ' tcx > NiceRegionError < ' a , ' tcx > {
406- fn get_impl_ident_and_self_ty_from_trait (
407- & self ,
393+ pub fn get_impl_ident_and_self_ty_from_trait (
394+ tcx : TyCtxt < ' tcx > ,
408395 def_id : DefId ,
409396 trait_objects : & FxIndexSet < DefId > ,
410397 ) -> Option < ( Ident , & ' tcx hir:: Ty < ' tcx > ) > {
411- let tcx = self . tcx ( ) ;
412- match tcx. hir ( ) . get_if_local ( def_id) {
413- Some ( Node :: ImplItem ( impl_item) ) => {
414- match tcx. hir ( ) . find_by_def_id ( tcx. hir ( ) . get_parent_item ( impl_item. hir_id ( ) ) . def_id )
398+ match tcx. hir ( ) . get_if_local ( def_id) ? {
399+ Node :: ImplItem ( impl_item) => {
400+ let impl_did = tcx. hir ( ) . get_parent_item ( impl_item. hir_id ( ) ) ;
401+ if let hir:: OwnerNode :: Item ( Item {
402+ kind : ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) ,
403+ ..
404+ } ) = tcx. hir ( ) . owner ( impl_did)
415405 {
416- Some ( Node :: Item ( Item {
417- kind : ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) ,
418- ..
419- } ) ) => Some ( ( impl_item. ident , self_ty) ) ,
420- _ => None ,
406+ Some ( ( impl_item. ident , self_ty) )
407+ } else {
408+ None
421409 }
422410 }
423- Some ( Node :: TraitItem ( trait_item) ) => {
424- let trait_did = tcx. hir ( ) . get_parent_item ( trait_item. hir_id ( ) ) ;
425- match tcx. hir ( ) . find_by_def_id ( trait_did. def_id ) {
426- Some ( Node :: Item ( Item { kind : ItemKind :: Trait ( ..) , .. } ) ) => {
427- // The method being called is defined in the `trait`, but the `'static`
428- // obligation comes from the `impl`. Find that `impl` so that we can point
429- // at it in the suggestion.
430- let trait_did = trait_did. to_def_id ( ) ;
431- match tcx
432- . hir ( )
433- . trait_impls ( trait_did)
434- . iter ( )
435- . filter_map ( |& impl_did| {
436- match tcx. hir ( ) . get_if_local ( impl_did. to_def_id ( ) ) {
437- Some ( Node :: Item ( Item {
438- kind : ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) ,
439- ..
440- } ) ) if trait_objects. iter ( ) . all ( |did| {
441- // FIXME: we should check `self_ty` against the receiver
442- // type in the `UnifyReceiver` context, but for now, use
443- // this imperfect proxy. This will fail if there are
444- // multiple `impl`s for the same trait like
445- // `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
446- // In that case, only the first one will get suggestions.
447- let mut traits = vec ! [ ] ;
448- let mut hir_v = HirTraitObjectVisitor ( & mut traits, * did) ;
449- hir_v. visit_ty ( self_ty) ;
450- !traits. is_empty ( )
451- } ) =>
452- {
453- Some ( self_ty)
454- }
455- _ => None ,
456- }
457- } )
458- . next ( )
459- {
460- Some ( self_ty) => Some ( ( trait_item. ident , self_ty) ) ,
461- _ => None ,
462- }
411+ Node :: TraitItem ( trait_item) => {
412+ let trait_id = tcx. hir ( ) . get_parent_item ( trait_item. hir_id ( ) ) ;
413+ debug_assert_eq ! ( tcx. def_kind( trait_id. def_id) , hir:: def:: DefKind :: Trait ) ;
414+ // The method being called is defined in the `trait`, but the `'static`
415+ // obligation comes from the `impl`. Find that `impl` so that we can point
416+ // at it in the suggestion.
417+ let trait_did = trait_id. to_def_id ( ) ;
418+ tcx. hir ( ) . trait_impls ( trait_did) . iter ( ) . find_map ( |& impl_did| {
419+ if let Node :: Item ( Item {
420+ kind : ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) ,
421+ ..
422+ } ) = tcx. hir ( ) . find_by_def_id ( impl_did) ?
423+ && trait_objects. iter ( ) . all ( |did| {
424+ // FIXME: we should check `self_ty` against the receiver
425+ // type in the `UnifyReceiver` context, but for now, use
426+ // this imperfect proxy. This will fail if there are
427+ // multiple `impl`s for the same trait like
428+ // `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
429+ // In that case, only the first one will get suggestions.
430+ let mut traits = vec ! [ ] ;
431+ let mut hir_v = HirTraitObjectVisitor ( & mut traits, * did) ;
432+ hir_v. visit_ty ( self_ty) ;
433+ !traits. is_empty ( )
434+ } )
435+ {
436+ Some ( ( trait_item. ident , * self_ty) )
437+ } else {
438+ None
463439 }
464- _ => None ,
465- }
440+ } )
466441 }
467442 _ => None ,
468443 }
@@ -493,7 +468,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
493468
494469 // Get the `Ident` of the method being called and the corresponding `impl` (to point at
495470 // `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
496- let Some ( ( ident, self_ty) ) = self . get_impl_ident_and_self_ty_from_trait ( instance. def_id ( ) , & v. 0 ) else {
471+ let Some ( ( ident, self_ty) ) = NiceRegionError :: get_impl_ident_and_self_ty_from_trait ( tcx , instance. def_id ( ) , & v. 0 ) else {
497472 return false ;
498473 } ;
499474
0 commit comments