1111use hir;
1212use hir:: def_id:: { DefId , DefIndex } ;
1313use hir:: map:: DefPathHash ;
14+ use hir:: map:: definitions:: Definitions ;
1415use ich:: { self , CachingCodemapView } ;
16+ use middle:: cstore:: CrateStore ;
1517use session:: config:: DebugInfoLevel :: NoDebugInfo ;
1618use ty:: { self , TyCtxt , fast_reject} ;
1719use session:: Session ;
@@ -41,8 +43,10 @@ thread_local!(static IGNORED_ATTR_NAMES: RefCell<FxHashSet<Symbol>> =
4143/// enough information to transform DefIds and HirIds into stable DefPaths (i.e.
4244/// a reference to the TyCtxt) and it holds a few caches for speeding up various
4345/// things (e.g. each DefId/DefPath is only hashed once).
44- pub struct StableHashingContext < ' a , ' gcx : ' a +' tcx , ' tcx : ' a > {
45- tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
46+ pub struct StableHashingContext < ' gcx > {
47+ sess : & ' gcx Session ,
48+ definitions : & ' gcx Definitions ,
49+ cstore : & ' gcx CrateStore ,
4650 body_resolver : BodyResolver < ' gcx > ,
4751 hash_spans : bool ,
4852 hash_bodies : bool ,
@@ -65,21 +69,27 @@ pub enum NodeIdHashingMode {
6569/// We could also just store a plain reference to the hir::Crate but we want
6670/// to avoid that the crate is used to get untracked access to all of the HIR.
6771#[ derive( Clone , Copy ) ]
68- struct BodyResolver < ' hir > ( & ' hir hir:: Crate ) ;
72+ struct BodyResolver < ' gcx > ( & ' gcx hir:: Crate ) ;
6973
70- impl < ' hir > BodyResolver < ' hir > {
74+ impl < ' gcx > BodyResolver < ' gcx > {
7175 // Return a reference to the hir::Body with the given BodyId.
7276 // DOES NOT DO ANY TRACKING, use carefully.
73- fn body ( self , id : hir:: BodyId ) -> & ' hir hir:: Body {
77+ fn body ( self , id : hir:: BodyId ) -> & ' gcx hir:: Body {
7478 self . 0 . body ( id)
7579 }
7680}
7781
78- impl < ' a , ' gcx , ' tcx > StableHashingContext < ' a , ' gcx , ' tcx > {
79-
80- pub fn new ( tcx : TyCtxt < ' a , ' gcx , ' tcx > ) -> Self {
81- let hash_spans_initial = tcx. sess . opts . debuginfo != NoDebugInfo ;
82- let check_overflow_initial = tcx. sess . overflow_checks ( ) ;
82+ impl < ' gcx > StableHashingContext < ' gcx > {
83+ // The `krate` here is only used for mapping BodyIds to Bodies.
84+ // Don't use it for anything else or you'll run the risk of
85+ // leaking data out of the tracking system.
86+ pub fn new ( sess : & ' gcx Session ,
87+ krate : & ' gcx hir:: Crate ,
88+ definitions : & ' gcx Definitions ,
89+ cstore : & ' gcx CrateStore )
90+ -> Self {
91+ let hash_spans_initial = sess. opts . debuginfo != NoDebugInfo ;
92+ let check_overflow_initial = sess. overflow_checks ( ) ;
8393
8494 debug_assert ! ( ich:: IGNORED_ATTRIBUTES . len( ) > 0 ) ;
8595 IGNORED_ATTR_NAMES . with ( |names| {
@@ -90,13 +100,13 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
90100 }
91101 } ) ;
92102
93- let body_resolver = BodyResolver ( tcx. dep_graph . with_ignore ( || tcx. hir . krate ( ) ) ) ;
94-
95103 StableHashingContext {
96- tcx,
97- body_resolver,
104+ sess,
105+ body_resolver : BodyResolver ( krate) ,
106+ definitions,
107+ cstore,
98108 caching_codemap : None ,
99- raw_codemap : tcx . sess . codemap ( ) ,
109+ raw_codemap : sess. codemap ( ) ,
100110 hash_spans : hash_spans_initial,
101111 hash_bodies : true ,
102112 overflow_checks_enabled : check_overflow_initial,
@@ -106,7 +116,7 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
106116
107117 #[ inline]
108118 pub fn sess ( & self ) -> & ' gcx Session {
109- self . tcx . sess
119+ self . sess
110120 }
111121
112122 pub fn force_span_hashing ( mut self ) -> Self {
@@ -146,12 +156,16 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
146156
147157 #[ inline]
148158 pub fn def_path_hash ( & self , def_id : DefId ) -> DefPathHash {
149- self . tcx . def_path_hash ( def_id)
159+ if def_id. is_local ( ) {
160+ self . definitions . def_path_hash ( def_id. index )
161+ } else {
162+ self . cstore . def_path_hash ( def_id)
163+ }
150164 }
151165
152166 #[ inline]
153167 pub fn local_def_path_hash ( & self , def_index : DefIndex ) -> DefPathHash {
154- self . tcx . hir . definitions ( ) . def_path_hash ( def_index)
168+ self . definitions . def_path_hash ( def_index)
155169 }
156170
157171 #[ inline]
@@ -239,26 +253,26 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
239253}
240254
241255impl < ' a , ' gcx , ' lcx > StableHashingContextProvider for ty:: TyCtxt < ' a , ' gcx , ' lcx > {
242- type ContextType = StableHashingContext < ' a , ' gcx , ' lcx > ;
256+ type ContextType = StableHashingContext < ' gcx > ;
243257 fn create_stable_hashing_context ( & self ) -> Self :: ContextType {
244- StableHashingContext :: new ( * self )
258+ ( * self ) . create_stable_hashing_context ( )
245259 }
246260}
247261
248- impl < ' a , ' gcx , ' tcx > HashStable < StableHashingContext < ' a , ' gcx , ' tcx > > for hir:: BodyId {
262+ impl < ' gcx > HashStable < StableHashingContext < ' gcx > > for hir:: BodyId {
249263 fn hash_stable < W : StableHasherResult > ( & self ,
250- hcx : & mut StableHashingContext < ' a , ' gcx , ' tcx > ,
264+ hcx : & mut StableHashingContext < ' gcx > ,
251265 hasher : & mut StableHasher < W > ) {
252266 if hcx. hash_bodies ( ) {
253267 hcx. body_resolver . body ( * self ) . hash_stable ( hcx, hasher) ;
254268 }
255269 }
256270}
257271
258- impl < ' a , ' gcx , ' tcx > HashStable < StableHashingContext < ' a , ' gcx , ' tcx > > for hir:: HirId {
272+ impl < ' gcx > HashStable < StableHashingContext < ' gcx > > for hir:: HirId {
259273 #[ inline]
260274 fn hash_stable < W : StableHasherResult > ( & self ,
261- hcx : & mut StableHashingContext < ' a , ' gcx , ' tcx > ,
275+ hcx : & mut StableHashingContext < ' gcx > ,
262276 hasher : & mut StableHasher < W > ) {
263277 match hcx. node_id_hashing_mode {
264278 NodeIdHashingMode :: Ignore => {
@@ -270,52 +284,52 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::H
270284 local_id,
271285 } = * self ;
272286
273- hcx. tcx . hir . definitions ( ) . def_path_hash ( owner) . hash_stable ( hcx, hasher) ;
287+ hcx. local_def_path_hash ( owner) . hash_stable ( hcx, hasher) ;
274288 local_id. hash_stable ( hcx, hasher) ;
275289 }
276290 }
277291 }
278292}
279293
280- impl < ' a , ' gcx , ' tcx > ToStableHashKey < StableHashingContext < ' a , ' gcx , ' tcx > > for hir:: HirId {
294+ impl < ' gcx > ToStableHashKey < StableHashingContext < ' gcx > > for hir:: HirId {
281295 type KeyType = ( DefPathHash , hir:: ItemLocalId ) ;
282296
283297 #[ inline]
284298 fn to_stable_hash_key ( & self ,
285- hcx : & StableHashingContext < ' a , ' gcx , ' tcx > )
299+ hcx : & StableHashingContext < ' gcx > )
286300 -> ( DefPathHash , hir:: ItemLocalId ) {
287301 let def_path_hash = hcx. local_def_path_hash ( self . owner ) ;
288302 ( def_path_hash, self . local_id )
289303 }
290304}
291305
292- impl < ' a , ' gcx , ' tcx > HashStable < StableHashingContext < ' a , ' gcx , ' tcx > > for ast:: NodeId {
306+ impl < ' gcx > HashStable < StableHashingContext < ' gcx > > for ast:: NodeId {
293307 fn hash_stable < W : StableHasherResult > ( & self ,
294- hcx : & mut StableHashingContext < ' a , ' gcx , ' tcx > ,
308+ hcx : & mut StableHashingContext < ' gcx > ,
295309 hasher : & mut StableHasher < W > ) {
296310 match hcx. node_id_hashing_mode {
297311 NodeIdHashingMode :: Ignore => {
298312 // Don't do anything.
299313 }
300314 NodeIdHashingMode :: HashDefPath => {
301- hcx. tcx . hir . node_to_hir_id ( * self ) . hash_stable ( hcx, hasher) ;
315+ hcx. definitions . node_to_hir_id ( * self ) . hash_stable ( hcx, hasher) ;
302316 }
303317 }
304318 }
305319}
306320
307- impl < ' a , ' gcx , ' tcx > ToStableHashKey < StableHashingContext < ' a , ' gcx , ' tcx > > for ast:: NodeId {
321+ impl < ' gcx > ToStableHashKey < StableHashingContext < ' gcx > > for ast:: NodeId {
308322 type KeyType = ( DefPathHash , hir:: ItemLocalId ) ;
309323
310324 #[ inline]
311325 fn to_stable_hash_key ( & self ,
312- hcx : & StableHashingContext < ' a , ' gcx , ' tcx > )
326+ hcx : & StableHashingContext < ' gcx > )
313327 -> ( DefPathHash , hir:: ItemLocalId ) {
314- hcx. tcx . hir . node_to_hir_id ( * self ) . to_stable_hash_key ( hcx)
328+ hcx. definitions . node_to_hir_id ( * self ) . to_stable_hash_key ( hcx)
315329 }
316330}
317331
318- impl < ' a , ' gcx , ' tcx > HashStable < StableHashingContext < ' a , ' gcx , ' tcx > > for Span {
332+ impl < ' gcx > HashStable < StableHashingContext < ' gcx > > for Span {
319333
320334 // Hash a span in a stable way. We can't directly hash the span's BytePos
321335 // fields (that would be similar to hashing pointers, since those are just
@@ -327,7 +341,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Span {
327341 // Also, hashing filenames is expensive so we avoid doing it twice when the
328342 // span starts and ends in the same file, which is almost always the case.
329343 fn hash_stable < W : StableHasherResult > ( & self ,
330- hcx : & mut StableHashingContext < ' a , ' gcx , ' tcx > ,
344+ hcx : & mut StableHashingContext < ' gcx > ,
331345 hasher : & mut StableHasher < W > ) {
332346 use syntax_pos:: Pos ;
333347
@@ -390,8 +404,8 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Span {
390404 }
391405}
392406
393- pub fn hash_stable_trait_impls < ' a , ' tcx , ' gcx , W , R > (
394- hcx : & mut StableHashingContext < ' a , ' gcx , ' tcx > ,
407+ pub fn hash_stable_trait_impls < ' gcx , W , R > (
408+ hcx : & mut StableHashingContext < ' gcx > ,
395409 hasher : & mut StableHasher < W > ,
396410 blanket_impls : & Vec < DefId > ,
397411 non_blanket_impls : & HashMap < fast_reject:: SimplifiedType , Vec < DefId > , R > )
0 commit comments