Skip to content

Commit 3eac643

Browse files
authored
Rollup merge of #79991 - camelid:rustdoc-for-lifetime, r=GuillaumeGomez,jyn514
rustdoc: Render HRTB correctly for bare functions The angle brackets were not rendered, so code like this: some_func: for<'a> fn(val: &'a i32) -> i32 would be rendered as: some_func: fn'a(val: &'a i32) -> i32 However, rendering with angle brackets is still invalid syntax: some_func: fn<'a>(val: &'a i32) -> i32 so now it renders correctly as: some_func: for<'a> fn(val: &'a i32) -> i32 ----- However, note that this code: some_trait: dyn for<'a> Trait<'a> will still render as: some_trait: dyn Trait<'a> which is not invalid syntax, but is still unclear. Unfortunately I think it's hard to fix that case because there isn't enough information in the `rustdoc::clean::Type` that this code operates on. Perhaps that case can be fixed in a later PR. r? ``@jyn514``
2 parents 4003a73 + cd8dcee commit 3eac643

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

src/librustdoc/html/format.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,8 @@ fn fmt_type(
659659
use_absolute: bool,
660660
cache: &Cache,
661661
) -> fmt::Result {
662+
debug!("fmt_type(t = {:?})", t);
663+
662664
match *t {
663665
clean::Generic(name) => write!(f, "{}", name),
664666
clean::ResolvedPath { did, ref param_names, ref path, is_generic } => {
@@ -675,21 +677,22 @@ fn fmt_type(
675677
if f.alternate() {
676678
write!(
677679
f,
678-
"{}{:#}fn{:#}{:#}",
680+
"{:#}{}{:#}fn{:#}",
681+
decl.print_hrtb_with_space(cache),
679682
decl.unsafety.print_with_space(),
680683
print_abi_with_space(decl.abi),
681-
decl.print_generic_params(cache),
682684
decl.decl.print(cache)
683685
)
684686
} else {
685687
write!(
686688
f,
687-
"{}{}",
689+
"{}{}{}",
690+
decl.print_hrtb_with_space(cache),
688691
decl.unsafety.print_with_space(),
689692
print_abi_with_space(decl.abi)
690693
)?;
691694
primitive_link(f, PrimitiveType::Fn, "fn", cache)?;
692-
write!(f, "{}{}", decl.print_generic_params(cache), decl.decl.print(cache))
695+
write!(f, "{}", decl.decl.print(cache))
693696
}
694697
}
695698
clean::Tuple(ref typs) => {
@@ -992,8 +995,14 @@ impl clean::FnRetTy {
992995
}
993996

994997
impl clean::BareFunctionDecl {
995-
fn print_generic_params<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a {
996-
comma_sep(self.generic_params.iter().map(move |g| g.print(cache)))
998+
fn print_hrtb_with_space<'a>(&'a self, cache: &'a Cache) -> impl fmt::Display + 'a {
999+
display_fn(move |f| {
1000+
if !self.generic_params.is_empty() {
1001+
write!(f, "for<{}> ", comma_sep(self.generic_params.iter().map(|g| g.print(cache))))
1002+
} else {
1003+
Ok(())
1004+
}
1005+
})
9971006
}
9981007
}
9991008

src/test/rustdoc/fn-type.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ignore-tidy-linelength
2+
3+
#![crate_name = "foo"]
4+
#![crate_type = "lib"]
5+
6+
pub struct Foo<'a, T> {
7+
pub generic: fn(val: &T) -> T,
8+
9+
pub lifetime: fn(val: &'a i32) -> i32,
10+
pub hrtb_lifetime: for<'b, 'c> fn(one: &'b i32, two: &'c &'b i32) -> (&'b i32, &'c i32),
11+
}
12+
13+
// @has 'foo/struct.Foo.html' '//span[@id="structfield.generic"]' "generic: fn(val: &T) -> T"
14+
// @has 'foo/struct.Foo.html' '//span[@id="structfield.lifetime"]' "lifetime: fn(val: &'a i32) -> i32"
15+
// @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)"

src/test/rustdoc/for-lifetime.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ignore-tidy-linelength
2+
3+
#![crate_name = "foo"]
4+
#![crate_type = "lib"]
5+
6+
pub struct Foo {
7+
pub some_func: for<'a> fn(val: &'a i32) -> i32,
8+
pub some_trait: dyn for<'a> Trait<'a>,
9+
}
10+
11+
// @has foo/struct.Foo.html '//span[@id="structfield.some_func"]' "some_func: for<'a> fn(val: &'a i32) -> i32"
12+
// @has foo/struct.Foo.html '//span[@id="structfield.some_trait"]' "some_trait: dyn Trait<'a>"
13+
14+
pub trait Trait<'a> {}

0 commit comments

Comments
 (0)