From 6622376ff65d5c5f60f8a8877b223bb69e9a9552 Mon Sep 17 00:00:00 2001 From: inquisitivecrystal <22333129+inquisitivecrystal@users.noreply.github.com> Date: Sun, 7 Nov 2021 19:54:19 -0800 Subject: [PATCH] Use computed visibility in rustdoc --- src/librustdoc/clean/mod.rs | 12 ++++++++---- src/librustdoc/clean/types.rs | 4 ++-- src/librustdoc/visit_ast.rs | 13 +++++-------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 3db0ef17fd810..a44641f4488e8 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1881,7 +1881,7 @@ fn clean_extern_crate( // this is the ID of the crate itself let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX }; let attrs = cx.tcx.hir().attrs(krate.hir_id()); - let please_inline = krate.vis.node.is_pub() + let please_inline = cx.tcx.visibility(krate.def_id).is_public() && attrs.iter().any(|a| { a.has_name(sym::doc) && match a.meta_item_list() { @@ -1933,9 +1933,12 @@ fn clean_use_statement( return Vec::new(); } + let visibility = cx.tcx.visibility(import.def_id); let attrs = cx.tcx.hir().attrs(import.hir_id()); let inline_attr = attrs.lists(sym::doc).get_word_attr(sym::inline); - let pub_underscore = import.vis.node.is_pub() && name == kw::Underscore; + let pub_underscore = visibility.is_public() && name == kw::Underscore; + let current_mod = cx.tcx.parent_module_from_def_id(import.def_id); + let parent_mod = cx.tcx.parent_module_from_def_id(current_mod); if pub_underscore { if let Some(ref inline) = inline_attr { @@ -1954,8 +1957,9 @@ fn clean_use_statement( // forcefully don't inline if this is not public or if the // #[doc(no_inline)] attribute is present. // Don't inline doc(hidden) imports so they can be stripped at a later stage. - let mut denied = !(import.vis.node.is_pub() - || (cx.render_options.document_private && import.vis.node.is_pub_restricted())) + let mut denied = !(visibility.is_public() + || (cx.render_options.document_private + && visibility.is_accessible_from(parent_mod.to_def_id(), cx.tcx))) || pub_underscore || attrs.iter().any(|a| { a.has_name(sym::doc) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index fd4d620c9591e..2dba52afcd9cd 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -254,7 +254,7 @@ impl ExternalCrate { as_keyword(Res::Def(DefKind::Mod, id.def_id.to_def_id())) } hir::ItemKind::Use(path, hir::UseKind::Single) - if item.vis.node.is_pub() => + if tcx.visibility(id.def_id).is_public() => { as_keyword(path.res.expect_non_local()) .map(|(_, prim)| (id.def_id.to_def_id(), prim)) @@ -320,7 +320,7 @@ impl ExternalCrate { as_primitive(Res::Def(DefKind::Mod, id.def_id.to_def_id())) } hir::ItemKind::Use(path, hir::UseKind::Single) - if item.vis.node.is_pub() => + if tcx.visibility(id.def_id).is_public() => { as_primitive(path.res.expect_non_local()).map(|(_, prim)| { // Pretend the primitive is local. diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs index 5d1f934240f03..1191a94a7039b 100644 --- a/src/librustdoc/visit_ast.rs +++ b/src/librustdoc/visit_ast.rs @@ -10,7 +10,6 @@ use rustc_hir::CRATE_HIR_ID; use rustc_middle::middle::privacy::AccessLevel; use rustc_middle::ty::TyCtxt; use rustc_span::def_id::{CRATE_DEF_ID, LOCAL_CRATE}; -use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Symbol}; use std::mem; @@ -72,9 +71,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } crate fn visit(mut self) -> Module<'tcx> { - let span = self.cx.tcx.def_span(CRATE_DEF_ID); let mut top_level_module = self.visit_mod_contents( - &Spanned { span, node: hir::VisibilityKind::Public }, hir::CRATE_HIR_ID, self.cx.tcx.hir().root_module(), self.cx.tcx.crate_name(LOCAL_CRATE), @@ -134,15 +131,15 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { fn visit_mod_contents( &mut self, - vis: &hir::Visibility<'_>, id: hir::HirId, m: &'tcx hir::Mod<'tcx>, name: Symbol, ) -> Module<'tcx> { let mut om = Module::new(name, id, m.inner); + let def_id = self.cx.tcx.hir().local_def_id(id).to_def_id(); // Keep track of if there were any private modules in the path. let orig_inside_public_path = self.inside_public_path; - self.inside_public_path &= vis.node.is_pub(); + self.inside_public_path &= self.cx.tcx.visibility(def_id).is_public(); for &i in m.item_ids { let item = self.cx.tcx.hir().item(i); self.visit_item(item, None, &mut om); @@ -259,7 +256,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { let name = renamed.unwrap_or(item.ident.name); let def_id = item.def_id.to_def_id(); - let is_pub = item.vis.node.is_pub() || self.cx.tcx.has_attr(def_id, sym::macro_export); + let is_pub = self.cx.tcx.visibility(def_id).is_public(); if is_pub { self.store_path(item.def_id.to_def_id()); @@ -332,7 +329,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { } } hir::ItemKind::Mod(ref m) => { - om.mods.push(self.visit_mod_contents(&item.vis, item.hir_id(), m, name)); + om.mods.push(self.visit_mod_contents(item.hir_id(), m, name)); } hir::ItemKind::Fn(..) | hir::ItemKind::ExternCrate(..) @@ -368,7 +365,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> { om: &mut Module<'tcx>, ) { // If inlining we only want to include public functions. - if !self.inlining || item.vis.node.is_pub() { + if !self.inlining || self.cx.tcx.visibility(item.def_id).is_public() { om.foreigns.push((item, renamed)); } }