]>
Commit | Line | Data |
---|---|---|
cc61c64b XL |
1 | //! This module contains `HashStable` implementations for various HIR data |
2 | //! types in no particular order. | |
3 | ||
3c0e092e | 4 | use crate::ich::hcx::BodyResolver; |
ba9703b0 | 5 | use crate::ich::{NodeIdHashingMode, StableHashingContext}; |
a2a8927a | 6 | use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; |
dfeec247 | 7 | use rustc_hir as hir; |
cc61c64b | 8 | use std::mem; |
cc61c64b | 9 | |
dfeec247 | 10 | impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> { |
dfeec247 XL |
11 | #[inline] |
12 | fn hash_hir_id(&mut self, hir_id: hir::HirId, hasher: &mut StableHasher) { | |
13 | let hcx = self; | |
5099ac24 | 14 | match hcx.hashing_controls.node_id_hashing_mode { |
dfeec247 XL |
15 | NodeIdHashingMode::Ignore => { |
16 | // Don't do anything. | |
17 | } | |
18 | NodeIdHashingMode::HashDefPath => { | |
19 | let hir::HirId { owner, local_id } = hir_id; | |
20 | ||
21 | hcx.local_def_path_hash(owner).hash_stable(hcx, hasher); | |
22 | local_id.hash_stable(hcx, hasher); | |
23 | } | |
24 | } | |
25 | } | |
26 | ||
c295e0f8 | 27 | #[inline] |
dfeec247 XL |
28 | fn hash_body_id(&mut self, id: hir::BodyId, hasher: &mut StableHasher) { |
29 | let hcx = self; | |
3c0e092e XL |
30 | match hcx.body_resolver { |
31 | BodyResolver::Forbidden => panic!("Hashing HIR bodies is forbidden."), | |
32 | BodyResolver::Traverse { hash_bodies: false, .. } => {} | |
33 | BodyResolver::Traverse { hash_bodies: true, owner, bodies } => { | |
34 | assert_eq!(id.hir_id.owner, owner); | |
35 | bodies[&id.hir_id.local_id].hash_stable(hcx, hasher); | |
36 | } | |
dfeec247 XL |
37 | } |
38 | } | |
39 | ||
c295e0f8 | 40 | #[inline] |
74b04a01 | 41 | fn hash_reference_to_item(&mut self, id: hir::HirId, hasher: &mut StableHasher) { |
dfeec247 | 42 | let hcx = self; |
dfeec247 XL |
43 | |
44 | hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| { | |
45 | id.hash_stable(hcx, hasher); | |
46 | }) | |
47 | } | |
48 | ||
dfeec247 XL |
49 | fn hash_hir_expr(&mut self, expr: &hir::Expr<'_>, hasher: &mut StableHasher) { |
50 | self.while_hashing_hir_bodies(true, |hcx| { | |
6a06907d | 51 | let hir::Expr { hir_id: _, ref span, ref kind } = *expr; |
dfeec247 XL |
52 | |
53 | span.hash_stable(hcx, hasher); | |
54 | kind.hash_stable(hcx, hasher); | |
dfeec247 XL |
55 | }) |
56 | } | |
57 | ||
58 | fn hash_hir_ty(&mut self, ty: &hir::Ty<'_>, hasher: &mut StableHasher) { | |
59 | self.while_hashing_hir_bodies(true, |hcx| { | |
60 | let hir::Ty { hir_id: _, ref kind, ref span } = *ty; | |
61 | ||
62 | kind.hash_stable(hcx, hasher); | |
63 | span.hash_stable(hcx, hasher); | |
64 | }) | |
65 | } | |
66 | ||
67 | fn hash_hir_visibility_kind( | |
68 | &mut self, | |
69 | vis: &hir::VisibilityKind<'_>, | |
70 | hasher: &mut StableHasher, | |
71 | ) { | |
72 | let hcx = self; | |
73 | mem::discriminant(vis).hash_stable(hcx, hasher); | |
74 | match *vis { | |
75 | hir::VisibilityKind::Public | hir::VisibilityKind::Inherited => { | |
76 | // No fields to hash. | |
77 | } | |
78 | hir::VisibilityKind::Crate(sugar) => { | |
79 | sugar.hash_stable(hcx, hasher); | |
80 | } | |
81 | hir::VisibilityKind::Restricted { ref path, hir_id } => { | |
82 | hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| { | |
83 | hir_id.hash_stable(hcx, hasher); | |
84 | }); | |
85 | path.hash_stable(hcx, hasher); | |
86 | } | |
87 | } | |
cc61c64b | 88 | } |
ba9703b0 | 89 | |
c295e0f8 | 90 | #[inline] |
ba9703b0 | 91 | fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F) { |
5099ac24 FG |
92 | let prev_hash_node_ids = self.hashing_controls.node_id_hashing_mode; |
93 | self.hashing_controls.node_id_hashing_mode = NodeIdHashingMode::Ignore; | |
ba9703b0 XL |
94 | |
95 | f(self); | |
96 | ||
5099ac24 | 97 | self.hashing_controls.node_id_hashing_mode = prev_hash_node_ids; |
ba9703b0 | 98 | } |
3c0e092e XL |
99 | |
100 | #[inline] | |
101 | fn hash_hir_trait_candidate(&mut self, tc: &hir::TraitCandidate, hasher: &mut StableHasher) { | |
102 | self.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| { | |
103 | let hir::TraitCandidate { def_id, import_ids } = tc; | |
104 | ||
105 | def_id.hash_stable(hcx, hasher); | |
106 | import_ids.hash_stable(hcx, hasher); | |
107 | }); | |
108 | } | |
ea8adc8c XL |
109 | } |
110 | ||
dfeec247 | 111 | impl<'a> HashStable<StableHashingContext<'a>> for hir::Body<'_> { |
c295e0f8 | 112 | #[inline] |
e74abb32 | 113 | fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { |
dfeec247 | 114 | let hir::Body { params, value, generator_kind } = self; |
ea8adc8c XL |
115 | |
116 | hcx.with_node_id_hashing_mode(NodeIdHashingMode::Ignore, |hcx| { | |
e1599b0c | 117 | params.hash_stable(hcx, hasher); |
ea8adc8c | 118 | value.hash_stable(hcx, hasher); |
dc9dc135 | 119 | generator_kind.hash_stable(hcx, hasher); |
ea8adc8c XL |
120 | }); |
121 | } | |
122 | } |