1 use std
::collections
::BTreeMap
;
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
;
11 use crate::clean
::{self, GetDefId, ItemId}
;
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
;
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.
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.
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.
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
>>,
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
43 crate paths
: FxHashMap
<DefId
, (Vec
<String
>, ItemType
)>,
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
)>,
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.
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
>>,
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
>,
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
>>,
72 /// Cache of where external crate documentation can be found.
73 crate extern_locations
: FxHashMap
<CrateNum
, ExternalLocation
>,
75 /// Cache of where documentation for primitives can be found.
76 crate primitive_locations
: FxHashMap
<clean
::PrimitiveType
, DefId
>,
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
>,
83 /// The version of the crate being documented, if given from the `--crate-version` flag.
84 crate crate_version
: Option
<String
>,
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
,
90 /// Crates marked with [`#[doc(masked)]`][doc_masked].
92 /// [doc_masked]: https://doc.rust-lang.org/nightly/unstable-book/language-features/doc-masked.html
93 crate masked_crates
: FxHashSet
<CrateNum
>,
95 // Private fields only used when initially crawling a crate to build a cache
97 parent_stack
: Vec
<DefId
>,
98 parent_is_trait_impl
: bool
,
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
>,
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
)>,
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
)>,
122 /// All intra-doc links resolved so far.
124 /// Links are indexed by the DefId of the item they document.
125 crate intra_doc_links
: FxHashMap
<ItemId
, Vec
<clean
::ItemLink
>>,
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
,
135 crate fn new(access_levels
: AccessLevels
<DefId
>, document_private
: bool
) -> Self {
136 Cache { access_levels, document_private, ..Cache::default() }
139 /// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
140 /// in `krate` due to the data being moved into the `Cache`.
143 mut krate
: clean
::Crate
,
145 extern_html_root_urls
: &BTreeMap
<String
, String
>,
148 // Crawl the crate to build various caches used for the output
149 debug
!(?
self.crate_version
);
150 self.traits
= krate
.external_traits
.take();
152 // Cache where all our extern crates are located
153 // FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
154 for &e
in &krate
.externs
{
155 let name
= e
.name(tcx
);
156 let extern_url
= extern_html_root_urls
.get(&*name
.as_str()).map(|u
| &**u
);
157 self.extern_locations
.insert(e
.crate_num
, e
.location(extern_url
, &dst
, tcx
));
158 self.external_paths
.insert(e
.def_id(), (vec
![name
.to_string()], ItemType
::Module
));
161 // Cache where all known primitives have their documentation located.
163 // Favor linking to as local extern as possible, so iterate all crates in
164 // reverse topological order.
165 for &e
in krate
.externs
.iter().rev() {
166 for &(def_id
, prim
) in &e
.primitives(tcx
) {
167 self.primitive_locations
.insert(prim
, def_id
);
170 for &(def_id
, prim
) in &krate
.primitives
{
171 self.primitive_locations
.insert(prim
, def_id
);
174 krate
= CacheBuilder { tcx, cache: self }
.fold_crate(krate
);
176 for (trait_did
, dids
, impl_
) in self.orphan_trait_impls
.drain(..) {
177 if self.traits
.contains_key(&trait_did
) {
179 self.impls
.entry(did
).or_default().push(impl_
.clone());
188 impl<'a
, 'tcx
> DocFolder
for CacheBuilder
<'a
, 'tcx
> {
189 fn fold_item(&mut self, item
: clean
::Item
) -> Option
<clean
::Item
> {
190 if item
.def_id
.is_local() {
191 debug
!("folding {} \"{:?}\", id {:?}", item
.type_(), item
.name
, item
.def_id
);
194 // If this is a stripped module,
195 // we don't want it or its children in the search index.
196 let orig_stripped_mod
= match *item
.kind
{
197 clean
::StrippedItem(box clean
::ModuleItem(..)) => {
198 mem
::replace(&mut self.cache
.stripped_mod
, true)
200 _
=> self.cache
.stripped_mod
,
203 // If the impl is from a masked crate or references something from a
204 // masked crate then remove it completely.
205 if let clean
::ImplItem(ref i
) = *item
.kind
{
206 if self.cache
.masked_crates
.contains(&item
.def_id
.krate())
207 || i
.trait_
.def_id().map_or(false, |d
| self.cache
.masked_crates
.contains(&d
.krate
))
208 || i
.for_
.def_id().map_or(false, |d
| self.cache
.masked_crates
.contains(&d
.krate
))
214 // Propagate a trait method's documentation to all implementors of the
216 if let clean
::TraitItem(ref t
) = *item
.kind
{
217 self.cache
.traits
.entry(item
.def_id
.expect_def_id()).or_insert_with(|| {
218 clean
::TraitWithExtraInfo
{
220 is_notable
: item
.attrs
.has_doc_flag(sym
::notable_trait
),
225 // Collect all the implementors of traits.
226 if let clean
::ImplItem(ref i
) = *item
.kind
{
227 if let Some(did
) = i
.trait_
.def_id() {
228 if i
.blanket_impl
.is_none() {
233 .push(Impl { impl_item: item.clone() }
);
238 // Index this method for searching later on.
239 if let Some(ref s
) = item
.name
{
240 let (parent
, is_inherent_impl_item
) = match *item
.kind
{
241 clean
::StrippedItem(..) => ((None
, None
), false),
242 clean
::AssocConstItem(..) | clean
::TypedefItem(_
, true)
243 if self.cache
.parent_is_trait_impl
=>
245 // skip associated items in trait impls
246 ((None
, None
), false)
248 clean
::AssocTypeItem(..)
249 | clean
::TyMethodItem(..)
250 | clean
::StructFieldItem(..)
251 | clean
::VariantItem(..) => (
253 Some(*self.cache
.parent_stack
.last().expect("parent_stack is empty")),
254 Some(&self.cache
.stack
[..self.cache
.stack
.len() - 1]),
258 clean
::MethodItem(..) | clean
::AssocConstItem(..) => {
259 if self.cache
.parent_stack
.is_empty() {
260 ((None
, None
), false)
262 let last
= self.cache
.parent_stack
.last().expect("parent_stack is empty 2");
264 let path
= match self.cache
.paths
.get(&did
) {
265 // The current stack not necessarily has correlation
266 // for where the type was defined. On the other
267 // hand, `paths` always has the right
268 // information if present.
275 )) => Some(&fqp
[..fqp
.len() - 1]),
276 Some(..) => Some(&*self.cache
.stack
),
279 ((Some(*last
), path
), true)
282 _
=> ((None
, Some(&*self.cache
.stack
)), false),
286 (parent
, Some(path
)) if is_inherent_impl_item
|| !self.cache
.stripped_mod
=> {
287 debug_assert
!(!item
.is_stripped());
289 // A crate has a module at its root, containing all items,
290 // which should not be indexed. The crate-item itself is
291 // inserted later on when serializing the search-index.
292 if item
.def_id
.index().map_or(false, |idx
| idx
!= CRATE_DEF_INDEX
) {
293 let desc
= item
.doc_value().map_or_else(String
::new
, |x
| {
294 short_markdown_summary(&x
.as_str(), &item
.link_names(&self.cache
))
296 self.cache
.search_index
.push(IndexItem
{
299 path
: path
.join("::"),
303 search_type
: get_index_search_type(&item
, self.tcx
),
304 aliases
: item
.attrs
.get_doc_aliases(),
308 (Some(parent
), None
) if is_inherent_impl_item
=> {
309 // We have a parent, but we don't know where they're
310 // defined yet. Wait for later to index this item.
311 self.cache
.orphan_impl_items
.push((parent
, item
.clone()));
317 // Keep track of the fully qualified path for this item.
318 let pushed
= match item
.name
{
319 Some(n
) if !n
.is_empty() => {
320 self.cache
.stack
.push(n
.to_string());
327 clean
::StructItem(..)
328 | clean
::EnumItem(..)
329 | clean
::TypedefItem(..)
330 | clean
::TraitItem(..)
331 | clean
::TraitAliasItem(..)
332 | clean
::FunctionItem(..)
333 | clean
::ModuleItem(..)
334 | clean
::ForeignFunctionItem(..)
335 | clean
::ForeignStaticItem(..)
336 | clean
::ConstantItem(..)
337 | clean
::StaticItem(..)
338 | clean
::UnionItem(..)
339 | clean
::ForeignTypeItem
340 | clean
::MacroItem(..)
341 | clean
::ProcMacroItem(..)
342 | clean
::VariantItem(..) => {
343 if !self.cache
.stripped_mod
{
344 // Re-exported items mean that the same id can show up twice
345 // in the rustdoc ast that we're looking at. We know,
346 // however, that a re-exported item doesn't show up in the
347 // `public_items` map, so we can skip inserting into the
348 // paths map if there was already an entry present and we're
349 // not a public item.
350 if !self.cache
.paths
.contains_key(&item
.def_id
.expect_def_id())
351 || self.cache
.access_levels
.is_public(item
.def_id
.expect_def_id())
353 self.cache
.paths
.insert(
354 item
.def_id
.expect_def_id(),
355 (self.cache
.stack
.clone(), item
.type_()),
360 clean
::PrimitiveItem(..) => {
363 .insert(item
.def_id
.expect_def_id(), (self.cache
.stack
.clone(), item
.type_()));
366 clean
::ExternCrateItem { .. }
367 | clean
::ImportItem(..)
368 | clean
::OpaqueTyItem(..)
369 | clean
::ImplItem(..)
370 | clean
::TyMethodItem(..)
371 | clean
::MethodItem(..)
372 | clean
::StructFieldItem(..)
373 | clean
::AssocConstItem(..)
374 | clean
::AssocTypeItem(..)
375 | clean
::StrippedItem(..)
376 | clean
::KeywordItem(..) => {
377 // FIXME: Do these need handling?
378 // The person writing this comment doesn't know.
379 // So would rather leave them to an expert,
380 // as at least the list is better than `_ => {}`.
384 // Maintain the parent stack
385 let orig_parent_is_trait_impl
= self.cache
.parent_is_trait_impl
;
386 let parent_pushed
= match *item
.kind
{
388 | clean
::EnumItem(..)
389 | clean
::ForeignTypeItem
390 | clean
::StructItem(..)
391 | clean
::UnionItem(..)
392 | clean
::VariantItem(..) => {
393 self.cache
.parent_stack
.push(item
.def_id
.expect_def_id());
394 self.cache
.parent_is_trait_impl
= false;
397 clean
::ImplItem(ref i
) => {
398 self.cache
.parent_is_trait_impl
= i
.trait_
.is_some();
400 clean
::ResolvedPath { did, .. }
=> {
401 self.cache
.parent_stack
.push(did
);
404 clean
::DynTrait(ref bounds
, _
)
405 | clean
::BorrowedRef { type_: box clean::DynTrait(ref bounds, _), .. }
=> {
406 if let Some(did
) = bounds
[0].trait_
.def_id() {
407 self.cache
.parent_stack
.push(did
);
416 .and_then(|t
| self.cache
.primitive_locations
.get(&t
).cloned());
419 self.cache
.parent_stack
.push(did
);
430 // Once we've recursively found all the generics, hoard off all the
431 // implementations elsewhere.
432 let item
= self.fold_item_recur(item
);
433 let ret
= if let clean
::Item { kind: box clean::ImplItem(ref i), .. }
= item
{
434 // Figure out the id of this impl. This may map to a
435 // primitive rather than always to a struct/enum.
436 // Note: matching twice to restrict the lifetime of the `i` borrow.
437 let mut dids
= FxHashSet
::default();
439 clean
::ResolvedPath { did, .. }
440 | clean
::BorrowedRef { type_: box clean::ResolvedPath { did, .. }
, .. } => {
443 clean
::DynTrait(ref bounds
, _
)
444 | clean
::BorrowedRef { type_: box clean::DynTrait(ref bounds, _), .. }
=> {
445 if let Some(did
) = bounds
[0].trait_
.def_id() {
452 .and_then(|t
| self.cache
.primitive_locations
.get(&t
).cloned());
454 if let Some(did
) = did
{
460 if let Some(generics
) = i
.trait_
.as_ref().and_then(|t
| t
.generics()) {
461 for bound
in generics
{
462 if let Some(did
) = bound
.def_id() {
467 let impl_item
= Impl { impl_item: item }
;
468 if impl_item
.trait_did().map_or(true, |d
| self.cache
.traits
.contains_key(&d
)) {
470 self.cache
.impls
.entry(did
).or_insert(vec
![]).push(impl_item
.clone());
473 let trait_did
= impl_item
.trait_did().expect("no trait did");
474 self.cache
.orphan_trait_impls
.push((trait_did
, dids
, impl_item
));
482 self.cache
.stack
.pop().expect("stack already empty");
485 self.cache
.parent_stack
.pop().expect("parent stack already empty");
487 self.cache
.stripped_mod
= orig_stripped_mod
;
488 self.cache
.parent_is_trait_impl
= orig_parent_is_trait_impl
;