Skip to content
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

rustdoc: Resolve some more doc links early #96261

Merged
merged 1 commit into from
Apr 22, 2022
Merged
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
8 changes: 8 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,14 @@ impl CStore {
self.get_crate_data(cnum).get_all_incoherent_impls()
}

pub fn associated_item_def_ids_untracked<'a>(
&'a self,
def_id: DefId,
sess: &'a Session,
) -> impl Iterator<Item = DefId> + 'a {
self.get_crate_data(def_id.krate).get_associated_item_def_ids(def_id.index, sess)
}

pub fn may_have_doc_links_untracked(&self, def_id: DefId) -> bool {
self.get_crate_data(def_id.krate).get_may_have_doc_links(def_id.index)
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ fn main_options(options: config::Options) -> MainResult {
let resolver_caches = resolver.borrow_mut().access(|resolver| {
collect_intra_doc_links::early_resolve_intra_doc_links(
resolver,
sess,
krate,
externs,
document_private,
Expand Down
26 changes: 19 additions & 7 deletions src/librustdoc/passes/collect_intra_doc_links/early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ use rustc_hir::TraitCandidate;
use rustc_middle::ty::{DefIdTree, Visibility};
use rustc_resolve::{ParentScope, Resolver};
use rustc_session::config::Externs;
use rustc_session::Session;
use rustc_span::{Symbol, SyntaxContext};

use std::collections::hash_map::Entry;
use std::mem;

crate fn early_resolve_intra_doc_links(
resolver: &mut Resolver<'_>,
sess: &Session,
krate: &ast::Crate,
externs: Externs,
document_private_items: bool,
) -> ResolverCaches {
let mut link_resolver = EarlyDocLinkResolver {
resolver,
sess,
current_mod: CRATE_DEF_ID,
visited_mods: Default::default(),
markdown_links: Default::default(),
Expand Down Expand Up @@ -70,6 +73,7 @@ fn doc_attrs<'a>(attrs: impl Iterator<Item = &'a ast::Attribute>) -> Attributes

struct EarlyDocLinkResolver<'r, 'ra> {
resolver: &'r mut Resolver<'ra>,
sess: &'r Session,
current_mod: LocalDefId,
visited_mods: DefIdSet,
markdown_links: FxHashMap<String, Vec<PreprocessedMarkdownLink>>,
Expand Down Expand Up @@ -167,14 +171,22 @@ impl EarlyDocLinkResolver<'_, '_> {
}
}

fn resolve_doc_links_extern_impl(&mut self, def_id: DefId, _is_inherent: bool) {
// FIXME: Resolve links in associated items in addition to traits themselves,
// `force` is used to provide traits in scope for the associated items.
self.resolve_doc_links_extern_outer(def_id, def_id, true);
fn resolve_doc_links_extern_impl(&mut self, def_id: DefId, is_inherent: bool) {
self.resolve_doc_links_extern_outer(def_id, def_id);
let assoc_item_def_ids = Vec::from_iter(
self.resolver.cstore().associated_item_def_ids_untracked(def_id, self.sess),
);
for assoc_def_id in assoc_item_def_ids {
if !is_inherent
|| self.resolver.cstore().visibility_untracked(assoc_def_id) == Visibility::Public
{
self.resolve_doc_links_extern_outer(assoc_def_id, def_id);
}
}
}

fn resolve_doc_links_extern_outer(&mut self, def_id: DefId, scope_id: DefId, force: bool) {
if !force && !self.resolver.cstore().may_have_doc_links_untracked(def_id) {
fn resolve_doc_links_extern_outer(&mut self, def_id: DefId, scope_id: DefId) {
if !self.resolver.cstore().may_have_doc_links_untracked(def_id) {
return;
}
// FIXME: actually resolve links, not just add traits in scope.
Expand Down Expand Up @@ -246,7 +258,7 @@ impl EarlyDocLinkResolver<'_, '_> {
Res::Def(DefKind::Variant, ..) => self.resolver.parent(def_id).unwrap(),
_ => def_id,
};
self.resolve_doc_links_extern_outer(def_id, scope_id, false); // Outer attribute scope
self.resolve_doc_links_extern_outer(def_id, scope_id); // Outer attribute scope
if let Res::Def(DefKind::Mod, ..) = child.res {
self.resolve_doc_links_extern_inner(def_id); // Inner attribute scope
}
Expand Down