@@ -628,11 +628,26 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
628628 return ;
629629 } ;
630630 let def_id = instance. def_id ( ) ;
631- let parent = tcx. parent ( def_id) ;
632- let hir:: def:: DefKind :: Impl { .. } = tcx. def_kind ( parent) else {
633- return ;
634- } ;
635- let ty = tcx. type_of ( parent) . instantiate ( tcx, instance. args ) ;
631+ let mut parent = tcx. parent ( def_id) ;
632+ match tcx. def_kind ( parent) {
633+ hir:: def:: DefKind :: Impl { .. } => { }
634+ hir:: def:: DefKind :: Trait => {
635+ let Some ( ty) = args. get ( 0 ) . and_then ( |arg| arg. as_type ( ) ) else {
636+ return ;
637+ } ;
638+ let mut impls = vec ! [ ] ;
639+ tcx. for_each_relevant_impl ( parent, ty, |id| { impls. push ( id) ; } ) ;
640+ if let [ def_id] = impls[ ..] {
641+ // The method we have is on the trait, but for `parent` we want to analyze the
642+ // relevant impl instead.
643+ parent = def_id;
644+ } else {
645+ return ;
646+ } ;
647+ }
648+ _ => return ,
649+ }
650+ let ty = tcx. type_of ( parent) . instantiate_identity ( ) ;
636651 if self . to_error_region ( outlived_fr) != Some ( tcx. lifetimes . re_static ) {
637652 return ;
638653 }
@@ -713,9 +728,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
713728 ..
714729 } ) ,
715730 ..
716- } ) ) = tcx. hir ( ) . get_if_local ( parent)
717- && let Some ( hir:: Node :: ImplItem ( hir:: ImplItem { .. } ) ) = tcx. hir ( ) . get_if_local ( def_id)
718- {
731+ } ) ) = tcx. hir ( ) . get_if_local ( parent) {
719732 let suggestion = match lt. res {
720733 hir:: LifetimeName :: ImplicitObjectLifetimeDefault if predicates. is_empty ( ) => {
721734 // `impl dyn Trait {}`
0 commit comments