Skip to content

Commit c818dba

Browse files
authored
Unrolled build for #151001
Rollup merge of #151001 - chojs23:fix/hidden-attr-docs, r=GuillaumeGomez rustdoc: render doc(hidden) as a code attribute Move `#[doc(hidden)]` into the shared code-attribute renderer so it matches the styling and placement of other attributes in rustdoc HTML. Closes #132304
2 parents d29e478 + b4c4d95 commit c818dba

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

src/librustdoc/html/format.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,10 +1390,6 @@ impl clean::FnDecl {
13901390

13911391
pub(crate) fn visibility_print_with_space(item: &clean::Item, cx: &Context<'_>) -> impl Display {
13921392
fmt::from_fn(move |f| {
1393-
if item.is_doc_hidden() {
1394-
f.write_str("#[doc(hidden)] ")?;
1395-
}
1396-
13971393
let Some(vis) = item.visibility(cx.tcx()) else {
13981394
return Ok(());
13991395
};

src/librustdoc/html/render/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ fn assoc_type(
10751075
cx: &Context<'_>,
10761076
) -> impl fmt::Display {
10771077
fmt::from_fn(move |w| {
1078+
render_attributes_in_code(w, it, &" ".repeat(indent), cx)?;
10781079
write!(
10791080
w,
10801081
"{indent}{vis}type <a{href} class=\"associatedtype\">{name}</a>{generics}",
@@ -2931,6 +2932,21 @@ fn render_attributes_in_code(
29312932
prefix: &str,
29322933
cx: &Context<'_>,
29332934
) -> fmt::Result {
2935+
render_attributes_in_code_with_options(w, item, prefix, cx, true, "")
2936+
}
2937+
2938+
pub(super) fn render_attributes_in_code_with_options(
2939+
w: &mut impl fmt::Write,
2940+
item: &clean::Item,
2941+
prefix: &str,
2942+
cx: &Context<'_>,
2943+
render_doc_hidden: bool,
2944+
open_tag: &str,
2945+
) -> fmt::Result {
2946+
w.write_str(open_tag)?;
2947+
if render_doc_hidden && item.is_doc_hidden() {
2948+
render_code_attribute(prefix, "#[doc(hidden)]", w)?;
2949+
}
29342950
for attr in &item.attrs.other_attrs {
29352951
let hir::Attribute::Parsed(kind) = attr else { continue };
29362952
let attr = match kind {

src/librustdoc/html/render/print_item.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,38 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
344344
}
345345

346346
for (_, myitem) in &not_stripped_items[&type_] {
347+
let visibility_and_hidden = |item: &clean::Item| match item.visibility(tcx) {
348+
Some(ty::Visibility::Restricted(_)) => {
349+
if item.is_doc_hidden() {
350+
// Don't separate with a space when there are two of them
351+
"<span title=\"Restricted Visibility\">&nbsp;🔒</span><span title=\"Hidden item\">👻</span> "
352+
} else {
353+
"<span title=\"Restricted Visibility\">&nbsp;🔒</span> "
354+
}
355+
}
356+
_ if item.is_doc_hidden() => "<span title=\"Hidden item\">&nbsp;👻</span> ",
357+
_ => "",
358+
};
359+
347360
match myitem.kind {
348361
clean::ExternCrateItem { ref src } => {
349362
use crate::html::format::print_anchor;
350363

364+
let visibility_and_hidden = visibility_and_hidden(myitem);
365+
// Module listings use the hidden marker, so skip doc(hidden) here.
366+
super::render_attributes_in_code_with_options(
367+
w,
368+
myitem,
369+
"",
370+
cx,
371+
false,
372+
"<dt><code>",
373+
)?;
351374
match *src {
352375
Some(src) => {
353376
write!(
354377
w,
355-
"<dt><code>{}extern crate {} as {};",
378+
"{}extern crate {} as {};",
356379
visibility_print_with_space(myitem, cx),
357380
print_anchor(myitem.item_id.expect_def_id(), src, cx),
358381
EscapeBodyTextWithWbr(myitem.name.unwrap().as_str())
@@ -361,7 +384,7 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
361384
None => {
362385
write!(
363386
w,
364-
"<dt><code>{}extern crate {};",
387+
"{}extern crate {};",
365388
visibility_print_with_space(myitem, cx),
366389
print_anchor(
367390
myitem.item_id.expect_def_id(),
@@ -371,7 +394,7 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
371394
)?;
372395
}
373396
}
374-
write!(w, "</code></dt>")?
397+
write!(w, "</code>{visibility_and_hidden}</dt>")?
375398
}
376399
clean::ImportItem(ref import) => {
377400
let (stab_tags, deprecation) = match import.source.did {
@@ -386,6 +409,7 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
386409
}
387410
None => (String::new(), item.is_deprecated(tcx)),
388411
};
412+
let visibility_and_hidden = visibility_and_hidden(myitem);
389413
let id = match import.kind {
390414
clean::ImportKind::Simple(s) => {
391415
format!(" id=\"{}\"", cx.derive_id(format!("reexport.{s}")))
@@ -397,13 +421,13 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
397421
"<dt{id}{deprecation_attr}><code>",
398422
deprecation_attr = deprecation_class_attr(deprecation)
399423
)?;
400-
render_attributes_in_code(w, myitem, "", cx)?;
401424
write!(
402425
w,
403-
"{vis}{imp}</code>{stab_tags}\
426+
"{vis}{imp}</code>{visibility_and_hidden}{stab_tags}\
404427
</dt>",
405428
vis = visibility_print_with_space(myitem, cx),
406429
imp = print_import(import, cx),
430+
visibility_and_hidden = visibility_and_hidden,
407431
)?;
408432
}
409433
_ => {
@@ -421,20 +445,7 @@ fn item_module(cx: &Context<'_>, item: &clean::Item, items: &[clean::Item]) -> i
421445
}
422446
_ => "",
423447
};
424-
let visibility_and_hidden = match myitem.visibility(tcx) {
425-
Some(ty::Visibility::Restricted(_)) => {
426-
if myitem.is_doc_hidden() {
427-
// Don't separate with a space when there are two of them
428-
"<span title=\"Restricted Visibility\">&nbsp;🔒</span><span title=\"Hidden item\">👻</span> "
429-
} else {
430-
"<span title=\"Restricted Visibility\">&nbsp;🔒</span> "
431-
}
432-
}
433-
_ if myitem.is_doc_hidden() => {
434-
"<span title=\"Hidden item\">&nbsp;👻</span> "
435-
}
436-
_ => "",
437-
};
448+
let visibility_and_hidden = visibility_and_hidden(myitem);
438449

439450
let docs = MarkdownSummaryLine(&myitem.doc_value(), &myitem.links(cx))
440451
.into_string();
@@ -1868,7 +1879,6 @@ fn item_variants(
18681879
fn item_macro(cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) -> impl fmt::Display {
18691880
fmt::from_fn(|w| {
18701881
wrap_item(w, |w| {
1871-
// FIXME: Also print `#[doc(hidden)]` for `macro_rules!` if it `is_doc_hidden`.
18721882
render_attributes_in_code(w, it, "", cx)?;
18731883
if !t.macro_rules {
18741884
write!(w, "{}", visibility_print_with_space(it, cx))?;

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@
55
#![crate_name = "foo"]
66

77
//@ has 'foo/index.html'
8-
//@ has - '//dt/span[@title="Hidden item"]' '👻'
98

10-
//@ has - '//*[@id="reexport.hidden_reexport"]/code' '#[doc(hidden)] pub use hidden::inside_hidden as hidden_reexport;'
9+
//@ matches - '//dt[code]' 'pub extern crate .*hidden_core;.*👻'
10+
//@ has - '//dt/code' 'pub extern crate core as hidden_core;'
11+
#[doc(hidden)]
12+
pub extern crate core as hidden_core;
13+
14+
//@ has - '//*[@id="reexport.hidden_reexport"]/span[@title="Hidden item"]' '👻'
15+
//@ has - '//*[@id="reexport.hidden_reexport"]/code' 'pub use hidden::inside_hidden as hidden_reexport;'
1116
#[doc(hidden)]
1217
pub use hidden::inside_hidden as hidden_reexport;
1318

1419
//@ has - '//dt/a[@class="trait"]' 'TraitHidden'
1520
//@ has 'foo/trait.TraitHidden.html'
16-
//@ has - '//code' '#[doc(hidden)] pub trait TraitHidden'
21+
//@ has 'foo/trait.TraitHidden.html' '//*[@class="rust item-decl"]//*[@class="code-attribute"]' '#[doc(hidden)]'
22+
//@ has 'foo/trait.TraitHidden.html' '//*[@class="rust item-decl"]/code' 'pub trait TraitHidden'
1723
#[doc(hidden)]
1824
pub trait TraitHidden {}
1925

2026
//@ has 'foo/index.html' '//dt/a[@class="trait"]' 'Trait'
2127
pub trait Trait {
2228
//@ has 'foo/trait.Trait.html'
23-
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]' '#[doc(hidden)] const BAR: u32 = 0'
29+
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]/*[@class="code-attribute"]' '#[doc(hidden)]'
2430
#[doc(hidden)]
2531
const BAR: u32 = 0;
2632

33+
//@ has - '//*[@id="method.foo"]/*[@class="code-header"]/*[@class="code-attribute"]' '#[doc(hidden)]'
2734
//@ has - '//*[@id="method.foo"]/*[@class="code-header"]' 'fn foo()'
2835
#[doc(hidden)]
2936
fn foo() {}
@@ -44,22 +51,24 @@ impl Struct {
4451
}
4552

4653
impl Trait for Struct {
47-
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]' '#[doc(hidden)] const BAR: u32 = 0'
48-
//@ has - '//*[@id="method.foo"]/*[@class="code-header"]' '#[doc(hidden)] fn foo()'
54+
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]/*[@class="code-attribute"]' '#[doc(hidden)]'
55+
//@ has - '//*[@id="method.foo"]/*[@class="code-header"]/*[@class="code-attribute"]' '#[doc(hidden)]'
4956
}
5057
//@ has - '//*[@id="impl-TraitHidden-for-Struct"]/*[@class="code-header"]' 'impl TraitHidden for Struct'
5158
impl TraitHidden for Struct {}
5259

5360
//@ has 'foo/index.html' '//dt/a[@class="enum"]' 'HiddenEnum'
5461
//@ has 'foo/enum.HiddenEnum.html'
55-
//@ has - '//code' '#[doc(hidden)] pub enum HiddenEnum'
62+
//@ has 'foo/enum.HiddenEnum.html' '//*[@class="rust item-decl"]//*[@class="code-attribute"]' '#[doc(hidden)]'
63+
//@ has 'foo/enum.HiddenEnum.html' '//*[@class="rust item-decl"]/code' 'pub enum HiddenEnum'
5664
#[doc(hidden)]
5765
pub enum HiddenEnum {
5866
A,
5967
}
6068

6169
//@ has 'foo/index.html' '//dt/a[@class="enum"]' 'Enum'
6270
pub enum Enum {
71+
//@ has 'foo/enum.Enum.html' '//*[@id="variant.A"]/*[@class="code-header"]/*[@class="code-attribute"]' '#[doc(hidden)]'
6372
//@ has 'foo/enum.Enum.html' '//*[@id="variant.A"]/*[@class="code-header"]' 'A'
6473
#[doc(hidden)]
6574
A,

0 commit comments

Comments
 (0)