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#[derive(Default)]
31pub(crate) struct Cache {
32 pub(crate) impls: DefIdMap<Vec<Impl>>,
39
40 pub(crate) paths: FxIndexMap<DefId, (Vec<Symbol>, ItemType)>,
46
47 pub(crate) external_paths: FxIndexMap<DefId, (Vec<Symbol>, ItemType)>,
50
51 pub(crate) exact_paths: DefIdMap<Vec<Symbol>>,
62
63 pub(crate) traits: FxIndexMap<DefId, clean::Trait>,
68
69 pub(crate) implementors: FxIndexMap<DefId, Vec<Impl>>,
73
74 pub(crate) extern_locations: FxIndexMap<CrateNum, ExternalLocation>,
76
77 pub(crate) primitive_locations: FxIndexMap<clean::PrimitiveType, DefId>,
79
80 pub(crate) effective_visibilities: RustdocEffectiveVisibilities,
84
85 pub(crate) crate_version: Option<String>,
87
88 pub(crate) document_private: bool,
91 pub(crate) document_hidden: bool,
94
95 pub(crate) masked_crates: FxHashSet<CrateNum>,
99
100 stack: Vec<Symbol>,
102 parent_stack: Vec<ParentStackItem>,
103 stripped_mod: bool,
104
105 pub(crate) search_index: Vec<IndexItem>,
106
107 pub(crate) orphan_impl_items: Vec<OrphanImplItem>,
113
114 orphan_trait_impls: Vec<(DefId, FxIndexSet<DefId>, Impl)>,
122
123 pub(crate) intra_doc_links: FxHashMap<ItemId, FxIndexSet<clean::ItemLink>>,
127
128 pub(crate) inlined_items: DefIdSet,
132}
133
134struct CacheBuilder<'a, 'tcx> {
136 cache: &'a mut Cache,
137 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 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 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 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 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 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 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 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 let orig_stripped_mod = match item.kind {
251 clean::StrippedItem(box 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 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 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 self.cache
288 .implementors
289 .entry(trait_.def_id())
290 .or_default()
291 .push(Impl { impl_item: item.clone() });
292 }
293
294 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 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 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 }
390
391 clean::PlaceholderImplItem => return None,
392 }
393
394 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 let ret = if let clean::Item {
413 inner: box clean::ItemInner { kind: clean::ImplItem(ref i), .. },
414 } = item
415 {
416 let mut dids = FxIndexSet::default();
420 match i.for_ {
421 clean::Type::Path { ref path }
422 | clean::BorrowedRef { type_: box 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_: box 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 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 return;
499 }
500 clean::RequiredMethodItem(..)
501 | clean::RequiredAssocConstItem(..)
502 | clean::RequiredAssocTypeItem(..)
503 | clean::StructFieldItem(..)
504 | clean::VariantItem(..) => {
505 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 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 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 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 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 info = IndexItemInfo::new(
592 tcx,
593 cache,
594 item,
595 parent_did,
596 clean_impl_generics(cache.parent_stack.last()).as_ref(),
597 );
598 let index_item = IndexItem {
599 defid: Some(defid),
600 name,
601 module_path: parent_path.to_vec(),
602 parent: parent_did,
603 parent_idx: None,
604 trait_parent,
605 trait_parent_idx: None,
606 exact_module_path: None,
607 impl_id,
608 info,
609 };
610
611 cache.search_index.push(index_item);
612}
613
614fn handle_orphan_impl_child(cache: &mut Cache, item: &clean::Item, parent_did: DefId) {
618 let impl_generics = clean_impl_generics(cache.parent_stack.last());
619 let (impl_id, trait_parent) = cache.parent_stack_last_impl_and_trait_id();
620 let orphan_item = OrphanImplItem {
621 parent: parent_did,
622 trait_parent,
623 item: item.clone(),
624 impl_generics,
625 impl_id,
626 };
627 cache.orphan_impl_items.push(orphan_item);
628}
629
630pub(crate) struct OrphanImplItem {
631 pub(crate) parent: DefId,
632 pub(crate) impl_id: Option<DefId>,
633 pub(crate) trait_parent: Option<DefId>,
634 pub(crate) item: clean::Item,
635 pub(crate) impl_generics: Option<(clean::Type, clean::Generics)>,
636}
637
638enum ParentStackItem {
645 Impl {
646 for_: clean::Type,
647 trait_: Option<clean::Path>,
648 generics: clean::Generics,
649 kind: clean::ImplKind,
650 item_id: ItemId,
651 },
652 Type(ItemId),
653}
654
655impl ParentStackItem {
656 fn new(item: &clean::Item) -> Self {
657 match &item.kind {
658 clean::ItemKind::ImplItem(box clean::Impl { for_, trait_, generics, kind, .. }) => {
659 ParentStackItem::Impl {
660 for_: for_.clone(),
661 trait_: trait_.clone(),
662 generics: generics.clone(),
663 kind: kind.clone(),
664 item_id: item.item_id,
665 }
666 }
667 _ => ParentStackItem::Type(item.item_id),
668 }
669 }
670 fn is_trait_impl(&self) -> bool {
671 matches!(self, ParentStackItem::Impl { trait_: Some(..), .. })
672 }
673 fn item_id(&self) -> ItemId {
674 match self {
675 ParentStackItem::Impl { item_id, .. } => *item_id,
676 ParentStackItem::Type(item_id) => *item_id,
677 }
678 }
679}
680
681fn clean_impl_generics(item: Option<&ParentStackItem>) -> Option<(clean::Type, clean::Generics)> {
682 if let Some(ParentStackItem::Impl { for_, generics, kind: clean::ImplKind::Normal, .. }) = item
683 {
684 Some((for_.clone(), generics.clone()))
685 } else {
686 None
687 }
688}