Skip to content

Commit 3776358

Browse files
committed
Auto merge of #145812 - karolzwolak:attrs-in-fields-and-variants-section, r=GuillaumeGomez
rustdoc: render attributes in Field and Variants sections Follow up to #145782. Render attributes in Field and Variants sections. Associated constants and methods are already rendered with attributes in their sections, so I figured out fields and variants should too. (no change here) <img width="378" height="265" alt="image" src="https://github.com/user-attachments/assets/b4f45c42-0146-486e-8881-138d2a7ad1c4" /> r? `@GuillaumeGomez` --- Before (left) / after (right): <img width="396" height="519" alt="image" src="https://github.com/user-attachments/assets/18288e13-09e7-448c-ba98-2023fa6df597" /> <img width="382" height="597" alt="image" src="https://github.com/user-attachments/assets/e624dc84-d169-41cc-bb89-7a1c2b2bb3e3" /> <img width="371" height="313" alt="image" src="https://github.com/user-attachments/assets/29833645-0b93-4900-80a8-c5a1e0b541b4" /> <img width="371" height="331" alt="image" src="https://github.com/user-attachments/assets/d5ce4b9e-f7f1-4f36-8ac0-08b0b5077e48" /> <img width="362" height="309" alt="image" src="https://github.com/user-attachments/assets/0436a51d-29a5-4403-a27c-7697524f807a" /> <img width="357" height="332" alt="image" src="https://github.com/user-attachments/assets/9a759fc5-30e7-4bbb-a88a-a3e3d1ed02aa" />
2 parents 41a79f1 + b8313df commit 3776358

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)