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

Add support for tuple struct field documentation #87451

Merged
merged 5 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 1 addition & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1730,9 +1730,7 @@ impl Clean<Variant> for hir::VariantData<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> Variant {
match self {
hir::VariantData::Struct(..) => Variant::Struct(self.clean(cx)),
hir::VariantData::Tuple(..) => {
Variant::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
}
hir::VariantData::Tuple(..) => Variant::Struct(self.clean(cx)),
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
Copy link

@dsherret dsherret Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this cause #87887?

It seems like tuple variants no longer exist in the outputted json, but shouldn't tuple variants be described as tuple variants?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very likely yes.

hir::VariantData::Unit(..) => Variant::CLike,
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,9 @@ fn sidebar_struct(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clea
}

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

Expand Down
28 changes: 16 additions & 12 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,8 +1037,9 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
write!(w, "<div class=\"sub-variant\" id=\"{id}\">", id = variant_id);
write!(
w,
"<h3>Fields of <b>{name}</b></h3><div>",
name = variant.name.as_ref().unwrap()
"<h3>{extra}Fields of <b>{name}</b></h3><div>",
extra = if s.struct_type == CtorKind::Fn { "Tuple " } else { "" },
name = variant.name.as_ref().unwrap(),
);
for field in &s.fields {
use crate::clean::StructFieldItem;
Expand Down Expand Up @@ -1176,21 +1177,21 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
_ => None,
})
.peekable();
if let CtorKind::Fictive = s.struct_type {
if let CtorKind::Fictive | CtorKind::Fn = s.struct_type {
if fields.peek().is_some() {
write!(
w,
"<h2 id=\"fields\" class=\"fields small-section-header\">\
Fields{}<a href=\"#fields\" class=\"anchor\"></a></h2>",
{}{}<a href=\"#fields\" class=\"anchor\"></a>\
</h2>",
if let CtorKind::Fictive = s.struct_type { "Fields" } else { "Tuple Fields" },
document_non_exhaustive_header(it)
);
document_non_exhaustive(w, it);
for (field, ty) in fields {
let id = cx.derive_id(format!(
"{}.{}",
ItemType::StructField,
field.name.as_ref().unwrap()
));
for (index, (field, ty)) in fields.enumerate() {
let field_name =
field.name.map_or_else(|| index.to_string(), |sym| (*sym.as_str()).to_string());
let id = cx.derive_id(format!("{}.{}", ItemType::StructField, field_name));
write!(
w,
"<span id=\"{id}\" class=\"{item_type} small-section-header\">\
Expand All @@ -1199,7 +1200,7 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
</span>",
item_type = ItemType::StructField,
id = id,
name = field.name.as_ref().unwrap(),
name = field_name,
ty = ty.print(cx)
);
document(w, cx, field, Some(it));
Expand Down Expand Up @@ -1509,7 +1510,10 @@ fn render_struct(
if let Some(g) = g {
write!(w, "{}", print_where_clause(g, cx, 0, false),)
}
w.write_str(";");
// We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
if structhead {
w.write_str(";");
}
}
CtorKind::Const => {
// Needed for PhantomData.
Expand Down
4 changes: 2 additions & 2 deletions src/test/rustdoc-ui/coverage/enums.stdout
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
+-------------------------------------+------------+------------+------------+------------+
| File | Documented | Percentage | Examples | Percentage |
+-------------------------------------+------------+------------+------------+------------+
| ...est/rustdoc-ui/coverage/enums.rs | 6 | 75.0% | 0 | 0.0% |
| ...est/rustdoc-ui/coverage/enums.rs | 6 | 66.7% | 0 | 0.0% |
+-------------------------------------+------------+------------+------------+------------+
| Total | 6 | 75.0% | 0 | 0.0% |
| Total | 6 | 66.7% | 0 | 0.0% |
+-------------------------------------+------------+------------+------------+------------+
4 changes: 2 additions & 2 deletions src/test/rustdoc/toggle-item-contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ pub enum EnumStructVariant {
}

// @has 'toggle_item_contents/enum.LargeEnum.html'
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 variants'
// @count - '//*[@class="rust enum"]//details[@class="rustdoc-toggle type-contents-toggle"]' 1
// @has - '//*[@class="rust enum"]//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 variants'
pub enum LargeEnum {
A, B, C, D, E, F(u8), G, H, I, J, K, L, M
}
Expand Down
36 changes: 36 additions & 0 deletions src/test/rustdoc/tuple-struct-fields-doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![crate_name = "foo"]

// @has foo/struct.Foo.html
// @has - '//h2[@id="fields"]' 'Tuple Fields'
// @has - '//h3[@class="sidebar-title"]/a[@href="#fields"]' 'Tuple Fields'
// @has - '//*[@id="structfield.0"]' '0: u32'
// @has - '//*[@id="main"]/div[@class="docblock"]' 'hello'
// @!has - '//*[@id="structfield.1"]'
// @has - '//*[@id="structfield.2"]' '2: char'
// @has - '//*[@id="structfield.3"]' '3: i8'
// @has - '//*[@id="main"]/div[@class="docblock"]' 'not hello'
pub struct Foo(
/// hello
pub u32,
char,
pub char,
/// not hello
pub i8,
);
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved

// @has foo/enum.Bar.html
// @has - '//pre[@class="rust enum"]' 'BarVariant(String),'
// @matches - '//*[@id="variant.BarVariant.fields"]/h3' '^Tuple Fields of BarVariant$'
// @has - '//*[@id="variant.BarVariant.field.0"]' '0: String'
// @has - '//*[@id="variant.BarVariant.fields"]//*[@class="docblock"]' 'Hello docs'
// @matches - '//*[@id="variant.FooVariant.fields"]/h3' '^Fields of FooVariant$'
pub enum Bar {
BarVariant(
/// Hello docs
String
),
FooVariant {
/// hello
x: u32,
},
}