Skip to content

Commit 4933a06

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

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,25 @@ impl Linker {
107107
codegen_backend.link(sess, codegen_results, self.metadata, &self.output_filenames)
108108
}
109109
}
110+
111+
/// Checks if the given defid refers to an item that is unnamable, such as one defined in a const block.
112+
// defined here so it can't accidentally be used directly instead of through the query.
113+
pub(crate) fn is_unnamable(tcx: TyCtxt<'_>, did: rustc_hir::def_id::DefId) -> bool {
114+
use rustc_hir::def::DefKind;
115+
let mut cur_did = did;
116+
while let Some(parent) = tcx.opt_parent(cur_did) {
117+
match tcx.def_kind(parent) {
118+
// items defined in these can be linked to, as long as they are visible
119+
DefKind::Mod | DefKind::ForeignMod => cur_did = parent,
120+
// items in impls can be linked to,
121+
// as long as we can link to the item the impl is on.
122+
// since associated traits are not yet a thing,
123+
// it should not be possible to refer to an impl item if
124+
// the base type is not namable.
125+
DefKind::Impl { .. } => return false,
126+
// everything else does not have docs generated for it
127+
_ => return true,
128+
}
129+
}
130+
return false;
131+
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,12 @@ 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+
/// Used by rustdoc
1552+
query is_unnamable(def_id: DefId) -> bool {
1553+
desc { |tcx| "checking if `{}` is unnamable", tcx.def_path_str(def_id) }
1554+
}
1555+
15501556
query impl_parent(def_id: DefId) -> Option<DefId> {
15511557
desc { |tcx| "computing specialization parent impl of `{}`", tcx.def_path_str(def_id) }
15521558
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)