]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/formats/cache.rs
Rollup merge of #83646 - glittershark:bound-map, r=m-ou-se
[rust.git] / src / librustdoc / formats / cache.rs
1 use std::collections::BTreeMap;
2 use std::mem;
3 use std::path::Path;
4
5 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6 use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX};
7 use rustc_middle::middle::privacy::AccessLevels;
8 use rustc_middle::ty::TyCtxt;
9 use rustc_span::symbol::sym;
10
11 use crate::clean::{self, FakeDefId, GetDefId};
12 use crate::fold::DocFolder;
13 use crate::formats::item_type::ItemType;
14 use crate::formats::Impl;
15 use crate::html::markdown::short_markdown_summary;
16 use crate::html::render::cache::{get_index_search_type, ExternalLocation};
17 use crate::html::render::IndexItem;
18
19 /// This cache is used to store information about the [`clean::Crate`] being
20 /// rendered in order to provide more useful documentation. This contains
21 /// information like all implementors of a trait, all traits a type implements,
22 /// documentation for all known traits, etc.
23 ///
24 /// This structure purposefully does not implement `Clone` because it's intended
25 /// to be a fairly large and expensive structure to clone. Instead this adheres
26 /// to `Send` so it may be stored in a `Arc` instance and shared among the various
27 /// rendering threads.
28 #[derive(Default)]
29 crate struct Cache {
30     /// Maps a type ID to all known implementations for that type. This is only
31     /// recognized for intra-crate `ResolvedPath` types, and is used to print
32     /// out extra documentation on the page of an enum/struct.
33     ///
34     /// The values of the map are a list of implementations and documentation
35     /// found on that implementation.
36     crate impls: FxHashMap<DefId, Vec<Impl>>,
37
38     /// Maintains a mapping of local crate `DefId`s to the fully qualified name
39     /// and "short type description" of that node. This is used when generating
40     /// URLs when a type is being linked to. External paths are not located in
41     /// this map because the `External` type itself has all the information
42     /// necessary.
43     crate paths: FxHashMap<DefId, (Vec<String>, ItemType)>,
44
45     /// Similar to `paths`, but only holds external paths. This is only used for
46     /// generating explicit hyperlinks to other crates.
47     crate external_paths: FxHashMap<DefId, (Vec<String>, ItemType)>,
48
49     /// Maps local `DefId`s of exported types to fully qualified paths.
50     /// Unlike 'paths', this mapping ignores any renames that occur
51     /// due to 'use' statements.
52     ///
53     /// This map is used when writing out the special 'implementors'
54     /// javascript file. By using the exact path that the type
55     /// is declared with, we ensure that each path will be identical
56     /// to the path used if the corresponding type is inlined. By
57     /// doing this, we can detect duplicate impls on a trait page, and only display
58     /// the impl for the inlined type.
59     crate exact_paths: FxHashMap<DefId, Vec<String>>,
60
61     /// This map contains information about all known traits of this crate.
62     /// Implementations of a crate should inherit the documentation of the
63     /// parent trait if no extra documentation is specified, and default methods
64     /// should show up in documentation about trait implementations.
65     crate traits: FxHashMap<DefId, clean::TraitWithExtraInfo>,
66
67     /// When rendering traits, it's often useful to be able to list all
68     /// implementors of the trait, and this mapping is exactly, that: a mapping
69     /// of trait ids to the list of known implementors of the trait
70     crate implementors: FxHashMap<DefId, Vec<Impl>>,
71
72     /// Cache of where external crate documentation can be found.
73     crate extern_locations: FxHashMap<CrateNum, ExternalLocation>,
74
75     /// Cache of where documentation for primitives can be found.
76     crate primitive_locations: FxHashMap<clean::PrimitiveType, DefId>,
77
78     // Note that external items for which `doc(hidden)` applies to are shown as
79     // non-reachable while local items aren't. This is because we're reusing
80     // the access levels from the privacy check pass.
81     crate access_levels: AccessLevels<DefId>,
82
83     /// The version of the crate being documented, if given from the `--crate-version` flag.
84     crate crate_version: Option<String>,
85
86     /// Whether to document private items.
87     /// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
88     crate document_private: bool,
89
90     /// Crates marked with [`#[doc(masked)]`][doc_masked].
91     ///
92     /// [doc_masked]: https://doc.rust-lang.org/nightly/unstable-book/language-features/doc-masked.html
93     crate masked_crates: FxHashSet<CrateNum>,
94
95     // Private fields only used when initially crawling a crate to build a cache
96     stack: Vec<String>,
97     parent_stack: Vec<DefId>,
98     parent_is_trait_impl: bool,
99     stripped_mod: bool,
100
101     crate search_index: Vec<IndexItem>,
102     crate deref_trait_did: Option<DefId>,
103     crate deref_mut_trait_did: Option<DefId>,
104     crate owned_box_did: Option<DefId>,
105
106     // In rare case where a structure is defined in one module but implemented
107     // in another, if the implementing module is parsed before defining module,
108     // then the fully qualified name of the structure isn't presented in `paths`
109     // yet when its implementation methods are being indexed. Caches such methods
110     // and their parent id here and indexes them at the end of crate parsing.
111     crate orphan_impl_items: Vec<(DefId, clean::Item)>,
112
113     // Similarly to `orphan_impl_items`, sometimes trait impls are picked up
114     // even though the trait itself is not exported. This can happen if a trait
115     // was defined in function/expression scope, since the impl will be picked
116     // up by `collect-trait-impls` but the trait won't be scraped out in the HIR
117     // crawl. In order to prevent crashes when looking for notable traits or
118     // when gathering trait documentation on a type, hold impls here while
119     // folding and add them to the cache later on if we find the trait.
120     orphan_trait_impls: Vec<(DefId, FxHashSet<DefId>, Impl)>,
121
122     /// All intra-doc links resolved so far.
123     ///
124     /// Links are indexed by the DefId of the item they document.
125     crate intra_doc_links: BTreeMap<FakeDefId, Vec<clean::ItemLink>>,
126 }
127
128 /// This struct is used to wrap the `cache` and `tcx` in order to run `DocFolder`.
129 struct CacheBuilder<'a, 'tcx> {
130     cache: &'a mut Cache,
131     empty_cache: Cache,
132     tcx: TyCtxt<'tcx>,
133 }
134
135 impl Cache {
136     crate fn new(access_levels: AccessLevels<DefId>, document_private: bool) -> Self {
137         Cache { access_levels, document_private, ..Cache::default() }
138     }
139
140     /// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
141     /// in `krate` due to the data being moved into the `Cache`.
142     crate fn populate(
143         &mut self,
144         mut krate: clean::Crate,
145         tcx: TyCtxt<'_>,
146         extern_html_root_urls: &BTreeMap<String, String>,
147         dst: &Path,
148     ) -> clean::Crate {
149         // Crawl the crate to build various caches used for the output
150         debug!(?self.crate_version);
151         self.traits = krate.external_traits.take();
152
153         // Cache where all our extern crates are located
154         // FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
155         for &(n, ref e) in &krate.externs {
156             let name = e.name(tcx);
157             let extern_url = extern_html_root_urls.get(&*name.as_str()).map(|u| &**u);
158             let did = DefId { krate: n, index: CRATE_DEF_INDEX };
159             self.extern_locations.insert(n, e.location(extern_url, &dst, tcx));
160             self.external_paths.insert(did, (vec![name.to_string()], ItemType::Module));
161         }
162
163         // Cache where all known primitives have their documentation located.
164         //
165         // Favor linking to as local extern as possible, so iterate all crates in
166         // reverse topological order.
167         for &(_, ref e) in krate.externs.iter().rev() {
168             for &(def_id, prim) in &e.primitives(tcx) {
169                 self.primitive_locations.insert(prim, def_id);
170             }
171         }
172         for &(def_id, prim) in &krate.primitives {
173             self.primitive_locations.insert(prim, def_id);
174         }
175
176         krate = CacheBuilder { tcx, cache: self, empty_cache: Cache::default() }.fold_crate(krate);
177
178         for (trait_did, dids, impl_) in self.orphan_trait_impls.drain(..) {
179             if self.traits.contains_key(&trait_did) {
180                 for did in dids {
181                     self.impls.entry(did).or_default().push(impl_.clone());
182                 }
183             }
184         }
185
186         krate
187     }
188 }
189
190 impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
191     fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
192         if item.def_id.is_local() {
193             debug!("folding {} \"{:?}\", id {:?}", item.type_(), item.name, item.def_id);
194         }
195
196         // If this is a stripped module,
197         // we don't want it or its children in the search index.
198         let orig_stripped_mod = match *item.kind {
199             clean::StrippedItem(box clean::ModuleItem(..)) => {
200                 mem::replace(&mut self.cache.stripped_mod, true)
201             }
202             _ => self.cache.stripped_mod,
203         };
204
205         // If the impl is from a masked crate or references something from a
206         // masked crate then remove it completely.
207         if let clean::ImplItem(ref i) = *item.kind {
208             if self.cache.masked_crates.contains(&item.def_id.krate())
209                 || i.trait_.def_id().map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
210                 || i.for_.def_id().map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
211             {
212                 return None;
213             }
214         }
215
216         // Propagate a trait method's documentation to all implementors of the
217         // trait.
218         if let clean::TraitItem(ref t) = *item.kind {
219             self.cache.traits.entry(item.def_id.expect_real()).or_insert_with(|| {
220                 clean::TraitWithExtraInfo {
221                     trait_: t.clone(),
222                     is_notable: item.attrs.has_doc_flag(sym::notable_trait),
223                 }
224             });
225         }
226
227         // Collect all the implementors of traits.
228         if let clean::ImplItem(ref i) = *item.kind {
229             if let Some(did) = i.trait_.def_id() {
230                 if i.blanket_impl.is_none() {
231                     self.cache
232                         .implementors
233                         .entry(did.into())
234                         .or_default()
235                         .push(Impl { impl_item: item.clone() });
236                 }
237             }
238         }
239
240         // Index this method for searching later on.
241         if let Some(ref s) = item.name {
242             let (parent, is_inherent_impl_item) = match *item.kind {
243                 clean::StrippedItem(..) => ((None, None), false),
244                 clean::AssocConstItem(..) | clean::TypedefItem(_, true)
245                     if self.cache.parent_is_trait_impl =>
246                 {
247                     // skip associated items in trait impls
248                     ((None, None), false)
249                 }
250                 clean::AssocTypeItem(..)
251                 | clean::TyMethodItem(..)
252                 | clean::StructFieldItem(..)
253                 | clean::VariantItem(..) => (
254                     (
255                         Some(*self.cache.parent_stack.last().expect("parent_stack is empty")),
256                         Some(&self.cache.stack[..self.cache.stack.len() - 1]),
257                     ),
258                     false,
259                 ),
260                 clean::MethodItem(..) | clean::AssocConstItem(..) => {
261                     if self.cache.parent_stack.is_empty() {
262                         ((None, None), false)
263                     } else {
264                         let last = self.cache.parent_stack.last().expect("parent_stack is empty 2");
265                         let did = *last;
266                         let path = match self.cache.paths.get(&did) {
267                             // The current stack not necessarily has correlation
268                             // for where the type was defined. On the other
269                             // hand, `paths` always has the right
270                             // information if present.
271                             Some(&(
272                                 ref fqp,
273                                 ItemType::Trait
274                                 | ItemType::Struct
275                                 | ItemType::Union
276                                 | ItemType::Enum,
277                             )) => Some(&fqp[..fqp.len() - 1]),
278                             Some(..) => Some(&*self.cache.stack),
279                             None => None,
280                         };
281                         ((Some(*last), path), true)
282                     }
283                 }
284                 _ => ((None, Some(&*self.cache.stack)), false),
285             };
286
287             match parent {
288                 (parent, Some(path)) if is_inherent_impl_item || !self.cache.stripped_mod => {
289                     debug_assert!(!item.is_stripped());
290
291                     // A crate has a module at its root, containing all items,
292                     // which should not be indexed. The crate-item itself is
293                     // inserted later on when serializing the search-index.
294                     if item.def_id.index().map_or(false, |idx| idx != CRATE_DEF_INDEX) {
295                         self.cache.search_index.push(IndexItem {
296                             ty: item.type_(),
297                             name: s.to_string(),
298                             path: path.join("::"),
299                             desc: item
300                                 .doc_value()
301                                 .map_or_else(String::new, |x| short_markdown_summary(&x.as_str())),
302                             parent,
303                             parent_idx: None,
304                             search_type: get_index_search_type(&item, &self.empty_cache, self.tcx),
305                             aliases: item.attrs.get_doc_aliases(),
306                         });
307                     }
308                 }
309                 (Some(parent), None) if is_inherent_impl_item => {
310                     // We have a parent, but we don't know where they're
311                     // defined yet. Wait for later to index this item.
312                     self.cache.orphan_impl_items.push((parent, item.clone()));
313                 }
314                 _ => {}
315             }
316         }
317
318         // Keep track of the fully qualified path for this item.
319         let pushed = match item.name {
320             Some(n) if !n.is_empty() => {
321                 self.cache.stack.push(n.to_string());
322                 true
323             }
324             _ => false,
325         };
326
327         match *item.kind {
328             clean::StructItem(..)
329             | clean::EnumItem(..)
330             | clean::TypedefItem(..)
331             | clean::TraitItem(..)
332             | clean::TraitAliasItem(..)
333             | clean::FunctionItem(..)
334             | clean::ModuleItem(..)
335             | clean::ForeignFunctionItem(..)
336             | clean::ForeignStaticItem(..)
337             | clean::ConstantItem(..)
338             | clean::StaticItem(..)
339             | clean::UnionItem(..)
340             | clean::ForeignTypeItem
341             | clean::MacroItem(..)
342             | clean::ProcMacroItem(..)
343             | clean::VariantItem(..) => {
344                 if !self.cache.stripped_mod {
345                     // Re-exported items mean that the same id can show up twice
346                     // in the rustdoc ast that we're looking at. We know,
347                     // however, that a re-exported item doesn't show up in the
348                     // `public_items` map, so we can skip inserting into the
349                     // paths map if there was already an entry present and we're
350                     // not a public item.
351                     if !self.cache.paths.contains_key(&item.def_id.expect_real())
352                         || self.cache.access_levels.is_public(item.def_id.expect_real())
353                     {
354                         self.cache.paths.insert(
355                             item.def_id.expect_real(),
356                             (self.cache.stack.clone(), item.type_()),
357                         );
358                     }
359                 }
360             }
361             clean::PrimitiveItem(..) => {
362                 self.cache
363                     .paths
364                     .insert(item.def_id.expect_real(), (self.cache.stack.clone(), item.type_()));
365             }
366
367             clean::ExternCrateItem { .. }
368             | clean::ImportItem(..)
369             | clean::OpaqueTyItem(..)
370             | clean::ImplItem(..)
371             | clean::TyMethodItem(..)
372             | clean::MethodItem(..)
373             | clean::StructFieldItem(..)
374             | clean::AssocConstItem(..)
375             | clean::AssocTypeItem(..)
376             | clean::StrippedItem(..)
377             | clean::KeywordItem(..) => {
378                 // FIXME: Do these need handling?
379                 // The person writing this comment doesn't know.
380                 // So would rather leave them to an expert,
381                 // as at least the list is better than `_ => {}`.
382             }
383         }
384
385         // Maintain the parent stack
386         let orig_parent_is_trait_impl = self.cache.parent_is_trait_impl;
387         let parent_pushed = match *item.kind {
388             clean::TraitItem(..)
389             | clean::EnumItem(..)
390             | clean::ForeignTypeItem
391             | clean::StructItem(..)
392             | clean::UnionItem(..)
393             | clean::VariantItem(..) => {
394                 self.cache.parent_stack.push(item.def_id.expect_real());
395                 self.cache.parent_is_trait_impl = false;
396                 true
397             }
398             clean::ImplItem(ref i) => {
399                 self.cache.parent_is_trait_impl = i.trait_.is_some();
400                 match i.for_ {
401                     clean::ResolvedPath { did, .. } => {
402                         self.cache.parent_stack.push(did);
403                         true
404                     }
405                     ref t => {
406                         let prim_did = t
407                             .primitive_type()
408                             .and_then(|t| self.cache.primitive_locations.get(&t).cloned());
409                         match prim_did {
410                             Some(did) => {
411                                 self.cache.parent_stack.push(did);
412                                 true
413                             }
414                             None => false,
415                         }
416                     }
417                 }
418             }
419             _ => false,
420         };
421
422         // Once we've recursively found all the generics, hoard off all the
423         // implementations elsewhere.
424         let item = self.fold_item_recur(item);
425         let ret = if let clean::Item { kind: box clean::ImplItem(ref i), .. } = item {
426             // Figure out the id of this impl. This may map to a
427             // primitive rather than always to a struct/enum.
428             // Note: matching twice to restrict the lifetime of the `i` borrow.
429             let mut dids = FxHashSet::default();
430             match i.for_ {
431                 clean::ResolvedPath { did, .. }
432                 | clean::BorrowedRef { type_: box clean::ResolvedPath { did, .. }, .. } => {
433                     dids.insert(did);
434                 }
435                 ref t => {
436                     let did = t
437                         .primitive_type()
438                         .and_then(|t| self.cache.primitive_locations.get(&t).cloned());
439
440                     if let Some(did) = did {
441                         dids.insert(did);
442                     }
443                 }
444             }
445
446             if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
447                 for bound in generics {
448                     if let Some(did) = bound.def_id() {
449                         dids.insert(did);
450                     }
451                 }
452             }
453             let impl_item = Impl { impl_item: item };
454             if impl_item.trait_did().map_or(true, |d| self.cache.traits.contains_key(&d)) {
455                 for did in dids {
456                     self.cache.impls.entry(did).or_insert(vec![]).push(impl_item.clone());
457                 }
458             } else {
459                 let trait_did = impl_item.trait_did().expect("no trait did");
460                 self.cache.orphan_trait_impls.push((trait_did, dids, impl_item));
461             }
462             None
463         } else {
464             Some(item)
465         };
466
467         if pushed {
468             self.cache.stack.pop().expect("stack already empty");
469         }
470         if parent_pushed {
471             self.cache.parent_stack.pop().expect("parent stack already empty");
472         }
473         self.cache.stripped_mod = orig_stripped_mod;
474         self.cache.parent_is_trait_impl = orig_parent_is_trait_impl;
475         ret
476     }
477 }