@@ -3759,13 +3759,13 @@ fn hint_missing_borrow<'tcx>(
37593759 err : & mut Diagnostic ,
37603760) {
37613761 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 ( ) ,
37633763 kind => {
37643764 span_bug ! ( span, "found was converted to a FnPtr above but is now {:?}" , kind)
37653765 }
37663766 } ;
37673767 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 ( ) ,
37693769 kind => {
37703770 span_bug ! ( span, "expected was converted to a FnPtr above but is now {:?}" , kind)
37713771 }
@@ -3776,12 +3776,12 @@ fn hint_missing_borrow<'tcx>(
37763776
37773777 let args = fn_decl. inputs . iter ( ) . map ( |ty| ty) ;
37783778
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 ! [ ] ;
37813781
3782- while let ty:: Ref ( _, new_ty, _ ) = ty. kind ( ) {
3782+ while let ty:: Ref ( _, new_ty, mutbl ) = ty. kind ( ) {
37833783 ty = * new_ty;
3784- refs += 1 ;
3784+ refs. push ( * mutbl ) ;
37853785 }
37863786
37873787 ( ty, refs)
@@ -3795,11 +3795,21 @@ fn hint_missing_borrow<'tcx>(
37953795 let ( expected_ty, expected_refs) = get_deref_type_and_refs ( * expected_arg) ;
37963796
37973797 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 ( ) {
38013811 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 ( ) ;
38033813 let mut ty = arg;
38043814 while let hir:: TyKind :: Ref ( _, mut_ty) = & ty. kind && left > 0 {
38053815 span = span. with_hi ( mut_ty. ty . span . lo ( ) ) ;
0 commit comments