@@ -581,7 +581,7 @@ fn try_parse_ref_op<'tcx>(
581
581
expr : & ' tcx Expr < ' _ > ,
582
582
) -> Option < ( RefOp , & ' tcx Expr < ' tcx > ) > {
583
583
let ( def_id, arg) = match expr. kind {
584
- ExprKind :: MethodCall ( _, [ arg] , _) => ( typeck. type_dependent_def_id ( expr. hir_id ) ?, arg) ,
584
+ ExprKind :: MethodCall ( _, arg, [ ] , _) => ( typeck. type_dependent_def_id ( expr. hir_id ) ?, arg) ,
585
585
ExprKind :: Call (
586
586
Expr {
587
587
kind : ExprKind :: Path ( path) ,
@@ -796,58 +796,59 @@ fn walk_parents<'tcx>(
796
796
} ,
797
797
} )
798
798
} ) ,
799
- ExprKind :: MethodCall ( _, args, _) => {
799
+ ExprKind :: MethodCall ( _, receiver , args, _) => {
800
800
let id = cx. typeck_results ( ) . type_dependent_def_id ( parent. hir_id ) . unwrap ( ) ;
801
- args. iter ( ) . position ( |arg| arg. hir_id == child_id) . map ( |i| {
802
- if i == 0 {
803
- // Check for calls to trait methods where the trait is implemented on a reference.
804
- // Two cases need to be handled:
805
- // * `self` methods on `&T` will never have auto-borrow
806
- // * `&self` methods on `&T` can have auto-borrow, but `&self` methods on `T` will take
807
- // priority.
808
- if e. hir_id != child_id {
809
- Position :: ReborrowStable ( precedence)
810
- } else if let Some ( trait_id) = cx. tcx . trait_of_item ( id)
811
- && let arg_ty = cx. tcx . erase_regions ( cx. typeck_results ( ) . expr_ty_adjusted ( e) )
812
- && let ty:: Ref ( _, sub_ty, _) = * arg_ty. kind ( )
813
- && let subs = match cx
814
- . typeck_results ( )
815
- . node_substs_opt ( parent. hir_id )
816
- . and_then ( |subs| subs. get ( 1 ..) )
817
- {
818
- Some ( subs) => cx. tcx . mk_substs ( subs. iter ( ) . copied ( ) ) ,
819
- None => cx. tcx . mk_substs ( std:: iter:: empty :: < ty:: subst:: GenericArg < ' _ > > ( ) ) ,
820
- } && let impl_ty = if cx. tcx . fn_sig ( id) . skip_binder ( ) . inputs ( ) [ 0 ] . is_ref ( ) {
821
- // Trait methods taking `&self`
822
- sub_ty
823
- } else {
824
- // Trait methods taking `self`
825
- arg_ty
826
- } && impl_ty. is_ref ( )
827
- && cx. tcx . infer_ctxt ( ) . enter ( |infcx|
828
- infcx
829
- . type_implements_trait ( trait_id, impl_ty, subs, cx. param_env )
830
- . must_apply_modulo_regions ( )
831
- )
801
+ if receiver. hir_id == child_id {
802
+ // Check for calls to trait methods where the trait is implemented on a reference.
803
+ // Two cases need to be handled:
804
+ // * `self` methods on `&T` will never have auto-borrow
805
+ // * `&self` methods on `&T` can have auto-borrow, but `&self` methods on `T` will take
806
+ // priority.
807
+ if e. hir_id != child_id {
808
+ return Some ( Position :: ReborrowStable ( precedence) )
809
+ } else if let Some ( trait_id) = cx. tcx . trait_of_item ( id)
810
+ && let arg_ty = cx. tcx . erase_regions ( cx. typeck_results ( ) . expr_ty_adjusted ( e) )
811
+ && let ty:: Ref ( _, sub_ty, _) = * arg_ty. kind ( )
812
+ && let subs = match cx
813
+ . typeck_results ( )
814
+ . node_substs_opt ( parent. hir_id )
815
+ . and_then ( |subs| subs. get ( 1 ..) )
832
816
{
833
- Position :: MethodReceiverRefImpl
817
+ Some ( subs) => cx. tcx . mk_substs ( subs. iter ( ) . copied ( ) ) ,
818
+ None => cx. tcx . mk_substs ( std:: iter:: empty :: < ty:: subst:: GenericArg < ' _ > > ( ) ) ,
819
+ } && let impl_ty = if cx. tcx . fn_sig ( id) . skip_binder ( ) . inputs ( ) [ 0 ] . is_ref ( ) {
820
+ // Trait methods taking `&self`
821
+ sub_ty
834
822
} else {
835
- Position :: MethodReceiver
836
- }
823
+ // Trait methods taking `self`
824
+ arg_ty
825
+ } && impl_ty. is_ref ( )
826
+ && cx. tcx . infer_ctxt ( ) . enter ( |infcx|
827
+ infcx
828
+ . type_implements_trait ( trait_id, impl_ty, subs, cx. param_env )
829
+ . must_apply_modulo_regions ( )
830
+ )
831
+ {
832
+ return Some ( Position :: MethodReceiverRefImpl )
837
833
} else {
838
- let ty = cx. tcx . fn_sig ( id) . skip_binder ( ) . inputs ( ) [ i] ;
834
+ return Some ( Position :: MethodReceiver )
835
+ }
836
+ }
837
+ args. iter ( )
838
+ . position ( |arg| arg. hir_id == child_id)
839
+ . map ( |i| {
840
+ let ty = cx. tcx . fn_sig ( id) . skip_binder ( ) . inputs ( ) [ i + 1 ] ;
839
841
if let ty:: Param ( param_ty) = ty. kind ( ) {
840
- needless_borrow_impl_arg_position ( cx, parent, i, * param_ty, e, precedence, msrv)
842
+ needless_borrow_impl_arg_position ( cx, parent, i + 1 , * param_ty, e, precedence, msrv)
841
843
} else {
842
844
ty_auto_deref_stability (
843
845
cx,
844
- cx. tcx . erase_late_bound_regions ( cx. tcx . fn_sig ( id) . input ( i) ) ,
846
+ cx. tcx . erase_late_bound_regions ( cx. tcx . fn_sig ( id) . input ( i + 1 ) ) ,
845
847
precedence,
846
848
)
847
849
. position_for_arg ( )
848
850
}
849
- }
850
- } )
851
+ } )
851
852
} ,
852
853
ExprKind :: Field ( child, name) if child. hir_id == e. hir_id => Some ( Position :: FieldAccess ( name. name ) ) ,
853
854
ExprKind :: Unary ( UnOp :: Deref , child) if child. hir_id == e. hir_id => Some ( Position :: Deref ) ,
0 commit comments