Skip to content

Commit 497d2c4

Browse files
committed
rustdoc: turn is_unnamable into a compiler query
1 parent 213d946 commit 497d2c4

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
879879
providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
880880
providers.early_lint_checks = early_lint_checks;
881881
providers.env_var_os = env_var_os;
882+
providers.is_unnamable = crate::queries::is_unnamable;
882883
limits::provide(providers);
883884
proc_macro_decls::provide(providers);
884885
rustc_const_eval::provide(providers);

compiler/rustc_interface/src/queries.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use rustc_codegen_ssa::CodegenResults;
55
use rustc_codegen_ssa::traits::CodegenBackend;
66
use rustc_data_structures::svh::Svh;
77
use rustc_errors::timings::TimingSection;
8-
use rustc_hir::def_id::LOCAL_CRATE;
8+
use rustc_hir::def::DefKind;
9+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
910
use rustc_metadata::EncodedMetadata;
1011
use rustc_middle::dep_graph::DepGraph;
1112
use rustc_middle::ty::TyCtxt;
@@ -107,3 +108,32 @@ impl Linker {
107108
codegen_backend.link(sess, codegen_results, self.metadata, &self.output_filenames)
108109
}
109110
}
111+
112+
/// Checks if the given `DefId` refers to an item that is unnamable, such as one defined in a const block.
113+
///
114+
/// # Example
115+
///
116+
/// ```
117+
/// pub fn f() {
118+
/// // In here, `X` is not reachable outside of `f`, making it unnamable.
119+
/// pub struct X;
120+
/// }
121+
/// ```
122+
pub(crate) fn is_unnamable(tcx: TyCtxt<'_>, did: DefId) -> bool {
123+
let mut cur_did = did;
124+
while let Some(parent) = tcx.opt_parent(cur_did) {
125+
match tcx.def_kind(parent) {
126+
// items defined in these can be linked to, as long as they are visible
127+
DefKind::Mod | DefKind::ForeignMod => cur_did = parent,
128+
// items in impls can be linked to,
129+
// as long as we can link to the item the impl is on.
130+
// since associated traits are not yet a thing,
131+
// it should not be possible to refer to an impl item if
132+
// the base type is not namable.
133+
DefKind::Impl { .. } => return false,
134+
// everything else does not have docs generated for it
135+
_ => return true,
136+
}
137+
}
138+
return false;
139+
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,22 @@ rustc_queries! {
15471547
separate_provide_extern
15481548
}
15491549

1550+
/// Checks if the given `DefId` refers to an item that is unnamable, such as one defined in a const block.
1551+
///
1552+
/// Used by rustdoc.
1553+
///
1554+
/// # Example
1555+
///
1556+
/// ```
1557+
/// pub fn f() {
1558+
/// // In here, `X` is not reachable outside of `f`, making it unnamable.
1559+
/// pub struct X;
1560+
/// }
1561+
/// ```
1562+
query is_unnamable(def_id: DefId) -> bool {
1563+
desc { |tcx| "checking if `{}` is unnamable", tcx.def_path_str(def_id) }
1564+
}
1565+
15501566
query impl_parent(def_id: DefId) -> Option<DefId> {
15511567
desc { |tcx| "computing specialization parent impl of `{}`", tcx.def_path_str(def_id) }
15521568
separate_provide_extern

src/librustdoc/html/format.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -481,26 +481,6 @@ fn generate_item_def_id_path(
481481
Ok((url_parts, shortty, fqp))
482482
}
483483

484-
/// Checks if the given defid refers to an item that is unnamable, such as one defined in a const block.
485-
fn is_unnamable(tcx: TyCtxt<'_>, did: DefId) -> bool {
486-
let mut cur_did = did;
487-
while let Some(parent) = tcx.opt_parent(cur_did) {
488-
match tcx.def_kind(parent) {
489-
// items defined in these can be linked to, as long as they are visible
490-
DefKind::Mod | DefKind::ForeignMod => cur_did = parent,
491-
// items in impls can be linked to,
492-
// as long as we can link to the item the impl is on.
493-
// since associated traits are not a thing,
494-
// it should not be possible to refer to an impl item if
495-
// the base type is not namable.
496-
DefKind::Impl { .. } => return false,
497-
// everything else does not have docs generated for it
498-
_ => return true,
499-
}
500-
}
501-
return false;
502-
}
503-
504484
fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
505485
if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
506486
}
@@ -574,7 +554,7 @@ pub(crate) fn href_with_root_path(
574554
}
575555
_ => original_did,
576556
};
577-
if is_unnamable(cx.tcx(), did) {
557+
if cx.tcx().is_unnamable(did) {
578558
return Err(HrefError::UnnamableItem);
579559
}
580560
let cache = cx.cache();

0 commit comments

Comments
 (0)