@@ -634,11 +634,26 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
634
634
return ;
635
635
} ;
636
636
let def_id = instance. def_id ( ) ;
637
- let parent = tcx. parent ( def_id) ;
638
- let hir:: def:: DefKind :: Impl { .. } = tcx. def_kind ( parent) else {
639
- return ;
640
- } ;
641
- let ty = tcx. type_of ( parent) . instantiate ( tcx, instance. args ) ;
637
+ let mut parent = tcx. parent ( def_id) ;
638
+ match tcx. def_kind ( parent) {
639
+ hir:: def:: DefKind :: Impl { .. } => { }
640
+ hir:: def:: DefKind :: Trait => {
641
+ let Some ( ty) = args. get ( 0 ) . and_then ( |arg| arg. as_type ( ) ) else {
642
+ return ;
643
+ } ;
644
+ let mut impls = vec ! [ ] ;
645
+ tcx. for_each_relevant_impl ( parent, ty, |id| { impls. push ( id) ; } ) ;
646
+ if let [ def_id] = impls[ ..] {
647
+ // The method we have is on the trait, but for `parent` we want to analyze the
648
+ // relevant impl instead.
649
+ parent = def_id;
650
+ } else {
651
+ return ;
652
+ } ;
653
+ }
654
+ _ => return ,
655
+ }
656
+ let ty = tcx. type_of ( parent) . instantiate_identity ( ) ;
642
657
if self . to_error_region ( outlived_fr) != Some ( tcx. lifetimes . re_static ) {
643
658
return ;
644
659
}
@@ -719,9 +734,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
719
734
..
720
735
} ) ,
721
736
..
722
- } ) ) = tcx. hir ( ) . get_if_local ( parent)
723
- && let Some ( hir:: Node :: ImplItem ( hir:: ImplItem { .. } ) ) = tcx. hir ( ) . get_if_local ( def_id)
724
- {
737
+ } ) ) = tcx. hir ( ) . get_if_local ( parent) {
725
738
let suggestion = match lt. res {
726
739
hir:: LifetimeName :: ImplicitObjectLifetimeDefault if predicates. is_empty ( ) => {
727
740
// `impl dyn Trait {}`
0 commit comments