Skip to content

Commit 81ee3ee

Browse files
Auto merge of #147511 - azhogin:azhogin/is_doc_hidden_opt, r=<try>
is_doc_hidden optimized to hook in case of non-incremental build
2 parents bd34871 + f6e1933 commit 81ee3ee

File tree

9 files changed

+41
-16
lines changed

9 files changed

+41
-16
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,11 @@ pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
963963

964964
let incremental = dep_graph.is_fully_enabled();
965965

966+
// is_doc_hidden hook is reset to query call for incremental build (fallback for optimization)
967+
if incremental {
968+
providers.hooks.is_doc_hidden = |tcx, def_id| tcx.is_doc_hidden_q(def_id);
969+
}
970+
966971
let gcx_cell = OnceLock::new();
967972
let arena = WorkerLocal::new(|_| Arena::default());
968973
let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::Arc;
44

55
use rustc_hir::attrs::Deprecation;
66
use rustc_hir::def::{CtorKind, DefKind};
7-
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
7+
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE, LocalDefId};
88
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
99
use rustc_middle::arena::ArenaAllocatable;
1010
use rustc_middle::bug;
@@ -18,7 +18,7 @@ use rustc_middle::util::Providers;
1818
use rustc_session::cstore::{CrateStore, ExternCrate};
1919
use rustc_session::{Session, StableCrateId};
2020
use rustc_span::hygiene::ExpnId;
21-
use rustc_span::{Span, Symbol, kw};
21+
use rustc_span::{Span, Symbol, kw, sym};
2222

2323
use super::{Decodable, DecodeContext, DecodeIterator};
2424
use crate::creader::{CStore, LoadedMacro};
@@ -396,7 +396,7 @@ provide! { tcx, def_id, other, cdata,
396396
crate_extern_paths => { cdata.source().paths().cloned().collect() }
397397
expn_that_defined => { cdata.get_expn_that_defined(def_id.index, tcx.sess) }
398398
default_field => { cdata.get_default_field(def_id.index) }
399-
is_doc_hidden => { cdata.get_attr_flags(def_id.index).contains(AttrFlags::IS_DOC_HIDDEN) }
399+
is_doc_hidden_q => { cdata.get_attr_flags(def_id.index).contains(AttrFlags::IS_DOC_HIDDEN) }
400400
doc_link_resolutions => { tcx.arena.alloc(cdata.get_doc_link_resolutions(def_id.index)) }
401401
doc_link_traits_in_scope => {
402402
tcx.arena.alloc_from_iter(cdata.get_doc_link_traits_in_scope(def_id.index))
@@ -679,6 +679,29 @@ impl CrateStore for CStore {
679679
}
680680
}
681681

682+
/// Determines whether an item is directly annotated with `doc(hidden)`.
683+
fn is_doc_hidden_local(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
684+
tcx.get_attrs(def_id, sym::doc)
685+
.filter_map(|attr| attr.meta_item_list())
686+
.any(|items| items.iter().any(|item| item.has_name(sym::hidden)))
687+
}
688+
689+
// Optimization of is_doc_hidden query in case of non-incremental build.
690+
// is_doc_hidden query itself renamed into is_doc_hidden_q.
691+
#[inline]
692+
fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
693+
if let Some(local) = def_id.as_local() {
694+
is_doc_hidden_local(tcx, local)
695+
} else {
696+
let cdata = rustc_data_structures::sync::FreezeReadGuard::map(CStore::from_tcx(tcx), |c| {
697+
c.get_crate_data(def_id.krate).cdata
698+
});
699+
let cdata =
700+
crate::creader::CrateMetadataRef { cdata: &cdata, cstore: &CStore::from_tcx(tcx) };
701+
cdata.get_attr_flags(def_id.index).contains(AttrFlags::IS_DOC_HIDDEN)
702+
}
703+
}
704+
682705
fn provide_cstore_hooks(providers: &mut Providers) {
683706
providers.hooks.def_path_hash_to_def_id_extern = |tcx, hash, stable_crate_id| {
684707
// If this is a DefPathHash from an upstream crate, let the CrateStore map
@@ -706,4 +729,6 @@ fn provide_cstore_hooks(providers: &mut Providers) {
706729
cdata.imported_source_file(file_index as u32, tcx.sess);
707730
}
708731
};
732+
providers.hooks.is_doc_hidden = is_doc_hidden;
733+
providers.is_doc_hidden_q = is_doc_hidden_local;
709734
}

compiler/rustc_middle/src/hooks/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ declare_hooks! {
102102
/// Ensure the given scalar is valid for the given type.
103103
/// This checks non-recursive runtime validity.
104104
hook validate_scalar_in_layout(scalar: crate::ty::ScalarInt, ty: Ty<'tcx>) -> bool;
105+
106+
/// Determines whether an item is annotated with `#[doc(hidden)]`.
107+
hook is_doc_hidden(def_id: DefId) -> bool;
105108
}
106109

107110
#[cold]

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ rustc_queries! {
15111511
}
15121512

15131513
/// Determines whether an item is annotated with `#[doc(hidden)]`.
1514-
query is_doc_hidden(def_id: DefId) -> bool {
1514+
query is_doc_hidden_q(def_id: DefId) -> bool {
15151515
desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) }
15161516
separate_provide_extern
15171517
}

compiler/rustc_middle/src/ty/util.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,13 +1662,6 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(
16621662
val.fold_with(&mut visitor)
16631663
}
16641664

1665-
/// Determines whether an item is directly annotated with `doc(hidden)`.
1666-
fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
1667-
tcx.get_attrs(def_id, sym::doc)
1668-
.filter_map(|attr| attr.meta_item_list())
1669-
.any(|items| items.iter().any(|item| item.has_name(sym::hidden)))
1670-
}
1671-
16721665
/// Determines whether an item is annotated with `doc(notable_trait)`.
16731666
pub fn is_doc_notable_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
16741667
tcx.get_attrs(def_id, sym::doc)
@@ -1702,7 +1695,6 @@ pub fn intrinsic_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<ty::Intrinsi
17021695
pub fn provide(providers: &mut Providers) {
17031696
*providers = Providers {
17041697
reveal_opaque_types_in_bounds,
1705-
is_doc_hidden,
17061698
is_doc_notable_trait,
17071699
intrinsic_raw,
17081700
..*providers

src/librustdoc/clean/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ pub(crate) fn try_inline_glob(
192192
.iter()
193193
.filter(|child| !child.reexport_chain.is_empty())
194194
.filter_map(|child| child.res.opt_def_id())
195-
.filter(|def_id| !cx.tcx.is_doc_hidden(def_id))
195+
.filter(|def_id| !cx.tcx.is_doc_hidden(*def_id))
196196
.collect();
197197
let attrs = cx.tcx.hir_attrs(import.hir_id());
198198
let mut items = build_module_items(

src/librustdoc/visit_ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
363363
.unwrap_or(target_def_id);
364364
item_def_id != import_def_id
365365
&& self.cx.cache.effective_visibilities.is_directly_public(tcx, item_def_id.to_def_id())
366-
&& !tcx.is_doc_hidden(item_def_id)
366+
&& !tcx.is_doc_hidden(item_def_id.to_def_id())
367367
&& !inherits_doc_hidden(tcx, item_def_id, None)
368368
}
369369

src/tools/clippy/clippy_lints/src/macro_metavars_in_unsafe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ struct BodyVisitor<'a, 'tcx> {
149149
fn is_public_macro(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
150150
(cx.effective_visibilities.is_exported(def_id)
151151
|| find_attr!(cx.tcx.get_all_attrs(def_id), AttributeKind::MacroExport { .. }))
152-
&& !cx.tcx.is_doc_hidden(def_id)
152+
&& !cx.tcx.is_doc_hidden(def_id.to_def_id())
153153
}
154154

155155
impl<'tcx> Visitor<'tcx> for BodyVisitor<'_, 'tcx> {

src/tools/clippy/clippy_lints/src/new_without_default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
8282
// can't be implemented for unsafe new
8383
&& !sig.header.is_unsafe()
8484
// shouldn't be implemented when it is hidden in docs
85-
&& !cx.tcx.is_doc_hidden(impl_item.owner_id.def_id)
85+
&& !cx.tcx.is_doc_hidden(impl_item.owner_id.def_id.to_def_id())
8686
// when the result of `new()` depends on a parameter we should not require
8787
// an impl of `Default`
8888
&& impl_item.generics.params.is_empty()

0 commit comments

Comments
 (0)