@@ -3807,13 +3807,13 @@ fn hint_missing_borrow<'tcx>(
38073807 err : & mut Diagnostic ,
38083808) {
38093809 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 ( ) ,
38113811 kind => {
38123812 span_bug ! ( span, "found was converted to a FnPtr above but is now {:?}" , kind)
38133813 }
38143814 } ;
38153815 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 ( ) ,
38173817 kind => {
38183818 span_bug ! ( span, "expected was converted to a FnPtr above but is now {:?}" , kind)
38193819 }
@@ -3824,12 +3824,12 @@ fn hint_missing_borrow<'tcx>(
38243824
38253825 let args = fn_decl. inputs . iter ( ) . map ( |ty| ty) ;
38263826
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 ! [ ] ;
38293829
3830- while let ty:: Ref ( _, new_ty, _ ) = ty. kind ( ) {
3830+ while let ty:: Ref ( _, new_ty, mutbl ) = ty. kind ( ) {
38313831 ty = * new_ty;
3832- refs += 1 ;
3832+ refs. push ( * mutbl ) ;
38333833 }
38343834
38353835 ( ty, refs)
@@ -3843,11 +3843,21 @@ fn hint_missing_borrow<'tcx>(
38433843 let ( expected_ty, expected_refs) = get_deref_type_and_refs ( * expected_arg) ;
38443844
38453845 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 ( ) {
38493859 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 ( ) ;
38513861 let mut ty = arg;
38523862 while let hir:: TyKind :: Ref ( _, mut_ty) = & ty. kind && left > 0 {
38533863 span = span. with_hi ( mut_ty. ty . span . lo ( ) ) ;
0 commit comments