@@ -3807,13 +3807,13 @@ fn hint_missing_borrow<'tcx>(
3807
3807
err : & mut Diagnostic ,
3808
3808
) {
3809
3809
let found_args = match found. kind ( ) {
3810
- ty:: FnPtr ( f) => f . inputs ( ) . skip_binder ( ) . iter ( ) ,
3810
+ ty:: FnPtr ( f) => infcx . replace_bound_vars_with_placeholders ( * f ) . inputs ( ) . iter ( ) ,
3811
3811
kind => {
3812
3812
span_bug ! ( span, "found was converted to a FnPtr above but is now {:?}" , kind)
3813
3813
}
3814
3814
} ;
3815
3815
let expected_args = match expected. kind ( ) {
3816
- ty:: FnPtr ( f) => f . inputs ( ) . skip_binder ( ) . iter ( ) ,
3816
+ ty:: FnPtr ( f) => infcx . replace_bound_vars_with_placeholders ( * f ) . inputs ( ) . iter ( ) ,
3817
3817
kind => {
3818
3818
span_bug ! ( span, "expected was converted to a FnPtr above but is now {:?}" , kind)
3819
3819
}
@@ -3824,12 +3824,12 @@ fn hint_missing_borrow<'tcx>(
3824
3824
3825
3825
let args = fn_decl. inputs . iter ( ) . map ( |ty| ty) ;
3826
3826
3827
- fn get_deref_type_and_refs ( mut ty : Ty < ' _ > ) -> ( Ty < ' _ > , usize ) {
3828
- let mut refs = 0 ;
3827
+ fn get_deref_type_and_refs ( mut ty : Ty < ' _ > ) -> ( Ty < ' _ > , Vec < hir :: Mutability > ) {
3828
+ let mut refs = vec ! [ ] ;
3829
3829
3830
- while let ty:: Ref ( _, new_ty, _ ) = ty. kind ( ) {
3830
+ while let ty:: Ref ( _, new_ty, mutbl ) = ty. kind ( ) {
3831
3831
ty = * new_ty;
3832
- refs += 1 ;
3832
+ refs. push ( * mutbl ) ;
3833
3833
}
3834
3834
3835
3835
( ty, refs)
@@ -3843,11 +3843,21 @@ fn hint_missing_borrow<'tcx>(
3843
3843
let ( expected_ty, expected_refs) = get_deref_type_and_refs ( * expected_arg) ;
3844
3844
3845
3845
if infcx. can_eq ( param_env, found_ty, expected_ty) . is_ok ( ) {
3846
- if found_refs < expected_refs {
3847
- to_borrow. push ( ( arg. span . shrink_to_lo ( ) , "&" . repeat ( expected_refs - found_refs) ) ) ;
3848
- } else if found_refs > expected_refs {
3846
+ // FIXME: This could handle more exotic cases like mutability mismatches too!
3847
+ if found_refs. len ( ) < expected_refs. len ( )
3848
+ && found_refs[ ..] == expected_refs[ expected_refs. len ( ) - found_refs. len ( ) ..]
3849
+ {
3850
+ to_borrow. push ( (
3851
+ arg. span . shrink_to_lo ( ) ,
3852
+ expected_refs[ ..expected_refs. len ( ) - found_refs. len ( ) ]
3853
+ . iter ( )
3854
+ . map ( |mutbl| format ! ( "&{}" , mutbl. prefix_str( ) ) )
3855
+ . collect :: < Vec < _ > > ( )
3856
+ . join ( "" ) ,
3857
+ ) ) ;
3858
+ } else if found_refs. len ( ) > expected_refs. len ( ) {
3849
3859
let mut span = arg. span . shrink_to_lo ( ) ;
3850
- let mut left = found_refs - expected_refs;
3860
+ let mut left = found_refs. len ( ) - expected_refs. len ( ) ;
3851
3861
let mut ty = arg;
3852
3862
while let hir:: TyKind :: Ref ( _, mut_ty) = & ty. kind && left > 0 {
3853
3863
span = span. with_hi ( mut_ty. ty . span . lo ( ) ) ;
0 commit comments