Skip to content

Commit 014e22c

Browse files
authored
Rollup merge of #87451 - GuillaumeGomez:tuple-struct-field-doc, r=jyn514
Add support for tuple struct field documentation Fixes #42615. This is #80320 updated to new codebase and with added tests. Part of #83255. cc ```@camelid``` (since you were involved on the original PR). r? ```@jyn514```
2 parents 9222984 + c4aa735 commit 014e22c

File tree

6 files changed

+66
-19
lines changed

6 files changed

+66
-19
lines changed

src/librustdoc/clean/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1730,9 +1730,13 @@ impl Clean<Variant> for hir::VariantData<'_> {
17301730
fn clean(&self, cx: &mut DocContext<'_>) -> Variant {
17311731
match self {
17321732
hir::VariantData::Struct(..) => Variant::Struct(self.clean(cx)),
1733-
hir::VariantData::Tuple(..) => {
1734-
Variant::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
1735-
}
1733+
// Important note here: `Variant::Tuple` is used on tuple structs which are not in an
1734+
// enum (so where converting from `ty::VariantDef`). In case we are in an enum, the kind
1735+
// is provided by the `Variant` wrapper directly, and since we need the fields' name
1736+
// (even for a tuple struct variant!), it's simpler to just store it as a
1737+
// `Variant::Struct` instead of a `Variant::Tuple` (otherwise it would force us to make
1738+
// a lot of changes when rendering them to generate the name as well).
1739+
hir::VariantData::Tuple(..) => Variant::Struct(self.clean(cx)),
17361740
hir::VariantData::Unit(..) => Variant::CLike,
17371741
}
17381742
}

src/librustdoc/html/render/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,9 @@ fn sidebar_struct(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clea
20072007
}
20082008

20092009
sidebar.push_str("</div>");
2010+
} else if let CtorKind::Fn = s.struct_type {
2011+
sidebar
2012+
.push_str("<h3 class=\"sidebar-title\"><a href=\"#fields\">Tuple Fields</a></h3>");
20102013
}
20112014
}
20122015

src/librustdoc/html/render/print_item.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -1037,8 +1037,9 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
10371037
write!(w, "<div class=\"sub-variant\" id=\"{id}\">", id = variant_id);
10381038
write!(
10391039
w,
1040-
"<h3>Fields of <b>{name}</b></h3><div>",
1041-
name = variant.name.as_ref().unwrap()
1040+
"<h3>{extra}Fields of <b>{name}</b></h3><div>",
1041+
extra = if s.struct_type == CtorKind::Fn { "Tuple " } else { "" },
1042+
name = variant.name.as_ref().unwrap(),
10421043
);
10431044
for field in &s.fields {
10441045
use crate::clean::StructFieldItem;
@@ -1176,21 +1177,21 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
11761177
_ => None,
11771178
})
11781179
.peekable();
1179-
if let CtorKind::Fictive = s.struct_type {
1180+
if let CtorKind::Fictive | CtorKind::Fn = s.struct_type {
11801181
if fields.peek().is_some() {
11811182
write!(
11821183
w,
11831184
"<h2 id=\"fields\" class=\"fields small-section-header\">\
1184-
Fields{}<a href=\"#fields\" class=\"anchor\"></a></h2>",
1185+
{}{}<a href=\"#fields\" class=\"anchor\"></a>\
1186+
</h2>",
1187+
if let CtorKind::Fictive = s.struct_type { "Fields" } else { "Tuple Fields" },
11851188
document_non_exhaustive_header(it)
11861189
);
11871190
document_non_exhaustive(w, it);
1188-
for (field, ty) in fields {
1189-
let id = cx.derive_id(format!(
1190-
"{}.{}",
1191-
ItemType::StructField,
1192-
field.name.as_ref().unwrap()
1193-
));
1191+
for (index, (field, ty)) in fields.enumerate() {
1192+
let field_name =
1193+
field.name.map_or_else(|| index.to_string(), |sym| (*sym.as_str()).to_string());
1194+
let id = cx.derive_id(format!("{}.{}", ItemType::StructField, field_name));
11941195
write!(
11951196
w,
11961197
"<span id=\"{id}\" class=\"{item_type} small-section-header\">\
@@ -1199,7 +1200,7 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
11991200
</span>",
12001201
item_type = ItemType::StructField,
12011202
id = id,
1202-
name = field.name.as_ref().unwrap(),
1203+
name = field_name,
12031204
ty = ty.print(cx)
12041205
);
12051206
document(w, cx, field, Some(it));
@@ -1507,7 +1508,10 @@ fn render_struct(
15071508
if let Some(g) = g {
15081509
write!(w, "{}", print_where_clause(g, cx, 0, false),)
15091510
}
1510-
w.write_str(";");
1511+
// We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
1512+
if structhead {
1513+
w.write_str(";");
1514+
}
15111515
}
15121516
CtorKind::Const => {
15131517
// Needed for PhantomData.
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
+-------------------------------------+------------+------------+------------+------------+
22
| File | Documented | Percentage | Examples | Percentage |
33
+-------------------------------------+------------+------------+------------+------------+
4-
| ...est/rustdoc-ui/coverage/enums.rs | 6 | 75.0% | 0 | 0.0% |
4+
| ...est/rustdoc-ui/coverage/enums.rs | 6 | 66.7% | 0 | 0.0% |
55
+-------------------------------------+------------+------------+------------+------------+
6-
| Total | 6 | 75.0% | 0 | 0.0% |
6+
| Total | 6 | 66.7% | 0 | 0.0% |
77
+-------------------------------------+------------+------------+------------+------------+

src/test/rustdoc/toggle-item-contents.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ pub enum EnumStructVariant {
8181
}
8282

8383
// @has 'toggle_item_contents/enum.LargeEnum.html'
84-
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
85-
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 variants'
84+
// @count - '//*[@class="rust enum"]//details[@class="rustdoc-toggle type-contents-toggle"]' 1
85+
// @has - '//*[@class="rust enum"]//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 variants'
8686
pub enum LargeEnum {
8787
A, B, C, D, E, F(u8), G, H, I, J, K, L, M
8888
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![crate_name = "foo"]
2+
3+
// @has foo/struct.Foo.html
4+
// @has - '//h2[@id="fields"]' 'Tuple Fields'
5+
// @has - '//h3[@class="sidebar-title"]/a[@href="#fields"]' 'Tuple Fields'
6+
// @has - '//*[@id="structfield.0"]' '0: u32'
7+
// @has - '//*[@id="main"]/div[@class="docblock"]' 'hello'
8+
// @!has - '//*[@id="structfield.1"]'
9+
// @has - '//*[@id="structfield.2"]' '2: char'
10+
// @has - '//*[@id="structfield.3"]' '3: i8'
11+
// @has - '//*[@id="main"]/div[@class="docblock"]' 'not hello'
12+
pub struct Foo(
13+
/// hello
14+
pub u32,
15+
char,
16+
pub char,
17+
/// not hello
18+
pub i8,
19+
);
20+
21+
// @has foo/enum.Bar.html
22+
// @has - '//pre[@class="rust enum"]' 'BarVariant(String),'
23+
// @matches - '//*[@id="variant.BarVariant.fields"]/h3' '^Tuple Fields of BarVariant$'
24+
// @has - '//*[@id="variant.BarVariant.field.0"]' '0: String'
25+
// @has - '//*[@id="variant.BarVariant.fields"]//*[@class="docblock"]' 'Hello docs'
26+
// @matches - '//*[@id="variant.FooVariant.fields"]/h3' '^Fields of FooVariant$'
27+
pub enum Bar {
28+
BarVariant(
29+
/// Hello docs
30+
String
31+
),
32+
FooVariant {
33+
/// hello
34+
x: u32,
35+
},
36+
}

0 commit comments

Comments
 (0)