Skip to content

Commit 273d673

Browse files
committed
render doc hidden via code attribute helper
1 parent 1052fb3 commit 273d673

3 files changed

Lines changed: 64 additions & 31 deletions

File tree

src/librustdoc/html/render/mod.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ fn assoc_const(
10381038
) -> impl fmt::Display {
10391039
let tcx = cx.tcx();
10401040
fmt::from_fn(move |w| {
1041-
render_attributes_in_code(w, it, &" ".repeat(indent), cx)?;
1041+
let _ = render_attributes_in_code(w, it, &" ".repeat(indent), cx)?;
10421042
write!(
10431043
w,
10441044
"{indent}{vis}const <a{href} class=\"constant\">{name}</a>{generics}: {ty}",
@@ -1078,7 +1078,7 @@ fn assoc_type(
10781078
cx: &Context<'_>,
10791079
) -> impl fmt::Display {
10801080
fmt::from_fn(move |w| {
1081-
render_attributes_in_code(w, it, &" ".repeat(indent), cx)?;
1081+
let _ = render_attributes_in_code(w, it, &" ".repeat(indent), cx)?;
10821082
write!(
10831083
w,
10841084
"{indent}{vis}type <a{href} class=\"associatedtype\">{name}</a>{generics}",
@@ -1147,10 +1147,10 @@ fn assoc_method(
11471147
let (indent, indent_str, end_newline) = if parent == ItemType::Trait {
11481148
header_len += 4;
11491149
let indent_str = " ";
1150-
render_attributes_in_code(w, meth, indent_str, cx)?;
1150+
let _ = render_attributes_in_code(w, meth, indent_str, cx)?;
11511151
(4, indent_str, Ending::NoNewline)
11521152
} else {
1153-
render_attributes_in_code(w, meth, "", cx)?;
1153+
let _ = render_attributes_in_code(w, meth, "", cx)?;
11541154
(0, "", Ending::Newline)
11551155
};
11561156
write!(
@@ -2915,9 +2915,31 @@ fn render_attributes_in_code(
29152915
item: &clean::Item,
29162916
prefix: &str,
29172917
cx: &Context<'_>,
2918-
) -> fmt::Result {
2919-
if item.is_doc_hidden() {
2920-
render_code_attribute(prefix, "#[doc(hidden)]", w)?;
2918+
) -> Result<bool, fmt::Error> {
2919+
render_attributes_in_code_with_options(w, item, prefix, cx, true, None)
2920+
}
2921+
2922+
pub(super) fn render_attributes_in_code_with_options(
2923+
w: &mut impl fmt::Write,
2924+
item: &clean::Item,
2925+
prefix: &str,
2926+
cx: &Context<'_>,
2927+
render_doc_hidden: bool,
2928+
open_tag: Option<&str>,
2929+
) -> Result<bool, fmt::Error> {
2930+
let mut wrote_any = false;
2931+
let mut render_attr = |attr: &str| -> fmt::Result {
2932+
if !wrote_any {
2933+
if let Some(open_tag) = open_tag {
2934+
w.write_str(open_tag)?;
2935+
}
2936+
wrote_any = true;
2937+
}
2938+
render_code_attribute(prefix, attr, w)
2939+
};
2940+
2941+
if render_doc_hidden && item.is_doc_hidden() {
2942+
render_attr("#[doc(hidden)]")?;
29212943
}
29222944
for attr in &item.attrs.other_attrs {
29232945
let hir::Attribute::Parsed(kind) = attr else { continue };
@@ -2932,15 +2954,15 @@ fn render_attributes_in_code(
29322954
AttributeKind::NonExhaustive(..) => Cow::Borrowed("#[non_exhaustive]"),
29332955
_ => continue,
29342956
};
2935-
render_code_attribute(prefix, attr.as_ref(), w)?;
2957+
render_attr(attr.as_ref())?;
29362958
}
29372959

29382960
if let Some(def_id) = item.def_id()
29392961
&& let Some(repr) = repr_attribute(cx.tcx(), cx.cache(), def_id)
29402962
{
2941-
render_code_attribute(prefix, &repr, w)?;
2963+
render_attr(&repr)?;
29422964
}
2943-
Ok(())
2965+
Ok(wrote_any)
29442966
}
29452967

29462968
fn render_repr_attribute_in_code(

src/librustdoc/html/render/print_item.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,18 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
358358
use crate::html::format::print_anchor;
359359

360360
let visibility_and_hidden = visibility_and_hidden(myitem);
361-
write!(w, "<dt><code>")?;
362-
render_attributes_in_code(w, myitem, "", cx)?;
361+
// Module listings use the hidden marker, so skip doc(hidden) here.
362+
let wrote_attrs = super::render_attributes_in_code_with_options(
363+
w,
364+
myitem,
365+
"",
366+
cx,
367+
false,
368+
Some("<dt><code>"),
369+
)?;
370+
if !wrote_attrs {
371+
w.write_str("<dt><code>")?;
372+
}
363373
match *src {
364374
Some(src) => {
365375
write!(
@@ -561,7 +571,7 @@ fn item_function(cx: &Context<'_>, it: &clean::Item, f: &clean::Function) -> imp
561571
let notable_traits = notable_traits_button(&f.decl.output, cx).maybe_display();
562572

563573
wrap_item(w, |w| {
564-
render_attributes_in_code(w, it, "", cx)?;
574+
let _ = render_attributes_in_code(w, it, "", cx)?;
565575
write!(
566576
w,
567577
"{vis}{constness}{asyncness}{safety}{abi}fn \
@@ -623,7 +633,7 @@ fn item_trait(cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) -> impl fmt:
623633

624634
// Output the trait definition
625635
wrap_item(w, |mut w| {
626-
render_attributes_in_code(&mut w, it, "", cx)?;
636+
let _ = render_attributes_in_code(&mut w, it, "", cx)?;
627637
write!(
628638
w,
629639
"{vis}{safety}{is_auto}trait {name}{generics}{bounds}",
@@ -1201,7 +1211,7 @@ fn item_trait_alias(
12011211
) -> impl fmt::Display {
12021212
fmt::from_fn(|w| {
12031213
wrap_item(w, |w| {
1204-
render_attributes_in_code(w, it, "", cx)?;
1214+
let _ = render_attributes_in_code(w, it, "", cx)?;
12051215
write!(
12061216
w,
12071217
"trait {name}{generics} = {bounds}{where_clause};",
@@ -1229,7 +1239,7 @@ fn item_trait_alias(
12291239
fn item_type_alias(cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) -> impl fmt::Display {
12301240
fmt::from_fn(|w| {
12311241
wrap_item(w, |w| {
1232-
render_attributes_in_code(w, it, "", cx)?;
1242+
let _ = render_attributes_in_code(w, it, "", cx)?;
12331243
write!(
12341244
w,
12351245
"{vis}type {name}{generics}{where_clause} = {type_};",
@@ -1437,7 +1447,7 @@ impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
14371447

14381448
fn print_field_attrs(&self, field: &'a clean::Item) -> impl Display {
14391449
fmt::from_fn(move |w| {
1440-
render_attributes_in_code(w, field, "", self.cx)?;
1450+
let _ = render_attributes_in_code(w, field, "", self.cx)?;
14411451
Ok(())
14421452
})
14431453
}
@@ -1529,7 +1539,7 @@ impl<'clean> DisplayEnum<'clean> {
15291539
// For now the only attributes we render for type aliases are `repr` attributes.
15301540
render_repr_attribute_in_code(w, cx, self.def_id)?;
15311541
} else {
1532-
render_attributes_in_code(w, it, "", cx)?;
1542+
let _ = render_attributes_in_code(w, it, "", cx)?;
15331543
}
15341544
write!(
15351545
w,
@@ -1668,7 +1678,7 @@ fn render_enum_fields(
16681678
if v.is_stripped() {
16691679
continue;
16701680
}
1671-
render_attributes_in_code(w, v, TAB, cx)?;
1681+
let _ = render_attributes_in_code(w, v, TAB, cx)?;
16721682
w.write_str(TAB)?;
16731683
match v.kind {
16741684
clean::VariantItem(ref var) => match var.kind {
@@ -1752,7 +1762,7 @@ fn item_variants(
17521762
)
17531763
.maybe_display()
17541764
)?;
1755-
render_attributes_in_code(w, variant, "", cx)?;
1765+
let _ = render_attributes_in_code(w, variant, "", cx)?;
17561766
if let clean::VariantItem(ref var) = variant.kind
17571767
&& let clean::VariantKind::CLike = var.kind
17581768
{
@@ -1828,7 +1838,7 @@ fn item_variants(
18281838
<a href=\"#{id}\" class=\"anchor field\">§</a>\
18291839
<code>"
18301840
)?;
1831-
render_attributes_in_code(w, field, "", cx)?;
1841+
let _ = render_attributes_in_code(w, field, "", cx)?;
18321842
write!(
18331843
w,
18341844
"{f}: {t}</code>\
@@ -1853,7 +1863,7 @@ fn item_variants(
18531863
fn item_macro(cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) -> impl fmt::Display {
18541864
fmt::from_fn(|w| {
18551865
wrap_item(w, |w| {
1856-
render_attributes_in_code(w, it, "", cx)?;
1866+
let _ = render_attributes_in_code(w, it, "", cx)?;
18571867
if !t.macro_rules {
18581868
write!(w, "{}", visibility_print_with_space(it, cx))?;
18591869
}
@@ -1921,7 +1931,7 @@ fn item_constant(
19211931
fmt::from_fn(|w| {
19221932
wrap_item(w, |w| {
19231933
let tcx = cx.tcx();
1924-
render_attributes_in_code(w, it, "", cx)?;
1934+
let _ = render_attributes_in_code(w, it, "", cx)?;
19251935

19261936
write!(
19271937
w,
@@ -1989,7 +1999,7 @@ impl<'a> DisplayStruct<'a> {
19891999
// For now the only attributes we render for type aliases are `repr` attributes.
19902000
render_repr_attribute_in_code(w, cx, self.def_id)?;
19912001
} else {
1992-
render_attributes_in_code(w, it, "", cx)?;
2002+
let _ = render_attributes_in_code(w, it, "", cx)?;
19932003
}
19942004
write!(
19952005
w,
@@ -2068,7 +2078,7 @@ fn item_fields(
20682078
<code>",
20692079
item_type = ItemType::StructField,
20702080
)?;
2071-
render_attributes_in_code(w, field, "", cx)?;
2081+
let _ = render_attributes_in_code(w, field, "", cx)?;
20722082
write!(
20732083
w,
20742084
"{field_name}: {ty}</code>\
@@ -2091,7 +2101,7 @@ fn item_static(
20912101
) -> impl fmt::Display {
20922102
fmt::from_fn(move |w| {
20932103
wrap_item(w, |w| {
2094-
render_attributes_in_code(w, it, "", cx)?;
2104+
let _ = render_attributes_in_code(w, it, "", cx)?;
20952105
write!(
20962106
w,
20972107
"{vis}{safe}static {mutability}{name}: {typ}",
@@ -2111,7 +2121,7 @@ fn item_foreign_type(cx: &Context<'_>, it: &clean::Item) -> impl fmt::Display {
21112121
fmt::from_fn(|w| {
21122122
wrap_item(w, |w| {
21132123
w.write_str("extern {\n")?;
2114-
render_attributes_in_code(w, it, "", cx)?;
2124+
let _ = render_attributes_in_code(w, it, "", cx)?;
21152125
write!(w, " {}type {};\n}}", visibility_print_with_space(it, cx), it.name.unwrap())
21162126
})?;
21172127

@@ -2354,7 +2364,7 @@ fn render_union(
23542364
// For now the only attributes we render for type aliases are `repr` attributes.
23552365
render_repr_attribute_in_code(f, cx, def_id)?;
23562366
} else {
2357-
render_attributes_in_code(f, it, "", cx)?;
2367+
let _ = render_attributes_in_code(f, it, "", cx)?;
23582368
}
23592369
write!(f, "{}union {}", visibility_print_with_space(it, cx), it.name.unwrap(),)?;
23602370

@@ -2385,7 +2395,7 @@ fn render_union(
23852395

23862396
for field in fields {
23872397
if let clean::StructFieldItem(ref ty) = field.kind {
2388-
render_attributes_in_code(&mut f, field, " ", cx)?;
2398+
let _ = render_attributes_in_code(&mut f, field, " ", cx)?;
23892399
writeln!(
23902400
f,
23912401
" {}{}: {},",
@@ -2482,7 +2492,7 @@ fn render_struct_fields(
24822492
}
24832493
for field in fields {
24842494
if let clean::StructFieldItem(ref ty) = field.kind {
2485-
render_attributes_in_code(w, field, &format!("{tab} "), cx)?;
2495+
let _ = render_attributes_in_code(w, field, &format!("{tab} "), cx)?;
24862496
writeln!(
24872497
w,
24882498
"{tab} {vis}{name}: {ty},",

tests/rustdoc-html/display-hidden-items.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
//@ has 'foo/index.html'
88
//@ has - '//dt/span[@title="Hidden item"]' '👻'
99

10-
//@ matchesraw 'foo/index.html' '(?s)<dt><code><div class="code-attribute">#\[doc\(hidden\)\]</div>pub extern crate .*?hidden_(?:<wbr>)?core;</code><span title="Hidden item">&nbsp;👻</span> </dt>'
10+
//@ has - '//dt/span[@title="Hidden item"]' '👻'
11+
//@ has - '//dt/code' 'pub extern crate core as hidden_core;'
1112
#[doc(hidden)]
1213
pub extern crate core as hidden_core;
1314

0 commit comments

Comments
 (0)