rustdoc/formats/mod.rs
1pub(crate) mod cache;
2pub(crate) mod item_type;
3pub(crate) mod renderer;
4
5pub(crate) use renderer::{FormatRenderer, run_format};
6use rustc_hir::def_id::DefId;
7
8use crate::clean::{self, ItemId};
9use crate::html::render::Context;
10
11/// Metadata about implementations for a type or trait.
12#[derive(Clone, Debug)]
13pub(crate) struct Impl {
14 pub(crate) impl_item: clean::Item,
15}
16
17impl Impl {
18 pub(crate) fn inner_impl(&self) -> &clean::Impl {
19 match self.impl_item.kind {
20 clean::ImplItem(ref impl_) => impl_,
21 _ => panic!("non-impl item found in impl"),
22 }
23 }
24
25 pub(crate) fn trait_did(&self) -> Option<DefId> {
26 self.inner_impl().trait_.as_ref().map(|t| t.def_id())
27 }
28
29 /// This function is used to extract a `DefId` to be used as a key for the `Cache::impls` field.
30 ///
31 /// It allows to prevent having duplicated implementations showing up (the biggest issue was
32 /// with blanket impls).
33 ///
34 /// It panics if `self` is a `ItemId::Primitive`.
35 pub(crate) fn def_id(&self) -> DefId {
36 match self.impl_item.item_id {
37 ItemId::Blanket { impl_id, .. } => impl_id,
38 ItemId::Auto { trait_, .. } => trait_,
39 ItemId::DefId(def_id) => def_id,
40 }
41 }
42
43 // Returns true if this is an implementation on a "local" type, meaning:
44 // the type is in the current crate, or the type and the trait are both
45 // re-exported by the current crate.
46 pub(crate) fn is_on_local_type(&self, cx: &Context<'_>) -> bool {
47 let cache = cx.cache();
48 let for_type = &self.inner_impl().for_;
49 if let Some(for_type_did) = for_type.def_id(cache) {
50 // The "for" type is local if it's in the paths for the current crate.
51 if cache.paths.contains_key(&for_type_did) {
52 return true;
53 }
54 if let Some(trait_did) = self.trait_did() {
55 // The "for" type and the trait are from the same crate. That could
56 // be different from the current crate, for instance when both were
57 // re-exported from some other crate. But they are local with respect to
58 // each other.
59 if for_type_did.krate == trait_did.krate {
60 return true;
61 }
62 // Hack: many traits and types in std are re-exported from
63 // core or alloc. In general, rustdoc is capable of recognizing
64 // these implementations as being on local types. However, in at
65 // least one case (https://github.com/rust-lang/rust/issues/97610),
66 // rustdoc gets confused and labels an implementation as being on
67 // a foreign type. To make sure that confusion doesn't pass on to
68 // the reader, consider all implementations in std, core, and alloc
69 // to be on local types.
70 let crate_name = cx.tcx().crate_name(trait_did.krate);
71 if matches!(crate_name.as_str(), "std" | "core" | "alloc") {
72 return true;
73 }
74 }
75 return false;
76 };
77 true
78 }
79}