Skip to content

Commit 57f2104

Browse files
authored
Rollup merge of #122495 - Manishearth:rustdoc-👻👻👻, r=GuillaumeGomez
Visually mark 👻hidden👻 items with document-hidden-items Fixes #122485 This adds a 👻 in the item list (much like the 🔒 used for private items), and also shows `#[doc(hidden)]` in the code view, where `pub(crate)` etc gets shown for private items. This does not do anything for enum variants, if people have ideas. I think we can just show the attribute.
2 parents 9e153cc + 9718144 commit 57f2104

File tree

8 files changed

+76
-62
lines changed

8 files changed

+76
-62
lines changed

src/librustdoc/clean/inline.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ pub(crate) fn try_inline(
6262
attrs_without_docs.as_ref().map(|(attrs, def_id)| (&attrs[..], *def_id));
6363

6464
let import_def_id = attrs.and_then(|(_, def_id)| def_id);
65+
66+
let (attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs);
67+
6568
let kind = match res {
6669
Res::Def(DefKind::Trait, did) => {
6770
record_extern_fqn(cx, did, ItemType::Trait);
@@ -131,7 +134,7 @@ pub(crate) fn try_inline(
131134
cx.with_param_env(did, |cx| clean::ConstantItem(build_const(cx, did)))
132135
}
133136
Res::Def(DefKind::Macro(kind), did) => {
134-
let mac = build_macro(cx, did, name, import_def_id, kind);
137+
let mac = build_macro(cx, did, name, import_def_id, kind, attrs.is_doc_hidden());
135138

136139
let type_kind = match kind {
137140
MacroKind::Bang => ItemType::Macro,
@@ -144,7 +147,6 @@ pub(crate) fn try_inline(
144147
_ => return None,
145148
};
146149

147-
let (attrs, cfg) = merge_attrs(cx, load_attrs(cx, did), attrs);
148150
cx.inlined.insert(did.into());
149151
let mut item =
150152
clean::Item::from_def_id_and_attrs_and_parts(did, Some(name), kind, Box::new(attrs), cfg);
@@ -751,14 +753,22 @@ fn build_macro(
751753
name: Symbol,
752754
import_def_id: Option<DefId>,
753755
macro_kind: MacroKind,
756+
is_doc_hidden: bool,
754757
) -> clean::ItemKind {
755758
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.tcx) {
756759
LoadedMacro::MacroDef(item_def, _) => match macro_kind {
757760
MacroKind::Bang => {
758761
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
759762
let vis = cx.tcx.visibility(import_def_id.unwrap_or(def_id));
760763
clean::MacroItem(clean::Macro {
761-
source: utils::display_macro_source(cx, name, def, def_id, vis),
764+
source: utils::display_macro_source(
765+
cx,
766+
name,
767+
def,
768+
def_id,
769+
vis,
770+
is_doc_hidden,
771+
),
762772
})
763773
} else {
764774
unreachable!()

src/librustdoc/clean/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2794,7 +2794,8 @@ fn clean_maybe_renamed_item<'tcx>(
27942794
ItemKind::Macro(ref macro_def, MacroKind::Bang) => {
27952795
let ty_vis = cx.tcx.visibility(def_id);
27962796
MacroItem(Macro {
2797-
source: display_macro_source(cx, name, macro_def, def_id, ty_vis),
2797+
// FIXME this shouldn't be false
2798+
source: display_macro_source(cx, name, macro_def, def_id, ty_vis, false),
27982799
})
27992800
}
28002801
ItemKind::Macro(_, macro_kind) => clean_proc_macro(item, &mut name, macro_kind, cx),

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ impl Attributes {
11611161
false
11621162
}
11631163

1164-
fn is_doc_hidden(&self) -> bool {
1164+
pub(crate) fn is_doc_hidden(&self) -> bool {
11651165
self.has_doc_flag(sym::hidden)
11661166
}
11671167

src/librustdoc/clean/utils.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ pub(super) fn display_macro_source(
625625
def: &ast::MacroDef,
626626
def_id: DefId,
627627
vis: ty::Visibility<DefId>,
628+
is_doc_hidden: bool,
628629
) -> String {
629630
// Extract the spans of all matchers. They represent the "interface" of the macro.
630631
let matchers = def.body.tokens.chunks(4).map(|arm| &arm[0]);
@@ -635,15 +636,15 @@ pub(super) fn display_macro_source(
635636
if matchers.len() <= 1 {
636637
format!(
637638
"{vis}macro {name}{matchers} {{\n ...\n}}",
638-
vis = visibility_to_src_with_space(Some(vis), cx.tcx, def_id),
639+
vis = visibility_to_src_with_space(Some(vis), cx.tcx, def_id, is_doc_hidden),
639640
matchers = matchers
640641
.map(|matcher| render_macro_matcher(cx.tcx, matcher))
641642
.collect::<String>(),
642643
)
643644
} else {
644645
format!(
645646
"{vis}macro {name} {{\n{arms}}}",
646-
vis = visibility_to_src_with_space(Some(vis), cx.tcx, def_id),
647+
vis = visibility_to_src_with_space(Some(vis), cx.tcx, def_id, is_doc_hidden),
647648
arms = render_macro_arms(cx.tcx, matchers, ","),
648649
)
649650
}

src/librustdoc/html/format.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ use rustc_target::spec::abi::Abi;
2929
use itertools::Itertools;
3030

3131
use crate::clean::{
32-
self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId,
33-
PrimitiveType,
32+
self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, PrimitiveType,
3433
};
3534
use crate::formats::cache::Cache;
3635
use crate::formats::item_type::ItemType;
@@ -1506,20 +1505,18 @@ impl clean::FnDecl {
15061505
}
15071506

15081507
pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
1509-
visibility: Option<ty::Visibility<DefId>>,
1510-
item_did: ItemId,
1508+
item: &clean::Item,
15111509
cx: &'a Context<'tcx>,
15121510
) -> impl Display + 'a + Captures<'tcx> {
15131511
use std::fmt::Write as _;
1514-
1515-
let to_print: Cow<'static, str> = match visibility {
1512+
let vis: Cow<'static, str> = match item.visibility(cx.tcx()) {
15161513
None => "".into(),
15171514
Some(ty::Visibility::Public) => "pub ".into(),
15181515
Some(ty::Visibility::Restricted(vis_did)) => {
15191516
// FIXME(camelid): This may not work correctly if `item_did` is a module.
15201517
// However, rustdoc currently never displays a module's
15211518
// visibility, so it shouldn't matter.
1522-
let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id());
1519+
let parent_module = find_nearest_parent_module(cx.tcx(), item.item_id.expect_def_id());
15231520

15241521
if vis_did.is_crate_root() {
15251522
"pub(crate) ".into()
@@ -1547,7 +1544,15 @@ pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
15471544
}
15481545
}
15491546
};
1550-
display_fn(move |f| f.write_str(&to_print))
1547+
1548+
let is_doc_hidden = item.is_doc_hidden();
1549+
display_fn(move |f| {
1550+
if is_doc_hidden {
1551+
f.write_str("#[doc(hidden)] ")?;
1552+
}
1553+
1554+
f.write_str(&vis)
1555+
})
15511556
}
15521557

15531558
/// This function is the same as print_with_space, except that it renders no links.
@@ -1557,8 +1562,9 @@ pub(crate) fn visibility_to_src_with_space<'a, 'tcx: 'a>(
15571562
visibility: Option<ty::Visibility<DefId>>,
15581563
tcx: TyCtxt<'tcx>,
15591564
item_did: DefId,
1565+
is_doc_hidden: bool,
15601566
) -> impl Display + 'a + Captures<'tcx> {
1561-
let to_print: Cow<'static, str> = match visibility {
1567+
let vis: Cow<'static, str> = match visibility {
15621568
None => "".into(),
15631569
Some(ty::Visibility::Public) => "pub ".into(),
15641570
Some(ty::Visibility::Restricted(vis_did)) => {
@@ -1582,7 +1588,12 @@ pub(crate) fn visibility_to_src_with_space<'a, 'tcx: 'a>(
15821588
}
15831589
}
15841590
};
1585-
display_fn(move |f| f.write_str(&to_print))
1591+
display_fn(move |f| {
1592+
if is_doc_hidden {
1593+
f.write_str("#[doc(hidden)] ")?;
1594+
}
1595+
f.write_str(&vis)
1596+
})
15861597
}
15871598

15881599
pub(crate) trait PrintWithSpace {

src/librustdoc/html/render/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ fn assoc_const(
883883
w,
884884
"{indent}{vis}const <a{href} class=\"constant\">{name}</a>{generics}: {ty}",
885885
indent = " ".repeat(indent),
886-
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
886+
vis = visibility_print_with_space(it, cx),
887887
href = assoc_href_attr(it, link, cx),
888888
name = it.name.as_ref().unwrap(),
889889
generics = generics.print(cx),
@@ -912,12 +912,11 @@ fn assoc_type(
912912
indent: usize,
913913
cx: &Context<'_>,
914914
) {
915-
let tcx = cx.tcx();
916915
write!(
917916
w,
918917
"{indent}{vis}type <a{href} class=\"associatedtype\">{name}</a>{generics}",
919918
indent = " ".repeat(indent),
920-
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
919+
vis = visibility_print_with_space(it, cx),
921920
href = assoc_href_attr(it, link, cx),
922921
name = it.name.as_ref().unwrap(),
923922
generics = generics.print(cx),
@@ -945,7 +944,7 @@ fn assoc_method(
945944
let tcx = cx.tcx();
946945
let header = meth.fn_header(tcx).expect("Trying to get header from a non-function item");
947946
let name = meth.name.as_ref().unwrap();
948-
let vis = visibility_print_with_space(meth.visibility(tcx), meth.item_id, cx).to_string();
947+
let vis = visibility_print_with_space(meth, cx).to_string();
949948
let defaultness = print_default_space(meth.is_default());
950949
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
951950
// this condition.

0 commit comments

Comments
 (0)