Skip to content

Commit 0200bbf

Browse files
committed
Verify that query keys result in unique dep nodes
1 parent 1b427b3 commit 0200bbf

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

compiler/rustc_interface/src/queries.rs

+2
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ impl Compiler {
321321
}
322322

323323
self.sess.time("serialize_dep_graph", || gcx.enter(rustc_incremental::save_dep_graph));
324+
325+
gcx.enter(rustc_query_impl::query_key_hash_verify_all);
324326
}
325327

326328
// The timer's lifetime spans the dropping of `queries`, which contains

compiler/rustc_query_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_span::{ErrorGuaranteed, Span};
4141

4242
#[macro_use]
4343
mod plumbing;
44-
pub use crate::plumbing::QueryCtxt;
44+
pub use crate::plumbing::{query_key_hash_verify_all, QueryCtxt};
4545

4646
mod profiling_support;
4747
pub use self::profiling_support::alloc_self_profile_query_strings;

compiler/rustc_query_impl/src/plumbing.rs

+43
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::rustc_middle::dep_graph::DepContext;
66
use crate::rustc_middle::ty::TyEncoder;
77
use crate::QueryConfigRestored;
8+
use rustc_data_structures::fx::FxHashMap;
89
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
910
use rustc_data_structures::sync::Lock;
1011
use rustc_errors::DiagInner;
@@ -189,6 +190,14 @@ pub(super) fn encode_all_query_results<'tcx>(
189190
}
190191
}
191192

193+
pub fn query_key_hash_verify_all<'tcx>(tcx: TyCtxt<'tcx>) {
194+
tcx.sess.time("query_key_hash_verify_all", || {
195+
for verify in super::QUERY_KEY_HASH_VERIFY.iter() {
196+
verify(tcx);
197+
}
198+
})
199+
}
200+
192201
macro_rules! handle_cycle_error {
193202
([]) => {{
194203
rustc_query_system::HandleCycleError::Error
@@ -370,6 +379,29 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q>(
370379
});
371380
}
372381

382+
pub(crate) fn query_key_hash_verify<'tcx>(
383+
query: impl QueryConfig<QueryCtxt<'tcx>>,
384+
qcx: QueryCtxt<'tcx>,
385+
) {
386+
let _timer =
387+
qcx.profiler().generic_activity_with_arg("query_key_hash_verify_for", query.name());
388+
389+
let mut map = FxHashMap::default();
390+
391+
let cache = query.query_cache(qcx);
392+
cache.iter(&mut |key, _, _| {
393+
let node = DepNode::construct(qcx.tcx, query.dep_kind(), key);
394+
if let Some(other_key) = map.insert(node, *key) {
395+
bug!(
396+
"query key `{:?}` and `{:?}` mapped to the same dep node {:?}",
397+
key,
398+
other_key,
399+
node
400+
);
401+
}
402+
});
403+
}
404+
373405
fn try_load_from_on_disk_cache<'tcx, Q>(query: Q, tcx: TyCtxt<'tcx>, dep_node: DepNode)
374406
where
375407
Q: QueryConfig<QueryCtxt<'tcx>>,
@@ -691,6 +723,13 @@ macro_rules! define_queries {
691723
)
692724
}
693725
}}
726+
727+
pub fn query_key_hash_verify<'tcx>(tcx: TyCtxt<'tcx>) {
728+
$crate::plumbing::query_key_hash_verify(
729+
query_impl::$name::QueryType::config(tcx),
730+
QueryCtxt::new(tcx),
731+
)
732+
}
694733
})*}
695734

696735
pub(crate) fn engine(incremental: bool) -> QueryEngine {
@@ -730,6 +769,10 @@ macro_rules! define_queries {
730769
>
731770
] = &[$(expand_if_cached!([$($modifiers)*], query_impl::$name::encode_query_results)),*];
732771

772+
const QUERY_KEY_HASH_VERIFY: &[
773+
for<'tcx> fn(TyCtxt<'tcx>)
774+
] = &[$(query_impl::$name::query_key_hash_verify),*];
775+
733776
#[allow(nonstandard_style)]
734777
mod query_callbacks {
735778
use super::*;

0 commit comments

Comments
 (0)