1 use crate::hir::map::definitions::Definitions;
2 use crate::hir::map::DefPathHash;
3 use crate::ich::{self, CachingSourceMapView};
4 use crate::middle::cstore::CrateStore;
5 use crate::session::Session;
6 use crate::ty::{fast_reject, TyCtxt};
8 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9 use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
10 use rustc_data_structures::sync::Lrc;
12 use rustc_hir::def_id::{DefId, DefIndex};
13 use rustc_span::source_map::SourceMap;
14 use rustc_span::symbol::Symbol;
15 use rustc_span::{BytePos, SourceFile};
18 use smallvec::SmallVec;
21 fn compute_ignored_attr_names() -> FxHashSet<Symbol> {
22 debug_assert!(ich::IGNORED_ATTRIBUTES.len() > 0);
23 ich::IGNORED_ATTRIBUTES.iter().map(|&s| s).collect()
26 /// This is the context state available during incr. comp. hashing. It contains
27 /// enough information to transform `DefId`s and `HirId`s into stable `DefPath`s (i.e.,
28 /// a reference to the `TyCtxt`) and it holds a few caches for speeding up various
29 /// things (e.g., each `DefId`/`DefPath` is only hashed once).
31 pub struct StableHashingContext<'a> {
33 definitions: &'a Definitions,
34 cstore: &'a dyn CrateStore,
35 pub(super) body_resolver: BodyResolver<'a>,
38 pub(super) node_id_hashing_mode: NodeIdHashingMode,
40 // Very often, we are hashing something that does not need the
41 // `CachingSourceMapView`, so we initialize it lazily.
42 raw_source_map: &'a SourceMap,
43 caching_source_map: Option<CachingSourceMapView<'a>>,
46 #[derive(PartialEq, Eq, Clone, Copy)]
47 pub enum NodeIdHashingMode {
52 /// The `BodyResolver` allows mapping a `BodyId` to the corresponding `hir::Body`.
53 /// We could also just store a plain reference to the `hir::Crate` but we want
54 /// to avoid that the crate is used to get untracked access to all of the HIR.
55 #[derive(Clone, Copy)]
56 pub(super) struct BodyResolver<'tcx>(&'tcx hir::Crate<'tcx>);
58 impl<'tcx> BodyResolver<'tcx> {
59 /// Returns a reference to the `hir::Body` with the given `BodyId`.
60 /// **Does not do any tracking**; use carefully.
61 pub(super) fn body(self, id: hir::BodyId) -> &'tcx hir::Body<'tcx> {
66 impl<'a> StableHashingContext<'a> {
67 /// The `krate` here is only used for mapping `BodyId`s to `Body`s.
68 /// Don't use it for anything else or you'll run the risk of
69 /// leaking data out of the tracking system.
73 krate: &'a hir::Crate<'a>,
74 definitions: &'a Definitions,
75 cstore: &'a dyn CrateStore,
77 let hash_spans_initial = !sess.opts.debugging_opts.incremental_ignore_spans;
79 StableHashingContext {
81 body_resolver: BodyResolver(krate),
84 caching_source_map: None,
85 raw_source_map: sess.source_map(),
86 hash_spans: hash_spans_initial,
88 node_id_hashing_mode: NodeIdHashingMode::HashDefPath,
93 pub fn sess(&self) -> &'a Session {
98 pub fn while_hashing_hir_bodies<F: FnOnce(&mut Self)>(&mut self, hash_bodies: bool, f: F) {
99 let prev_hash_bodies = self.hash_bodies;
100 self.hash_bodies = hash_bodies;
102 self.hash_bodies = prev_hash_bodies;
106 pub fn while_hashing_spans<F: FnOnce(&mut Self)>(&mut self, hash_spans: bool, f: F) {
107 let prev_hash_spans = self.hash_spans;
108 self.hash_spans = hash_spans;
110 self.hash_spans = prev_hash_spans;
114 pub fn with_node_id_hashing_mode<F: FnOnce(&mut Self)>(
116 mode: NodeIdHashingMode,
119 let prev = self.node_id_hashing_mode;
120 self.node_id_hashing_mode = mode;
122 self.node_id_hashing_mode = prev;
126 pub fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
127 if def_id.is_local() {
128 self.definitions.def_path_hash(def_id.index)
130 self.cstore.def_path_hash(def_id)
135 pub fn local_def_path_hash(&self, def_index: DefIndex) -> DefPathHash {
136 self.definitions.def_path_hash(def_index)
140 pub fn node_to_hir_id(&self, node_id: ast::NodeId) -> hir::HirId {
141 self.definitions.node_to_hir_id(node_id)
145 pub fn hash_bodies(&self) -> bool {
150 pub fn source_map(&mut self) -> &mut CachingSourceMapView<'a> {
151 match self.caching_source_map {
152 Some(ref mut cm) => cm,
154 *none = Some(CachingSourceMapView::new(self.raw_source_map));
155 none.as_mut().unwrap()
161 pub fn is_ignored_attr(&self, name: Symbol) -> bool {
163 static IGNORED_ATTRIBUTES: FxHashSet<Symbol> = compute_ignored_attr_names();
165 IGNORED_ATTRIBUTES.with(|attrs| attrs.contains(&name))
168 pub fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F) {
169 let prev_hash_node_ids = self.node_id_hashing_mode;
170 self.node_id_hashing_mode = NodeIdHashingMode::Ignore;
174 self.node_id_hashing_mode = prev_hash_node_ids;
178 /// Something that can provide a stable hashing context.
179 pub trait StableHashingContextProvider<'a> {
180 fn get_stable_hashing_context(&self) -> StableHashingContext<'a>;
183 impl<'a, 'b, T: StableHashingContextProvider<'a>> StableHashingContextProvider<'a> for &'b T {
184 fn get_stable_hashing_context(&self) -> StableHashingContext<'a> {
185 (**self).get_stable_hashing_context()
189 impl<'a, 'b, T: StableHashingContextProvider<'a>> StableHashingContextProvider<'a> for &'b mut T {
190 fn get_stable_hashing_context(&self) -> StableHashingContext<'a> {
191 (**self).get_stable_hashing_context()
195 impl StableHashingContextProvider<'tcx> for TyCtxt<'tcx> {
196 fn get_stable_hashing_context(&self) -> StableHashingContext<'tcx> {
197 (*self).create_stable_hashing_context()
201 impl<'a> StableHashingContextProvider<'a> for StableHashingContext<'a> {
202 fn get_stable_hashing_context(&self) -> StableHashingContext<'a> {
207 impl<'a> crate::dep_graph::DepGraphSafe for StableHashingContext<'a> {}
209 impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::HirId {
210 type KeyType = (DefPathHash, hir::ItemLocalId);
213 fn to_stable_hash_key(
215 hcx: &StableHashingContext<'a>,
216 ) -> (DefPathHash, hir::ItemLocalId) {
217 let def_path_hash = hcx.local_def_path_hash(self.owner);
218 (def_path_hash, self.local_id)
222 impl<'a> HashStable<StableHashingContext<'a>> for ast::NodeId {
223 fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
224 match hcx.node_id_hashing_mode {
225 NodeIdHashingMode::Ignore => {
226 // Don't do anything.
228 NodeIdHashingMode::HashDefPath => {
229 hcx.definitions.node_to_hir_id(*self).hash_stable(hcx, hasher);
235 impl<'a> ToStableHashKey<StableHashingContext<'a>> for ast::NodeId {
236 type KeyType = (DefPathHash, hir::ItemLocalId);
239 fn to_stable_hash_key(
241 hcx: &StableHashingContext<'a>,
242 ) -> (DefPathHash, hir::ItemLocalId) {
243 hcx.definitions.node_to_hir_id(*self).to_stable_hash_key(hcx)
247 impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
248 fn hash_spans(&self) -> bool {
252 fn byte_pos_to_line_and_col(
255 ) -> Option<(Lrc<SourceFile>, usize, BytePos)> {
256 self.source_map().byte_pos_to_line_and_col(byte)
260 pub fn hash_stable_trait_impls<'a>(
261 hcx: &mut StableHashingContext<'a>,
262 hasher: &mut StableHasher,
263 blanket_impls: &[DefId],
264 non_blanket_impls: &FxHashMap<fast_reject::SimplifiedType, Vec<DefId>>,
267 let mut blanket_impls: SmallVec<[_; 8]> =
268 blanket_impls.iter().map(|&def_id| hcx.def_path_hash(def_id)).collect();
270 if blanket_impls.len() > 1 {
271 blanket_impls.sort_unstable();
274 blanket_impls.hash_stable(hcx, hasher);
278 let mut keys: SmallVec<[_; 8]> =
279 non_blanket_impls.keys().map(|k| (k, k.map_def(|d| hcx.def_path_hash(d)))).collect();
280 keys.sort_unstable_by(|&(_, ref k1), &(_, ref k2)| k1.cmp(k2));
281 keys.len().hash_stable(hcx, hasher);
282 for (key, ref stable_key) in keys {
283 stable_key.hash_stable(hcx, hasher);
284 let mut impls: SmallVec<[_; 8]> =
285 non_blanket_impls[key].iter().map(|&impl_id| hcx.def_path_hash(impl_id)).collect();
288 impls.sort_unstable();
291 impls.hash_stable(hcx, hasher);