Skip to content

Commit b4fdd74

Browse files
committed
fix(rustdoc): print the doc(hidden) attribute the same as others
Closes #132304
1 parent 24afc84 commit b4fdd74

File tree

5 files changed

+190
-78
lines changed

5 files changed

+190
-78
lines changed

src/librustdoc/clean/types.rs

+6
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,11 @@ impl Item {
760760
}
761761
})
762762
.collect();
763+
764+
if !keep_as_is && self.is_doc_hidden() {
765+
attrs.push("#[doc(hidden)]".into());
766+
}
767+
763768
if !keep_as_is
764769
&& let Some(def_id) = self.def_id()
765770
&& let ItemType::Struct | ItemType::Enum | ItemType::Union = self.type_()
@@ -822,6 +827,7 @@ impl Item {
822827
attrs.push(format!("#[repr({})]", out.join(", ")));
823828
}
824829
}
830+
825831
attrs
826832
}
827833

src/librustdoc/html/format.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1610,14 +1610,7 @@ pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
16101610
}
16111611
};
16121612

1613-
let is_doc_hidden = item.is_doc_hidden();
1614-
display_fn(move |f| {
1615-
if is_doc_hidden {
1616-
f.write_str("#[doc(hidden)] ")?;
1617-
}
1618-
1619-
f.write_str(&vis)
1620-
})
1613+
display_fn(move |f| f.write_str(&vis))
16211614
}
16221615

16231616
pub(crate) trait PrintWithSpace {

src/librustdoc/html/render/mod.rs

+62-35
Original file line numberDiff line numberDiff line change
@@ -854,14 +854,22 @@ fn assoc_const(
854854
ty: &clean::Type,
855855
value: AssocConstValue<'_>,
856856
link: AssocItemLink<'_>,
857-
indent: usize,
857+
parent: Option<ItemType>,
858858
cx: &Context<'_>,
859859
) {
860860
let tcx = cx.tcx();
861+
let (indent, indent_str, end_newline) = if parent == Some(ItemType::Trait) {
862+
let indent_str = " ";
863+
write!(w, "{}", render_attributes_in_pre(it, indent_str, cx));
864+
(4, indent_str, Ending::NoNewline)
865+
} else {
866+
render_attributes_in_code(w, it, cx);
867+
(0, "", Ending::Newline)
868+
};
861869
write!(
862870
w,
863871
"{indent}{vis}const <a{href} class=\"constant\">{name}</a>{generics}: {ty}",
864-
indent = " ".repeat(indent),
872+
indent = indent_str,
865873
vis = visibility_print_with_space(it, cx),
866874
href = assoc_href_attr(it, link, cx),
867875
name = it.name.as_ref().unwrap(),
@@ -883,7 +891,7 @@ fn assoc_const(
883891
write!(w, " = {}", Escape(&repr));
884892
}
885893
}
886-
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline));
894+
write!(w, "{}", print_where_clause(generics, cx, indent, end_newline));
887895
}
888896

889897
fn assoc_type(
@@ -893,13 +901,21 @@ fn assoc_type(
893901
bounds: &[clean::GenericBound],
894902
default: Option<&clean::Type>,
895903
link: AssocItemLink<'_>,
896-
indent: usize,
904+
parent: Option<ItemType>,
897905
cx: &Context<'_>,
898906
) {
907+
let (indent, indent_str, end_newline) = if parent == Some(ItemType::Trait) {
908+
let indent_str = " ";
909+
write!(w, "{}", render_attributes_in_pre(it, indent_str, cx));
910+
(4, indent_str, Ending::NoNewline)
911+
} else {
912+
render_attributes_in_code(w, it, cx);
913+
(0, "", Ending::Newline)
914+
};
899915
write!(
900916
w,
901917
"{indent}{vis}type <a{href} class=\"associatedtype\">{name}</a>{generics}",
902-
indent = " ".repeat(indent),
918+
indent = indent_str,
903919
vis = visibility_print_with_space(it, cx),
904920
href = assoc_href_attr(it, link, cx),
905921
name = it.name.as_ref().unwrap(),
@@ -912,7 +928,7 @@ fn assoc_type(
912928
if let Some(default) = default {
913929
write!(w, " = {}", default.print(cx))
914930
}
915-
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline));
931+
write!(w, "{}", print_where_clause(generics, cx, indent, end_newline));
916932
}
917933

918934
fn assoc_method(
@@ -1098,24 +1114,17 @@ fn render_assoc_item(
10981114
clean::MethodItem(m, _) => {
10991115
assoc_method(w, item, &m.generics, &m.decl, link, parent, cx, render_mode)
11001116
}
1101-
clean::RequiredAssocConstItem(generics, ty) => assoc_const(
1102-
w,
1103-
item,
1104-
generics,
1105-
ty,
1106-
AssocConstValue::None,
1107-
link,
1108-
if parent == ItemType::Trait { 4 } else { 0 },
1109-
cx,
1110-
),
1117+
clean::RequiredAssocConstItem(generics, ty) => {
1118+
assoc_const(w, item, generics, ty, AssocConstValue::None, link, Some(parent), cx)
1119+
}
11111120
clean::ProvidedAssocConstItem(ci) => assoc_const(
11121121
w,
11131122
item,
11141123
&ci.generics,
11151124
&ci.type_,
11161125
AssocConstValue::TraitDefault(&ci.kind),
11171126
link,
1118-
if parent == ItemType::Trait { 4 } else { 0 },
1127+
Some(parent),
11191128
cx,
11201129
),
11211130
clean::ImplAssocConstItem(ci) => assoc_const(
@@ -1125,27 +1134,20 @@ fn render_assoc_item(
11251134
&ci.type_,
11261135
AssocConstValue::Impl(&ci.kind),
11271136
link,
1128-
if parent == ItemType::Trait { 4 } else { 0 },
1129-
cx,
1130-
),
1131-
clean::RequiredAssocTypeItem(ref generics, ref bounds) => assoc_type(
1132-
w,
1133-
item,
1134-
generics,
1135-
bounds,
1136-
None,
1137-
link,
1138-
if parent == ItemType::Trait { 4 } else { 0 },
1137+
Some(parent),
11391138
cx,
11401139
),
1140+
clean::RequiredAssocTypeItem(ref generics, ref bounds) => {
1141+
assoc_type(w, item, generics, bounds, None, link, Some(parent), cx)
1142+
}
11411143
clean::AssocTypeItem(ref ty, ref bounds) => assoc_type(
11421144
w,
11431145
item,
11441146
&ty.generics,
11451147
bounds,
11461148
Some(ty.item_type.as_ref().unwrap_or(&ty.type_)),
11471149
link,
1148-
if parent == ItemType::Trait { 4 } else { 0 },
1150+
Some(parent),
11491151
cx,
11501152
),
11511153
_ => panic!("render_assoc_item called on non-associated-item"),
@@ -1167,6 +1169,20 @@ fn render_attributes_in_pre<'a, 'tcx: 'a>(
11671169
})
11681170
}
11691171

1172+
// When an attribute is rendered inside a `<pre>` tag, it is formatted using
1173+
// a whitespace suffix.
1174+
fn render_attributes_in_pre_same_line<'a, 'tcx: 'a>(
1175+
it: &'a clean::Item,
1176+
cx: &'a Context<'tcx>,
1177+
) -> impl fmt::Display + Captures<'a> + Captures<'tcx> {
1178+
crate::html::format::display_fn(move |f| {
1179+
for a in it.attributes(cx.tcx(), cx.cache(), false) {
1180+
write!(f, "{a} ")?;
1181+
}
1182+
Ok(())
1183+
})
1184+
}
1185+
11701186
// When an attribute is rendered inside a <code> tag, it is formatted using
11711187
// a div to produce a newline after it.
11721188
fn render_attributes_in_code(w: &mut impl fmt::Write, it: &clean::Item, cx: &Context<'_>) {
@@ -1175,6 +1191,17 @@ fn render_attributes_in_code(w: &mut impl fmt::Write, it: &clean::Item, cx: &Con
11751191
}
11761192
}
11771193

1194+
// Same as `render_attributes_in_code()`, but on the same line.
1195+
fn render_attributes_in_code_same_line(
1196+
w: &mut impl fmt::Write,
1197+
it: &clean::Item,
1198+
cx: &Context<'_>,
1199+
) {
1200+
for attr in it.attributes(cx.tcx(), cx.cache(), false) {
1201+
write!(w, "<span class=\"code-attribute\">{attr} </span>").unwrap();
1202+
}
1203+
}
1204+
11781205
#[derive(Copy, Clone)]
11791206
enum AssocItemLink<'a> {
11801207
Anchor(Option<&'a str>),
@@ -1529,7 +1556,7 @@ fn notable_traits_decl(ty: &clean::Type, cx: &Context<'_>) -> (String, String) {
15291556
&[], // intentionally leaving out bounds
15301557
Some(&tydef.type_),
15311558
src_link,
1532-
0,
1559+
None,
15331560
cx,
15341561
);
15351562
out.push_str(";</div>");
@@ -1733,7 +1760,7 @@ fn render_impl(
17331760
ty,
17341761
AssocConstValue::None,
17351762
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1736-
0,
1763+
None,
17371764
cx,
17381765
);
17391766
w.write_str("</h4></section>");
@@ -1759,7 +1786,7 @@ fn render_impl(
17591786
_ => unreachable!(),
17601787
},
17611788
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1762-
0,
1789+
None,
17631790
cx,
17641791
);
17651792
w.write_str("</h4></section>");
@@ -1781,7 +1808,7 @@ fn render_impl(
17811808
bounds,
17821809
None,
17831810
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1784-
0,
1811+
None,
17851812
cx,
17861813
);
17871814
w.write_str("</h4></section>");
@@ -1803,7 +1830,7 @@ fn render_impl(
18031830
&[], // intentionally leaving out bounds
18041831
Some(tydef.item_type.as_ref().unwrap_or(&tydef.type_)),
18051832
link.anchor(if trait_.is_some() { &source_id } else { &id }),
1806-
0,
1833+
None,
18071834
cx,
18081835
);
18091836
w.write_str("</h4></section>");
@@ -2100,7 +2127,7 @@ pub(crate) fn render_impl_summary(
21002127
&[], // intentionally leaving out bounds
21012128
Some(&tydef.type_),
21022129
AssocItemLink::Anchor(None),
2103-
0,
2130+
None,
21042131
cx,
21052132
);
21062133
w.write_str(";</div>");

0 commit comments

Comments
 (0)