@@ -1065,16 +1065,12 @@ fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[hir::Attrib
10651065 for ( pos, literal) in meta_item_list. iter ( ) . filter_map ( |meta| meta. lit ( ) ) . enumerate ( ) {
10661066 match literal. kind {
10671067 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+ ) ;
10781074 } else {
10791075 panic ! ( "unexpected non const in position {pos}" ) ;
10801076 }
@@ -1101,7 +1097,10 @@ fn clean_function<'tcx>(
11011097 let generics = clean_generics ( generics, cx) ;
11021098 let params = match params {
11031099 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+ } ) ,
11051104 } ;
11061105 let decl = clean_fn_decl_with_params ( cx, sig. decl , Some ( & sig. header ) , params) ;
11071106 ( generics, decl)
@@ -1113,30 +1112,13 @@ fn clean_params<'tcx>(
11131112 cx : & mut DocContext < ' tcx > ,
11141113 types : & [ hir:: Ty < ' tcx > ] ,
11151114 idents : & [ Option < Ident > ] ,
1115+ postprocess : impl Fn ( Option < Ident > ) -> Option < Symbol > ,
11161116) -> 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-
11351117 types
11361118 . iter ( )
11371119 . enumerate ( )
11381120 . map ( |( i, ty) | Parameter {
1139- name : idents . get ( i ) . and_then ( nonempty_name ) . unwrap_or ( default_name ) ,
1121+ name : postprocess ( idents [ i ] ) ,
11401122 type_ : clean_ty ( ty, cx) ,
11411123 is_const : false ,
11421124 } )
@@ -1152,7 +1134,7 @@ fn clean_params_via_body<'tcx>(
11521134 . iter ( )
11531135 . zip ( cx. tcx . hir_body ( body_id) . params )
11541136 . map ( |( ty, param) | Parameter {
1155- name : name_from_pat ( param. pat ) ,
1137+ name : Some ( name_from_pat ( param. pat ) ) ,
11561138 type_ : clean_ty ( ty, cx) ,
11571139 is_const : false ,
11581140 } )
@@ -1182,8 +1164,6 @@ fn clean_poly_fn_sig<'tcx>(
11821164 did : Option < DefId > ,
11831165 sig : ty:: PolyFnSig < ' tcx > ,
11841166) -> FnDecl {
1185- let mut names = did. map_or ( & [ ] as & [ _ ] , |did| cx. tcx . fn_arg_idents ( did) ) . iter ( ) ;
1186-
11871167 let mut output = clean_middle_ty ( sig. output ( ) , cx, None , None ) ;
11881168
11891169 // 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>(
11961176 output = output. sugared_async_return_type ( ) ;
11971177 }
11981178
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+
11991187 let params = sig
12001188 . inputs ( )
12011189 . 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 ) ,
12051193 is_const : false ,
12061194 } )
12071195 . collect ( ) ;
@@ -2591,7 +2579,17 @@ fn clean_bare_fn_ty<'tcx>(
25912579 . filter ( |p| !is_elided_lifetime ( p) )
25922580 . map ( |x| clean_generic_param ( cx, None , x) )
25932581 . 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+ } ) ;
25952593 let decl = clean_fn_decl_with_params ( cx, bare_fn. decl , None , params) ;
25962594 ( generic_params, decl)
25972595 } ) ;
0 commit comments