@@ -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 ) ;
@@ -390,60 +390,54 @@ pub fn suggest_new_region_bound(
390390}
391391
392392impl < ' a , ' tcx > NiceRegionError < ' a , ' tcx > {
393- fn get_impl_ident_and_self_ty_from_trait (
394- & self ,
393+ pub fn get_impl_ident_and_self_ty_from_trait (
394+ tcx : TyCtxt < ' tcx > ,
395395 def_id : DefId ,
396396 trait_objects : & FxIndexSet < DefId > ,
397397 ) -> Option < ( Ident , & ' tcx hir:: Ty < ' tcx > ) > {
398- let tcx = self . tcx ( ) ;
399- match tcx. hir ( ) . get_if_local ( def_id) {
400- Some ( Node :: ImplItem ( impl_item) ) => {
401- 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)
402405 {
403- Some ( Node :: Item ( Item {
404- kind : ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) ,
405- ..
406- } ) ) => Some ( ( impl_item. ident , self_ty) ) ,
407- _ => None ,
406+ Some ( ( impl_item. ident , self_ty) )
407+ } else {
408+ None
408409 }
409410 }
410- Some ( Node :: TraitItem ( trait_item) ) => {
411- let trait_did = tcx. hir ( ) . get_parent_item ( trait_item. hir_id ( ) ) ;
412- match tcx. hir ( ) . find_by_def_id ( trait_did. def_id ) {
413- Some ( Node :: Item ( Item { kind : ItemKind :: 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_did. to_def_id ( ) ;
418- match tcx. hir ( ) . trait_impls ( trait_did) . iter ( ) . find_map ( |& impl_did| {
419- match tcx. hir ( ) . get_if_local ( impl_did. to_def_id ( ) ) {
420- Some ( Node :: Item ( Item {
421- kind : ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) ,
422- ..
423- } ) ) if 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 ( self_ty)
437- }
438- _ => None ,
439- }
440- } ) {
441- Some ( self_ty) => Some ( ( trait_item. ident , self_ty) ) ,
442- _ => None ,
443- }
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
444439 }
445- _ => None ,
446- }
440+ } )
447441 }
448442 _ => None ,
449443 }
@@ -474,7 +468,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
474468
475469 // Get the `Ident` of the method being called and the corresponding `impl` (to point at
476470 // `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
477- 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 {
478472 return false ;
479473 } ;
480474
0 commit comments