]> git.lizzy.rs Git - rust.git/blob - src/librustc_hir/stable_hash_impls.rs
Auto merge of #70052 - Amanieu:hashbrown7, r=Mark-Simulacrum
[rust.git] / src / librustc_hir / stable_hash_impls.rs
1 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
2
3 use crate::hir::{
4     BodyId, Expr, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId, Ty,
5     VisibilityKind,
6 };
7 use crate::hir_id::{HirId, ItemLocalId};
8 use rustc_span::def_id::{DefPathHash, LocalDefId};
9
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 librustc_middle.
13 pub trait HashStableContext:
14     rustc_ast::HashStableContext + rustc_target::HashStableContext
15 {
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);
24     fn local_def_path_hash(&self, def_id: LocalDefId) -> DefPathHash;
25 }
26
27 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId {
28     type KeyType = (DefPathHash, ItemLocalId);
29
30     #[inline]
31     fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
32         let def_path_hash = hcx.local_def_path_hash(self.owner);
33         (def_path_hash, self.local_id)
34     }
35 }
36
37 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId {
38     type KeyType = (DefPathHash, ItemLocalId);
39
40     #[inline]
41     fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
42         self.hir_id.to_stable_hash_key(hcx)
43     }
44 }
45
46 impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId {
47     type KeyType = (DefPathHash, ItemLocalId);
48
49     #[inline]
50     fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) {
51         self.hir_id.to_stable_hash_key(hcx)
52     }
53 }
54
55 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
56     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
57         hcx.hash_hir_id(*self, hasher)
58     }
59 }
60
61 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
62     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
63         hcx.hash_body_id(*self, hasher)
64     }
65 }
66
67 // The following implementations of HashStable for `ItemId`, `TraitItemId`, and
68 // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
69 // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
70 // are used when another item in the HIR is *referenced* and we certainly
71 // want to pick up on a reference changing its target, so we hash the NodeIds
72 // in "DefPath Mode".
73
74 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
75     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
76         hcx.hash_reference_to_item(self.id, hasher)
77     }
78 }
79
80 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
81     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
82         hcx.hash_reference_to_item(self.hir_id, hasher)
83     }
84 }
85
86 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
87     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
88         hcx.hash_reference_to_item(self.hir_id, hasher)
89     }
90 }
91
92 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Mod<'_> {
93     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
94         hcx.hash_hir_mod(self, hasher)
95     }
96 }
97
98 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
99     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
100         hcx.hash_hir_expr(self, hasher)
101     }
102 }
103
104 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
105     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
106         hcx.hash_hir_ty(self, hasher)
107     }
108 }
109
110 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
111     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
112         hcx.hash_hir_visibility_kind(self, hasher)
113     }
114 }
115
116 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
117     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
118         let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
119
120         hcx.hash_hir_item_like(|hcx| {
121             ident.name.hash_stable(hcx, hasher);
122             attrs.hash_stable(hcx, hasher);
123             generics.hash_stable(hcx, hasher);
124             kind.hash_stable(hcx, hasher);
125             span.hash_stable(hcx, hasher);
126         });
127     }
128 }
129
130 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
131     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
132         let ImplItem {
133             hir_id: _,
134             ident,
135             ref vis,
136             defaultness,
137             ref attrs,
138             ref generics,
139             ref kind,
140             span,
141         } = *self;
142
143         hcx.hash_hir_item_like(|hcx| {
144             ident.name.hash_stable(hcx, hasher);
145             vis.hash_stable(hcx, hasher);
146             defaultness.hash_stable(hcx, hasher);
147             attrs.hash_stable(hcx, hasher);
148             generics.hash_stable(hcx, hasher);
149             kind.hash_stable(hcx, hasher);
150             span.hash_stable(hcx, hasher);
151         });
152     }
153 }
154
155 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
156     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
157         let Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self;
158
159         hcx.hash_hir_item_like(|hcx| {
160             ident.name.hash_stable(hcx, hasher);
161             attrs.hash_stable(hcx, hasher);
162             kind.hash_stable(hcx, hasher);
163             vis.hash_stable(hcx, hasher);
164             span.hash_stable(hcx, hasher);
165         });
166     }
167 }