@@ -410,18 +410,18 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
410
410
}
411
411
let typeck = self . infcx . tcx . typeck ( self . mir_def_id ( ) ) ;
412
412
let parent = self . infcx . tcx . parent_hir_node ( expr. hir_id ) ;
413
- let ( def_id, call_id , args, offset) = if let hir:: Node :: Expr ( parent_expr) = parent
413
+ let ( def_id, args, offset) = if let hir:: Node :: Expr ( parent_expr) = parent
414
414
&& let hir:: ExprKind :: MethodCall ( _, _, args, _) = parent_expr. kind
415
415
{
416
416
let def_id = typeck. type_dependent_def_id ( parent_expr. hir_id ) ;
417
- ( def_id, Some ( parent_expr . hir_id ) , args, 1 )
417
+ ( def_id, args, 1 )
418
418
} else if let hir:: Node :: Expr ( parent_expr) = parent
419
419
&& let hir:: ExprKind :: Call ( call, args) = parent_expr. kind
420
420
&& let ty:: FnDef ( def_id, _) = typeck. node_type ( call. hir_id ) . kind ( )
421
421
{
422
- ( Some ( * def_id) , Some ( call . hir_id ) , args, 0 )
422
+ ( Some ( * def_id) , args, 0 )
423
423
} else {
424
- ( None , None , & [ ] [ ..] , 0 )
424
+ ( None , & [ ] [ ..] , 0 )
425
425
} ;
426
426
let ty = place. ty ( self . body , self . infcx . tcx ) . ty ;
427
427
@@ -459,11 +459,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
459
459
// If the moved place is used generically by the callee and a reference to it
460
460
// would still satisfy any bounds on its type, suggest borrowing.
461
461
if let Some ( & param) = arg_param
462
- && let Some ( generic_args ) = call_id . and_then ( |id| typeck . node_args_opt ( id ) )
462
+ && let hir :: Node :: Expr ( call_expr ) = parent
463
463
&& let Some ( ref_mutability) = self . suggest_borrow_generic_arg (
464
464
err,
465
+ typeck,
466
+ call_expr,
465
467
def_id,
466
- generic_args,
467
468
param,
468
469
moved_place,
469
470
pos + offset,
@@ -627,8 +628,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
627
628
fn suggest_borrow_generic_arg (
628
629
& self ,
629
630
err : & mut Diag < ' _ > ,
631
+ typeck : & ty:: TypeckResults < ' tcx > ,
632
+ call_expr : & hir:: Expr < ' tcx > ,
630
633
callee_did : DefId ,
631
- generic_args : ty:: GenericArgsRef < ' tcx > ,
632
634
param : ty:: ParamTy ,
633
635
moved_place : PlaceRef < ' tcx > ,
634
636
moved_arg_pos : usize ,
@@ -639,6 +641,19 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
639
641
let sig = tcx. fn_sig ( callee_did) . instantiate_identity ( ) . skip_binder ( ) ;
640
642
let clauses = tcx. predicates_of ( callee_did) ;
641
643
644
+ let generic_args = match call_expr. kind {
645
+ // For method calls, generic arguments are attached to the call node.
646
+ hir:: ExprKind :: MethodCall ( ..) => typeck. node_args_opt ( call_expr. hir_id ) ?,
647
+ // For normal calls, generic arguments are in the callee's type.
648
+ // This diagnostic is only run for `FnDef` callees.
649
+ hir:: ExprKind :: Call ( callee, _)
650
+ if let & ty:: FnDef ( _, args) = typeck. node_type ( callee. hir_id ) . kind ( ) =>
651
+ {
652
+ args
653
+ }
654
+ _ => return None ,
655
+ } ;
656
+
642
657
// First, is there at least one method on one of `param`'s trait bounds?
643
658
// This keeps us from suggesting borrowing the argument to `mem::drop`, e.g.
644
659
if !clauses. instantiate_identity ( tcx) . predicates . iter ( ) . any ( |clause| {
0 commit comments