1 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
4 BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem,
5 TraitItemId, Ty, VisibilityKind,
7 use crate::hir_id::{HirId, ItemLocalId};
8 use rustc_span::def_id::DefPathHash;
10 /// Requirements for a `StableHashingContext` to be used in this crate.
11 /// This is a hack to allow using the `HashStable_Generic` derive macro
12 /// instead of implementing everything in `rustc_middle`.
13 pub trait HashStableContext:
14 rustc_ast::HashStableContext + rustc_target::HashStableContext
16 fn hash_hir_id(&mut self, _: HirId, hasher: &mut StableHasher);
17 fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
18 fn hash_reference_to_item(&mut self, _: HirId, hasher: &mut StableHasher);
19 fn hash_hir_mod(&mut self, _: &Mod<'_>, hasher: &mut StableHasher);
20 fn hash_hir_expr(&mut self, _: &Expr<'_>, hasher: &mut StableHasher);
21 fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
22 fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher);
23 fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F);
26 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
27 type KeyType = (DefPathHash, ItemLocalId);
30 fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
31 let def_path_hash = self.owner.to_stable_hash_key(hcx);
32 (def_path_hash, self.local_id)
36 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemLocalId {
37 type KeyType = ItemLocalId;
40 fn to_stable_hash_key(&self, _: &HirCtx) -> ItemLocalId {
45 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for BodyId {
46 type KeyType = (DefPathHash, ItemLocalId);
49 fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
50 let BodyId { hir_id } = *self;
51 hir_id.to_stable_hash_key(hcx)
55 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId {
56 type KeyType = DefPathHash;
59 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
60 self.def_id.to_stable_hash_key(hcx)
64 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
65 type KeyType = DefPathHash;
68 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
69 self.def_id.to_stable_hash_key(hcx)
73 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
74 type KeyType = DefPathHash;
77 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
78 self.def_id.to_stable_hash_key(hcx)
82 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
83 type KeyType = DefPathHash;
86 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
87 self.def_id.to_stable_hash_key(hcx)
91 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
92 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
93 hcx.hash_hir_id(*self, hasher)
97 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
98 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
99 hcx.hash_body_id(*self, hasher)
103 // The following implementations of HashStable for `ItemId`, `TraitItemId`, and
104 // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
105 // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
106 // are used when another item in the HIR is *referenced* and we certainly
107 // want to pick up on a reference changing its target, so we hash the NodeIds
108 // in "DefPath Mode".
110 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
111 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
112 hcx.hash_reference_to_item(self.hir_id(), hasher)
116 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
117 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
118 hcx.hash_reference_to_item(self.hir_id(), hasher)
122 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
123 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
124 hcx.hash_reference_to_item(self.hir_id(), hasher)
128 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
129 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
130 hcx.hash_reference_to_item(self.hir_id(), hasher)
134 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Mod<'_> {
135 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
136 hcx.hash_hir_mod(self, hasher)
140 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
141 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
142 hcx.hash_hir_expr(self, hasher)
146 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
147 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
148 hcx.hash_hir_ty(self, hasher)
152 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
153 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
154 hcx.hash_hir_visibility_kind(self, hasher)
158 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
159 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
160 let TraitItem { def_id: _, ident, ref generics, ref kind, span } = *self;
162 hcx.hash_hir_item_like(|hcx| {
163 ident.name.hash_stable(hcx, hasher);
164 generics.hash_stable(hcx, hasher);
165 kind.hash_stable(hcx, hasher);
166 span.hash_stable(hcx, hasher);
171 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
172 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
173 let ImplItem { def_id: _, ident, ref vis, defaultness, ref generics, ref kind, span } =
176 hcx.hash_hir_item_like(|hcx| {
177 ident.name.hash_stable(hcx, hasher);
178 vis.hash_stable(hcx, hasher);
179 defaultness.hash_stable(hcx, hasher);
180 generics.hash_stable(hcx, hasher);
181 kind.hash_stable(hcx, hasher);
182 span.hash_stable(hcx, hasher);
187 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItem<'_> {
188 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
189 let ForeignItem { def_id: _, ident, ref kind, span, ref vis } = *self;
191 hcx.hash_hir_item_like(|hcx| {
192 ident.name.hash_stable(hcx, hasher);
193 kind.hash_stable(hcx, hasher);
194 span.hash_stable(hcx, hasher);
195 vis.hash_stable(hcx, hasher);
200 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
201 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
202 let Item { ident, def_id: _, ref kind, ref vis, span } = *self;
204 hcx.hash_hir_item_like(|hcx| {
205 ident.name.hash_stable(hcx, hasher);
206 kind.hash_stable(hcx, hasher);
207 vis.hash_stable(hcx, hasher);
208 span.hash_stable(hcx, hasher);