Skip to content

Commit a282336

Browse files
committed
Extract most code from define_feedable!
1 parent cb0c092 commit a282336

File tree

2 files changed

+65
-42
lines changed

2 files changed

+65
-42
lines changed

compiler/rustc_middle/src/query/inner.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
//! Helper functions that serve as the immediate implementation of
22
//! `tcx.$query(..)` and its variations.
33
4+
use std::fmt::Debug;
5+
6+
use rustc_data_structures::fingerprint::Fingerprint;
7+
use rustc_query_system::dep_graph::{DepKind, DepNodeParams};
8+
use rustc_query_system::ich::StableHashingContext;
49
use rustc_query_system::query::{QueryCache, QueryMode, try_get_cached};
510
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
611

12+
use crate::dep_graph;
713
use crate::query::IntoQueryParam;
814
use crate::query::erase::{self, Erase, EraseType};
915
use crate::ty::TyCtxt;
@@ -76,3 +82,53 @@ where
7682
.unwrap_or(Ok(()))
7783
}
7884
}
85+
86+
/// Common implementation of query feeding, used by `define_feedable!`.
87+
pub(crate) fn query_feed<'tcx, Cache, Value>(
88+
tcx: TyCtxt<'tcx>,
89+
dep_kind: DepKind,
90+
hasher: Option<fn(&mut StableHashingContext<'_>, &Value) -> Fingerprint>,
91+
cache: &Cache,
92+
key: Cache::Key,
93+
erased: Erase<Value>,
94+
) where
95+
Cache: QueryCache<Value = Erase<Value>>,
96+
Cache::Key: DepNodeParams<TyCtxt<'tcx>>,
97+
Value: EraseType + Debug,
98+
{
99+
let value = erase::restore::<Value>(erased);
100+
101+
match try_get_cached(tcx, cache, &key) {
102+
Some(old) => {
103+
let old = erase::restore::<Value>(old);
104+
if let Some(hasher) = hasher {
105+
let (value_hash, old_hash): (Fingerprint, Fingerprint) = tcx
106+
.with_stable_hashing_context(|mut hcx| {
107+
(hasher(&mut hcx, &value), hasher(&mut hcx, &old))
108+
});
109+
if old_hash != value_hash {
110+
// We have an inconsistency. This can happen if one of the two
111+
// results is tainted by errors. In this case, delay a bug to
112+
// ensure compilation is doomed, and keep the `old` value.
113+
tcx.dcx().delayed_bug(format!(
114+
"Trying to feed an already recorded value for query {dep_kind:?} key={key:?}:\n\
115+
old value: {old:?}\nnew value: {value:?}",
116+
));
117+
}
118+
} else {
119+
// The query is `no_hash`, so we have no way to perform a sanity check.
120+
// If feeding the same value multiple times needs to be supported,
121+
// the query should not be marked `no_hash`.
122+
bug!(
123+
"Trying to feed an already recorded value for query {dep_kind:?} key={key:?}:\n\
124+
old value: {old:?}\nnew value: {value:?}",
125+
)
126+
}
127+
}
128+
None => {
129+
let dep_node = dep_graph::DepNode::construct(tcx, dep_kind, &key);
130+
let dep_node_index = tcx.dep_graph.with_feed_task(dep_node, tcx, &value, hasher);
131+
cache.complete(key, erased, dep_node_index);
132+
}
133+
}
134+
}

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -501,56 +501,23 @@ macro_rules! define_feedable {
501501
$(#[$attr])*
502502
#[inline(always)]
503503
pub fn $name(self, value: queries::$name::ProvidedValue<'tcx>) {
504-
#![allow(unused_imports)] // Removed later in this PR.
505-
use rustc_data_structures::fingerprint::Fingerprint;
506-
use rustc_query_system::query::{QueryCache, try_get_cached};
507-
508504
let key = self.key().into_query_param();
509505

510506
let tcx = self.tcx;
511507
let erased = queries::$name::provided_to_erased(tcx, value);
512-
let value = erase::restore::<$V>(erased);
513508
let cache = &tcx.query_system.caches.$name;
514509

515510
let dep_kind: dep_graph::DepKind = dep_graph::dep_kinds::$name;
516511
let hasher: Option<fn(&mut StableHashingContext<'_>, &_) -> _> = hash_result!([$($modifiers)*]);
517-
match try_get_cached(tcx, cache, &key) {
518-
Some(old) => {
519-
let old = erase::restore::<$V>(old);
520-
if let Some(hasher) = hasher {
521-
let (value_hash, old_hash): (Fingerprint, Fingerprint) = tcx.with_stable_hashing_context(|mut hcx|
522-
(hasher(&mut hcx, &value), hasher(&mut hcx, &old))
523-
);
524-
if old_hash != value_hash {
525-
// We have an inconsistency. This can happen if one of the two
526-
// results is tainted by errors. In this case, delay a bug to
527-
// ensure compilation is doomed, and keep the `old` value.
528-
tcx.dcx().delayed_bug(format!(
529-
"Trying to feed an already recorded value for query {dep_kind:?} key={key:?}:\n\
530-
old value: {old:?}\nnew value: {value:?}",
531-
));
532-
}
533-
} else {
534-
// The query is `no_hash`, so we have no way to perform a sanity check.
535-
// If feeding the same value multiple times needs to be supported,
536-
// the query should not be marked `no_hash`.
537-
bug!(
538-
"Trying to feed an already recorded value for query {dep_kind:?} key={key:?}:\n\
539-
old value: {old:?}\nnew value: {value:?}",
540-
)
541-
}
542-
}
543-
None => {
544-
let dep_node = dep_graph::DepNode::construct(tcx, dep_kind, &key);
545-
let dep_node_index = tcx.dep_graph.with_feed_task(
546-
dep_node,
547-
tcx,
548-
&value,
549-
hasher,
550-
);
551-
cache.complete(key, erased, dep_node_index);
552-
}
553-
}
512+
513+
$crate::query::inner::query_feed(
514+
tcx,
515+
dep_kind,
516+
hasher,
517+
cache,
518+
key,
519+
erased,
520+
);
554521
}
555522
})*
556523
}

0 commit comments

Comments
 (0)