@@ -1587,23 +1587,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1587
1587
self . ty_to_value_string ( rcvr_ty. peel_refs ( ) )
1588
1588
} ;
1589
1589
if let SelfSource :: MethodCall ( _) = source {
1590
- let first_arg = if let Some ( CandidateSource :: Impl ( impl_did) ) = static_candidates. get ( 0 )
1591
- && let Some ( assoc) = self . associated_value ( * impl_did, item_name)
1592
- && assoc. kind == ty:: AssocKind :: Fn
1593
- {
1590
+ let first_arg = static_candidates. get ( 0 ) . and_then ( |candidate_source| {
1591
+ let ( assoc_did, impl_ty) = match candidate_source {
1592
+ CandidateSource :: Impl ( impl_did) => {
1593
+ ( * impl_did, self . tcx . type_of ( * impl_did) . instantiate_identity ( ) )
1594
+ }
1595
+ CandidateSource :: Trait ( trait_did) => ( * trait_did, rcvr_ty) ,
1596
+ } ;
1597
+
1598
+ let assoc = self . associated_value ( assoc_did, item_name) ?;
1599
+ if assoc. kind != ty:: AssocKind :: Fn {
1600
+ return None ;
1601
+ }
1602
+
1603
+ // for CandidateSource::Impl, `Self` will be instantiated to a concrete type
1604
+ // but for CandidateSource::Trait, `Self` is still `Self`
1594
1605
let sig = self . tcx . fn_sig ( assoc. def_id ) . instantiate_identity ( ) ;
1595
1606
sig. inputs ( ) . skip_binder ( ) . get ( 0 ) . and_then ( |first| {
1596
- let impl_ty = self . tcx . type_of ( * impl_did) . instantiate_identity ( ) ;
1597
1607
// if the type of first arg is the same as the current impl type, we should take the first arg into assoc function
1598
- if first. peel_refs ( ) == impl_ty {
1608
+ let first_ty = first. peel_refs ( ) ;
1609
+ if first_ty == impl_ty || first_ty == self . tcx . types . self_param {
1599
1610
Some ( first. ref_mutability ( ) . map_or ( "" , |mutbl| mutbl. ref_prefix_str ( ) ) )
1600
1611
} else {
1601
1612
None
1602
1613
}
1603
1614
} )
1604
- } else {
1605
- None
1606
- } ;
1615
+ } ) ;
1616
+
1607
1617
let mut applicability = Applicability :: MachineApplicable ;
1608
1618
let args = if let SelfSource :: MethodCall ( receiver) = source
1609
1619
&& let Some ( args) = args
0 commit comments