Skip to content

Commit 28f391c

Browse files
committed
Type erase query results.
1 parent 1073461 commit 28f391c

File tree

6 files changed

+60
-28
lines changed

6 files changed

+60
-28
lines changed

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::ty::{self, print::describe_as_module, TyCtxt};
88
use rustc_span::def_id::LOCAL_CRATE;
99

10-
mod erase;
10+
pub mod erase;
1111
mod keys;
1212
pub use keys::Key;
1313

compiler/rustc_middle/src/ty/query.rs

+34-8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use std::ops::Deref;
6969
use std::path::PathBuf;
7070
use std::sync::Arc;
7171

72+
use crate::query::erase::{erase, restore, Erase};
7273
pub(crate) use rustc_query_system::query::QueryJobId;
7374
use 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:?}:\nold value: {old:?}\nnew 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
}

compiler/rustc_query_impl/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern crate rustc_middle;
2020
use rustc_data_structures::sync::AtomicU64;
2121
use rustc_middle::arena::Arena;
2222
use rustc_middle::dep_graph::{self, DepKindStruct};
23+
use rustc_middle::query::erase::{erase, restore, Erase};
2324
use rustc_middle::query::Key;
2425
use rustc_middle::ty::query::{
2526
query_keys, query_provided, query_provided_to_value, query_storage, query_values,

compiler/rustc_query_impl/src/on_disk_cache.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_index::vec::{Idx, IndexVec};
1010
use rustc_middle::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
1111
use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState};
1212
use rustc_middle::mir::{self, interpret};
13+
use rustc_middle::query::erase::{restore_ref, Erase, EraseType};
1314
use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
1415
use rustc_middle::ty::{self, Ty, TyCtxt};
1516
use rustc_query_system::dep_graph::DepContext;
@@ -1056,14 +1057,14 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for [u8] {
10561057
}
10571058
}
10581059

1059-
pub fn encode_query_results<'a, 'tcx, CTX, Q>(
1060+
pub fn encode_query_results<'a, 'tcx, CTX, Q, V>(
10601061
tcx: CTX,
10611062
encoder: &mut CacheEncoder<'a, 'tcx>,
10621063
query_result_index: &mut EncodedDepNodeIndex,
10631064
) where
10641065
CTX: QueryContext + 'tcx,
1065-
Q: super::QueryConfig<CTX>,
1066-
Q::Value: Encodable<CacheEncoder<'a, 'tcx>>,
1066+
Q: super::QueryConfig<CTX, Value = Erase<V>>,
1067+
V: EraseType + std::fmt::Debug + Encodable<CacheEncoder<'a, 'tcx>>,
10671068
{
10681069
let _timer = tcx
10691070
.dep_context()
@@ -1081,6 +1082,7 @@ pub fn encode_query_results<'a, 'tcx, CTX, Q>(
10811082

10821083
// Encode the type check tables with the `SerializedDepNodeIndex`
10831084
// as tag.
1085+
let value = restore_ref(value);
10841086
encoder.encode_tagged(dep_node, value);
10851087
}
10861088
});

compiler/rustc_query_impl/src/plumbing.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ macro_rules! feedable {
264264

265265
macro_rules! hash_result {
266266
([]) => {{
267-
Some(dep_graph::hash_result)
267+
Some(|hcx, result| {
268+
dep_graph::hash_result(hcx, &restore(*result))
269+
})
268270
}};
269271
([(no_hash) $($rest:tt)*]) => {{
270272
None
@@ -469,7 +471,7 @@ macro_rules! define_queries {
469471

470472
$(impl<'tcx> QueryConfig<QueryCtxt<'tcx>> for queries::$name<'tcx> {
471473
type Key = query_keys::$name<'tcx>;
472-
type Value = query_values::$name<'tcx>;
474+
type Value = Erase<query_values::$name<'tcx>>;
473475
const NAME: &'static str = stringify!($name);
474476

475477
#[inline]
@@ -490,20 +492,20 @@ macro_rules! define_queries {
490492
fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
491493
where 'tcx:'a
492494
{
493-
&tcx.query_system.caches.$name
495+
tcx.query_system.caches.$name.cache()
494496
}
495497

496498
fn execute_query(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
497-
tcx.$name(key)
499+
erase(tcx.$name(key))
498500
}
499501

500502
#[inline]
501503
#[allow(unused_variables)]
502504
fn compute(qcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
503-
query_provided_to_value::$name(
505+
erase(query_provided_to_value::$name(
504506
qcx.tcx,
505507
get_provider!([$($modifiers)*][qcx, $name, key])(qcx.tcx, key)
506-
)
508+
))
507509
}
508510

509511
#[inline]
@@ -516,6 +518,7 @@ macro_rules! define_queries {
516518
dep_node
517519
);
518520
value.map(|value| query_provided_to_value::$name(qcx.tcx, value))
521+
.map(erase)
519522
})
520523
} else {
521524
None
@@ -645,12 +648,12 @@ macro_rules! define_queries {
645648
$crate::profiling_support::alloc_self_profile_query_strings_for_query_cache(
646649
tcx,
647650
stringify!($name),
648-
&tcx.query_system.caches.$name,
651+
tcx.query_system.caches.$name.cache(),
649652
string_cache,
650653
)
651654
},
652655
encode_query_results: expand_if_cached!([$($modifiers)*], |tcx, encoder, query_result_index|
653-
$crate::on_disk_cache::encode_query_results::<_, super::queries::$name<'_>>(tcx, encoder, query_result_index)
656+
$crate::on_disk_cache::encode_query_results::<_, super::queries::$name<'_>, _>(tcx, encoder, query_result_index)
654657
),
655658
}})*
656659
}
@@ -695,7 +698,7 @@ macro_rules! define_queries_struct {
695698
$(
696699
$(#[$attr])*
697700
$name: QueryState<
698-
<queries::$name<'tcx> as QueryConfig<QueryCtxt<'tcx>>>::Key,
701+
query_keys::$name<'tcx>,
699702
rustc_middle::dep_graph::DepKind,
700703
>,
701704
)*
@@ -737,7 +740,7 @@ macro_rules! define_queries_struct {
737740
span: Span,
738741
key: <queries::$name<'tcx> as QueryConfig<QueryCtxt<'tcx>>>::Key,
739742
mode: QueryMode,
740-
) -> Option<query_values::$name<'tcx>> {
743+
) -> Option<Erase<query_values::$name<'tcx>>> {
741744
let qcx = QueryCtxt { tcx, queries: self };
742745
get_query::<queries::$name<'tcx>, _, rustc_middle::dep_graph::DepKind>(qcx, span, key, mode)
743746
})*

compiler/rustc_query_system/src/query/caches.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ impl<K, V> Default for DefaultCache<K, V> {
5555
}
5656
}
5757

58-
impl<K: Eq + Hash, V: Copy + Debug> QueryStorage for DefaultCache<K, V> {
58+
impl<K: Eq + Hash, V: Copy> QueryStorage for DefaultCache<K, V> {
5959
type Value = V;
6060
}
6161

6262
impl<K, V> QueryCache for DefaultCache<K, V>
6363
where
6464
K: Eq + Hash + Copy + Debug,
65-
V: Copy + Debug,
65+
V: Copy,
6666
{
6767
type Key = K;
6868

@@ -127,13 +127,13 @@ impl<V> Default for SingleCache<V> {
127127
}
128128
}
129129

130-
impl<V: Copy + Debug> QueryStorage for SingleCache<V> {
130+
impl<V: Copy> QueryStorage for SingleCache<V> {
131131
type Value = V;
132132
}
133133

134134
impl<V> QueryCache for SingleCache<V>
135135
where
136-
V: Copy + Debug,
136+
V: Copy,
137137
{
138138
type Key = ();
139139

@@ -173,14 +173,14 @@ impl<K: Idx, V> Default for VecCache<K, V> {
173173
}
174174
}
175175

176-
impl<K: Eq + Idx, V: Copy + Debug> QueryStorage for VecCache<K, V> {
176+
impl<K: Eq + Idx, V: Copy> QueryStorage for VecCache<K, V> {
177177
type Value = V;
178178
}
179179

180180
impl<K, V> QueryCache for VecCache<K, V>
181181
where
182182
K: Eq + Idx + Copy + Debug,
183-
V: Copy + Debug,
183+
V: Copy,
184184
{
185185
type Key = K;
186186

0 commit comments

Comments
 (0)