@@ -69,6 +69,7 @@ use std::ops::Deref;
69
69
use std:: path:: PathBuf ;
70
70
use std:: sync:: Arc ;
71
71
72
+ use crate :: query:: erase:: { erase, restore, Erase } ;
72
73
pub ( crate ) use rustc_query_system:: query:: QueryJobId ;
73
74
use rustc_query_system:: query:: * ;
74
75
@@ -246,7 +247,30 @@ macro_rules! define_callbacks {
246
247
use super :: * ;
247
248
248
249
$(
249
- pub type $name<' tcx> = <<$( $K) * as Key >:: CacheSelector as CacheSelector <' tcx, $V>>:: Cache ;
250
+ pub type $name<' tcx> = <<$( $K) * as Key >:: CacheSelector as CacheSelector <' tcx, Erase <$V>>>:: Cache ;
251
+ ) *
252
+ }
253
+ #[ allow( nonstandard_style, unused_lifetimes) ]
254
+ pub mod query_cache {
255
+ $(
256
+ #[ derive( Default ) ]
257
+ pub struct $name<' tcx> {
258
+ _phantom_lt: std:: marker:: PhantomData <& ' tcx ( ) >,
259
+ _phantom_key: std:: marker:: PhantomData <super :: query_keys:: $name<' tcx>>,
260
+ _phantom_value: std:: marker:: PhantomData <super :: query_values:: $name<' tcx>>,
261
+ cache: super :: query_storage:: $name<' static >,
262
+ }
263
+
264
+ impl <' tcx> $name<' tcx> {
265
+ pub fn cache( & ' tcx self ) -> & ' tcx super :: query_storage:: $name<' tcx> {
266
+ unsafe {
267
+ std:: mem:: transmute:: <
268
+ & super :: query_storage:: $name<' _>,
269
+ & super :: query_storage:: $name<' _>,
270
+ >( & self . cache)
271
+ }
272
+ }
273
+ }
250
274
) *
251
275
}
252
276
@@ -270,7 +294,7 @@ macro_rules! define_callbacks {
270
294
271
295
#[ derive( Default ) ]
272
296
pub struct QueryCaches <' tcx> {
273
- $( $( #[ $attr] ) * pub $name: query_storage :: $name<' tcx>, ) *
297
+ $( $( #[ $attr] ) * pub $name: query_cache :: $name<' tcx>, ) *
274
298
}
275
299
276
300
impl <' tcx> TyCtxtEnsure <' tcx> {
@@ -280,7 +304,7 @@ macro_rules! define_callbacks {
280
304
let key = key. into_query_param( ) ;
281
305
opt_remap_env_constness!( [ $( $modifiers) * ] [ key] ) ;
282
306
283
- match try_get_cached( self . tcx, & self . tcx. query_system. caches. $name, & key) {
307
+ match try_get_cached( self . tcx, self . tcx. query_system. caches. $name. cache ( ) , & key) {
284
308
Some ( _) => return ,
285
309
None => self . tcx. queries. $name( self . tcx, DUMMY_SP , key, QueryMode :: Ensure ) ,
286
310
} ;
@@ -305,10 +329,11 @@ macro_rules! define_callbacks {
305
329
let key = key. into_query_param( ) ;
306
330
opt_remap_env_constness!( [ $( $modifiers) * ] [ key] ) ;
307
331
308
- match try_get_cached( self . tcx, & self . tcx. query_system. caches. $name, & key) {
332
+ let value = match try_get_cached( self . tcx, self . tcx. query_system. caches. $name. cache ( ) , & key) {
309
333
Some ( value) => value,
310
334
None => self . tcx. queries. $name( self . tcx, self . span, key, QueryMode :: Get ) . unwrap( ) ,
311
- }
335
+ } ;
336
+ restore( value)
312
337
} ) *
313
338
}
314
339
@@ -372,7 +397,7 @@ macro_rules! define_callbacks {
372
397
span: Span ,
373
398
key: query_keys:: $name<' tcx>,
374
399
mode: QueryMode ,
375
- ) -> Option <$V >; ) *
400
+ ) -> Option <Erase <$V> >; ) *
376
401
}
377
402
} ;
378
403
}
@@ -400,10 +425,11 @@ macro_rules! define_feedable {
400
425
401
426
let tcx = self . tcx;
402
427
let value = query_provided_to_value:: $name( tcx, value) ;
403
- let cache = & tcx. query_system. caches. $name;
428
+ let cache = tcx. query_system. caches. $name. cache ( ) ;
404
429
405
430
match try_get_cached( tcx, cache, & key) {
406
431
Some ( old) => {
432
+ let old = restore( old) ;
407
433
bug!(
408
434
"Trying to feed an already recorded value for query {} key={key:?}:\n old value: {old:?}\n new value: {value:?}" ,
409
435
stringify!( $name) ,
@@ -418,7 +444,7 @@ macro_rules! define_feedable {
418
444
& value,
419
445
hash_result!( [ $( $modifiers) * ] ) ,
420
446
) ;
421
- cache. complete( key, value, dep_node_index) ;
447
+ cache. complete( key, erase ( value) , dep_node_index) ;
422
448
value
423
449
}
424
450
}
0 commit comments