Skip to content

Commit d3f4c48

Browse files
committed
rustdoc: Render visibilities succinctly
1 parent 3d10d3e commit d3f4c48

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2299,14 +2299,14 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
22992299
if matchers.len() <= 1 {
23002300
format!(
23012301
"{}macro {}{} {{\n ...\n}}",
2302-
vis.print_with_space(cx.tcx),
2302+
vis.print_with_space(cx.tcx, item.hir_id.owner),
23032303
name,
23042304
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
23052305
)
23062306
} else {
23072307
format!(
23082308
"{}macro {} {{\n{}}}",
2309-
vis.print_with_space(cx.tcx),
2309+
vis.print_with_space(cx.tcx, item.hir_id.owner),
23102310
name,
23112311
matchers
23122312
.iter()

src/librustdoc/html/format.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::fmt;
1212
use rustc_data_structures::fx::FxHashSet;
1313
use rustc_hir as hir;
1414
use rustc_middle::ty::TyCtxt;
15-
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
15+
use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_INDEX};
1616
use rustc_target::spec::abi::Abi;
1717

1818
use crate::clean::{self, PrimitiveType};
@@ -1085,12 +1085,21 @@ impl Function<'_> {
10851085
}
10861086

10871087
impl clean::Visibility {
1088-
crate fn print_with_space<'tcx>(self, tcx: TyCtxt<'tcx>) -> impl fmt::Display + 'tcx {
1088+
crate fn print_with_space<'tcx>(
1089+
self,
1090+
tcx: TyCtxt<'tcx>,
1091+
item_did: LocalDefId,
1092+
) -> impl fmt::Display + 'tcx {
10891093
use rustc_span::symbol::kw;
10901094

10911095
display_fn(move |f| match self {
10921096
clean::Public => f.write_str("pub "),
10931097
clean::Inherited => Ok(()),
1098+
clean::Visibility::Restricted(did)
1099+
if did.index == tcx.parent_module_from_def_id(item_did).local_def_index =>
1100+
{
1101+
Ok(())
1102+
}
10941103
clean::Visibility::Restricted(did) if did.index == CRATE_DEF_INDEX => {
10951104
write!(f, "pub(crate) ")
10961105
}

src/librustdoc/html/render/mod.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -2157,14 +2157,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
21572157
Some(ref src) => write!(
21582158
w,
21592159
"<tr><td><code>{}extern crate {} as {};",
2160-
myitem.visibility.print_with_space(cx.tcx()),
2160+
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()),
21612161
anchor(myitem.def_id, &*src.as_str()),
21622162
name
21632163
),
21642164
None => write!(
21652165
w,
21662166
"<tr><td><code>{}extern crate {};",
2167-
myitem.visibility.print_with_space(cx.tcx()),
2167+
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()),
21682168
anchor(myitem.def_id, &*name.as_str())
21692169
),
21702170
}
@@ -2175,7 +2175,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
21752175
write!(
21762176
w,
21772177
"<tr><td><code>{}{}</code></td></tr>",
2178-
myitem.visibility.print_with_space(cx.tcx()),
2178+
myitem.visibility.print_with_space(cx.tcx(), myitem.def_id.expect_local()),
21792179
import.print()
21802180
);
21812181
}
@@ -2392,7 +2392,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::
23922392
write!(
23932393
w,
23942394
"{vis}const {name}: {typ}",
2395-
vis = it.visibility.print_with_space(cx.tcx()),
2395+
vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
23962396
name = it.name.as_ref().unwrap(),
23972397
typ = c.type_.print(),
23982398
);
@@ -2426,7 +2426,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
24262426
write!(
24272427
w,
24282428
"{vis}static {mutability}{name}: {typ}</pre>",
2429-
vis = it.visibility.print_with_space(cx.tcx()),
2429+
vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
24302430
mutability = s.mutability.print_with_space(),
24312431
name = it.name.as_ref().unwrap(),
24322432
typ = s.type_.print()
@@ -2437,7 +2437,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
24372437
fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) {
24382438
let header_len = format!(
24392439
"{}{}{}{}{:#}fn {}{:#}",
2440-
it.visibility.print_with_space(cx.tcx()),
2440+
it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
24412441
f.header.constness.print_with_space(),
24422442
f.header.asyncness.print_with_space(),
24432443
f.header.unsafety.print_with_space(),
@@ -2452,7 +2452,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::
24522452
w,
24532453
"{vis}{constness}{asyncness}{unsafety}{abi}fn \
24542454
{name}{generics}{decl}{spotlight}{where_clause}</pre>",
2455-
vis = it.visibility.print_with_space(cx.tcx()),
2455+
vis = it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
24562456
constness = f.header.constness.print_with_space(),
24572457
asyncness = f.header.asyncness.print_with_space(),
24582458
unsafety = f.header.unsafety.print_with_space(),
@@ -2578,7 +2578,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
25782578
write!(
25792579
w,
25802580
"{}{}{}trait {}{}{}",
2581-
it.visibility.print_with_space(cx.tcx()),
2581+
it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
25822582
t.unsafety.print_with_space(),
25832583
if t.is_auto { "auto " } else { "" },
25842584
it.name.as_ref().unwrap(),
@@ -2896,7 +2896,7 @@ fn assoc_const(
28962896
w,
28972897
"{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}",
28982898
extra,
2899-
it.visibility.print_with_space(cx.tcx()),
2899+
it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
29002900
naive_assoc_href(it, link),
29012901
it.name.as_ref().unwrap(),
29022902
ty.print()
@@ -3015,7 +3015,7 @@ fn render_assoc_item(
30153015
};
30163016
let mut header_len = format!(
30173017
"{}{}{}{}{}{:#}fn {}{:#}",
3018-
meth.visibility.print_with_space(cx.tcx()),
3018+
meth.visibility.print_with_space(cx.tcx(), meth.def_id.expect_local()),
30193019
header.constness.print_with_space(),
30203020
header.asyncness.print_with_space(),
30213021
header.unsafety.print_with_space(),
@@ -3037,7 +3037,7 @@ fn render_assoc_item(
30373037
"{}{}{}{}{}{}{}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\
30383038
{generics}{decl}{spotlight}{where_clause}",
30393039
if parent == ItemType::Trait { " " } else { "" },
3040-
meth.visibility.print_with_space(cx.tcx()),
3040+
meth.visibility.print_with_space(cx.tcx(), meth.def_id.expect_local()),
30413041
header.constness.print_with_space(),
30423042
header.asyncness.print_with_space(),
30433043
header.unsafety.print_with_space(),
@@ -3189,7 +3189,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
31893189
write!(
31903190
w,
31913191
"{}enum {}{}{}",
3192-
it.visibility.print_with_space(cx.tcx()),
3192+
it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
31933193
it.name.as_ref().unwrap(),
31943194
e.generics.print(),
31953195
WhereClause { gens: &e.generics, indent: 0, end_newline: true }
@@ -3364,7 +3364,7 @@ fn render_struct(
33643364
write!(
33653365
w,
33663366
"{}{}{}",
3367-
it.visibility.print_with_space(cx.tcx()),
3367+
it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
33683368
if structhead { "struct " } else { "" },
33693369
it.name.as_ref().unwrap()
33703370
);
@@ -3384,7 +3384,7 @@ fn render_struct(
33843384
w,
33853385
"\n{} {}{}: {},",
33863386
tab,
3387-
field.visibility.print_with_space(cx.tcx()),
3387+
field.visibility.print_with_space(cx.tcx(), field.def_id.expect_local()),
33883388
field.name.as_ref().unwrap(),
33893389
ty.print()
33903390
);
@@ -3413,7 +3413,14 @@ fn render_struct(
34133413
match field.kind {
34143414
clean::StrippedItem(box clean::StructFieldItem(..)) => write!(w, "_"),
34153415
clean::StructFieldItem(ref ty) => {
3416-
write!(w, "{}{}", field.visibility.print_with_space(cx.tcx()), ty.print())
3416+
write!(
3417+
w,
3418+
"{}{}",
3419+
field
3420+
.visibility
3421+
.print_with_space(cx.tcx(), field.def_id.expect_local()),
3422+
ty.print()
3423+
)
34173424
}
34183425
_ => unreachable!(),
34193426
}
@@ -3446,7 +3453,7 @@ fn render_union(
34463453
write!(
34473454
w,
34483455
"{}{}{}",
3449-
it.visibility.print_with_space(cx.tcx()),
3456+
it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
34503457
if structhead { "union " } else { "" },
34513458
it.name.as_ref().unwrap()
34523459
);
@@ -3461,7 +3468,7 @@ fn render_union(
34613468
write!(
34623469
w,
34633470
" {}{}: {},\n{}",
3464-
field.visibility.print_with_space(cx.tcx()),
3471+
field.visibility.print_with_space(cx.tcx(), field.def_id.expect_local()),
34653472
field.name.as_ref().unwrap(),
34663473
ty.print(),
34673474
tab
@@ -4100,7 +4107,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, cache:
41004107
write!(
41014108
w,
41024109
" {}type {};\n}}</pre>",
4103-
it.visibility.print_with_space(cx.tcx()),
4110+
it.visibility.print_with_space(cx.tcx(), it.def_id.expect_local()),
41044111
it.name.as_ref().unwrap(),
41054112
);
41064113

0 commit comments

Comments
 (0)