rustdoc/formats/cache.rs
1use std::mem;
2
3use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
4use rustc_hir::StabilityLevel;
5use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet};
6use rustc_metadata::creader::CStore;
7use rustc_middle::ty::{self, TyCtxt};
8use rustc_span::Symbol;
9use tracing::debug;
10
11use crate::clean::types::ExternalLocation;
12use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
13use crate::config::RenderOptions;
14use crate::core::DocContext;
15use crate::fold::DocFolder;
16use crate::formats::Impl;
17use crate::formats::item_type::ItemType;
18use crate::html::render::{IndexItem, IndexItemInfo};
19use crate::visit_lib::RustdocEffectiveVisibilities;
20
21/// This cache is used to store information about the [`clean::Crate`] being
22/// rendered in order to provide more useful documentation. This contains
23/// information like all implementors of a trait, all traits a type implements,
24/// documentation for all known traits, etc.
25///
26/// This structure purposefully does not implement `Clone` because it's intended
27/// to be a fairly large and expensive structure to clone. Instead this adheres
28/// to `Send` so it may be stored in an `Arc` instance and shared among the various
29/// rendering threads.
30#[derive(Default)]
31pub(crate) struct Cache {
32 /// Maps a type ID to all known implementations for that type. This is only
33 /// recognized for intra-crate [`clean::Type::Path`]s, and is used to print
34 /// out extra documentation on the page of an enum/struct.
35 ///
36 /// The values of the map are a list of implementations and documentation
37 /// found on that implementation.
38 pub(crate) impls: DefIdMap<Vec<Impl>>,
39
40 /// Maintains a mapping of local crate `DefId`s to the fully qualified name
41 /// and "short type description" of that node. This is used when generating
42 /// URLs when a type is being linked to. External paths are not located in
43 /// this map because the `External` type itself has all the information
44 /// necessary.
45 pub(crate) paths: FxIndexMap<DefId, (Vec<Symbol>, ItemType)>,
46
47 /// Similar to `paths`, but only holds external paths. This is only used for
48 /// generating explicit hyperlinks to other crates.
49 pub(crate) external_paths: FxIndexMap<DefId, (Vec<Symbol>, ItemType)>,
50
51 /// Maps local `DefId`s of exported types to fully qualified paths.
52 /// Unlike 'paths', this mapping ignores any renames that occur
53 /// due to 'use' statements.
54 ///
55 /// This map is used when writing out the `impl.trait` and `impl.type`
56 /// javascript files. By using the exact path that the type
57 /// is declared with, we ensure that each path will be identical
58 /// to the path used if the corresponding type is inlined. By
59 /// doing this, we can detect duplicate impls on a trait page, and only display
60 /// the impl for the inlined type.
61 pub(crate) exact_paths: DefIdMap<Vec<Symbol>>,
62
63 /// This map contains information about all known traits of this crate.
64 /// Implementations of a crate should inherit the documentation of the
65 /// parent trait if no extra documentation is specified, and default methods
66 /// should show up in documentation about trait implementations.
67 pub(crate) traits: FxIndexMap<DefId, clean::Trait>,
68
69 /// When rendering traits, it's often useful to be able to list all
70 /// implementors of the trait, and this mapping is exactly, that: a mapping
71 /// of trait ids to the list of known implementors of the trait
72 pub(crate) implementors: FxIndexMap<DefId, Vec<Impl>>,
73
74 /// Cache of where external crate documentation can be found.
75 pub(crate) extern_locations: FxIndexMap<CrateNum, ExternalLocation>,
76
77 /// Cache of where documentation for primitives can be found.
78 pub(crate) primitive_locations: FxIndexMap<clean::PrimitiveType, DefId>,
79
80 // Note that external items for which `doc(hidden)` applies to are shown as
81 // non-reachable while local items aren't. This is because we're reusing
82 // the effective visibilities from the privacy check pass.
83 pub(crate) effective_visibilities: RustdocEffectiveVisibilities,
84
85 /// The version of the crate being documented, if given from the `--crate-version` flag.
86 pub(crate) crate_version: Option<String>,
87
88 /// Whether to document private items.
89 /// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
90 pub(crate) document_private: bool,
91 /// Whether to document hidden items.
92 /// This is stored in `Cache` so it doesn't need to be passed through all rustdoc functions.
93 pub(crate) document_hidden: bool,
94
95 /// Crates marked with [`#[doc(masked)]`][doc_masked].
96 ///
97 /// [doc_masked]: https://doc.rust-lang.org/nightly/unstable-book/language-features/doc-masked.html
98 pub(crate) masked_crates: FxHashSet<CrateNum>,
99
100 // Private fields only used when initially crawling a crate to build a cache
101 stack: Vec<Symbol>,
102 parent_stack: Vec<ParentStackItem>,
103 stripped_mod: bool,
104
105 pub(crate) search_index: Vec<IndexItem>,
106
107 // In rare case where a structure is defined in one module but implemented
108 // in another, if the implementing module is parsed before defining module,
109 // then the fully qualified name of the structure isn't presented in `paths`
110 // yet when its implementation methods are being indexed. Caches such methods
111 // and their parent id here and indexes them at the end of crate parsing.
112 pub(crate) orphan_impl_items: Vec<OrphanImplItem>,
113
114 // Similarly to `orphan_impl_items`, sometimes trait impls are picked up
115 // even though the trait itself is not exported. This can happen if a trait
116 // was defined in function/expression scope, since the impl will be picked
117 // up by `collect-trait-impls` but the trait won't be scraped out in the HIR
118 // crawl. In order to prevent crashes when looking for notable traits or
119 // when gathering trait documentation on a type, hold impls here while
120 // folding and add them to the cache later on if we find the trait.
121 orphan_trait_impls: Vec<(DefId, FxIndexSet<DefId>, Impl)>,
122
123 /// All intra-doc links resolved so far.
124 ///
125 /// Links are indexed by the DefId of the item they document.
126 pub(crate) intra_doc_links: FxHashMap<ItemId, FxIndexSet<clean::ItemLink>>,
127
128 /// Contains the list of `DefId`s which have been inlined. It is used when generating files
129 /// to check if a stripped item should get its file generated or not: if it's inside a
130 /// `#[doc(hidden)]` item or a private one and not inlined, it shouldn't get a file.
131 pub(crate) inlined_items: DefIdSet,
132}
133
134/// This struct is used to wrap the `cache` and `tcx` in order to run `DocFolder`.
135struct CacheBuilder<'a, 'tcx> {
136 cache: &'a mut Cache,
137 /// This field is used to prevent duplicated impl blocks.
138 impl_ids: DefIdMap<DefIdSet>,
139 tcx: TyCtxt<'tcx>,
140 is_json_output: bool,
141}
142
143impl Cache {
144 pub(crate) fn new(document_private: bool, document_hidden: bool) -> Self {
145 Cache { document_private, document_hidden, ..Cache::default() }
146 }
147
148 fn parent_stack_last_impl_and_trait_id(&self) -> (Option<DefId>, Option<DefId>) {
149 if let Some(ParentStackItem::Impl { item_id, trait_, .. }) = self.parent_stack.last() {
150 (item_id.as_def_id(), trait_.as_ref().map(|tr| tr.def_id()))
151 } else {
152 (None, None)
153 }
154 }
155
156 /// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
157 /// in `krate` due to the data being moved into the `Cache`.
158 pub(crate) fn populate(
159 cx: &mut DocContext<'_>,
160 mut krate: clean::Crate,
161 render_options: &RenderOptions,
162 ) -> clean::Crate {
163 let tcx = cx.tcx;
164
165 // Crawl the crate to build various caches used for the output
166 debug!(?cx.cache.crate_version);
167 assert!(cx.external_traits.is_empty());
168 cx.cache.traits = mem::take(&mut krate.external_traits);
169
170 let extern_url_takes_precedence = render_options.extern_html_root_takes_precedence;
171 let dst = &render_options.output;
172
173 // Make `--extern-html-root-url` support the same names as `--extern` whenever possible
174 let cstore = CStore::from_tcx(tcx);
175 for (name, extern_url) in &render_options.extern_html_root_urls {
176 if let Some(crate_num) = cstore.resolved_extern_crate(Symbol::intern(name)) {
177 let e = ExternalCrate { crate_num };
178 let location = e.location(Some(extern_url), extern_url_takes_precedence, dst, tcx);
179 cx.cache.extern_locations.insert(e.crate_num, location);
180 }
181 }
182
183 // Cache where all our extern crates are located
184 // This is also used in the JSON output.
185 for &crate_num in tcx.crates(()) {
186 let e = ExternalCrate { crate_num };
187
188 let name = e.name(tcx);
189 cx.cache.extern_locations.entry(e.crate_num).or_insert_with(|| {
190 // falls back to matching by crates' own names, because
191 // transitive dependencies and injected crates may be loaded without `--extern`
192 let extern_url =
193 render_options.extern_html_root_urls.get(name.as_str()).map(|u| &**u);
194 e.location(extern_url, extern_url_takes_precedence, dst, tcx)
195 });
196 cx.cache.external_paths.insert(e.def_id(), (vec![name], ItemType::Module));
197 }
198
199 // FIXME: avoid this clone (requires implementing Default manually)
200 cx.cache.primitive_locations = PrimitiveType::primitive_locations(tcx).clone();
201 for (prim, &def_id) in &cx.cache.primitive_locations {
202 let crate_name = tcx.crate_name(def_id.krate);
203 // Recall that we only allow primitive modules to be at the root-level of the crate.
204 // If that restriction is ever lifted, this will have to include the relative paths instead.
205 cx.cache
206 .external_paths
207 .insert(def_id, (vec![crate_name, prim.as_sym()], ItemType::Primitive));
208 }
209
210 let (krate, mut impl_ids) = {
211 let is_json_output = cx.is_json_output();
212 let mut cache_builder = CacheBuilder {
213 tcx,
214 cache: &mut cx.cache,
215 impl_ids: Default::default(),
216 is_json_output,
217 };
218 krate = cache_builder.fold_crate(krate);
219 (krate, cache_builder.impl_ids)
220 };
221
222 for (trait_did, dids, impl_) in cx.cache.orphan_trait_impls.drain(..) {
223 if cx.cache.traits.contains_key(&trait_did) {
224 for did in dids {
225 if impl_ids.entry(did).or_default().insert(impl_.def_id()) {
226 cx.cache.impls.entry(did).or_default().push(impl_.clone());
227 }
228 }
229 }
230 }
231
232 krate
233 }
234}
235
236impl DocFolder for CacheBuilder<'_, '_> {
237 fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
238 if item.item_id.is_local() {
239 debug!(
240 "folding {} (stripped: {:?}) \"{:?}\", id {:?}",
241 item.type_(),
242 item.is_stripped(),
243 item.name,
244 item.item_id
245 );
246 }
247
248 // If this is a stripped module,
249 // we don't want it or its children in the search index.
250 let orig_stripped_mod = match item.kind {
251 clean::StrippedItem(clean::ModuleItem(..)) => {
252 mem::replace(&mut self.cache.stripped_mod, true)
253 }
254 _ => self.cache.stripped_mod,
255 };
256
257 #[inline]
258 fn is_from_private_dep(tcx: TyCtxt<'_>, cache: &Cache, def_id: DefId) -> bool {
259 let krate = def_id.krate;
260
261 cache.masked_crates.contains(&krate) || tcx.is_private_dep(krate)
262 }
263
264 // If the impl is from a masked crate or references something from a
265 // masked crate then remove it completely.
266 if let clean::ImplItem(ref i) = item.kind
267 && (self.cache.masked_crates.contains(&item.item_id.krate())
268 || i.trait_
269 .as_ref()
270 .is_some_and(|t| is_from_private_dep(self.tcx, self.cache, t.def_id()))
271 || i.for_
272 .def_id(self.cache)
273 .is_some_and(|d| is_from_private_dep(self.tcx, self.cache, d)))
274 {
275 return None;
276 }
277
278 // Propagate a trait method's documentation to all implementors of the
279 // trait.
280 if let clean::TraitItem(ref t) = item.kind {
281 self.cache.traits.entry(item.item_id.expect_def_id()).or_insert_with(|| (**t).clone());
282 } else if let clean::ImplItem(ref i) = item.kind
283 && let Some(trait_) = &i.trait_
284 && !i.kind.is_blanket()
285 {
286 // Collect all the implementors of traits.
287 self.cache
288 .implementors
289 .entry(trait_.def_id())
290 .or_default()
291 .push(Impl { impl_item: item.clone() });
292 }
293
294 // Index this method for searching later on.
295 let search_name = if !item.is_stripped() {
296 item.name.or_else(|| {
297 if let clean::ImportItem(ref i) = item.kind
298 && let clean::ImportKind::Simple(s) = i.kind
299 {
300 Some(s)
301 } else {
302 None
303 }
304 })
305 } else {
306 None
307 };
308 if let Some(name) = search_name {
309 add_item_to_search_index(self.tcx, self.cache, &item, name)
310 }
311
312 // Keep track of the fully qualified path for this item.
313 let pushed = match item.name {
314 Some(n) => {
315 self.cache.stack.push(n);
316 true
317 }
318 _ => false,
319 };
320
321 match item.kind {
322 clean::StructItem(..)
323 | clean::EnumItem(..)
324 | clean::TypeAliasItem(..)
325 | clean::TraitItem(..)
326 | clean::TraitAliasItem(..)
327 | clean::FunctionItem(..)
328 | clean::ModuleItem(..)
329 | clean::ForeignFunctionItem(..)
330 | clean::ForeignStaticItem(..)
331 | clean::ConstantItem(..)
332 | clean::StaticItem(..)
333 | clean::UnionItem(..)
334 | clean::ForeignTypeItem
335 | clean::MacroItem(..)
336 | clean::ProcMacroItem(..)
337 | clean::VariantItem(..) => {
338 use rustc_data_structures::fx::IndexEntry as Entry;
339
340 let skip_because_unstable = matches!(
341 item.stability.map(|stab| stab.level),
342 Some(StabilityLevel::Stable { allowed_through_unstable_modules: Some(_), .. })
343 );
344
345 if (!self.cache.stripped_mod && !skip_because_unstable) || self.is_json_output {
346 // Re-exported items mean that the same id can show up twice
347 // in the rustdoc ast that we're looking at. We know,
348 // however, that a re-exported item doesn't show up in the
349 // `public_items` map, so we can skip inserting into the
350 // paths map if there was already an entry present and we're
351 // not a public item.
352 let item_def_id = item.item_id.expect_def_id();
353 match self.cache.paths.entry(item_def_id) {
354 Entry::Vacant(entry) => {
355 entry.insert((self.cache.stack.clone(), item.type_()));
356 }
357 Entry::Occupied(mut entry) => {
358 if entry.get().0.len() > self.cache.stack.len() {
359 entry.insert((self.cache.stack.clone(), item.type_()));
360 }
361 }
362 }
363 }
364 }
365 clean::PrimitiveItem(..) => {
366 self.cache
367 .paths
368 .insert(item.item_id.expect_def_id(), (self.cache.stack.clone(), item.type_()));
369 }
370
371 clean::ExternCrateItem { .. }
372 | clean::ImportItem(..)
373 | clean::ImplItem(..)
374 | clean::RequiredMethodItem(..)
375 | clean::MethodItem(..)
376 | clean::StructFieldItem(..)
377 | clean::RequiredAssocConstItem(..)
378 | clean::ProvidedAssocConstItem(..)
379 | clean::ImplAssocConstItem(..)
380 | clean::RequiredAssocTypeItem(..)
381 | clean::AssocTypeItem(..)
382 | clean::StrippedItem(..)
383 | clean::KeywordItem
384 | clean::AttributeItem => {
385 // FIXME: Do these need handling?
386 // The person writing this comment doesn't know.
387 // So would rather leave them to an expert,
388 // as at least the list is better than `_ => {}`.
389 }
390
391 clean::PlaceholderImplItem => return None,
392 }
393
394 // Maintain the parent stack.
395 let (item, parent_pushed) = match item.kind {
396 clean::TraitItem(..)
397 | clean::EnumItem(..)
398 | clean::ForeignTypeItem
399 | clean::StructItem(..)
400 | clean::UnionItem(..)
401 | clean::VariantItem(..)
402 | clean::TypeAliasItem(..)
403 | clean::ImplItem(..) => {
404 self.cache.parent_stack.push(ParentStackItem::new(&item));
405 (self.fold_item_recur(item), true)
406 }
407 _ => (self.fold_item_recur(item), false),
408 };
409
410 // Once we've recursively found all the generics, hoard off all the
411 // implementations elsewhere.
412 let ret =
413 if let clean::Item { inner: clean::ItemInner { kind: clean::ImplItem(ref i), .. } } =
414 item
415 {
416 // Figure out the id of this impl. This may map to a
417 // primitive rather than always to a struct/enum.
418 // Note: matching twice to restrict the lifetime of the `i` borrow.
419 let mut dids = FxIndexSet::default();
420 match i.for_ {
421 clean::Type::Path { ref path }
422 | clean::BorrowedRef { type_: clean::Type::Path { ref path }, .. } => {
423 dids.insert(path.def_id());
424 if let Some(generics) = path.generics()
425 && let ty::Adt(adt, _) = self
426 .tcx
427 .type_of(path.def_id())
428 .instantiate_identity()
429 .skip_norm_wip()
430 .kind()
431 && adt.is_fundamental()
432 {
433 for ty in generics {
434 dids.extend(ty.def_id(self.cache));
435 }
436 }
437 }
438 clean::DynTrait(ref bounds, _)
439 | clean::BorrowedRef { type_: clean::DynTrait(ref bounds, _), .. } => {
440 dids.insert(bounds[0].trait_.def_id());
441 }
442 ref t => {
443 let did = t
444 .primitive_type()
445 .and_then(|t| self.cache.primitive_locations.get(&t).cloned());
446
447 dids.extend(did);
448 }
449 }
450
451 if let Some(trait_) = &i.trait_
452 && let Some(generics) = trait_.generics()
453 {
454 for bound in generics {
455 dids.extend(bound.def_id(self.cache));
456 }
457 }
458 let impl_item = Impl { impl_item: item };
459 let impl_did = impl_item.def_id();
460 let trait_did = impl_item.trait_did();
461 if trait_did.is_none_or(|d| self.cache.traits.contains_key(&d)) {
462 for did in dids {
463 if self.impl_ids.entry(did).or_default().insert(impl_did) {
464 self.cache.impls.entry(did).or_default().push(impl_item.clone());
465 }
466 }
467 } else {
468 let trait_did = trait_did.expect("no trait did");
469 self.cache.orphan_trait_impls.push((trait_did, dids, impl_item));
470 }
471 None
472 } else {
473 Some(item)
474 };
475
476 if pushed {
477 self.cache.stack.pop().expect("stack already empty");
478 }
479 if parent_pushed {
480 self.cache.parent_stack.pop().expect("parent stack already empty");
481 }
482 self.cache.stripped_mod = orig_stripped_mod;
483 ret
484 }
485}
486
487fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::Item, name: Symbol) {
488 // Item has a name, so it must also have a DefId (can't be an impl, let alone a blanket or auto impl).
489 let item_def_id = item.item_id.as_def_id().unwrap();
490 let (parent_did, parent_path) = match item.kind {
491 clean::StrippedItem(..) => return,
492 clean::ProvidedAssocConstItem(..)
493 | clean::ImplAssocConstItem(..)
494 | clean::AssocTypeItem(..)
495 if cache.parent_stack.last().is_some_and(|parent| parent.is_trait_impl()) =>
496 {
497 // skip associated items in trait impls
498 return;
499 }
500 clean::RequiredMethodItem(..)
501 | clean::RequiredAssocConstItem(..)
502 | clean::RequiredAssocTypeItem(..)
503 | clean::StructFieldItem(..)
504 | clean::VariantItem(..) => {
505 // Don't index if containing module is stripped (i.e., private),
506 // or if item is tuple struct/variant field (name is a number -> not useful for search).
507 if cache.stripped_mod
508 || item.type_() == ItemType::StructField
509 && name.as_str().chars().all(|c| c.is_ascii_digit())
510 {
511 return;
512 }
513 let parent_did =
514 cache.parent_stack.last().expect("parent_stack is empty").item_id().expect_def_id();
515 let parent_path = &cache.stack[..cache.stack.len() - 1];
516 (Some(parent_did), parent_path)
517 }
518 clean::MethodItem(..)
519 | clean::ProvidedAssocConstItem(..)
520 | clean::ImplAssocConstItem(..)
521 | clean::AssocTypeItem(..) => {
522 let last = cache.parent_stack.last().expect("parent_stack is empty 2");
523 let parent_did = match last {
524 // impl Trait for &T { fn method(self); }
525 //
526 // When generating a function index with the above shape, we want it
527 // associated with `T`, not with the primitive reference type. It should
528 // show up as `T::method`, rather than `reference::method`, in the search
529 // results page.
530 ParentStackItem::Impl { for_: clean::Type::BorrowedRef { type_, .. }, .. } => {
531 type_.def_id(cache)
532 }
533 ParentStackItem::Impl { for_, .. } => for_.def_id(cache),
534 ParentStackItem::Type(item_id) => item_id.as_def_id(),
535 };
536 let Some(parent_did) = parent_did else { return };
537 // The current stack reflects the CacheBuilder's recursive
538 // walk over HIR. For associated items, this is the module
539 // where the `impl` block is defined. That's an implementation
540 // detail that we don't want to affect the search engine.
541 //
542 // In particular, you can arrange things like this:
543 //
544 // #![crate_name="me"]
545 // mod private_mod {
546 // impl Clone for MyThing { fn clone(&self) -> MyThing { MyThing } }
547 // }
548 // pub struct MyThing;
549 //
550 // When that happens, we need to:
551 // - ignore the `cache.stripped_mod` flag, since the Clone impl is actually
552 // part of the public API even though it's defined in a private module
553 // - present the method as `me::MyThing::clone`, its publicly-visible path
554 // - deal with the fact that the recursive walk hasn't actually reached `MyThing`
555 // until it's already past `private_mod`, since that's first, and doesn't know
556 // yet if `MyThing` will actually be public or not (it could be re-exported)
557 //
558 // We accomplish the last two points by recording children of "orphan impls"
559 // in a field of the cache whose elements are added to the search index later,
560 // after cache building is complete (see `handle_orphan_impl_child`).
561 match cache.paths.get(&parent_did) {
562 Some((fqp, _)) => (Some(parent_did), &fqp[..fqp.len() - 1]),
563 None => {
564 handle_orphan_impl_child(cache, item, parent_did);
565 return;
566 }
567 }
568 }
569 _ => {
570 // Don't index if item is crate root, which is inserted later on when serializing the index.
571 // Don't index if containing module is stripped (i.e., private),
572 if item_def_id.is_crate_root() || cache.stripped_mod {
573 return;
574 }
575 (None, &*cache.stack)
576 }
577 };
578
579 debug_assert!(!item.is_stripped());
580
581 // For searching purposes, a re-export is a duplicate if:
582 //
583 // - It's either an inline, or a true re-export
584 // - It's got the same name
585 // - Both of them have the same exact path
586 let defid = match &item.kind {
587 clean::ItemKind::ImportItem(import) => import.source.did.unwrap_or(item_def_id),
588 _ => item_def_id,
589 };
590 let (impl_id, trait_parent) = cache.parent_stack_last_impl_and_trait_id();
591 let mut types = item.types();
592 let info = IndexItemInfo::new(
593 tcx,
594 cache,
595 item,
596 parent_did,
597 clean_impl_generics(cache.parent_stack.last()).as_ref(),
598 types.next().unwrap(),
599 );
600 let index_item = IndexItem {
601 defid: Some(defid),
602 name,
603 module_path: parent_path.to_vec(),
604 parent: parent_did,
605 parent_idx: None,
606 trait_parent,
607 trait_parent_idx: None,
608 exact_module_path: None,
609 impl_id,
610 info,
611 };
612 for type_ in types {
613 let mut index_item_copy = index_item.clone();
614 index_item_copy.info.ty = type_;
615 cache.search_index.push(index_item_copy);
616 }
617 cache.search_index.push(index_item);
618}
619
620/// We have a parent, but we don't know where they're
621/// defined yet. Wait for later to index this item.
622/// See [`Cache::orphan_impl_items`].
623fn handle_orphan_impl_child(cache: &mut Cache, item: &clean::Item, parent_did: DefId) {
624 let impl_generics = clean_impl_generics(cache.parent_stack.last());
625 let (impl_id, trait_parent) = cache.parent_stack_last_impl_and_trait_id();
626 let orphan_item = OrphanImplItem {
627 parent: parent_did,
628 trait_parent,
629 item: item.clone(),
630 impl_generics,
631 impl_id,
632 };
633 cache.orphan_impl_items.push(orphan_item);
634}
635
636pub(crate) struct OrphanImplItem {
637 pub(crate) parent: DefId,
638 pub(crate) impl_id: Option<DefId>,
639 pub(crate) trait_parent: Option<DefId>,
640 pub(crate) item: clean::Item,
641 pub(crate) impl_generics: Option<(clean::Type, clean::Generics)>,
642}
643
644/// Information about trait and type parents is tracked while traversing the item tree to build
645/// the cache.
646///
647/// We don't just store `Item` in there, because `Item` contains the list of children being
648/// traversed and it would be wasteful to clone all that. We also need the item id, so just
649/// storing `ItemKind` won't work, either.
650enum ParentStackItem {
651 Impl {
652 for_: clean::Type,
653 trait_: Option<clean::Path>,
654 generics: clean::Generics,
655 kind: clean::ImplKind,
656 item_id: ItemId,
657 },
658 Type(ItemId),
659}
660
661impl ParentStackItem {
662 fn new(item: &clean::Item) -> Self {
663 match &item.kind {
664 clean::ItemKind::ImplItem(clean::Impl { for_, trait_, generics, kind, .. }) => {
665 ParentStackItem::Impl {
666 for_: for_.clone(),
667 trait_: trait_.clone(),
668 generics: generics.clone(),
669 kind: kind.clone(),
670 item_id: item.item_id,
671 }
672 }
673 _ => ParentStackItem::Type(item.item_id),
674 }
675 }
676 fn is_trait_impl(&self) -> bool {
677 matches!(self, ParentStackItem::Impl { trait_: Some(..), .. })
678 }
679 fn item_id(&self) -> ItemId {
680 match self {
681 ParentStackItem::Impl { item_id, .. } => *item_id,
682 ParentStackItem::Type(item_id) => *item_id,
683 }
684 }
685}
686
687fn clean_impl_generics(item: Option<&ParentStackItem>) -> Option<(clean::Type, clean::Generics)> {
688 if let Some(ParentStackItem::Impl { for_, generics, kind: clean::ImplKind::Normal, .. }) = item
689 {
690 Some((for_.clone(), generics.clone()))
691 } else {
692 None
693 }
694}