@@ -69,6 +69,7 @@ use std::ops::Deref;
6969use std:: path:: PathBuf ;
7070use std:: sync:: Arc ;
7171
72+ use crate :: query:: erase:: { erase, restore, Erase } ;
7273pub ( crate ) use rustc_query_system:: query:: QueryJobId ;
7374use rustc_query_system:: query:: * ;
7475
@@ -246,7 +247,30 @@ macro_rules! define_callbacks {
246247 use super :: * ;
247248
248249 $(
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+ }
250274 ) *
251275 }
252276
@@ -270,7 +294,7 @@ macro_rules! define_callbacks {
270294
271295 #[ derive( Default ) ]
272296 pub struct QueryCaches <' tcx> {
273- $( $( #[ $attr] ) * pub $name: query_storage :: $name<' tcx>, ) *
297+ $( $( #[ $attr] ) * pub $name: query_cache :: $name<' tcx>, ) *
274298 }
275299
276300 impl <' tcx> TyCtxtEnsure <' tcx> {
@@ -280,7 +304,7 @@ macro_rules! define_callbacks {
280304 let key = key. into_query_param( ) ;
281305 opt_remap_env_constness!( [ $( $modifiers) * ] [ key] ) ;
282306
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) {
284308 Some ( _) => return ,
285309 None => self . tcx. queries. $name( self . tcx, DUMMY_SP , key, QueryMode :: Ensure ) ,
286310 } ;
@@ -305,10 +329,11 @@ macro_rules! define_callbacks {
305329 let key = key. into_query_param( ) ;
306330 opt_remap_env_constness!( [ $( $modifiers) * ] [ key] ) ;
307331
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) {
309333 Some ( value) => value,
310334 None => self . tcx. queries. $name( self . tcx, self . span, key, QueryMode :: Get ) . unwrap( ) ,
311- }
335+ } ;
336+ restore( value)
312337 } ) *
313338 }
314339
@@ -372,7 +397,7 @@ macro_rules! define_callbacks {
372397 span: Span ,
373398 key: query_keys:: $name<' tcx>,
374399 mode: QueryMode ,
375- ) -> Option <$V >; ) *
400+ ) -> Option <Erase <$V> >; ) *
376401 }
377402 } ;
378403}
@@ -400,10 +425,11 @@ macro_rules! define_feedable {
400425
401426 let tcx = self . tcx;
402427 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 ( ) ;
404429
405430 match try_get_cached( tcx, cache, & key) {
406431 Some ( old) => {
432+ let old = restore( old) ;
407433 bug!(
408434 "Trying to feed an already recorded value for query {} key={key:?}:\n old value: {old:?}\n new value: {value:?}" ,
409435 stringify!( $name) ,
@@ -418,7 +444,7 @@ macro_rules! define_feedable {
418444 & value,
419445 hash_result!( [ $( $modifiers) * ] ) ,
420446 ) ;
421- cache. complete( key, value, dep_node_index) ;
447+ cache. complete( key, erase ( value) , dep_node_index) ;
422448 value
423449 }
424450 }
0 commit comments