1 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
4 BodyId, Expr, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId, Ty,
7 use crate::hir_id::HirId;
9 /// Requirements for a `StableHashingContext` to be used in this crate.
10 /// This is a hack to allow using the `HashStable_Generic` derive macro
11 /// instead of implementing everything in librustc.
12 pub trait HashStableContext:
13 rustc_ast::HashStableContext + rustc_target::HashStableContext
15 fn hash_hir_id(&mut self, _: HirId, hasher: &mut StableHasher);
16 fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
17 fn hash_reference_to_item(&mut self, _: HirId, hasher: &mut StableHasher);
18 fn hash_hir_mod(&mut self, _: &Mod<'_>, hasher: &mut StableHasher);
19 fn hash_hir_expr(&mut self, _: &Expr<'_>, hasher: &mut StableHasher);
20 fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
21 fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher);
22 fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F);
25 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
26 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
27 hcx.hash_hir_id(*self, hasher)
31 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
32 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
33 hcx.hash_body_id(*self, hasher)
37 // The following implementations of HashStable for `ItemId`, `TraitItemId`, and
38 // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
39 // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
40 // are used when another item in the HIR is *referenced* and we certainly
41 // want to pick up on a reference changing its target, so we hash the NodeIds
44 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
45 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
46 hcx.hash_reference_to_item(self.id, hasher)
50 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
51 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
52 hcx.hash_reference_to_item(self.hir_id, hasher)
56 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
57 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
58 hcx.hash_reference_to_item(self.hir_id, hasher)
62 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Mod<'_> {
63 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
64 hcx.hash_hir_mod(self, hasher)
68 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
69 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
70 hcx.hash_hir_expr(self, hasher)
74 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
75 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
76 hcx.hash_hir_ty(self, hasher)
80 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
81 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
82 hcx.hash_hir_visibility_kind(self, hasher)
86 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
87 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
88 let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
90 hcx.hash_hir_item_like(|hcx| {
91 ident.name.hash_stable(hcx, hasher);
92 attrs.hash_stable(hcx, hasher);
93 generics.hash_stable(hcx, hasher);
94 kind.hash_stable(hcx, hasher);
95 span.hash_stable(hcx, hasher);
100 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
101 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
113 hcx.hash_hir_item_like(|hcx| {
114 ident.name.hash_stable(hcx, hasher);
115 vis.hash_stable(hcx, hasher);
116 defaultness.hash_stable(hcx, hasher);
117 attrs.hash_stable(hcx, hasher);
118 generics.hash_stable(hcx, hasher);
119 kind.hash_stable(hcx, hasher);
120 span.hash_stable(hcx, hasher);
125 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
126 fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
127 let Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self;
129 hcx.hash_hir_item_like(|hcx| {
130 ident.name.hash_stable(hcx, hasher);
131 attrs.hash_stable(hcx, hasher);
132 kind.hash_stable(hcx, hasher);
133 vis.hash_stable(hcx, hasher);
134 span.hash_stable(hcx, hasher);