@@ -222,6 +222,15 @@ pub trait TypeErrCtxtExt<'tcx> {
222
222
param_env : ty:: ParamEnv < ' tcx > ,
223
223
) -> DiagnosticBuilder < ' tcx , ErrorGuaranteed > ;
224
224
225
+ fn note_conflicting_fn_args (
226
+ & self ,
227
+ err : & mut Diagnostic ,
228
+ cause : & ObligationCauseCode < ' tcx > ,
229
+ expected : Ty < ' tcx > ,
230
+ found : Ty < ' tcx > ,
231
+ param_env : ty:: ParamEnv < ' tcx > ,
232
+ ) ;
233
+
225
234
fn note_conflicting_closure_bounds (
226
235
& self ,
227
236
cause : & ObligationCauseCode < ' tcx > ,
@@ -2005,6 +2014,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
2005
2014
let signature_kind = format ! ( "{argument_kind} signature" ) ;
2006
2015
err. note_expected_found ( & signature_kind, expected_str, & signature_kind, found_str) ;
2007
2016
2017
+ self . note_conflicting_fn_args ( & mut err, cause, expected, found, param_env) ;
2008
2018
self . note_conflicting_closure_bounds ( cause, & mut err) ;
2009
2019
2010
2020
if let Some ( found_node) = found_node {
@@ -2014,6 +2024,152 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
2014
2024
err
2015
2025
}
2016
2026
2027
+ fn note_conflicting_fn_args (
2028
+ & self ,
2029
+ err : & mut Diagnostic ,
2030
+ cause : & ObligationCauseCode < ' tcx > ,
2031
+ expected : Ty < ' tcx > ,
2032
+ found : Ty < ' tcx > ,
2033
+ param_env : ty:: ParamEnv < ' tcx > ,
2034
+ ) {
2035
+ let ObligationCauseCode :: FunctionArgumentObligation { arg_hir_id, .. } = cause else {
2036
+ return ;
2037
+ } ;
2038
+ let ty:: FnPtr ( expected) = expected. kind ( ) else {
2039
+ return ;
2040
+ } ;
2041
+ let ty:: FnPtr ( found) = found. kind ( ) else {
2042
+ return ;
2043
+ } ;
2044
+ let Some ( Node :: Expr ( arg) ) = self . tcx . hir ( ) . find ( * arg_hir_id) else {
2045
+ return ;
2046
+ } ;
2047
+ let hir:: ExprKind :: Path ( path) = arg. kind else {
2048
+ return ;
2049
+ } ;
2050
+ let expected_inputs = self . tcx . erase_late_bound_regions ( * expected) . inputs ( ) ;
2051
+ let found_inputs = self . tcx . erase_late_bound_regions ( * found) . inputs ( ) ;
2052
+ let both_tys = expected_inputs. iter ( ) . cloned ( ) . zip ( found_inputs. iter ( ) . cloned ( ) ) ;
2053
+
2054
+ let arg_expr = |infcx : & InferCtxt < ' tcx > , name, expected : Ty < ' tcx > , found : Ty < ' tcx > | {
2055
+ let ( expected_ty, expected_refs) = get_deref_type_and_refs ( expected) ;
2056
+ let ( found_ty, found_refs) = get_deref_type_and_refs ( found) ;
2057
+
2058
+ if infcx. can_eq ( param_env, found_ty, expected_ty) {
2059
+ if found_refs. len ( ) == expected_refs. len ( )
2060
+ && found_refs. iter ( ) . zip ( expected_refs. iter ( ) ) . all ( |( e, f) | e == f)
2061
+ {
2062
+ name
2063
+ } else if found_refs. len ( ) > expected_refs. len ( ) {
2064
+ if found_refs[ ..found_refs. len ( ) - expected_refs. len ( ) ]
2065
+ . iter ( )
2066
+ . zip ( expected_refs. iter ( ) )
2067
+ . any ( |( e, f) | e != f)
2068
+ {
2069
+ // The refs have different mutability.
2070
+ format ! (
2071
+ "{}*{name}" ,
2072
+ found_refs[ ..found_refs. len( ) - expected_refs. len( ) ]
2073
+ . iter( )
2074
+ . map( |mutbl| format!( "&{}" , mutbl. prefix_str( ) ) )
2075
+ . collect:: <Vec <_>>( )
2076
+ . join( "" ) ,
2077
+ )
2078
+ } else {
2079
+ format ! (
2080
+ "{}{name}" ,
2081
+ found_refs[ ..found_refs. len( ) - expected_refs. len( ) ]
2082
+ . iter( )
2083
+ . map( |mutbl| format!( "&{}" , mutbl. prefix_str( ) ) )
2084
+ . collect:: <Vec <_>>( )
2085
+ . join( "" ) ,
2086
+ )
2087
+ }
2088
+ } else if expected_refs. len ( ) > found_refs. len ( ) {
2089
+ format ! (
2090
+ "{}{name}" ,
2091
+ ( 0 ..( expected_refs. len( ) - found_refs. len( ) ) )
2092
+ . map( |_| "*" )
2093
+ . collect:: <Vec <_>>( )
2094
+ . join( "" ) ,
2095
+ )
2096
+ } else {
2097
+ format ! (
2098
+ "{}{name}" ,
2099
+ found_refs
2100
+ . iter( )
2101
+ . map( |mutbl| format!( "&{}" , mutbl. prefix_str( ) ) )
2102
+ . chain( found_refs. iter( ) . map( |_| "*" . to_string( ) ) )
2103
+ . collect:: <Vec <_>>( )
2104
+ . join( "" ) ,
2105
+ )
2106
+ }
2107
+ } else {
2108
+ format ! ( "/* {found} */" )
2109
+ }
2110
+ } ;
2111
+ let ( closure_names, call_names) : ( Vec < _ > , Vec < _ > ) =
2112
+ if both_tys. clone ( ) . all ( |( expected, found) | {
2113
+ let ( expected_ty, _) = get_deref_type_and_refs ( expected) ;
2114
+ let ( found_ty, _) = get_deref_type_and_refs ( found) ;
2115
+ self . can_eq ( param_env, found_ty, expected_ty)
2116
+ } ) && !expected_inputs. is_empty ( )
2117
+ && expected_inputs. len ( ) == found_inputs. len ( )
2118
+ && let hir:: QPath :: Resolved ( _, path) = path
2119
+ && let hir:: def:: Res :: Def ( _, fn_def_id) = path. res
2120
+ && let Some ( node) = self . tcx . hir ( ) . get_if_local ( fn_def_id)
2121
+ && let Some ( body_id) = node. body_id ( )
2122
+ {
2123
+ let closure = self
2124
+ . tcx
2125
+ . hir ( )
2126
+ . body_param_names ( body_id)
2127
+ . map ( |name| format ! ( "{name}" ) )
2128
+ . collect ( ) ;
2129
+ let args = self
2130
+ . tcx
2131
+ . hir ( )
2132
+ . body_param_names ( body_id)
2133
+ . zip ( both_tys)
2134
+ . map ( |( name, ( expected, found) ) | {
2135
+ arg_expr ( self . infcx , format ! ( "{name}" ) , expected, found)
2136
+ } )
2137
+ . collect ( ) ;
2138
+ ( closure, args)
2139
+ } else {
2140
+ let closure_args = expected_inputs
2141
+ . iter ( )
2142
+ . enumerate ( )
2143
+ . map ( |( i, _) | format ! ( "arg{i}" ) )
2144
+ . collect :: < Vec < _ > > ( ) ;
2145
+ let call_args = both_tys
2146
+ . enumerate ( )
2147
+ . map ( |( i, ( expected, found) ) | {
2148
+ arg_expr ( self . infcx , format ! ( "arg{i}" ) , expected, found)
2149
+ } )
2150
+ . collect :: < Vec < _ > > ( ) ;
2151
+ ( closure_args, call_args)
2152
+ } ;
2153
+ let closure_names: Vec < _ > = closure_names
2154
+ . into_iter ( )
2155
+ . zip ( expected_inputs. iter ( ) )
2156
+ . map ( |( name, ty) | {
2157
+ format ! (
2158
+ "{name}{}" ,
2159
+ if ty. has_infer_types( ) { String :: new( ) } else { format!( ": {ty}" ) }
2160
+ )
2161
+ } )
2162
+ . collect ( ) ;
2163
+ err. multipart_suggestion (
2164
+ format ! ( "consider wrapping the function in a closure" ) ,
2165
+ vec ! [
2166
+ ( arg. span. shrink_to_lo( ) , format!( "|{}| " , closure_names. join( ", " ) ) ) ,
2167
+ ( arg. span. shrink_to_hi( ) , format!( "({})" , call_names. join( ", " ) ) ) ,
2168
+ ] ,
2169
+ Applicability :: MaybeIncorrect ,
2170
+ ) ;
2171
+ }
2172
+
2017
2173
// Add a note if there are two `Fn`-family bounds that have conflicting argument
2018
2174
// requirements, which will always cause a closure to have a type error.
2019
2175
fn note_conflicting_closure_bounds (
@@ -4349,17 +4505,6 @@ fn hint_missing_borrow<'tcx>(
4349
4505
4350
4506
let args = fn_decl. inputs . iter ( ) ;
4351
4507
4352
- fn get_deref_type_and_refs ( mut ty : Ty < ' _ > ) -> ( Ty < ' _ > , Vec < hir:: Mutability > ) {
4353
- let mut refs = vec ! [ ] ;
4354
-
4355
- while let ty:: Ref ( _, new_ty, mutbl) = ty. kind ( ) {
4356
- ty = * new_ty;
4357
- refs. push ( * mutbl) ;
4358
- }
4359
-
4360
- ( ty, refs)
4361
- }
4362
-
4363
4508
let mut to_borrow = Vec :: new ( ) ;
4364
4509
let mut remove_borrow = Vec :: new ( ) ;
4365
4510
@@ -4652,3 +4797,14 @@ pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
4652
4797
4653
4798
Some ( sugg)
4654
4799
}
4800
+
4801
+ fn get_deref_type_and_refs ( mut ty : Ty < ' _ > ) -> ( Ty < ' _ > , Vec < hir:: Mutability > ) {
4802
+ let mut refs = vec ! [ ] ;
4803
+
4804
+ while let ty:: Ref ( _, new_ty, mutbl) = ty. kind ( ) {
4805
+ ty = * new_ty;
4806
+ refs. push ( * mutbl) ;
4807
+ }
4808
+
4809
+ ( ty, refs)
4810
+ }
0 commit comments