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

rustdoc: Render HRTB correctly for bare functions #79991

Merged
merged 1 commit into from
Jan 29, 2021
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
21 changes: 15 additions & 6 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ fn fmt_type(
use_absolute: bool,
cache: &Cache,
) -> fmt::Result {
debug!("fmt_type(t = {:?})", t);

match *t {
clean::Generic(name) => write!(f, "{}", name),
clean::ResolvedPath { did, ref param_names, ref path, is_generic } => {
Expand All @@ -675,21 +677,22 @@ fn fmt_type(
if f.alternate() {
write!(
f,
"{}{:#}fn{:#}{:#}",
"{:#}{}{:#}fn{:#}",
decl.print_hrtb_with_space(cache),
decl.unsafety.print_with_space(),
print_abi_with_space(decl.abi),
decl.print_generic_params(cache),
decl.decl.print(cache)
)
} else {
write!(
f,
"{}{}",
"{}{}{}",
decl.print_hrtb_with_space(cache),
decl.unsafety.print_with_space(),
print_abi_with_space(decl.abi)
)?;
primitive_link(f, PrimitiveType::Fn, "fn", cache)?;
write!(f, "{}{}", decl.print_generic_params(cache), decl.decl.print(cache))
write!(f, "{}", decl.decl.print(cache))
}
}
clean::Tuple(ref typs) => {
Expand Down Expand Up @@ -992,8 +995,14 @@ impl clean::FnRetTy {
}

impl clean::BareFunctionDecl {
fn print_generic_params<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a {
comma_sep(self.generic_params.iter().map(move |g| g.print(cache)))
fn print_hrtb_with_space<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a {
display_fn(move |f| {
if !self.generic_params.is_empty() {
camelid marked this conversation as resolved.
Show resolved Hide resolved
write!(f, "for<{}> ", comma_sep(self.generic_params.iter().map(|g| g.print(cache))))
} else {
Ok(())
}
})
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/test/rustdoc/fn-type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ignore-tidy-linelength

#![crate_name = "foo"]
#![crate_type = "lib"]

pub struct Foo<'a, T> {
pub generic: fn(val: &T) -> T,

pub lifetime: fn(val: &'a i32) -> i32,
pub hrtb_lifetime: for<'b, 'c> fn(one: &'b i32, two: &'c &'b i32) -> (&'b i32, &'c i32),
}
camelid marked this conversation as resolved.
Show resolved Hide resolved

// @has 'foo/struct.Foo.html' '//span[@id="structfield.generic"]' "generic: fn(val: &T) -> T"
// @has 'foo/struct.Foo.html' '//span[@id="structfield.lifetime"]' "lifetime: fn(val: &'a i32) -> i32"
// @has 'foo/struct.Foo.html' '//span[@id="structfield.hrtb_lifetime"]' "hrtb_lifetime: for<'b, 'c> fn(one: &'b i32, two: &'c &'b i32) -> (&'b i32, &'c i32)"
14 changes: 14 additions & 0 deletions src/test/rustdoc/for-lifetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ignore-tidy-linelength

#![crate_name = "foo"]
#![crate_type = "lib"]

pub struct Foo {
pub some_func: for<'a> fn(val: &'a i32) -> i32,
pub some_trait: dyn for<'a> Trait<'a>,
}

// @has foo/struct.Foo.html '//span[@id="structfield.some_func"]' "some_func: for<'a> fn(val: &'a i32) -> i32"
// @has foo/struct.Foo.html '//span[@id="structfield.some_trait"]' "some_trait: dyn Trait<'a>"

pub trait Trait<'a> {}