11
11
use hir;
12
12
use hir:: def_id:: { DefId , DefIndex } ;
13
13
use hir:: map:: DefPathHash ;
14
+ use hir:: map:: definitions:: Definitions ;
14
15
use ich:: { self , CachingCodemapView } ;
16
+ use middle:: cstore:: CrateStore ;
15
17
use session:: config:: DebugInfoLevel :: NoDebugInfo ;
16
18
use ty:: { self , TyCtxt , fast_reject} ;
17
19
use session:: Session ;
@@ -41,8 +43,10 @@ thread_local!(static IGNORED_ATTR_NAMES: RefCell<FxHashSet<Symbol>> =
41
43
/// enough information to transform DefIds and HirIds into stable DefPaths (i.e.
42
44
/// a reference to the TyCtxt) and it holds a few caches for speeding up various
43
45
/// 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 ,
46
50
body_resolver : BodyResolver < ' gcx > ,
47
51
hash_spans : bool ,
48
52
hash_bodies : bool ,
@@ -65,21 +69,27 @@ pub enum NodeIdHashingMode {
65
69
/// We could also just store a plain reference to the hir::Crate but we want
66
70
/// to avoid that the crate is used to get untracked access to all of the HIR.
67
71
#[ derive( Clone , Copy ) ]
68
- struct BodyResolver < ' hir > ( & ' hir hir:: Crate ) ;
72
+ struct BodyResolver < ' gcx > ( & ' gcx hir:: Crate ) ;
69
73
70
- impl < ' hir > BodyResolver < ' hir > {
74
+ impl < ' gcx > BodyResolver < ' gcx > {
71
75
// Return a reference to the hir::Body with the given BodyId.
72
76
// 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 {
74
78
self . 0 . body ( id)
75
79
}
76
80
}
77
81
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 ( ) ;
83
93
84
94
debug_assert ! ( ich:: IGNORED_ATTRIBUTES . len( ) > 0 ) ;
85
95
IGNORED_ATTR_NAMES . with ( |names| {
@@ -90,13 +100,13 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
90
100
}
91
101
} ) ;
92
102
93
- let body_resolver = BodyResolver ( tcx. dep_graph . with_ignore ( || tcx. hir . krate ( ) ) ) ;
94
-
95
103
StableHashingContext {
96
- tcx,
97
- body_resolver,
104
+ sess,
105
+ body_resolver : BodyResolver ( krate) ,
106
+ definitions,
107
+ cstore,
98
108
caching_codemap : None ,
99
- raw_codemap : tcx . sess . codemap ( ) ,
109
+ raw_codemap : sess. codemap ( ) ,
100
110
hash_spans : hash_spans_initial,
101
111
hash_bodies : true ,
102
112
overflow_checks_enabled : check_overflow_initial,
@@ -106,7 +116,7 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
106
116
107
117
#[ inline]
108
118
pub fn sess ( & self ) -> & ' gcx Session {
109
- self . tcx . sess
119
+ self . sess
110
120
}
111
121
112
122
pub fn force_span_hashing ( mut self ) -> Self {
@@ -146,12 +156,16 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
146
156
147
157
#[ inline]
148
158
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
+ }
150
164
}
151
165
152
166
#[ inline]
153
167
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)
155
169
}
156
170
157
171
#[ inline]
@@ -239,26 +253,26 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
239
253
}
240
254
241
255
impl < ' a , ' gcx , ' lcx > StableHashingContextProvider for ty:: TyCtxt < ' a , ' gcx , ' lcx > {
242
- type ContextType = StableHashingContext < ' a , ' gcx , ' lcx > ;
256
+ type ContextType = StableHashingContext < ' gcx > ;
243
257
fn create_stable_hashing_context ( & self ) -> Self :: ContextType {
244
- StableHashingContext :: new ( * self )
258
+ ( * self ) . create_stable_hashing_context ( )
245
259
}
246
260
}
247
261
248
- impl < ' a , ' gcx , ' tcx > HashStable < StableHashingContext < ' a , ' gcx , ' tcx > > for hir:: BodyId {
262
+ impl < ' gcx > HashStable < StableHashingContext < ' gcx > > for hir:: BodyId {
249
263
fn hash_stable < W : StableHasherResult > ( & self ,
250
- hcx : & mut StableHashingContext < ' a , ' gcx , ' tcx > ,
264
+ hcx : & mut StableHashingContext < ' gcx > ,
251
265
hasher : & mut StableHasher < W > ) {
252
266
if hcx. hash_bodies ( ) {
253
267
hcx. body_resolver . body ( * self ) . hash_stable ( hcx, hasher) ;
254
268
}
255
269
}
256
270
}
257
271
258
- impl < ' a , ' gcx , ' tcx > HashStable < StableHashingContext < ' a , ' gcx , ' tcx > > for hir:: HirId {
272
+ impl < ' gcx > HashStable < StableHashingContext < ' gcx > > for hir:: HirId {
259
273
#[ inline]
260
274
fn hash_stable < W : StableHasherResult > ( & self ,
261
- hcx : & mut StableHashingContext < ' a , ' gcx , ' tcx > ,
275
+ hcx : & mut StableHashingContext < ' gcx > ,
262
276
hasher : & mut StableHasher < W > ) {
263
277
match hcx. node_id_hashing_mode {
264
278
NodeIdHashingMode :: Ignore => {
@@ -270,52 +284,52 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for hir::H
270
284
local_id,
271
285
} = * self ;
272
286
273
- hcx. tcx . hir . definitions ( ) . def_path_hash ( owner) . hash_stable ( hcx, hasher) ;
287
+ hcx. local_def_path_hash ( owner) . hash_stable ( hcx, hasher) ;
274
288
local_id. hash_stable ( hcx, hasher) ;
275
289
}
276
290
}
277
291
}
278
292
}
279
293
280
- impl < ' a , ' gcx , ' tcx > ToStableHashKey < StableHashingContext < ' a , ' gcx , ' tcx > > for hir:: HirId {
294
+ impl < ' gcx > ToStableHashKey < StableHashingContext < ' gcx > > for hir:: HirId {
281
295
type KeyType = ( DefPathHash , hir:: ItemLocalId ) ;
282
296
283
297
#[ inline]
284
298
fn to_stable_hash_key ( & self ,
285
- hcx : & StableHashingContext < ' a , ' gcx , ' tcx > )
299
+ hcx : & StableHashingContext < ' gcx > )
286
300
-> ( DefPathHash , hir:: ItemLocalId ) {
287
301
let def_path_hash = hcx. local_def_path_hash ( self . owner ) ;
288
302
( def_path_hash, self . local_id )
289
303
}
290
304
}
291
305
292
- impl < ' a , ' gcx , ' tcx > HashStable < StableHashingContext < ' a , ' gcx , ' tcx > > for ast:: NodeId {
306
+ impl < ' gcx > HashStable < StableHashingContext < ' gcx > > for ast:: NodeId {
293
307
fn hash_stable < W : StableHasherResult > ( & self ,
294
- hcx : & mut StableHashingContext < ' a , ' gcx , ' tcx > ,
308
+ hcx : & mut StableHashingContext < ' gcx > ,
295
309
hasher : & mut StableHasher < W > ) {
296
310
match hcx. node_id_hashing_mode {
297
311
NodeIdHashingMode :: Ignore => {
298
312
// Don't do anything.
299
313
}
300
314
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) ;
302
316
}
303
317
}
304
318
}
305
319
}
306
320
307
- impl < ' a , ' gcx , ' tcx > ToStableHashKey < StableHashingContext < ' a , ' gcx , ' tcx > > for ast:: NodeId {
321
+ impl < ' gcx > ToStableHashKey < StableHashingContext < ' gcx > > for ast:: NodeId {
308
322
type KeyType = ( DefPathHash , hir:: ItemLocalId ) ;
309
323
310
324
#[ inline]
311
325
fn to_stable_hash_key ( & self ,
312
- hcx : & StableHashingContext < ' a , ' gcx , ' tcx > )
326
+ hcx : & StableHashingContext < ' gcx > )
313
327
-> ( 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)
315
329
}
316
330
}
317
331
318
- impl < ' a , ' gcx , ' tcx > HashStable < StableHashingContext < ' a , ' gcx , ' tcx > > for Span {
332
+ impl < ' gcx > HashStable < StableHashingContext < ' gcx > > for Span {
319
333
320
334
// Hash a span in a stable way. We can't directly hash the span's BytePos
321
335
// 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 {
327
341
// Also, hashing filenames is expensive so we avoid doing it twice when the
328
342
// span starts and ends in the same file, which is almost always the case.
329
343
fn hash_stable < W : StableHasherResult > ( & self ,
330
- hcx : & mut StableHashingContext < ' a , ' gcx , ' tcx > ,
344
+ hcx : & mut StableHashingContext < ' gcx > ,
331
345
hasher : & mut StableHasher < W > ) {
332
346
use syntax_pos:: Pos ;
333
347
@@ -390,8 +404,8 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Span {
390
404
}
391
405
}
392
406
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 > ,
395
409
hasher : & mut StableHasher < W > ,
396
410
blanket_impls : & Vec < DefId > ,
397
411
non_blanket_impls : & HashMap < fast_reject:: SimplifiedType , Vec < DefId > , R > )
0 commit comments