]>
Commit | Line | Data |
---|---|---|
cc61c64b | 1 | //! This module contains `HashStable` implementations for various data types |
ba9703b0 | 2 | //! from `rustc_middle::ty` in no particular order. |
cc61c64b | 3 | |
dfeec247 XL |
4 | use crate::middle::region; |
5 | use crate::mir; | |
6 | use crate::ty; | |
ba9703b0 | 7 | use rustc_data_structures::fingerprint::Fingerprint; |
2c00a5a8 | 8 | use rustc_data_structures::fx::FxHashMap; |
5099ac24 | 9 | use rustc_data_structures::stable_hasher::HashingControls; |
dfeec247 | 10 | use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey}; |
c295e0f8 | 11 | use rustc_query_system::ich::StableHashingContext; |
2c00a5a8 | 12 | use std::cell::RefCell; |
e8be2606 | 13 | use std::ptr; |
31ef2f64 | 14 | use tracing::trace; |
cc61c64b | 15 | |
e8be2606 | 16 | impl<'a, 'tcx, H, T> HashStable<StableHashingContext<'a>> for &'tcx ty::list::RawList<H, T> |
dc9dc135 XL |
17 | where |
18 | T: HashStable<StableHashingContext<'a>>, | |
19 | { | |
e74abb32 | 20 | fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { |
2c00a5a8 | 21 | thread_local! { |
e8be2606 | 22 | static CACHE: RefCell<FxHashMap<(*const (), HashingControls), Fingerprint>> = |
0bf4aa26 | 23 | RefCell::new(Default::default()); |
2c00a5a8 XL |
24 | } |
25 | ||
26 | let hash = CACHE.with(|cache| { | |
e8be2606 | 27 | let key = (ptr::from_ref(*self).cast::<()>(), hcx.hashing_controls()); |
2c00a5a8 XL |
28 | if let Some(&hash) = cache.borrow().get(&key) { |
29 | return hash; | |
30 | } | |
31 | ||
32 | let mut hasher = StableHasher::new(); | |
4b012472 | 33 | self[..].hash_stable(hcx, &mut hasher); |
2c00a5a8 XL |
34 | |
35 | let hash: Fingerprint = hasher.finish(); | |
36 | cache.borrow_mut().insert(key, hash); | |
37 | hash | |
38 | }); | |
39 | ||
40 | hash.hash_stable(hcx, hasher); | |
cc61c64b XL |
41 | } |
42 | } | |
43 | ||
e8be2606 | 44 | impl<'a, 'tcx, H, T> ToStableHashKey<StableHashingContext<'a>> for &'tcx ty::list::RawList<H, T> |
dc9dc135 XL |
45 | where |
46 | T: HashStable<StableHashingContext<'a>>, | |
83c7162d XL |
47 | { |
48 | type KeyType = Fingerprint; | |
49 | ||
50 | #[inline] | |
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); | |
55 | hasher.finish() | |
56 | } | |
57 | } | |
58 | ||
add651ee | 59 | impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::GenericArg<'tcx> { |
e74abb32 | 60 | fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { |
0531ce1d XL |
61 | self.unpack().hash_stable(hcx, hasher); |
62 | } | |
63 | } | |
64 | ||
a1dfa0c6 | 65 | // AllocIds get resolved to whatever they point to (to be stable) |
0531ce1d | 66 | impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId { |
e74abb32 | 67 | fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { |
0531ce1d XL |
68 | ty::tls::with_opt(|tcx| { |
69 | trace!("hashing {:?}", *self); | |
70 | let tcx = tcx.expect("can't hash AllocIds during hir lowering"); | |
064997fb | 71 | tcx.try_get_global_alloc(*self).hash_stable(hcx, hasher); |
0531ce1d XL |
72 | }); |
73 | } | |
74 | } | |
75 | ||
4b012472 FG |
76 | // CtfeProvenance is an AllocId and a bool. |
77 | impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::CtfeProvenance { | |
78 | fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { | |
79 | self.alloc_id().hash_stable(hcx, hasher); | |
80 | self.immutable().hash_stable(hcx, hasher); | |
81 | } | |
82 | } | |
83 | ||
0531ce1d | 84 | impl<'a> ToStableHashKey<StableHashingContext<'a>> for region::Scope { |
ea8adc8c XL |
85 | type KeyType = region::Scope; |
86 | ||
87 | #[inline] | |
0531ce1d | 88 | fn to_stable_hash_key(&self, _: &StableHashingContext<'a>) -> region::Scope { |
ea8adc8c | 89 | *self |
cc61c64b XL |
90 | } |
91 | } |