]> git.proxmox.com Git - rustc.git/blame - src/librustdoc/formats/mod.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / librustdoc / formats / mod.rs
CommitLineData
923072b8
FG
1pub(crate) mod cache;
2pub(crate) mod item_type;
3pub(crate) mod renderer;
3dfed10e 4
17df50a5 5use rustc_hir::def_id::DefId;
3dfed10e 6
923072b8 7pub(crate) use renderer::{run_format, FormatRenderer};
3dfed10e 8
04454e1e 9use crate::clean::{self, ItemId};
923072b8 10use crate::html::render::Context;
3dfed10e
XL
11
12/// Specifies whether rendering directly implemented trait items or ones from a certain Deref
13/// impl.
923072b8 14pub(crate) enum AssocItemRender<'a> {
3dfed10e 15 All,
c295e0f8 16 DerefFor { trait_: &'a clean::Path, type_: &'a clean::Type, deref_mut_: bool },
3dfed10e
XL
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)]
923072b8 22pub(crate) enum RenderMode {
3dfed10e
XL
23 Normal,
24 ForDeref { mut_: bool },
25}
26
27/// Metadata about implementations for a type or trait.
28#[derive(Clone, Debug)]
923072b8
FG
29pub(crate) struct Impl {
30 pub(crate) impl_item: clean::Item,
3dfed10e
XL
31}
32
33impl Impl {
923072b8 34 pub(crate) fn inner_impl(&self) -> &clean::Impl {
5869c6ff 35 match *self.impl_item.kind {
3dfed10e
XL
36 clean::ImplItem(ref impl_) => impl_,
37 _ => panic!("non-impl item found in impl"),
38 }
39 }
40
923072b8 41 pub(crate) fn trait_did(&self) -> Option<DefId> {
c295e0f8 42 self.inner_impl().trait_.as_ref().map(|t| t.def_id())
5869c6ff 43 }
04454e1e
FG
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`.
923072b8 51 pub(crate) fn def_id(&self) -> DefId {
04454e1e
FG
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 ItemId::Primitive(_, _) => {
57 panic!(
58 "Unexpected ItemId::Primitive in expect_def_id: {:?}",
59 self.impl_item.item_id
60 )
61 }
62 }
63 }
923072b8
FG
64
65 // Returns true if this is an implementation on a "local" type, meaning:
66 // the type is in the current crate, or the type and the trait are both
67 // re-exported by the current crate.
68 pub(crate) fn is_on_local_type(&self, cx: &Context<'_>) -> bool {
69 let cache = cx.cache();
70 let for_type = &self.inner_impl().for_;
71 if let Some(for_type_did) = for_type.def_id(cache) {
72 // The "for" type is local if it's in the paths for the current crate.
73 if cache.paths.contains_key(&for_type_did) {
74 return true;
75 }
76 if let Some(trait_did) = self.trait_did() {
77 // The "for" type and the trait are from the same crate. That could
78 // be different from the current crate, for instance when both were
79 // re-exported from some other crate. But they are local with respect to
80 // each other.
81 if for_type_did.krate == trait_did.krate {
82 return true;
83 }
84 // Hack: many traits and types in std are re-exported from
85 // core or alloc. In general, rustdoc is capable of recognizing
86 // these implementations as being on local types. However, in at
87 // least one case (https://github.com/rust-lang/rust/issues/97610),
88 // rustdoc gets confused and labels an implementation as being on
89 // a foreign type. To make sure that confusion doesn't pass on to
90 // the reader, consider all implementations in std, core, and alloc
91 // to be on local types.
92 let crate_name = cx.tcx().crate_name(trait_did.krate);
93 if matches!(crate_name.as_str(), "std" | "core" | "alloc") {
94 return true;
95 }
96 }
97 return false;
98 };
99 true
100 }
3dfed10e 101}