Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit argument names from function pointers that do not have argument names #136411

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,17 +1104,29 @@ fn clean_args_from_types_and_names<'tcx>(
types: &[hir::Ty<'tcx>],
names: &[Ident],
) -> Arguments {
fn nonempty_name(ident: &Ident) -> Option<Symbol> {
if ident.name == kw::Underscore || ident.name == kw::Empty {
None
} else {
Some(ident.name)
}
}

// If at least one argument has a name, use `_` as the name of unnamed
// arguments. Otherwise omit argument names.
let default_name = if names.iter().any(|ident| nonempty_name(ident).is_some()) {
kw::Underscore
} else {
kw::Empty
};

Arguments {
values: types
.iter()
.enumerate()
.map(|(i, ty)| Argument {
type_: clean_ty(ty, cx),
name: names
.get(i)
.map(|ident| ident.name)
.filter(|ident| !ident.is_empty())
.unwrap_or(kw::Underscore),
name: names.get(i).and_then(nonempty_name).unwrap_or(default_name),
is_const: false,
})
.collect(),
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,9 @@ impl clean::Arguments {
) -> impl Display + 'a + Captures<'tcx> {
fmt::from_fn(move |f| {
for (i, input) in self.values.iter().enumerate() {
write!(f, "{}: ", input.name)?;
if !input.name.is_empty() {
write!(f, "{}: ", input.name)?;
}
input.type_.print(cx).fmt(f)?;
if i + 1 < self.values.len() {
write!(f, ", ")?;
Expand Down
2 changes: 1 addition & 1 deletion tests/rustdoc/assoc-consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn f(_: &(ToString + 'static)) {}

impl Bar {
//@ has assoc_consts/struct.Bar.html '//*[@id="associatedconstant.F"]' \
// "const F: fn(_: &(dyn ToString + 'static))"
// "const F: fn(&(dyn ToString + 'static))"
pub const F: fn(_: &(ToString + 'static)) = f;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/rustdoc/fn-pointer-arg-name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@
//@ has foo/fn.f.html
//@ has - '//pre[@class="rust item-decl"]' 'pub fn f(callback: fn(len: usize, foo: u32))'
pub fn f(callback: fn(len: usize, foo: u32)) {}

//@ has foo/fn.g.html
//@ has - '//pre[@class="rust item-decl"]' 'pub fn g(_: fn(usize, u32))'
pub fn g(_: fn(usize, _: u32)) {}

//@ has foo/fn.mixed.html
//@ has - '//pre[@class="rust item-decl"]' 'pub fn mixed(_: fn(_: usize, foo: u32))'
pub fn mixed(_: fn(usize, foo: u32)) {}
2 changes: 1 addition & 1 deletion tests/rustdoc/inherent-projections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl Owner {
}

// Make sure we handle bound vars correctly.
//@ has 'inherent_projections/fn.user.html' '//pre[@class="rust item-decl"]' "user(_: for<'a> fn(_: Carrier<'a>::Focus))"
//@ has 'inherent_projections/fn.user.html' '//pre[@class="rust item-decl"]' "user(_: for<'a> fn(Carrier<'a>::Focus))"
pub fn user(_: for<'a> fn(Carrier<'a>::Focus)) {}

pub struct Carrier<'a>(&'a ());
Expand Down
8 changes: 4 additions & 4 deletions tests/rustdoc/macro-higher-kinded-function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ macro_rules! gen {
}

//@ has 'foo/struct.Providers.html'
//@ has - '//*[@class="rust item-decl"]//code' "pub a: for<'tcx> fn(_: TyCtxt<'tcx>, _: u8) -> i8,"
//@ has - '//*[@class="rust item-decl"]//code' "pub b: for<'tcx> fn(_: TyCtxt<'tcx>, _: u16) -> i16,"
//@ has - '//*[@id="structfield.a"]/code' "a: for<'tcx> fn(_: TyCtxt<'tcx>, _: u8) -> i8"
//@ has - '//*[@id="structfield.b"]/code' "b: for<'tcx> fn(_: TyCtxt<'tcx>, _: u16) -> i16"
//@ has - '//*[@class="rust item-decl"]//code' "pub a: for<'tcx> fn(TyCtxt<'tcx>, u8) -> i8,"
//@ has - '//*[@class="rust item-decl"]//code' "pub b: for<'tcx> fn(TyCtxt<'tcx>, u16) -> i16,"
//@ has - '//*[@id="structfield.a"]/code' "a: for<'tcx> fn(TyCtxt<'tcx>, u8) -> i8"
//@ has - '//*[@id="structfield.b"]/code' "b: for<'tcx> fn(TyCtxt<'tcx>, u16) -> i16"
gen! {
(a, 'tcx, [u8], [i8])
(b, 'tcx, [u16], [i16])
Expand Down
Loading