Skip to content

rustdoc: turn is_unnamable into a compiler query #145021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1;
providers.early_lint_checks = early_lint_checks;
providers.env_var_os = env_var_os;
providers.is_unnamable = crate::queries::is_unnamable;
limits::provide(providers);
proc_macro_decls::provide(providers);
rustc_const_eval::provide(providers);
Expand Down
32 changes: 31 additions & 1 deletion compiler/rustc_interface/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use rustc_codegen_ssa::CodegenResults;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::svh::Svh;
use rustc_errors::timings::TimingSection;
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::TyCtxt;
Expand Down Expand Up @@ -107,3 +108,32 @@ impl Linker {
codegen_backend.link(sess, codegen_results, self.metadata, &self.output_filenames)
}
}

/// Checks if the given `DefId` refers to an item that is unnamable, such as one defined in a const block.
///
/// # Example
///
/// ```
/// pub fn f() {
/// // In here, `X` is not reachable outside of `f`, making it unnamable.
/// pub struct X;
/// }
/// ```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need docs here when we have the query's?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not

pub(crate) fn is_unnamable(tcx: TyCtxt<'_>, did: DefId) -> bool {
let mut cur_did = did;
while let Some(parent) = tcx.opt_parent(cur_did) {
match tcx.def_kind(parent) {
// items defined in these can be linked to, as long as they are visible
DefKind::Mod | DefKind::ForeignMod => cur_did = parent,
// items in impls can be linked to,
// as long as we can link to the item the impl is on.
// since associated traits are not yet a thing,
// it should not be possible to refer to an impl item if
// the base type is not namable.
DefKind::Impl { .. } => return false,
// everything else does not have docs generated for it
_ => return true,
}
}
return false;
}
Copy link
Contributor

@cjgillot cjgillot Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put this in compiler/rustc_interface/src/passes.rs? compiler/rustc_interface/src/queries.rs refers to a completely different kind of queries, that were eventually extinguished.

16 changes: 16 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,22 @@ rustc_queries! {
separate_provide_extern
}

/// Checks if the given `DefId` refers to an item that is unnamable, such as one defined in a const block.
///
/// Used by rustdoc.
///
/// # Example
///
/// ```
/// pub fn f() {
/// // In here, `X` is not reachable outside of `f`, making it unnamable.
/// pub struct X;
/// }
/// ```
query is_unnamable(def_id: DefId) -> bool {
desc { |tcx| "checking if `{}` is unnamable", tcx.def_path_str(def_id) }
}

query impl_parent(def_id: DefId) -> Option<DefId> {
desc { |tcx| "computing specialization parent impl of `{}`", tcx.def_path_str(def_id) }
separate_provide_extern
Expand Down
22 changes: 1 addition & 21 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,26 +481,6 @@ fn generate_item_def_id_path(
Ok((url_parts, shortty, fqp))
}

/// Checks if the given defid refers to an item that is unnamable, such as one defined in a const block.
fn is_unnamable(tcx: TyCtxt<'_>, did: DefId) -> bool {
let mut cur_did = did;
while let Some(parent) = tcx.opt_parent(cur_did) {
match tcx.def_kind(parent) {
// items defined in these can be linked to, as long as they are visible
DefKind::Mod | DefKind::ForeignMod => cur_did = parent,
// items in impls can be linked to,
// as long as we can link to the item the impl is on.
// since associated traits are not a thing,
// it should not be possible to refer to an impl item if
// the base type is not namable.
DefKind::Impl { .. } => return false,
// everything else does not have docs generated for it
_ => return true,
}
}
return false;
}

fn to_module_fqp(shortty: ItemType, fqp: &[Symbol]) -> &[Symbol] {
if shortty == ItemType::Module { fqp } else { &fqp[..fqp.len() - 1] }
}
Expand Down Expand Up @@ -574,7 +554,7 @@ pub(crate) fn href_with_root_path(
}
_ => original_did,
};
if is_unnamable(cx.tcx(), did) {
if cx.tcx().is_unnamable(did) {
return Err(HrefError::UnnamableItem);
}
let cache = cx.cache();
Expand Down
Loading