Skip to content

Commit b8313df

Browse files
committed
rustdoc: render attributes in Field and Variants sections
1 parent 4eedad3 commit b8313df

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/librustdoc/html/render/print_item.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,13 @@ impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
14601460
)
14611461
}
14621462

1463+
fn print_field_attrs(&self, field: &'a clean::Item) -> impl Display {
1464+
fmt::from_fn(move |w| {
1465+
render_attributes_in_code(w, field, "", self.cx);
1466+
Ok(())
1467+
})
1468+
}
1469+
14631470
fn document_field(&self, field: &'a clean::Item) -> impl Display {
14641471
document(self.cx, field, Some(self.it), HeadingOffset::H3)
14651472
}
@@ -1770,6 +1777,7 @@ fn item_variants(
17701777
)
17711778
.maybe_display()
17721779
)?;
1780+
render_attributes_in_code(w, variant, "", cx);
17731781
if let clean::VariantItem(ref var) = variant.kind
17741782
&& let clean::VariantKind::CLike = var.kind
17751783
{
@@ -1843,7 +1851,12 @@ fn item_variants(
18431851
"<div class=\"sub-variant-field\">\
18441852
<span id=\"{id}\" class=\"section-header\">\
18451853
<a href=\"#{id}\" class=\"anchor field\">§</a>\
1846-
<code>{f}: {t}</code>\
1854+
<code>"
1855+
)?;
1856+
render_attributes_in_code(w, field, "", cx);
1857+
write!(
1858+
w,
1859+
"{f}: {t}</code>\
18471860
</span>\
18481861
{doc}\
18491862
</div>",
@@ -2079,10 +2092,15 @@ fn item_fields(
20792092
w,
20802093
"<span id=\"{id}\" class=\"{item_type} section-header\">\
20812094
<a href=\"#{id}\" class=\"anchor field\">§</a>\
2082-
<code>{field_name}: {ty}</code>\
2095+
<code>",
2096+
item_type = ItemType::StructField,
2097+
)?;
2098+
render_attributes_in_code(w, field, "", cx);
2099+
write!(
2100+
w,
2101+
"{field_name}: {ty}</code>\
20832102
</span>\
20842103
{doc}",
2085-
item_type = ItemType::StructField,
20862104
ty = ty.print(cx),
20872105
doc = document(cx, field, Some(it), HeadingOffset::H3),
20882106
)?;

src/librustdoc/html/templates/item_union.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ <h2 id="fields" class="fields section-header"> {# #}
1212
{% let name = field.name.expect("union field name") %}
1313
<span id="structfield.{{ name }}" class="{{ ItemType::StructField +}} section-header"> {# #}
1414
<a href="#structfield.{{ name }}" class="anchor field">§</a> {# #}
15-
<code>{{ name }}: {{+ self.print_ty(ty)|safe }}</code> {# #}
15+
<code>{{+ self.print_field_attrs(field)|safe }}{{ name }}: {{+ self.print_ty(ty)|safe }}</code> {# #}
1616
</span>
1717
{% if let Some(stability_class) = self.stability_field(field) %}
1818
<span class="stab {{ stability_class }}"></span>

tests/rustdoc/attributes.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ macro_rules! macro_rule {
2828
#[unsafe(link_section = "enum")]
2929
pub enum Enum {
3030
//@ has 'foo/enum.Enum.html' '//*[@class="code-attribute"]' '#[unsafe(link_section = "a")]'
31+
//@ has - '//*[@class="variants"]//*[@class="code-attribute"]' '#[unsafe(link_section = "a")]'
3132
#[unsafe(link_section = "a")]
3233
A,
3334
//@ has 'foo/enum.Enum.html' '//*[@class="code-attribute"]' '#[unsafe(link_section = "quz")]'
35+
//@ has - '//*[@class="variants"]//*[@class="code-attribute"]' '#[unsafe(link_section = "quz")]'
3436
#[unsafe(link_section = "quz")]
3537
Quz {
3638
//@ has 'foo/enum.Enum.html' '//*[@class="code-attribute"]' '#[unsafe(link_section = "b")]'
39+
//@ has - '//*[@class="variants"]//*[@class="code-attribute"]' '#[unsafe(link_section = "b")]'
3740
#[unsafe(link_section = "b")]
3841
b: (),
3942
},
@@ -66,6 +69,7 @@ pub union Union {
6669
#[unsafe(link_section = "struct")]
6770
pub struct Struct {
6871
//@ has 'foo/struct.Struct.html' '//*[@class="code-attribute"]' '#[unsafe(link_section = "x")]'
72+
//@ has - '//*[@id="structfield.x"]//*[@class="code-attribute"]' '#[unsafe(link_section = "x")]'
6973
#[unsafe(link_section = "x")]
7074
pub x: u32,
7175
y: f32,

0 commit comments

Comments
 (0)