]> git.lizzy.rs Git - rust.git/blob - src/librustc_hir/stable_hash_impls.rs
Auto merge of #69482 - lqd:poloniusup, r=nikomatsakis
[rust.git] / src / librustc_hir / stable_hash_impls.rs
1 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2
3 use crate::hir::{BodyId, Expr, ImplItemId, ItemId, Mod, TraitItemId, Ty, VisibilityKind};
4 use crate::hir_id::HirId;
5
6 /// Requirements for a `StableHashingContext` to be used in this crate.
7 /// This is a hack to allow using the `HashStable_Generic` derive macro
8 /// instead of implementing everything in librustc.
9 pub trait HashStableContext:
10     rustc_ast::HashStableContext + rustc_target::HashStableContext
11 {
12     fn hash_hir_id(&mut self, _: HirId, hasher: &mut StableHasher);
13     fn hash_body_id(&mut self, _: BodyId, hasher: &mut StableHasher);
14     fn hash_reference_to_item(&mut self, _: HirId, hasher: &mut StableHasher);
15     fn hash_hir_mod(&mut self, _: &Mod<'_>, hasher: &mut StableHasher);
16     fn hash_hir_expr(&mut self, _: &Expr<'_>, hasher: &mut StableHasher);
17     fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
18     fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher);
19 }
20
21 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
22     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
23         hcx.hash_hir_id(*self, hasher)
24     }
25 }
26
27 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for BodyId {
28     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
29         hcx.hash_body_id(*self, hasher)
30     }
31 }
32
33 // The following implementations of HashStable for `ItemId`, `TraitItemId`, and
34 // `ImplItemId` deserve special attention. Normally we do not hash `NodeId`s within
35 // the HIR, since they just signify a HIR nodes own path. But `ItemId` et al
36 // are used when another item in the HIR is *referenced* and we certainly
37 // want to pick up on a reference changing its target, so we hash the NodeIds
38 // in "DefPath Mode".
39
40 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ItemId {
41     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
42         hcx.hash_reference_to_item(self.id, hasher)
43     }
44 }
45
46 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItemId {
47     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
48         hcx.hash_reference_to_item(self.hir_id, hasher)
49     }
50 }
51
52 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItemId {
53     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
54         hcx.hash_reference_to_item(self.hir_id, hasher)
55     }
56 }
57
58 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Mod<'_> {
59     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
60         hcx.hash_hir_mod(self, hasher)
61     }
62 }
63
64 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Expr<'_> {
65     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
66         hcx.hash_hir_expr(self, hasher)
67     }
68 }
69
70 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Ty<'_> {
71     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
72         hcx.hash_hir_ty(self, hasher)
73     }
74 }
75
76 impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_> {
77     fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
78         hcx.hash_hir_visibility_kind(self, hasher)
79     }
80 }