2 use crate::core::ResolverCaches;
3 use crate::html::markdown::markdown_links;
4 use crate::passes::collect_intra_doc_links::preprocess_link;
6 use rustc_ast::visit::{self, AssocCtxt, Visitor};
7 use rustc_ast::{self as ast, ItemKind};
8 use rustc_ast_lowering::ResolverAstLowering;
9 use rustc_hir::def::Namespace::TypeNS;
10 use rustc_hir::def::{DefKind, Res};
11 use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId, CRATE_DEF_ID};
12 use rustc_hir::TraitCandidate;
13 use rustc_middle::ty::{DefIdTree, Visibility};
14 use rustc_resolve::{ParentScope, Resolver};
15 use rustc_session::config::Externs;
16 use rustc_span::SyntaxContext;
18 use std::collections::hash_map::Entry;
21 crate fn early_resolve_intra_doc_links(
22 resolver: &mut Resolver<'_>,
25 document_private_items: bool,
27 let mut loader = IntraLinkCrateLoader {
29 current_mod: CRATE_DEF_ID,
30 visited_mods: Default::default(),
31 traits_in_scope: Default::default(),
32 all_traits: Default::default(),
33 all_trait_impls: Default::default(),
34 document_private_items,
37 // Overridden `visit_item` below doesn't apply to the crate root,
38 // so we have to visit its attributes and reexports separately.
39 loader.load_links_in_attrs(&krate.attrs);
40 loader.process_module_children_or_reexports(CRATE_DEF_ID.to_def_id());
41 visit::walk_crate(&mut loader, krate);
42 loader.add_foreign_traits_in_scope();
44 // FIXME: somehow rustdoc is still missing crates even though we loaded all
45 // the known necessary crates. Load them all unconditionally until we find a way to fix this.
46 // DO NOT REMOVE THIS without first testing on the reproducer in
47 // https://github.com/jyn514/objr/commit/edcee7b8124abf0e4c63873e8422ff81beb11ebb
48 for (extern_name, _) in externs.iter().filter(|(_, entry)| entry.add_prelude) {
49 loader.resolver.resolve_rustdoc_path(extern_name, TypeNS, CRATE_DEF_ID.to_def_id());
53 traits_in_scope: loader.traits_in_scope,
54 all_traits: Some(loader.all_traits),
55 all_trait_impls: Some(loader.all_trait_impls),
59 struct IntraLinkCrateLoader<'r, 'ra> {
60 resolver: &'r mut Resolver<'ra>,
61 current_mod: LocalDefId,
62 visited_mods: DefIdSet,
63 traits_in_scope: DefIdMap<Vec<TraitCandidate>>,
64 all_traits: Vec<DefId>,
65 all_trait_impls: Vec<DefId>,
66 document_private_items: bool,
69 impl IntraLinkCrateLoader<'_, '_> {
70 fn add_traits_in_scope(&mut self, def_id: DefId) {
71 // Calls to `traits_in_scope` are expensive, so try to avoid them if only possible.
72 // Keys in the `traits_in_scope` cache are always module IDs.
73 if let Entry::Vacant(entry) = self.traits_in_scope.entry(def_id) {
74 let module = self.resolver.get_nearest_non_block_module(def_id);
75 let module_id = module.def_id();
76 let entry = if module_id == def_id {
78 } else if let Entry::Vacant(entry) = self.traits_in_scope.entry(module_id) {
83 if let Some(entry) = entry {
84 entry.insert(self.resolver.traits_in_scope(
86 &ParentScope::module(module, self.resolver),
87 SyntaxContext::root(),
94 fn add_traits_in_parent_scope(&mut self, def_id: DefId) {
95 if let Some(module_id) = self.resolver.parent(def_id) {
96 self.add_traits_in_scope(module_id);
100 /// Add traits in scope for links in impls collected by the `collect-intra-doc-links` pass.
101 /// That pass filters impls using type-based information, but we don't yet have such
102 /// information here, so we just conservatively calculate traits in scope for *all* modules
103 /// having impls in them.
104 fn add_foreign_traits_in_scope(&mut self) {
105 for cnum in Vec::from_iter(self.resolver.cstore().crates_untracked()) {
106 let all_traits = Vec::from_iter(self.resolver.cstore().traits_in_crate_untracked(cnum));
107 let all_trait_impls =
108 Vec::from_iter(self.resolver.cstore().trait_impls_in_crate_untracked(cnum));
109 let all_inherent_impls =
110 Vec::from_iter(self.resolver.cstore().inherent_impls_in_crate_untracked(cnum));
111 let all_incoherent_impls =
112 Vec::from_iter(self.resolver.cstore().incoherent_impls_in_crate_untracked(cnum));
114 // Querying traits in scope is expensive so we try to prune the impl and traits lists
115 // using privacy, private traits and impls from other crates are never documented in
116 // the current crate, and links in their doc comments are not resolved.
117 for &def_id in &all_traits {
118 if self.resolver.cstore().visibility_untracked(def_id) == Visibility::Public {
119 self.add_traits_in_parent_scope(def_id);
122 for &(trait_def_id, impl_def_id, simplified_self_ty) in &all_trait_impls {
123 if self.resolver.cstore().visibility_untracked(trait_def_id) == Visibility::Public
124 && simplified_self_ty.and_then(|ty| ty.def()).map_or(true, |ty_def_id| {
125 self.resolver.cstore().visibility_untracked(ty_def_id) == Visibility::Public
128 self.add_traits_in_parent_scope(impl_def_id);
131 for (ty_def_id, impl_def_id) in all_inherent_impls {
132 if self.resolver.cstore().visibility_untracked(ty_def_id) == Visibility::Public {
133 self.add_traits_in_parent_scope(impl_def_id);
136 for def_id in all_incoherent_impls {
137 self.add_traits_in_parent_scope(def_id);
140 self.all_traits.extend(all_traits);
141 self.all_trait_impls.extend(all_trait_impls.into_iter().map(|(_, def_id, _)| def_id));
145 fn load_links_in_attrs(&mut self, attrs: &[ast::Attribute]) {
146 // FIXME: this needs to consider reexport inlining.
147 let attrs = clean::Attributes::from_ast(attrs, None);
148 for (parent_module, doc) in attrs.collapsed_doc_value_by_module_level() {
149 let module_id = parent_module.unwrap_or(self.current_mod.to_def_id());
151 self.add_traits_in_scope(module_id);
153 for link in markdown_links(&doc.as_str()) {
154 let path_str = if let Some(Ok(x)) = preprocess_link(&link) {
159 self.resolver.resolve_rustdoc_path(&path_str, TypeNS, module_id);
164 /// When reexports are inlined, they are replaced with item which they refer to, those items
165 /// may have links in their doc comments, those links are resolved at the item definition site,
166 /// so we need to know traits in scope at that definition site.
167 fn process_module_children_or_reexports(&mut self, module_id: DefId) {
168 if !self.visited_mods.insert(module_id) {
169 return; // avoid infinite recursion
172 for child in self.resolver.module_children_or_reexports(module_id) {
173 if child.vis == Visibility::Public || self.document_private_items {
174 if let Some(def_id) = child.res.opt_def_id() {
175 self.add_traits_in_parent_scope(def_id);
177 if let Res::Def(DefKind::Mod, module_id) = child.res {
178 self.process_module_children_or_reexports(module_id);
185 impl Visitor<'_> for IntraLinkCrateLoader<'_, '_> {
186 fn visit_item(&mut self, item: &ast::Item) {
187 if let ItemKind::Mod(..) = item.kind {
188 let old_mod = mem::replace(&mut self.current_mod, self.resolver.local_def_id(item.id));
190 // A module written with a outline doc comments will resolve traits relative
191 // to the parent module. Make sure the parent module's traits-in-scope are
192 // loaded, even if the module itself has no doc comments.
193 self.add_traits_in_parent_scope(self.current_mod.to_def_id());
195 self.load_links_in_attrs(&item.attrs);
196 self.process_module_children_or_reexports(self.current_mod.to_def_id());
197 visit::walk_item(self, item);
199 self.current_mod = old_mod;
202 ItemKind::Trait(..) => {
203 self.all_traits.push(self.resolver.local_def_id(item.id).to_def_id());
205 ItemKind::Impl(box ast::Impl { of_trait: Some(..), .. }) => {
206 self.all_trait_impls.push(self.resolver.local_def_id(item.id).to_def_id());
210 self.load_links_in_attrs(&item.attrs);
211 visit::walk_item(self, item);
215 fn visit_assoc_item(&mut self, item: &ast::AssocItem, ctxt: AssocCtxt) {
216 self.load_links_in_attrs(&item.attrs);
217 visit::walk_assoc_item(self, item, ctxt)
220 fn visit_foreign_item(&mut self, item: &ast::ForeignItem) {
221 self.load_links_in_attrs(&item.attrs);
222 visit::walk_foreign_item(self, item)
225 fn visit_variant(&mut self, v: &ast::Variant) {
226 self.load_links_in_attrs(&v.attrs);
227 visit::walk_variant(self, v)
230 fn visit_field_def(&mut self, field: &ast::FieldDef) {
231 self.load_links_in_attrs(&field.attrs);
232 visit::walk_field_def(self, field)
235 // NOTE: if doc-comments are ever allowed on other nodes (e.g. function parameters),
236 // then this will have to implement other visitor methods too.