1 //! This module contains `HashStable` implementations for various data types
2 //! from rustc::ty in no particular order.
4 use crate::ich::{Fingerprint, StableHashingContext, NodeIdHashingMode};
5 use rustc_data_structures::fx::FxHashMap;
6 use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
7 StableHasher, StableHasherResult};
8 use std::cell::RefCell;
10 use crate::middle::region;
14 impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for &'tcx ty::List<T>
16 T: HashStable<StableHashingContext<'a>>,
18 fn hash_stable<W: StableHasherResult>(&self,
19 hcx: &mut StableHashingContext<'a>,
20 hasher: &mut StableHasher<W>) {
22 static CACHE: RefCell<FxHashMap<(usize, usize), Fingerprint>> =
23 RefCell::new(Default::default());
26 let hash = CACHE.with(|cache| {
27 let key = (self.as_ptr() as usize, self.len());
28 if let Some(&hash) = cache.borrow().get(&key) {
32 let mut hasher = StableHasher::new();
33 (&self[..]).hash_stable(hcx, &mut hasher);
35 let hash: Fingerprint = hasher.finish();
36 cache.borrow_mut().insert(key, hash);
40 hash.hash_stable(hcx, hasher);
44 impl<'a, 'tcx, T> ToStableHashKey<StableHashingContext<'a>> for &'tcx ty::List<T>
46 T: HashStable<StableHashingContext<'a>>,
48 type KeyType = Fingerprint;
51 fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> Fingerprint {
52 let mut hasher = StableHasher::new();
53 let mut hcx: StableHashingContext<'a> = hcx.clone();
54 self.hash_stable(&mut hcx, &mut hasher);
59 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::subst::GenericArg<'tcx> {
60 fn hash_stable<W: StableHasherResult>(&self,
61 hcx: &mut StableHashingContext<'a>,
62 hasher: &mut StableHasher<W>) {
63 self.unpack().hash_stable(hcx, hasher);
67 impl<'a> HashStable<StableHashingContext<'a>>
69 fn hash_stable<W: StableHasherResult>(&self,
70 hcx: &mut StableHashingContext<'a>,
71 hasher: &mut StableHasher<W>) {
72 mem::discriminant(self).hash_stable(hcx, hasher);
77 // No variant fields to hash for these ...
79 ty::ReLateBound(db, ty::BrAnon(i)) => {
80 db.hash_stable(hcx, hasher);
81 i.hash_stable(hcx, hasher);
83 ty::ReLateBound(db, ty::BrNamed(def_id, name)) => {
84 db.hash_stable(hcx, hasher);
85 def_id.hash_stable(hcx, hasher);
86 name.hash_stable(hcx, hasher);
88 ty::ReLateBound(db, ty::BrEnv) => {
89 db.hash_stable(hcx, hasher);
91 ty::ReEarlyBound(ty::EarlyBoundRegion { def_id, index, name }) => {
92 def_id.hash_stable(hcx, hasher);
93 index.hash_stable(hcx, hasher);
94 name.hash_stable(hcx, hasher);
96 ty::ReScope(scope) => {
97 scope.hash_stable(hcx, hasher);
99 ty::ReFree(ref free_region) => {
100 free_region.hash_stable(hcx, hasher);
102 ty::ReClosureBound(vid) => {
103 vid.hash_stable(hcx, hasher);
106 ty::RePlaceholder(..) => {
107 bug!("StableHasher: unexpected region {:?}", *self)
113 impl<'a> HashStable<StableHashingContext<'a>> for ty::RegionVid {
115 fn hash_stable<W: StableHasherResult>(&self,
116 hcx: &mut StableHashingContext<'a>,
117 hasher: &mut StableHasher<W>) {
118 self.index().hash_stable(hcx, hasher);
122 impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::ConstVid<'tcx> {
124 fn hash_stable<W: StableHasherResult>(
126 hcx: &mut StableHashingContext<'a>,
127 hasher: &mut StableHasher<W>,
129 self.index.hash_stable(hcx, hasher);
133 impl<'tcx> HashStable<StableHashingContext<'tcx>> for ty::BoundVar {
135 fn hash_stable<W: StableHasherResult>(
137 hcx: &mut StableHashingContext<'tcx>,
138 hasher: &mut StableHasher<W>,
140 self.index().hash_stable(hcx, hasher);
144 impl<'a, T> HashStable<StableHashingContext<'a>> for ty::Binder<T>
146 T: HashStable<StableHashingContext<'a>>,
148 fn hash_stable<W: StableHasherResult>(&self,
149 hcx: &mut StableHashingContext<'a>,
150 hasher: &mut StableHasher<W>) {
151 self.skip_binder().hash_stable(hcx, hasher);
155 // AllocIds get resolved to whatever they point to (to be stable)
156 impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
157 fn hash_stable<W: StableHasherResult>(
159 hcx: &mut StableHashingContext<'a>,
160 hasher: &mut StableHasher<W>,
162 ty::tls::with_opt(|tcx| {
163 trace!("hashing {:?}", *self);
164 let tcx = tcx.expect("can't hash AllocIds during hir lowering");
165 let alloc_kind = tcx.alloc_map.lock().get(*self);
166 alloc_kind.hash_stable(hcx, hasher);
171 // `Relocations` with default type parameters is a sorted map.
172 impl<'a, Tag> HashStable<StableHashingContext<'a>>
173 for mir::interpret::Relocations<Tag>
175 Tag: HashStable<StableHashingContext<'a>>,
177 fn hash_stable<W: StableHasherResult>(
179 hcx: &mut StableHashingContext<'a>,
180 hasher: &mut StableHasher<W>,
182 self.len().hash_stable(hcx, hasher);
183 for reloc in self.iter() {
184 reloc.hash_stable(hcx, hasher);
189 impl_stable_hash_for!(enum ::syntax::ast::Mutability {
194 impl<'a> ToStableHashKey<StableHashingContext<'a>> for region::Scope {
195 type KeyType = region::Scope;
198 fn to_stable_hash_key(&self, _: &StableHashingContext<'a>) -> region::Scope {
203 impl<'a> HashStable<StableHashingContext<'a>> for ty::TyVid {
204 fn hash_stable<W: StableHasherResult>(&self,
205 _hcx: &mut StableHashingContext<'a>,
206 _hasher: &mut StableHasher<W>) {
207 // `TyVid` values are confined to an inference context and hence
208 // should not be hashed.
209 bug!("ty::TyKind::hash_stable() - can't hash a TyVid {:?}.", *self)
213 impl<'a> HashStable<StableHashingContext<'a>> for ty::IntVid {
214 fn hash_stable<W: StableHasherResult>(&self,
215 _hcx: &mut StableHashingContext<'a>,
216 _hasher: &mut StableHasher<W>) {
217 // `IntVid` values are confined to an inference context and hence
218 // should not be hashed.
219 bug!("ty::TyKind::hash_stable() - can't hash an IntVid {:?}.", *self)
223 impl<'a> HashStable<StableHashingContext<'a>> for ty::FloatVid {
224 fn hash_stable<W: StableHasherResult>(&self,
225 _hcx: &mut StableHashingContext<'a>,
226 _hasher: &mut StableHasher<W>) {
227 // `FloatVid` values are confined to an inference context and hence
228 // should not be hashed.
229 bug!("ty::TyKind::hash_stable() - can't hash a FloatVid {:?}.", *self)
233 impl<'a, T> HashStable<StableHashingContext<'a>> for ty::steal::Steal<T>
235 T: HashStable<StableHashingContext<'a>>,
237 fn hash_stable<W: StableHasherResult>(&self,
238 hcx: &mut StableHashingContext<'a>,
239 hasher: &mut StableHasher<W>) {
240 self.borrow().hash_stable(hcx, hasher);
244 impl<'a> HashStable<StableHashingContext<'a>>
245 for crate::middle::privacy::AccessLevels {
246 fn hash_stable<W: StableHasherResult>(&self,
247 hcx: &mut StableHashingContext<'a>,
248 hasher: &mut StableHasher<W>) {
249 hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
250 let crate::middle::privacy::AccessLevels {
254 map.hash_stable(hcx, hasher);