Skip to content

Commit 28d73a3

Browse files
committed
Auto merge of #80099 - jyn514:visibility-on-demand, r=GuillaumeGomez
Remove `DefPath` from `Visibility` and calculate it on demand Depends on #80090 and should not be merged before. Helps with #79103 and #76382. cc #80014 (comment) - `@nnethercote` I figured it out! It was simpler than I expected :) This brings the size of `clean::Visibility` down from 40 bytes to 8. Note that this does *not* remove `clean::Visibility`, even though it's now basically the same as `ty::Visibility`, because the `Invsible` variant means something different from `Inherited` and I thought it would be be confusing to merge the two. See the new comments on `impl Clean for ty::Visibility` for details.
2 parents 18b745e + a2fb4b9 commit 28d73a3

File tree

5 files changed

+70
-60
lines changed

5 files changed

+70
-60
lines changed

src/librustdoc/clean/mod.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -1777,25 +1777,28 @@ impl Clean<Visibility> for hir::Visibility<'_> {
17771777
hir::VisibilityKind::Inherited => Visibility::Inherited,
17781778
hir::VisibilityKind::Crate(_) => {
17791779
let krate = DefId::local(CRATE_DEF_INDEX);
1780-
Visibility::Restricted(krate, cx.tcx.def_path(krate))
1780+
Visibility::Restricted(krate)
17811781
}
17821782
hir::VisibilityKind::Restricted { ref path, .. } => {
17831783
let path = path.clean(cx);
17841784
let did = register_res(cx, path.res);
1785-
Visibility::Restricted(did, cx.tcx.def_path(did))
1785+
Visibility::Restricted(did)
17861786
}
17871787
}
17881788
}
17891789
}
17901790

17911791
impl Clean<Visibility> for ty::Visibility {
1792-
fn clean(&self, cx: &DocContext<'_>) -> Visibility {
1792+
fn clean(&self, _cx: &DocContext<'_>) -> Visibility {
17931793
match *self {
17941794
ty::Visibility::Public => Visibility::Public,
1795+
// NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
1796+
// while rustdoc really does mean inherited. That means that for enum variants, such as
1797+
// `pub enum E { V }`, `V` will be marked as `Public` by `ty`, but as `Inherited` by rustdoc.
1798+
// This is the main reason `impl Clean for hir::Visibility` still exists; various parts of clean
1799+
// override `tcx.visibility` explicitly to make sure this distinction is captured.
17951800
ty::Visibility::Invisible => Visibility::Inherited,
1796-
ty::Visibility::Restricted(module) => {
1797-
Visibility::Restricted(module, cx.tcx.def_path(module))
1798-
}
1801+
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
17991802
}
18001803
}
18011804
}
@@ -2296,14 +2299,14 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
22962299
if matchers.len() <= 1 {
22972300
format!(
22982301
"{}macro {}{} {{\n ...\n}}",
2299-
vis.print_with_space(),
2302+
vis.print_with_space(cx.tcx),
23002303
name,
23012304
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
23022305
)
23032306
} else {
23042307
format!(
23052308
"{}macro {} {{\n{}}}",
2306-
vis.print_with_space(),
2309+
vis.print_with_space(cx.tcx),
23072310
name,
23082311
matchers
23092312
.iter()

src/librustdoc/clean/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1573,11 +1573,11 @@ impl From<hir::PrimTy> for PrimitiveType {
15731573
}
15741574
}
15751575

1576-
#[derive(Clone, Debug)]
1576+
#[derive(Copy, Clone, Debug)]
15771577
crate enum Visibility {
15781578
Public,
15791579
Inherited,
1580-
Restricted(DefId, rustc_hir::definitions::DefPath),
1580+
Restricted(DefId),
15811581
}
15821582

15831583
impl Visibility {

src/librustdoc/html/format.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::fmt;
1111

1212
use rustc_data_structures::fx::FxHashSet;
1313
use rustc_hir as hir;
14+
use rustc_middle::ty::TyCtxt;
1415
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
1516
use rustc_target::spec::abi::Abi;
1617

@@ -1084,18 +1085,18 @@ impl Function<'_> {
10841085
}
10851086

10861087
impl clean::Visibility {
1087-
crate fn print_with_space(&self) -> impl fmt::Display + '_ {
1088+
crate fn print_with_space<'tcx>(self, tcx: TyCtxt<'tcx>) -> impl fmt::Display + 'tcx {
10881089
use rustc_span::symbol::kw;
10891090

1090-
display_fn(move |f| match *self {
1091+
display_fn(move |f| match self {
10911092
clean::Public => f.write_str("pub "),
10921093
clean::Inherited => Ok(()),
1093-
// If this is `pub(crate)`, `path` will be empty.
1094-
clean::Visibility::Restricted(did, _) if did.index == CRATE_DEF_INDEX => {
1094+
clean::Visibility::Restricted(did) if did.index == CRATE_DEF_INDEX => {
10951095
write!(f, "pub(crate) ")
10961096
}
1097-
clean::Visibility::Restricted(did, ref path) => {
1097+
clean::Visibility::Restricted(did) => {
10981098
f.write_str("pub(")?;
1099+
let path = tcx.def_path(did);
10991100
debug!("path={:?}", path);
11001101
let first_name =
11011102
path.data[0].data.get_opt_name().expect("modules are always named");

0 commit comments

Comments
 (0)