1 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
4 BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, MacroDef, Mod,
5 TraitItem, 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 ItemId {
37 type KeyType = DefPathHash;
40 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
41 self.def_id.to_stable_hash_key(hcx)
45 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
46 type KeyType = DefPathHash;
49 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
50 self.def_id.to_stable_hash_key(hcx)
54 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
55 type KeyType = DefPathHash;
58 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
59 self.def_id.to_stable_hash_key(hcx)
63 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId {
64 type KeyType = DefPathHash;
67 fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash {
68 self.def_id.to_stable_hash_key(hcx)
72 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
73 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
74 hcx.hash_hir_id(*self, hasher)
78 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
79 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
80 hcx.hash_body_id(*self, hasher)
84 // The following implementations of HashStable for `ItemId`, `TraitItemId`, and
85 // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
86 // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
87 // are used when another item in the HIR is *referenced* and we certainly
88 // want to pick up on a reference changing its target, so we hash the NodeIds
91 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
92 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
93 hcx.hash_reference_to_item(self.hir_id(), hasher)
97 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItemId {
98 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
99 hcx.hash_reference_to_item(self.hir_id(), hasher)
103 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
104 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
105 hcx.hash_reference_to_item(self.hir_id(), hasher)
109 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
110 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
111 hcx.hash_reference_to_item(self.hir_id(), hasher)
115 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Mod<'_> {
116 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
117 hcx.hash_hir_mod(self, hasher)
121 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
122 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
123 hcx.hash_hir_expr(self, hasher)
127 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
128 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
129 hcx.hash_hir_ty(self, hasher)
133 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
134 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
135 hcx.hash_hir_visibility_kind(self, hasher)
139 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
140 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
141 let TraitItem { def_id: _, ident, ref generics, ref kind, span } = *self;
143 hcx.hash_hir_item_like(|hcx| {
144 ident.name.hash_stable(hcx, hasher);
145 generics.hash_stable(hcx, hasher);
146 kind.hash_stable(hcx, hasher);
147 span.hash_stable(hcx, hasher);
152 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
153 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
154 let ImplItem { def_id: _, ident, ref vis, defaultness, ref generics, ref kind, span } =
157 hcx.hash_hir_item_like(|hcx| {
158 ident.name.hash_stable(hcx, hasher);
159 vis.hash_stable(hcx, hasher);
160 defaultness.hash_stable(hcx, hasher);
161 generics.hash_stable(hcx, hasher);
162 kind.hash_stable(hcx, hasher);
163 span.hash_stable(hcx, hasher);
168 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ForeignItem<'_> {
169 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
170 let ForeignItem { def_id: _, ident, ref kind, span, ref vis } = *self;
172 hcx.hash_hir_item_like(|hcx| {
173 ident.name.hash_stable(hcx, hasher);
174 kind.hash_stable(hcx, hasher);
175 span.hash_stable(hcx, hasher);
176 vis.hash_stable(hcx, hasher);
181 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
182 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
183 let Item { ident, def_id: _, ref kind, ref vis, span } = *self;
185 hcx.hash_hir_item_like(|hcx| {
186 ident.name.hash_stable(hcx, hasher);
187 kind.hash_stable(hcx, hasher);
188 vis.hash_stable(hcx, hasher);
189 span.hash_stable(hcx, hasher);
194 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for MacroDef<'_> {
195 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
196 let MacroDef { ident, def_id: _, ref ast, ref vis, span } = *self;
198 hcx.hash_hir_item_like(|hcx| {
199 ident.name.hash_stable(hcx, hasher);
200 ast.hash_stable(hcx, hasher);
201 vis.hash_stable(hcx, hasher);
202 span.hash_stable(hcx, hasher);