Skip to content

Commit 343c77c

Browse files
committed
Refactor visibility_print_with_space to directly take an item
1 parent 8da2621 commit 343c77c

File tree

7 files changed

+42
-51
lines changed

7 files changed

+42
-51
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 13 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2796,7 +2796,8 @@ fn clean_maybe_renamed_item<'tcx>(
27962796
ItemKind::Macro(ref macro_def, MacroKind::Bang) => {
27972797
let ty_vis = cx.tcx.visibility(def_id);
27982798
MacroItem(Macro {
2799-
source: display_macro_source(cx, name, macro_def, def_id, ty_vis),
2799+
// FIXME this shouldn't be false
2800+
source: display_macro_source(cx, name, macro_def, def_id, ty_vis, false),
28002801
})
28012802
}
28022803
ItemKind::Macro(_, macro_kind) => clean_proc_macro(item, &mut name, macro_kind, cx),

src/librustdoc/clean/types.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 5 additions & 7 deletions
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 to_print: 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()
@@ -1557,6 +1554,7 @@ pub(crate) fn visibility_to_src_with_space<'a, 'tcx: 'a>(
15571554
visibility: Option<ty::Visibility<DefId>>,
15581555
tcx: TyCtxt<'tcx>,
15591556
item_did: DefId,
1557+
_is_doc_hidden: bool,
15601558
) -> impl Display + 'a + Captures<'tcx> {
15611559
let to_print: Cow<'static, str> = match visibility {
15621560
None => "".into(),

src/librustdoc/html/render/mod.rs

Lines changed: 3 additions & 4 deletions
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.

src/librustdoc/html/render/print_item.rs

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,14 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
445445
Some(src) => write!(
446446
w,
447447
"<div class=\"item-name\"><code>{}extern crate {} as {};",
448-
visibility_print_with_space(myitem.visibility(tcx), myitem.item_id, cx),
448+
visibility_print_with_space(myitem, cx),
449449
anchor(myitem.item_id.expect_def_id(), src, cx),
450450
myitem.name.unwrap(),
451451
),
452452
None => write!(
453453
w,
454454
"<div class=\"item-name\"><code>{}extern crate {};",
455-
visibility_print_with_space(myitem.visibility(tcx), myitem.item_id, cx),
455+
visibility_print_with_space(myitem, cx),
456456
anchor(myitem.item_id.expect_def_id(), myitem.name.unwrap(), cx),
457457
),
458458
}
@@ -491,7 +491,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
491491
<code>{vis}{imp}</code>\
492492
</div>\
493493
{stab_tags_before}{stab_tags}{stab_tags_after}",
494-
vis = visibility_print_with_space(myitem.visibility(tcx), myitem.item_id, cx),
494+
vis = visibility_print_with_space(myitem, cx),
495495
imp = import.print(cx),
496496
);
497497
w.write_str(ITEM_TABLE_ROW_CLOSE);
@@ -631,7 +631,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
631631
let unsafety = header.unsafety.print_with_space();
632632
let abi = print_abi_with_space(header.abi).to_string();
633633
let asyncness = header.asyncness.print_with_space();
634-
let visibility = visibility_print_with_space(it.visibility(tcx), it.item_id, cx).to_string();
634+
let visibility = visibility_print_with_space(it, cx).to_string();
635635
let name = it.name.unwrap();
636636

637637
let generics_len = format!("{:#}", f.generics.print(cx)).len();
@@ -688,7 +688,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
688688
w,
689689
"{attrs}{vis}{unsafety}{is_auto}trait {name}{generics}{bounds}",
690690
attrs = render_attributes_in_pre(it, "", cx),
691-
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
691+
vis = visibility_print_with_space(it, cx),
692692
unsafety = t.unsafety(tcx).print_with_space(),
693693
is_auto = if t.is_auto(tcx) { "auto " } else { "" },
694694
name = it.name.unwrap(),
@@ -1243,7 +1243,7 @@ fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &c
12431243
w,
12441244
"{attrs}{vis}type {name}{generics}{where_clause} = {type_};",
12451245
attrs = render_attributes_in_pre(it, "", cx),
1246-
vis = visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
1246+
vis = visibility_print_with_space(it, cx),
12471247
name = it.name.unwrap(),
12481248
generics = t.generics.print(cx),
12491249
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
@@ -1522,14 +1522,13 @@ fn print_tuple_struct_fields<'a, 'cx: 'a>(
15221522
}
15231523

15241524
fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::Enum) {
1525-
let tcx = cx.tcx();
15261525
let count_variants = e.variants().count();
15271526
wrap_item(w, |w| {
15281527
render_attributes_in_code(w, it, cx);
15291528
write!(
15301529
w,
15311530
"{}enum {}{}",
1532-
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
1531+
visibility_print_with_space(it, cx),
15331532
it.name.unwrap(),
15341533
e.generics.print(cx),
15351534
);
@@ -1860,7 +1859,7 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
18601859
write!(
18611860
w,
18621861
"{vis}const {name}{generics}: {typ}{where_clause}",
1863-
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
1862+
vis = visibility_print_with_space(it, cx),
18641863
name = it.name.unwrap(),
18651864
generics = c.generics.print(cx),
18661865
typ = c.type_.print(cx),
@@ -1964,7 +1963,7 @@ fn item_static(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Item,
19641963
write!(
19651964
buffer,
19661965
"{vis}static {mutability}{name}: {typ}",
1967-
vis = visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
1966+
vis = visibility_print_with_space(it, cx),
19681967
mutability = s.mutability.print_with_space(),
19691968
name = it.name.unwrap(),
19701969
typ = s.type_.print(cx)
@@ -1982,7 +1981,7 @@ fn item_foreign_type(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::
19821981
write!(
19831982
buffer,
19841983
" {}type {};\n}}",
1985-
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
1984+
visibility_print_with_space(it, cx),
19861985
it.name.unwrap(),
19871986
)
19881987
.unwrap();
@@ -2139,13 +2138,7 @@ fn render_union<'a, 'cx: 'a>(
21392138
cx: &'a Context<'cx>,
21402139
) -> impl fmt::Display + 'a + Captures<'cx> {
21412140
display_fn(move |mut f| {
2142-
let tcx = cx.tcx();
2143-
write!(
2144-
f,
2145-
"{}union {}",
2146-
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
2147-
it.name.unwrap(),
2148-
)?;
2141+
write!(f, "{}union {}", visibility_print_with_space(it, cx), it.name.unwrap(),)?;
21492142

21502143
let where_displayed = g
21512144
.map(|g| {
@@ -2175,7 +2168,7 @@ fn render_union<'a, 'cx: 'a>(
21752168
write!(
21762169
f,
21772170
" {}{}: {},\n",
2178-
visibility_print_with_space(field.visibility(tcx), field.item_id, cx),
2171+
visibility_print_with_space(field, cx),
21792172
field.name.unwrap(),
21802173
ty.print(cx)
21812174
)?;
@@ -2203,11 +2196,10 @@ fn render_struct(
22032196
structhead: bool,
22042197
cx: &Context<'_>,
22052198
) {
2206-
let tcx = cx.tcx();
22072199
write!(
22082200
w,
22092201
"{}{}{}",
2210-
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
2202+
visibility_print_with_space(it, cx),
22112203
if structhead { "struct " } else { "" },
22122204
it.name.unwrap()
22132205
);
@@ -2236,7 +2228,6 @@ fn render_struct_fields(
22362228
has_stripped_entries: bool,
22372229
cx: &Context<'_>,
22382230
) {
2239-
let tcx = cx.tcx();
22402231
match ty {
22412232
None => {
22422233
let where_displayed =
@@ -2260,7 +2251,7 @@ fn render_struct_fields(
22602251
write!(
22612252
w,
22622253
"\n{tab} {vis}{name}: {ty},",
2263-
vis = visibility_print_with_space(field.visibility(tcx), field.item_id, cx),
2254+
vis = visibility_print_with_space(field, cx),
22642255
name = field.name.unwrap(),
22652256
ty = ty.print(cx),
22662257
);
@@ -2296,16 +2287,7 @@ fn render_struct_fields(
22962287
match *field.kind {
22972288
clean::StrippedItem(box clean::StructFieldItem(..)) => write!(w, "_"),
22982289
clean::StructFieldItem(ref ty) => {
2299-
write!(
2300-
w,
2301-
"{}{}",
2302-
visibility_print_with_space(
2303-
field.visibility(tcx),
2304-
field.item_id,
2305-
cx
2306-
),
2307-
ty.print(cx),
2308-
)
2290+
write!(w, "{}{}", visibility_print_with_space(field, cx), ty.print(cx),)
23092291
}
23102292
_ => unreachable!(),
23112293
}

0 commit comments

Comments
 (0)