Skip to main content

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