Skip to content

Commit b949e8e

Browse files
Move inherits_doc_hidden function directly into TyCtxt
1 parent 24c0b81 commit b949e8e

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

compiler/rustc_middle/src/ty/context.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,31 @@ impl<'tcx> TyCtxt<'tcx> {
931931
self.def_path(def_id).to_string_no_crate_verbose()
932932
)
933933
}
934+
935+
/// Returns `true` if the provided `def_id` has any ancestor with the `#[doc(hidden)]`
936+
/// attribute.
937+
///
938+
/// It doesn't check if `def_id` itself has this attribute though. You will need to use
939+
/// [`TyCtxt::is_doc_hidden`] to get this information.
940+
pub fn inherits_doc_hidden(self, mut def_id: LocalDefId) -> bool {
941+
let hir = self.hir();
942+
while let Some(id) = self.opt_local_parent(def_id) {
943+
def_id = id;
944+
if self.is_doc_hidden(def_id.to_def_id()) {
945+
return true;
946+
} else if let Some(node) = hir.find_by_def_id(def_id) &&
947+
matches!(
948+
node,
949+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(_), .. }),
950+
)
951+
{
952+
// `impl` blocks stand a bit on their own: unless they have `#[doc(hidden)]` directly
953+
// on them, they don't inherit it from the parent context.
954+
return false;
955+
}
956+
}
957+
false
958+
}
934959
}
935960

936961
impl<'tcx> TyCtxtAt<'tcx> {

src/librustdoc/passes/check_doc_test_visibility.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::clean::*;
1111
use crate::core::DocContext;
1212
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
1313
use crate::visit::DocVisitor;
14-
use crate::visit_ast::inherits_doc_hidden;
1514
use rustc_hir as hir;
1615
use rustc_middle::lint::LintLevelSource;
1716
use rustc_session::lint;
@@ -95,7 +94,7 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
9594
}
9695

9796
if cx.tcx.is_doc_hidden(def_id.to_def_id())
98-
|| inherits_doc_hidden(cx.tcx, def_id)
97+
|| cx.tcx.inherits_doc_hidden(def_id)
9998
|| cx.tcx.def_span(def_id.to_def_id()).in_derive_expansion()
10099
{
101100
return false;

src/librustdoc/passes/strip_hidden.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::clean::{Item, ItemIdSet, NestedAttributesExt};
99
use crate::core::DocContext;
1010
use crate::fold::{strip_item, DocFolder};
1111
use crate::passes::{ImplStripper, Pass};
12-
use crate::visit_ast::inherits_doc_hidden;
1312

1413
pub(crate) const STRIP_HIDDEN: Pass = Pass {
1514
name: "strip-hidden",
@@ -92,7 +91,7 @@ impl<'a, 'tcx> DocFolder for Stripper<'a, 'tcx> {
9291
.item_id
9392
.as_def_id()
9493
.and_then(|def_id| def_id.as_local())
95-
.map(|def_id| inherits_doc_hidden(self.tcx, def_id))
94+
.map(|def_id| self.tcx.inherits_doc_hidden(def_id))
9695
.unwrap_or(false);
9796
}
9897
}

src/librustdoc/visit_ast.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,26 +59,6 @@ fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<Symbol> {
5959
std::iter::once(crate_name).chain(relative).collect()
6060
}
6161

62-
pub(crate) fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut def_id: LocalDefId) -> bool {
63-
let hir = tcx.hir();
64-
while let Some(id) = tcx.opt_local_parent(def_id) {
65-
def_id = id;
66-
if tcx.is_doc_hidden(def_id.to_def_id()) {
67-
return true;
68-
} else if let Some(node) = hir.find_by_def_id(def_id) &&
69-
matches!(
70-
node,
71-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(_), .. }),
72-
)
73-
{
74-
// `impl` blocks stand a bit on their own: unless they have `#[doc(hidden)]` directly
75-
// on them, they don't inherit it from the parent context.
76-
return false;
77-
}
78-
}
79-
false
80-
}
81-
8262
pub(crate) struct RustdocVisitor<'a, 'tcx> {
8363
cx: &'a mut core::DocContext<'tcx>,
8464
view_item_stack: LocalDefIdSet,
@@ -258,7 +238,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
258238

259239
let is_private =
260240
!self.cx.cache.effective_visibilities.is_directly_public(self.cx.tcx, ori_res_did);
261-
let is_hidden = inherits_doc_hidden(self.cx.tcx, res_did);
241+
let is_hidden = self.cx.tcx.inherits_doc_hidden(res_did);
262242

263243
// Only inline if requested or if the item would otherwise be stripped.
264244
if (!please_inline && !is_private && !is_hidden) || is_no_inline {
@@ -279,7 +259,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
279259
.cache
280260
.effective_visibilities
281261
.is_directly_public(self.cx.tcx, item_def_id.to_def_id()) &&
282-
!inherits_doc_hidden(self.cx.tcx, item_def_id)
262+
!self.cx.tcx.inherits_doc_hidden(item_def_id)
283263
{
284264
// The imported item is public and not `doc(hidden)` so no need to inline it.
285265
return false;

0 commit comments

Comments
 (0)