@@ -3759,13 +3759,13 @@ fn hint_missing_borrow<'tcx>(
3759
3759
err : & mut Diagnostic ,
3760
3760
) {
3761
3761
let found_args = match found. kind ( ) {
3762
- ty:: FnPtr ( f) => f . inputs ( ) . skip_binder ( ) . iter ( ) ,
3762
+ ty:: FnPtr ( f) => infcx . replace_bound_vars_with_placeholders ( * f ) . inputs ( ) . iter ( ) ,
3763
3763
kind => {
3764
3764
span_bug ! ( span, "found was converted to a FnPtr above but is now {:?}" , kind)
3765
3765
}
3766
3766
} ;
3767
3767
let expected_args = match expected. kind ( ) {
3768
- ty:: FnPtr ( f) => f . inputs ( ) . skip_binder ( ) . iter ( ) ,
3768
+ ty:: FnPtr ( f) => infcx . replace_bound_vars_with_placeholders ( * f ) . inputs ( ) . iter ( ) ,
3769
3769
kind => {
3770
3770
span_bug ! ( span, "expected was converted to a FnPtr above but is now {:?}" , kind)
3771
3771
}
@@ -3776,12 +3776,12 @@ fn hint_missing_borrow<'tcx>(
3776
3776
3777
3777
let args = fn_decl. inputs . iter ( ) . map ( |ty| ty) ;
3778
3778
3779
- fn get_deref_type_and_refs ( mut ty : Ty < ' _ > ) -> ( Ty < ' _ > , usize ) {
3780
- let mut refs = 0 ;
3779
+ fn get_deref_type_and_refs ( mut ty : Ty < ' _ > ) -> ( Ty < ' _ > , Vec < hir :: Mutability > ) {
3780
+ let mut refs = vec ! [ ] ;
3781
3781
3782
- while let ty:: Ref ( _, new_ty, _ ) = ty. kind ( ) {
3782
+ while let ty:: Ref ( _, new_ty, mutbl ) = ty. kind ( ) {
3783
3783
ty = * new_ty;
3784
- refs += 1 ;
3784
+ refs. push ( * mutbl ) ;
3785
3785
}
3786
3786
3787
3787
( ty, refs)
@@ -3795,11 +3795,21 @@ fn hint_missing_borrow<'tcx>(
3795
3795
let ( expected_ty, expected_refs) = get_deref_type_and_refs ( * expected_arg) ;
3796
3796
3797
3797
if infcx. can_eq ( param_env, found_ty, expected_ty) . is_ok ( ) {
3798
- if found_refs < expected_refs {
3799
- to_borrow. push ( ( arg. span . shrink_to_lo ( ) , "&" . repeat ( expected_refs - found_refs) ) ) ;
3800
- } else if found_refs > expected_refs {
3798
+ // FIXME: This could handle more exotic cases like mutability mismatches too!
3799
+ if found_refs. len ( ) < expected_refs. len ( )
3800
+ && found_refs[ ..] == expected_refs[ expected_refs. len ( ) - found_refs. len ( ) ..]
3801
+ {
3802
+ to_borrow. push ( (
3803
+ arg. span . shrink_to_lo ( ) ,
3804
+ expected_refs[ ..expected_refs. len ( ) - found_refs. len ( ) ]
3805
+ . iter ( )
3806
+ . map ( |mutbl| format ! ( "&{}" , mutbl. prefix_str( ) ) )
3807
+ . collect :: < Vec < _ > > ( )
3808
+ . join ( "" ) ,
3809
+ ) ) ;
3810
+ } else if found_refs. len ( ) > expected_refs. len ( ) {
3801
3811
let mut span = arg. span . shrink_to_lo ( ) ;
3802
- let mut left = found_refs - expected_refs;
3812
+ let mut left = found_refs. len ( ) - expected_refs. len ( ) ;
3803
3813
let mut ty = arg;
3804
3814
while let hir:: TyKind :: Ref ( _, mut_ty) = & ty. kind && left > 0 {
3805
3815
span = span. with_hi ( mut_ty. ty . span . lo ( ) ) ;
0 commit comments