rustc_hir/
stable_hash_impls.rs1use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
2use rustc_span::def_id::DefPathHash;
3
4use crate::HashIgnoredAttrId;
5use crate::hir::{
6    AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId,
7};
8use crate::hir_id::{HirId, ItemLocalId};
9use crate::lints::DelayedLints;
10
11pub trait HashStableContext:
15    rustc_attr_data_structures::HashStableContext
16    + rustc_ast::HashStableContext
17    + rustc_abi::HashStableContext
18{
19    fn hash_attr_id(&mut self, id: &HashIgnoredAttrId, hasher: &mut StableHasher);
20}
21
22impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
23    type KeyType = (DefPathHash, ItemLocalId);
24
25    #[inline]
26    fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
27        let def_path_hash = self.owner.def_id.to_stable_hash_key(hcx);
28        (def_path_hash, self.local_id)
29    }
30}
31
32impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemLocalId {
33    type KeyType = ItemLocalId;
34
35    #[inline]
36    fn to_stable_hash_key(&self, _: &HirCtx) -> ItemLocalId {
37        *self
38    }
39}
40
41impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for BodyId {
42    type KeyType = (DefPathHash, ItemLocalId);
43
44    #[inline]
45    fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
46        let BodyId { hir_id } = *self;
47        hir_id.to_stable_hash_key(hcx)
48    }
49}
50
51impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
52    type KeyType = DefPathHash;
53
54    #[inline]
55    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
56        self.owner_id.def_id.to_stable_hash_key(hcx)
57    }
58}
59
60impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
61    type KeyType = DefPathHash;
62
63    #[inline]
64    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
65        self.owner_id.def_id.to_stable_hash_key(hcx)
66    }
67}
68
69impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
70    type KeyType = DefPathHash;
71
72    #[inline]
73    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
74        self.owner_id.def_id.to_stable_hash_key(hcx)
75    }
76}
77
78impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
79    type KeyType = DefPathHash;
80
81    #[inline]
82    fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
83        self.owner_id.def_id.to_stable_hash_key(hcx)
84    }
85}
86
87impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> {
95    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
96        let OwnerNodes { opt_hash_including_bodies, nodes: _, bodies: _ } = *self;
102        opt_hash_including_bodies.unwrap().hash_stable(hcx, hasher);
103    }
104}
105
106impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for DelayedLints {
107    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
108        let DelayedLints { opt_hash, .. } = *self;
109        opt_hash.unwrap().hash_stable(hcx, hasher);
110    }
111}
112
113impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> {
114    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
115        let AttributeMap { opt_hash, define_opaque: _, map: _ } = *self;
118        opt_hash.unwrap().hash_stable(hcx, hasher);
119    }
120}
121
122impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> {
123    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
124        let Crate { owners: _, opt_hir_hash } = self;
125        opt_hir_hash.unwrap().hash_stable(hcx, hasher)
126    }
127}
128
129impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HashIgnoredAttrId {
130    fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
131        hcx.hash_attr_id(self, hasher)
132    }
133}