@@ -1065,16 +1065,12 @@ fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[hir::Attrib
1065
1065
for ( pos, literal) in meta_item_list. iter ( ) . filter_map ( |meta| meta. lit ( ) ) . enumerate ( ) {
1066
1066
match literal. kind {
1067
1067
ast:: LitKind :: Int ( a, _) => {
1068
- let param = func. generics . params . remove ( 0 ) ;
1069
- if let GenericParamDef {
1070
- name,
1071
- kind : GenericParamDefKind :: Const { ty, .. } ,
1072
- ..
1073
- } = param
1074
- {
1075
- func. decl
1076
- . inputs
1077
- . insert ( a. get ( ) as _ , Parameter { name, type_ : * ty, is_const : true } ) ;
1068
+ let GenericParamDef { name, kind, .. } = func. generics . params . remove ( 0 ) ;
1069
+ if let GenericParamDefKind :: Const { ty, .. } = kind {
1070
+ func. decl . inputs . insert (
1071
+ a. get ( ) as _ ,
1072
+ Parameter { name : Some ( name) , type_ : * ty, is_const : true } ,
1073
+ ) ;
1078
1074
} else {
1079
1075
panic ! ( "unexpected non const in position {pos}" ) ;
1080
1076
}
@@ -1101,7 +1097,10 @@ fn clean_function<'tcx>(
1101
1097
let generics = clean_generics ( generics, cx) ;
1102
1098
let params = match params {
1103
1099
ParamsSrc :: Body ( body_id) => clean_params_via_body ( cx, sig. decl . inputs , body_id) ,
1104
- ParamsSrc :: Idents ( idents) => clean_params ( cx, sig. decl . inputs , idents) ,
1100
+ // Let's not perpetuate anon params from Rust 2015; use `_` for them.
1101
+ ParamsSrc :: Idents ( idents) => clean_params ( cx, sig. decl . inputs , idents, |ident| {
1102
+ Some ( ident. map_or ( kw:: Underscore , |ident| ident. name ) )
1103
+ } ) ,
1105
1104
} ;
1106
1105
let decl = clean_fn_decl_with_params ( cx, sig. decl , Some ( & sig. header ) , params) ;
1107
1106
( generics, decl)
@@ -1113,30 +1112,13 @@ fn clean_params<'tcx>(
1113
1112
cx : & mut DocContext < ' tcx > ,
1114
1113
types : & [ hir:: Ty < ' tcx > ] ,
1115
1114
idents : & [ Option < Ident > ] ,
1115
+ postprocess : impl Fn ( Option < Ident > ) -> Option < Symbol > ,
1116
1116
) -> Vec < Parameter > {
1117
- fn nonempty_name ( ident : & Option < Ident > ) -> Option < Symbol > {
1118
- if let Some ( ident) = ident
1119
- && ident. name != kw:: Underscore
1120
- {
1121
- Some ( ident. name )
1122
- } else {
1123
- None
1124
- }
1125
- }
1126
-
1127
- // If at least one argument has a name, use `_` as the name of unnamed
1128
- // arguments. Otherwise omit argument names.
1129
- let default_name = if idents. iter ( ) . any ( |ident| nonempty_name ( ident) . is_some ( ) ) {
1130
- kw:: Underscore
1131
- } else {
1132
- kw:: Empty // FIXME: using kw::Empty is a bit of a hack
1133
- } ;
1134
-
1135
1117
types
1136
1118
. iter ( )
1137
1119
. enumerate ( )
1138
1120
. map ( |( i, ty) | Parameter {
1139
- name : idents . get ( i ) . and_then ( nonempty_name ) . unwrap_or ( default_name ) ,
1121
+ name : postprocess ( idents [ i ] ) ,
1140
1122
type_ : clean_ty ( ty, cx) ,
1141
1123
is_const : false ,
1142
1124
} )
@@ -1152,7 +1134,7 @@ fn clean_params_via_body<'tcx>(
1152
1134
. iter ( )
1153
1135
. zip ( cx. tcx . hir_body ( body_id) . params )
1154
1136
. map ( |( ty, param) | Parameter {
1155
- name : name_from_pat ( param. pat ) ,
1137
+ name : Some ( name_from_pat ( param. pat ) ) ,
1156
1138
type_ : clean_ty ( ty, cx) ,
1157
1139
is_const : false ,
1158
1140
} )
@@ -1182,8 +1164,6 @@ fn clean_poly_fn_sig<'tcx>(
1182
1164
did : Option < DefId > ,
1183
1165
sig : ty:: PolyFnSig < ' tcx > ,
1184
1166
) -> FnDecl {
1185
- let mut names = did. map_or ( & [ ] as & [ _ ] , |did| cx. tcx . fn_arg_idents ( did) ) . iter ( ) ;
1186
-
1187
1167
let mut output = clean_middle_ty ( sig. output ( ) , cx, None , None ) ;
1188
1168
1189
1169
// If the return type isn't an `impl Trait`, we can safely assume that this
@@ -1196,12 +1176,20 @@ fn clean_poly_fn_sig<'tcx>(
1196
1176
output = output. sugared_async_return_type ( ) ;
1197
1177
}
1198
1178
1179
+ let mut idents = did. map ( |did| cx. tcx . fn_arg_idents ( did) ) . unwrap_or_default ( ) . iter ( ) . copied ( ) ;
1180
+
1181
+ // If this comes from a fn item, let's not perpetuate anon params from Rust 2015; use `_` for them.
1182
+ // If this comes from a fn ptr ty, we just keep params unnamed since it's more conventional stylistically.
1183
+ // Since the param name is not part of the semantic type, these params never bear a name unlike
1184
+ // in the HIR case, thus we can't peform any fancy fallback logic unlike `clean_bare_fn_ty`.
1185
+ let fallback = did. map ( |_| kw:: Underscore ) ;
1186
+
1199
1187
let params = sig
1200
1188
. inputs ( )
1201
1189
. iter ( )
1202
- . map ( |t | Parameter {
1203
- type_ : clean_middle_ty ( t . map_bound ( |t| * t ) , cx , None , None ) ,
1204
- name : if let Some ( Some ( ident ) ) = names . next ( ) { ident . name } else { kw :: Underscore } ,
1190
+ . map ( |ty | Parameter {
1191
+ name : idents . next ( ) . flatten ( ) . map ( |ident| ident . name ) . or ( fallback ) ,
1192
+ type_ : clean_middle_ty ( ty . map_bound ( |ty| * ty ) , cx , None , None ) ,
1205
1193
is_const : false ,
1206
1194
} )
1207
1195
. collect ( ) ;
@@ -2591,7 +2579,17 @@ fn clean_bare_fn_ty<'tcx>(
2591
2579
. filter ( |p| !is_elided_lifetime ( p) )
2592
2580
. map ( |x| clean_generic_param ( cx, None , x) )
2593
2581
. collect ( ) ;
2594
- let params = clean_params ( cx, bare_fn. decl . inputs , bare_fn. param_idents ) ;
2582
+ // Since it's more conventional stylistically, elide the name of all params called `_`
2583
+ // unless there's at least one interestingly named param in which case don't elide any
2584
+ // name since mixing named and unnamed params is less legible.
2585
+ let filter = |ident : Option < Ident > | {
2586
+ ident. map ( |ident| ident. name ) . filter ( |& ident| ident != kw:: Underscore )
2587
+ } ;
2588
+ let fallback =
2589
+ bare_fn. param_idents . iter ( ) . copied ( ) . find_map ( filter) . map ( |_| kw:: Underscore ) ;
2590
+ let params = clean_params ( cx, bare_fn. decl . inputs , bare_fn. param_idents , |ident| {
2591
+ filter ( ident) . or ( fallback)
2592
+ } ) ;
2595
2593
let decl = clean_fn_decl_with_params ( cx, bare_fn. decl , None , params) ;
2596
2594
( generic_params, decl)
2597
2595
} ) ;
0 commit comments