@@ -581,7 +581,7 @@ fn try_parse_ref_op<'tcx>(
581581 expr : & ' tcx Expr < ' _ > ,
582582) -> Option < ( RefOp , & ' tcx Expr < ' tcx > ) > {
583583 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) ,
585585 ExprKind :: Call (
586586 Expr {
587587 kind : ExprKind :: Path ( path) ,
@@ -796,58 +796,59 @@ fn walk_parents<'tcx>(
796796 } ,
797797 } )
798798 } ) ,
799- ExprKind :: MethodCall ( _, args, _) => {
799+ ExprKind :: MethodCall ( _, receiver , args, _) => {
800800 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 ..) )
832816 {
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
834822 } 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 )
837833 } 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 ] ;
839841 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)
841843 } else {
842844 ty_auto_deref_stability (
843845 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 ) ) ,
845847 precedence,
846848 )
847849 . position_for_arg ( )
848850 }
849- }
850- } )
851+ } )
851852 } ,
852853 ExprKind :: Field ( child, name) if child. hir_id == e. hir_id => Some ( Position :: FieldAccess ( name. name ) ) ,
853854 ExprKind :: Unary ( UnOp :: Deref , child) if child. hir_id == e. hir_id => Some ( Position :: Deref ) ,
0 commit comments