@@ -638,11 +638,28 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
638638 return ;
639639 } ;
640640 let def_id = instance. def_id ( ) ;
641- let parent = tcx. parent ( def_id) ;
642- let hir:: def:: DefKind :: Impl { .. } = tcx. def_kind ( parent) else {
643- return ;
644- } ;
645- let ty = tcx. type_of ( parent) . instantiate ( tcx, instance. args ) ;
641+ let mut parent = tcx. parent ( def_id) ;
642+ match tcx. def_kind ( parent) {
643+ hir:: def:: DefKind :: Impl { .. } => { }
644+ hir:: def:: DefKind :: Trait => {
645+ let Some ( ty) = args. get ( 0 ) . and_then ( |arg| arg. as_type ( ) ) else {
646+ return ;
647+ } ;
648+ let mut impls = vec ! [ ] ;
649+ tcx. for_each_relevant_impl ( parent, ty, |id| {
650+ impls. push ( id) ;
651+ } ) ;
652+ if let [ def_id] = impls[ ..] {
653+ // The method we have is on the trait, but for `parent` we want to analyze the
654+ // relevant impl instead.
655+ parent = def_id;
656+ } else {
657+ return ;
658+ } ;
659+ }
660+ _ => return ,
661+ }
662+ let ty = tcx. type_of ( parent) . instantiate_identity ( ) ;
646663 if self . to_error_region ( outlived_fr) != Some ( tcx. lifetimes . re_static ) {
647664 return ;
648665 }
@@ -724,7 +741,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
724741 } ) ,
725742 ..
726743 } ) ) = tcx. hir ( ) . get_if_local ( parent)
727- && let Some ( hir:: Node :: ImplItem ( hir:: ImplItem { .. } ) ) = tcx. hir ( ) . get_if_local ( def_id)
728744 {
729745 let suggestion = match lt. res {
730746 hir:: LifetimeName :: ImplicitObjectLifetimeDefault if predicates. is_empty ( ) => {
0 commit comments