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