]>
Commit | Line | Data |
---|---|---|
ba9703b0 | 1 | use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; |
dfeec247 | 2 | |
ba9703b0 | 3 | use crate::hir::{ |
f2b60f7d | 4 | AttributeMap, BodyId, Crate, ForeignItemId, ImplItemId, ItemId, OwnerNodes, TraitItemId, |
ba9703b0 XL |
5 | }; |
6 | use crate::hir_id::{HirId, ItemLocalId}; | |
136023e0 | 7 | use rustc_span::def_id::DefPathHash; |
dfeec247 XL |
8 | |
9 | /// Requirements for a `StableHashingContext` to be used in this crate. | |
10 | /// This is a hack to allow using the `HashStable_Generic` derive macro | |
cdc7bbd5 | 11 | /// instead of implementing everything in `rustc_middle`. |
74b04a01 XL |
12 | pub trait HashStableContext: |
13 | rustc_ast::HashStableContext + rustc_target::HashStableContext | |
14 | { | |
dfeec247 | 15 | fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher); |
ba9703b0 XL |
16 | } |
17 | ||
18 | impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for HirId { | |
19 | type KeyType = (DefPathHash, ItemLocalId); | |
20 | ||
21 | #[inline] | |
22 | fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) { | |
2b03887a | 23 | let def_path_hash = self.owner.def_id.to_stable_hash_key(hcx); |
ba9703b0 XL |
24 | (def_path_hash, self.local_id) |
25 | } | |
26 | } | |
27 | ||
c295e0f8 XL |
28 | impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemLocalId { |
29 | type KeyType = ItemLocalId; | |
30 | ||
31 | #[inline] | |
32 | fn to_stable_hash_key(&self, _: &HirCtx) -> ItemLocalId { | |
33 | *self | |
34 | } | |
35 | } | |
36 | ||
37 | impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for BodyId { | |
38 | type KeyType = (DefPathHash, ItemLocalId); | |
39 | ||
40 | #[inline] | |
41 | fn to_stable_hash_key(&self, hcx: &HirCtx) -> (DefPathHash, ItemLocalId) { | |
42 | let BodyId { hir_id } = *self; | |
43 | hir_id.to_stable_hash_key(hcx) | |
44 | } | |
45 | } | |
46 | ||
6a06907d XL |
47 | impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ItemId { |
48 | type KeyType = DefPathHash; | |
49 | ||
50 | #[inline] | |
51 | fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { | |
2b03887a | 52 | self.owner_id.def_id.to_stable_hash_key(hcx) |
6a06907d XL |
53 | } |
54 | } | |
55 | ||
ba9703b0 | 56 | impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for TraitItemId { |
6a06907d | 57 | type KeyType = DefPathHash; |
ba9703b0 XL |
58 | |
59 | #[inline] | |
6a06907d | 60 | fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { |
2b03887a | 61 | self.owner_id.def_id.to_stable_hash_key(hcx) |
ba9703b0 XL |
62 | } |
63 | } | |
64 | ||
65 | impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ImplItemId { | |
6a06907d | 66 | type KeyType = DefPathHash; |
ba9703b0 XL |
67 | |
68 | #[inline] | |
6a06907d | 69 | fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { |
2b03887a | 70 | self.owner_id.def_id.to_stable_hash_key(hcx) |
ba9703b0 | 71 | } |
dfeec247 XL |
72 | } |
73 | ||
fc512014 | 74 | impl<HirCtx: crate::HashStableContext> ToStableHashKey<HirCtx> for ForeignItemId { |
6a06907d | 75 | type KeyType = DefPathHash; |
fc512014 XL |
76 | |
77 | #[inline] | |
6a06907d | 78 | fn to_stable_hash_key(&self, hcx: &HirCtx) -> DefPathHash { |
2b03887a | 79 | self.owner_id.def_id.to_stable_hash_key(hcx) |
fc512014 XL |
80 | } |
81 | } | |
82 | ||
dfeec247 XL |
83 | impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId { |
84 | fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { | |
85 | hcx.hash_body_id(*self, hasher) | |
86 | } | |
87 | } | |
88 | ||
74b04a01 XL |
89 | // The following implementations of HashStable for `ItemId`, `TraitItemId`, and |
90 | // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within | |
91 | // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al | |
92 | // are used when another item in the HIR is *referenced* and we certainly | |
93 | // want to pick up on a reference changing its target, so we hash the NodeIds | |
94 | // in "DefPath Mode". | |
95 | ||
a2a8927a | 96 | impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'tcx> { |
3c0e092e XL |
97 | fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { |
98 | // We ignore the `nodes` and `bodies` fields since these refer to information included in | |
99 | // `hash` which is hashed in the collector and used for the crate hash. | |
5099ac24 FG |
100 | // `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing |
101 | // the body satisfies the condition of two nodes being different have different | |
102 | // `hash_stable` results. | |
9ffffee4 FG |
103 | let OwnerNodes { hash_including_bodies, hash_without_bodies: _, nodes: _, bodies: _ } = |
104 | *self; | |
3c0e092e XL |
105 | hash_including_bodies.hash_stable(hcx, hasher); |
106 | } | |
107 | } | |
108 | ||
a2a8927a | 109 | impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for AttributeMap<'tcx> { |
3c0e092e XL |
110 | fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { |
111 | // We ignore the `map` since it refers to information included in `hash` which is hashed in | |
112 | // the collector and used for the crate hash. | |
113 | let AttributeMap { hash, map: _ } = *self; | |
114 | hash.hash_stable(hcx, hasher); | |
115 | } | |
116 | } | |
117 | ||
118 | impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Crate<'_> { | |
119 | fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) { | |
120 | let Crate { owners: _, hir_hash } = self; | |
121 | hir_hash.hash_stable(hcx, hasher) | |
122 | } | |
123 | } |