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 https://github.com/rust-lang/rust/issues/83255. cc ```@camelid``` (since you were involved on the original PR). r? ```@jyn514```
This commit is contained in:
commit
014e22c836
@ -1730,9 +1730,13 @@ impl Clean<Variant> for hir::VariantData<'_> {
|
|||||||
fn clean(&self, cx: &mut DocContext<'_>) -> Variant {
|
fn clean(&self, cx: &mut DocContext<'_>) -> Variant {
|
||||||
match self {
|
match self {
|
||||||
hir::VariantData::Struct(..) => Variant::Struct(self.clean(cx)),
|
hir::VariantData::Struct(..) => Variant::Struct(self.clean(cx)),
|
||||||
hir::VariantData::Tuple(..) => {
|
// Important note here: `Variant::Tuple` is used on tuple structs which are not in an
|
||||||
Variant::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
|
// enum (so where converting from `ty::VariantDef`). In case we are in an enum, the kind
|
||||||
}
|
// is provided by the `Variant` wrapper directly, and since we need the fields' name
|
||||||
|
// (even for a tuple struct variant!), it's simpler to just store it as a
|
||||||
|
// `Variant::Struct` instead of a `Variant::Tuple` (otherwise it would force us to make
|
||||||
|
// a lot of changes when rendering them to generate the name as well).
|
||||||
|
hir::VariantData::Tuple(..) => Variant::Struct(self.clean(cx)),
|
||||||
hir::VariantData::Unit(..) => Variant::CLike,
|
hir::VariantData::Unit(..) => Variant::CLike,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2007,6 +2007,9 @@ fn sidebar_struct(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, s: &clea
|
|||||||
}
|
}
|
||||||
|
|
||||||
sidebar.push_str("</div>");
|
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>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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, "<div class=\"sub-variant\" id=\"{id}\">", id = variant_id);
|
||||||
write!(
|
write!(
|
||||||
w,
|
w,
|
||||||
"<h3>Fields of <b>{name}</b></h3><div>",
|
"<h3>{extra}Fields of <b>{name}</b></h3><div>",
|
||||||
name = variant.name.as_ref().unwrap()
|
extra = if s.struct_type == CtorKind::Fn { "Tuple " } else { "" },
|
||||||
|
name = variant.name.as_ref().unwrap(),
|
||||||
);
|
);
|
||||||
for field in &s.fields {
|
for field in &s.fields {
|
||||||
use crate::clean::StructFieldItem;
|
use crate::clean::StructFieldItem;
|
||||||
@ -1176,21 +1177,21 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
|
|||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.peekable();
|
.peekable();
|
||||||
if let CtorKind::Fictive = s.struct_type {
|
if let CtorKind::Fictive | CtorKind::Fn = s.struct_type {
|
||||||
if fields.peek().is_some() {
|
if fields.peek().is_some() {
|
||||||
write!(
|
write!(
|
||||||
w,
|
w,
|
||||||
"<h2 id=\"fields\" class=\"fields small-section-header\">\
|
"<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_header(it)
|
||||||
);
|
);
|
||||||
document_non_exhaustive(w, it);
|
document_non_exhaustive(w, it);
|
||||||
for (field, ty) in fields {
|
for (index, (field, ty)) in fields.enumerate() {
|
||||||
let id = cx.derive_id(format!(
|
let field_name =
|
||||||
"{}.{}",
|
field.name.map_or_else(|| index.to_string(), |sym| (*sym.as_str()).to_string());
|
||||||
ItemType::StructField,
|
let id = cx.derive_id(format!("{}.{}", ItemType::StructField, field_name));
|
||||||
field.name.as_ref().unwrap()
|
|
||||||
));
|
|
||||||
write!(
|
write!(
|
||||||
w,
|
w,
|
||||||
"<span id=\"{id}\" class=\"{item_type} small-section-header\">\
|
"<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
|
|||||||
</span>",
|
</span>",
|
||||||
item_type = ItemType::StructField,
|
item_type = ItemType::StructField,
|
||||||
id = id,
|
id = id,
|
||||||
name = field.name.as_ref().unwrap(),
|
name = field_name,
|
||||||
ty = ty.print(cx)
|
ty = ty.print(cx)
|
||||||
);
|
);
|
||||||
document(w, cx, field, Some(it));
|
document(w, cx, field, Some(it));
|
||||||
@ -1507,7 +1508,10 @@ fn render_struct(
|
|||||||
if let Some(g) = g {
|
if let Some(g) = g {
|
||||||
write!(w, "{}", print_where_clause(g, cx, 0, false),)
|
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 => {
|
CtorKind::Const => {
|
||||||
// Needed for PhantomData.
|
// Needed for PhantomData.
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
+-------------------------------------+------------+------------+------------+------------+
|
+-------------------------------------+------------+------------+------------+------------+
|
||||||
| File | Documented | Percentage | Examples | Percentage |
|
| 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% |
|
||||||
+-------------------------------------+------------+------------+------------+------------+
|
+-------------------------------------+------------+------------+------------+------------+
|
||||||
|
@ -81,8 +81,8 @@ pub enum EnumStructVariant {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// @has 'toggle_item_contents/enum.LargeEnum.html'
|
// @has 'toggle_item_contents/enum.LargeEnum.html'
|
||||||
// @count - '//details[@class="rustdoc-toggle type-contents-toggle"]' 1
|
// @count - '//*[@class="rust enum"]//details[@class="rustdoc-toggle type-contents-toggle"]' 1
|
||||||
// @has - '//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 variants'
|
// @has - '//*[@class="rust enum"]//details[@class="rustdoc-toggle type-contents-toggle"]' 'Show 13 variants'
|
||||||
pub enum LargeEnum {
|
pub enum LargeEnum {
|
||||||
A, B, C, D, E, F(u8), G, H, I, J, K, L, M
|
A, B, C, D, E, F(u8), G, H, I, J, K, L, M
|
||||||
}
|
}
|
||||||
|
36
src/test/rustdoc/tuple-struct-fields-doc.rs
Normal file
36
src/test/rustdoc/tuple-struct-fields-doc.rs
Normal file
@ -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,
|
||||||
|
);
|
||||||
|
|
||||||
|
// @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,
|
||||||
|
},
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user