Skip to content

Commit a2fb4b9

Browse files
committed
Remove DefPath from Visibility and calculate it on demand
1 parent d8d3ab9 commit a2fb4b9

File tree

5 files changed

+75
-61
lines changed

5 files changed

+75
-61
lines changed

src/librustdoc/clean/mod.rs

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

17891789
impl Clean<Visibility> for ty::Visibility {
1790-
fn clean(&self, cx: &DocContext<'_>) -> Visibility {
1790+
fn clean(&self, _cx: &DocContext<'_>) -> Visibility {
17911791
match *self {
17921792
ty::Visibility::Public => Visibility::Public,
1793+
// NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
1794+
// while rustdoc really does mean inherited. That means that for enum variants, such as
1795+
// `pub enum E { V }`, `V` will be marked as `Public` by `ty`, but as `Inherited` by rustdoc.
1796+
// This is the main reason `impl Clean for hir::Visibility` still exists; various parts of clean
1797+
// override `tcx.visibility` explicitly to make sure this distinction is captured.
17931798
ty::Visibility::Invisible => Visibility::Inherited,
1794-
ty::Visibility::Restricted(module) => {
1795-
Visibility::Restricted(module, cx.tcx.def_path(module))
1796-
}
1799+
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
17971800
}
17981801
}
17991802
}
@@ -2303,14 +2306,14 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
23032306
if matchers.len() <= 1 {
23042307
format!(
23052308
"{}macro {}{} {{\n ...\n}}",
2306-
vis.print_with_space(),
2309+
vis.print_with_space(cx.tcx),
23072310
name,
23082311
matchers.iter().map(|span| span.to_src(cx)).collect::<String>(),
23092312
)
23102313
} else {
23112314
format!(
23122315
"{}macro {} {{\n{}}}",
2313-
vis.print_with_space(),
2316+
vis.print_with_space(cx.tcx),
23142317
name,
23152318
matchers
23162319
.iter()

src/librustdoc/clean/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1569,11 +1569,11 @@ impl From<hir::PrimTy> for PrimitiveType {
15691569
}
15701570
}
15711571

1572-
#[derive(Clone, Debug)]
1572+
#[derive(Copy, Clone, Debug)]
15731573
crate enum Visibility {
15741574
Public,
15751575
Inherited,
1576-
Restricted(DefId, rustc_hir::definitions::DefPath),
1576+
Restricted(DefId),
15771577
}
15781578

15791579
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)