Skip to content

Commit 983b302

Browse files
committed
rustdoc: make #[doc(hidden)] render the same as other attrs
Makes `#[doc(hidden)]` render the same as other attrs, instead of being rendered it the same line like visibility specifiers (`pub` etc.). This only has effect when unstable flag `document-hidden-items` is on, as otherwise items with this attr wouldn't be rendered at all. Also makes `#[doc(hidden)]` render for `macro_rules!`.
1 parent 4eedad3 commit 983b302

File tree

4 files changed

+56
-30
lines changed

4 files changed

+56
-30
lines changed

src/librustdoc/clean/types.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -779,20 +779,25 @@ impl Item {
779779
self.attrs
780780
.other_attrs
781781
.iter()
782-
.filter_map(|attr| match attr {
783-
hir::Attribute::Parsed(AttributeKind::LinkSection { name, .. }) => {
784-
Some(format!("#[unsafe(link_section = \"{name}\")]"))
782+
.filter_map(|attr| {
783+
if attr_has_doc_flag(attr, sym::hidden) {
784+
return Some("#[doc(hidden)]".to_string());
785785
}
786-
hir::Attribute::Parsed(AttributeKind::NoMangle(..)) => {
787-
Some("#[unsafe(no_mangle)]".to_string())
788-
}
789-
hir::Attribute::Parsed(AttributeKind::ExportName { name, .. }) => {
790-
Some(format!("#[unsafe(export_name = \"{name}\")]"))
791-
}
792-
hir::Attribute::Parsed(AttributeKind::NonExhaustive(..)) => {
793-
Some("#[non_exhaustive]".to_string())
786+
match attr {
787+
hir::Attribute::Parsed(AttributeKind::LinkSection { name, .. }) => {
788+
Some(format!("#[unsafe(link_section = \"{name}\")]"))
789+
}
790+
hir::Attribute::Parsed(AttributeKind::NoMangle(..)) => {
791+
Some("#[unsafe(no_mangle)]".to_string())
792+
}
793+
hir::Attribute::Parsed(AttributeKind::ExportName { name, .. }) => {
794+
Some(format!("#[unsafe(export_name = \"{name}\")]"))
795+
}
796+
hir::Attribute::Parsed(AttributeKind::NonExhaustive(..)) => {
797+
Some("#[non_exhaustive]".to_string())
798+
}
799+
_ => None,
794800
}
795-
_ => None,
796801
})
797802
.collect()
798803
}
@@ -1159,20 +1164,27 @@ pub(crate) struct Attributes {
11591164
pub(crate) other_attrs: ThinVec<hir::Attribute>,
11601165
}
11611166

1167+
/// Returns `true` if the attribute is a `#[doc(...)]` attribute with the given flag, e.g.
1168+
/// `#[doc(hidden)]` or `#[doc(inline)]`.
1169+
fn attr_has_doc_flag(attr: &hir::Attribute, flag: Symbol) -> bool {
1170+
if !attr.has_name(sym::doc) {
1171+
return false;
1172+
}
1173+
1174+
if let Some(items) = attr.meta_item_list() {
1175+
return items.iter().filter_map(|i| i.meta_item()).any(|it| it.has_name(flag));
1176+
}
1177+
false
1178+
}
1179+
11621180
impl Attributes {
11631181
pub(crate) fn lists(&self, name: Symbol) -> impl Iterator<Item = ast::MetaItemInner> {
11641182
hir_attr_lists(&self.other_attrs[..], name)
11651183
}
11661184

11671185
pub(crate) fn has_doc_flag(&self, flag: Symbol) -> bool {
11681186
for attr in &self.other_attrs {
1169-
if !attr.has_name(sym::doc) {
1170-
continue;
1171-
}
1172-
1173-
if let Some(items) = attr.meta_item_list()
1174-
&& items.iter().filter_map(|i| i.meta_item()).any(|it| it.has_name(flag))
1175-
{
1187+
if attr_has_doc_flag(attr, flag) {
11761188
return true;
11771189
}
11781190
}

src/librustdoc/html/format.rs

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

14081408
pub(crate) fn visibility_print_with_space(item: &clean::Item, cx: &Context<'_>) -> impl Display {
14091409
fmt::from_fn(move |f| {
1410-
if item.is_doc_hidden() {
1411-
f.write_str("#[doc(hidden)] ")?;
1412-
}
1413-
14141410
match item.visibility(cx.tcx()) {
14151411
None => {}
14161412
Some(ty::Visibility::Public) => f.write_str("pub ")?,

src/librustdoc/html/render/print_item.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,6 @@ fn item_variants(
18651865
fn item_macro(cx: &Context<'_>, it: &clean::Item, t: &clean::Macro) -> impl fmt::Display {
18661866
fmt::from_fn(|w| {
18671867
wrap_item(w, |w| {
1868-
// FIXME: Also print `#[doc(hidden)]` for `macro_rules!` if it `is_doc_hidden`.
18691868
render_attributes_in_code(w, it, "", cx);
18701869
if !t.macro_rules {
18711870
write!(w, "{}", visibility_print_with_space(it, cx))?;

tests/rustdoc/display-hidden-items.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,27 @@
77
//@ has 'foo/index.html'
88
//@ has - '//dt/span[@title="Hidden item"]' '👻'
99

10-
//@ has - '//*[@id="reexport.hidden_reexport"]/code' '#[doc(hidden)] pub use hidden::inside_hidden as hidden_reexport;'
10+
//@ has - '//*[@id="reexport.hidden_reexport"]/code/*[@class="code-attribute"]' '#[doc(hidden)]'
11+
//@ has - '//*[@id="reexport.hidden_reexport"]/code' 'pub use hidden::inside_hidden as hidden_reexport;'
1112
#[doc(hidden)]
1213
pub use hidden::inside_hidden as hidden_reexport;
1314

1415
//@ has - '//dt/a[@class="trait"]' 'TraitHidden'
1516
//@ has 'foo/trait.TraitHidden.html'
16-
//@ has - '//code' '#[doc(hidden)] pub trait TraitHidden'
17+
//@ has - '//*[@class="code-attribute"]' '#[doc(hidden)]'
18+
//@ has - '//code' 'pub trait TraitHidden'
1719
#[doc(hidden)]
1820
pub trait TraitHidden {}
1921

2022
//@ has 'foo/index.html' '//dt/a[@class="trait"]' 'Trait'
2123
pub trait Trait {
2224
//@ has 'foo/trait.Trait.html'
23-
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]' '#[doc(hidden)] const BAR: u32 = 0u32'
25+
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]/*[@class="code-attribute"]' '#[doc(hidden)]'
26+
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]' 'const BAR: u32 = 0u32'
2427
#[doc(hidden)]
2528
const BAR: u32 = 0;
2629

30+
//@ has - '//*[@class="code-attribute"]' '#[doc(hidden)]'
2731
//@ has - '//*[@id="method.foo"]/*[@class="code-header"]' 'fn foo()'
2832
#[doc(hidden)]
2933
fn foo() {}
@@ -33,33 +37,39 @@ pub trait Trait {
3337
//@ has 'foo/struct.Struct.html'
3438
pub struct Struct {
3539
//@ has - '//*[@id="structfield.a"]/code' 'a: u32'
40+
//@ has - '//*[@class="code-attribute"]' '#[doc(hidden)]'
3641
#[doc(hidden)]
3742
pub a: u32,
3843
}
3944

4045
impl Struct {
46+
//@ has - '//*[@class="code-attribute"]' '#[doc(hidden)]'
4147
//@ has - '//*[@id="method.new"]/*[@class="code-header"]' 'pub fn new() -> Self'
4248
#[doc(hidden)]
4349
pub fn new() -> Self { Self { a: 0 } }
4450
}
4551

4652
impl Trait for Struct {
47-
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]' '#[doc(hidden)] const BAR: u32 = 0u32'
48-
//@ has - '//*[@id="method.foo"]/*[@class="code-header"]' '#[doc(hidden)] fn foo()'
53+
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]/*[@class="code-attribute"]' '#[doc(hidden)]'
54+
//@ has - '//*[@id="associatedconstant.BAR"]/*[@class="code-header"]' 'const BAR: u32 = 0u32'
55+
//@ has - '//*[@id="method.foo"]/*[@class="code-header"]/*[@class="code-attribute"]' '#[doc(hidden)]'
56+
//@ has - '//*[@id="method.foo"]/*[@class="code-header"]' 'fn foo()'
4957
}
5058
//@ has - '//*[@id="impl-TraitHidden-for-Struct"]/*[@class="code-header"]' 'impl TraitHidden for Struct'
5159
impl TraitHidden for Struct {}
5260

5361
//@ has 'foo/index.html' '//dt/a[@class="enum"]' 'HiddenEnum'
5462
//@ has 'foo/enum.HiddenEnum.html'
55-
//@ has - '//code' '#[doc(hidden)] pub enum HiddenEnum'
63+
//@ has - '//*[@class="code-attribute"]' '#[doc(hidden)]'
64+
//@ has - '//code' 'pub enum HiddenEnum'
5665
#[doc(hidden)]
5766
pub enum HiddenEnum {
5867
A,
5968
}
6069

6170
//@ has 'foo/index.html' '//dt/a[@class="enum"]' 'Enum'
6271
pub enum Enum {
72+
//@ has - '//*[@class="code-attribute"]' '#[doc(hidden)]'
6373
//@ has 'foo/enum.Enum.html' '//*[@id="variant.A"]/*[@class="code-header"]' 'A'
6474
#[doc(hidden)]
6575
A,
@@ -73,3 +83,12 @@ pub mod hidden {
7383
//@ has 'foo/hidden/fn.inside_hidden.html'
7484
pub fn inside_hidden() {}
7585
}
86+
87+
//@ has 'foo/index.html'
88+
//@ has - '//*[@class="code-attribute"]' '#[doc(hidden)]'
89+
//@ has - '//a[@class="macro"]' 'macro_rule'
90+
#[doc(hidden)]
91+
#[macro_export]
92+
macro_rules! macro_rule {
93+
() => {};
94+
}

0 commit comments

Comments
 (0)