Skip to content

Commit 3f21bdd

Browse files
committed
rustdoc: Simplify modifications of effective visibility table
1 parent 68c836a commit 3f21bdd

File tree

3 files changed

+32
-79
lines changed

3 files changed

+32
-79
lines changed

src/librustdoc/clean/utils.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::clean::{
88
};
99
use crate::core::DocContext;
1010
use crate::formats::item_type::ItemType;
11-
use crate::visit_lib::LibEmbargoVisitor;
1211

1312
use rustc_ast as ast;
1413
use rustc_ast::tokenstream::TokenTree;
@@ -32,7 +31,7 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
3231

3332
for &cnum in cx.tcx.crates(()) {
3433
// Analyze doc-reachability for extern items
35-
LibEmbargoVisitor::new(cx).visit_lib(cnum);
34+
crate::visit_lib::lib_embargo_visit_item(cx, cnum.as_def_id());
3635
}
3736

3837
// Clean the crate, translating the entire librustc_ast AST to one that is

src/librustdoc/visit_ast.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::def_id::DefId;
88
use rustc_hir::Node;
99
use rustc_hir::CRATE_HIR_ID;
10-
use rustc_middle::middle::privacy::Level;
11-
use rustc_middle::ty::{TyCtxt, Visibility};
10+
use rustc_middle::ty::TyCtxt;
1211
use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
1312
use rustc_span::symbol::{kw, sym, Symbol};
1413
use rustc_span::Span;
1514

1615
use std::mem;
1716

18-
use crate::clean::{self, cfg::Cfg, AttributesExt, NestedAttributesExt};
17+
use crate::clean::{cfg::Cfg, AttributesExt, NestedAttributesExt};
1918
use crate::core;
2019

2120
/// This module is used to store stuff from Rust's AST in a more convenient
@@ -221,23 +220,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
221220
// made reachable by cross-crate inlining which we're checking here.
222221
// (this is done here because we need to know this upfront).
223222
if !res_did.is_local() && !is_no_inline {
224-
let attrs = clean::inline::load_attrs(self.cx, res_did);
225-
let self_is_hidden = attrs.lists(sym::doc).has_word(sym::hidden);
226-
if !self_is_hidden {
227-
if let Res::Def(kind, did) = res {
228-
if kind == DefKind::Mod {
229-
crate::visit_lib::LibEmbargoVisitor::new(self.cx).visit_mod(did)
230-
} else {
231-
// All items need to be handled here in case someone wishes to link
232-
// to them with intra-doc links
233-
self.cx.cache.effective_visibilities.set_public_at_level(
234-
did,
235-
|| Visibility::Restricted(CRATE_DEF_ID),
236-
Level::Direct,
237-
);
238-
}
239-
}
240-
}
223+
crate::visit_lib::lib_embargo_visit_item(self.cx, res_did);
241224
return false;
242225
}
243226

src/librustdoc/visit_lib.rs

+28-57
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,57 @@
1+
use crate::core::DocContext;
12
use rustc_data_structures::fx::FxHashSet;
2-
use rustc_hir::def::{DefKind, Res};
3-
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_ID};
3+
use rustc_hir::def::DefKind;
4+
use rustc_hir::def_id::{DefId, CRATE_DEF_ID};
45
use rustc_middle::middle::privacy::{EffectiveVisibilities, Level};
56
use rustc_middle::ty::{TyCtxt, Visibility};
67

78
// FIXME: this may not be exhaustive, but is sufficient for rustdocs current uses
89

10+
pub(crate) fn lib_embargo_visit_item(cx: &mut DocContext<'_>, def_id: DefId) {
11+
assert!(!def_id.is_local());
12+
LibEmbargoVisitor {
13+
tcx: cx.tcx,
14+
effective_visibilities: &mut cx.cache.effective_visibilities,
15+
visited_mods: FxHashSet::default(),
16+
}
17+
.visit_item(def_id)
18+
}
19+
920
/// Similar to `librustc_privacy::EmbargoVisitor`, but also takes
1021
/// specific rustdoc annotations into account (i.e., `doc(hidden)`)
11-
pub(crate) struct LibEmbargoVisitor<'a, 'tcx> {
22+
struct LibEmbargoVisitor<'a, 'tcx> {
1223
tcx: TyCtxt<'tcx>,
1324
// Effective visibilities for reachable nodes
1425
effective_visibilities: &'a mut EffectiveVisibilities<DefId>,
15-
// Previous level, None means unreachable
16-
prev_level: Option<Level>,
1726
// Keeps track of already visited modules, in case a module re-exports its parent
1827
visited_mods: FxHashSet<DefId>,
1928
}
2029

21-
impl<'a, 'tcx> LibEmbargoVisitor<'a, 'tcx> {
22-
pub(crate) fn new(cx: &'a mut crate::core::DocContext<'tcx>) -> LibEmbargoVisitor<'a, 'tcx> {
23-
LibEmbargoVisitor {
24-
tcx: cx.tcx,
25-
effective_visibilities: &mut cx.cache.effective_visibilities,
26-
prev_level: Some(Level::Direct),
27-
visited_mods: FxHashSet::default(),
28-
}
29-
}
30-
31-
pub(crate) fn visit_lib(&mut self, cnum: CrateNum) {
32-
let did = cnum.as_def_id();
33-
self.update(did, Some(Level::Direct));
34-
self.visit_mod(did);
35-
}
36-
37-
// Updates node level and returns the updated level
38-
fn update(&mut self, did: DefId, level: Option<Level>) -> Option<Level> {
39-
let is_hidden = self.tcx.is_doc_hidden(did);
40-
41-
let old_level = self.effective_visibilities.public_at_level(did);
42-
// Visibility levels can only grow
43-
if level > old_level && !is_hidden {
44-
self.effective_visibilities.set_public_at_level(
45-
did,
46-
|| Visibility::Restricted(CRATE_DEF_ID),
47-
level.unwrap(),
48-
);
49-
level
50-
} else {
51-
old_level
52-
}
53-
}
54-
55-
pub(crate) fn visit_mod(&mut self, def_id: DefId) {
30+
impl LibEmbargoVisitor<'_, '_> {
31+
fn visit_mod(&mut self, def_id: DefId) {
5632
if !self.visited_mods.insert(def_id) {
5733
return;
5834
}
5935

6036
for item in self.tcx.module_children(def_id).iter() {
6137
if let Some(def_id) = item.res.opt_def_id() {
62-
if self.tcx.def_key(def_id).parent.map_or(false, |d| d == def_id.index)
63-
|| item.vis.is_public()
64-
{
65-
self.visit_item(item.res);
38+
if item.vis.is_public() {
39+
self.visit_item(def_id);
6640
}
6741
}
6842
}
6943
}
7044

71-
fn visit_item(&mut self, res: Res<!>) {
72-
let def_id = res.def_id();
73-
let vis = self.tcx.visibility(def_id);
74-
let inherited_item_level = if vis.is_public() { self.prev_level } else { None };
75-
76-
let item_level = self.update(def_id, inherited_item_level);
77-
78-
if let Res::Def(DefKind::Mod, _) = res {
79-
let orig_level = self.prev_level;
80-
81-
self.prev_level = item_level;
82-
self.visit_mod(def_id);
83-
self.prev_level = orig_level;
45+
fn visit_item(&mut self, def_id: DefId) {
46+
if !self.tcx.is_doc_hidden(def_id) {
47+
self.effective_visibilities.set_public_at_level(
48+
def_id,
49+
|| Visibility::Restricted(CRATE_DEF_ID),
50+
Level::Direct,
51+
);
52+
if self.tcx.def_kind(def_id) == DefKind::Mod {
53+
self.visit_mod(def_id);
54+
}
8455
}
8556
}
8657
}

0 commit comments

Comments
 (0)