Allow arbitrary keys in feeding API

This commit is contained in:
Oli Scherer 2022-12-03 13:36:39 +00:00
parent c38ff3b385
commit 125b729ddd
2 changed files with 19 additions and 12 deletions

View file

@ -1034,16 +1034,23 @@ pub struct FreeRegionInfo {
/// This struct should only be created by `create_def`. /// This struct should only be created by `create_def`.
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct TyCtxtFeed<'tcx> { pub struct TyCtxtFeed<'tcx, KEY: Copy> {
pub tcx: TyCtxt<'tcx>, pub tcx: TyCtxt<'tcx>,
// Do not allow direct access, as downstream code must not mutate this field. // Do not allow direct access, as downstream code must not mutate this field.
def_id: LocalDefId, key: KEY,
} }
impl<'tcx> TyCtxtFeed<'tcx> { impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
#[inline(always)]
pub fn key(&self) -> KEY {
self.key
}
}
impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
#[inline(always)] #[inline(always)]
pub fn def_id(&self) -> LocalDefId { pub fn def_id(&self) -> LocalDefId {
self.def_id self.key
} }
} }
@ -1515,7 +1522,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
self, self,
parent: LocalDefId, parent: LocalDefId,
data: hir::definitions::DefPathData, data: hir::definitions::DefPathData,
) -> TyCtxtFeed<'tcx> { ) -> TyCtxtFeed<'tcx, LocalDefId> {
// This function modifies `self.definitions` using a side-effect. // This function modifies `self.definitions` using a side-effect.
// We need to ensure that these side effects are re-run by the incr. comp. engine. // We need to ensure that these side effects are re-run by the incr. comp. engine.
// Depending on the forever-red node will tell the graph that the calling query // Depending on the forever-red node will tell the graph that the calling query
@ -1536,9 +1543,9 @@ impl<'tcx> TyCtxtAt<'tcx> {
// This is fine because: // This is fine because:
// - those queries are `eval_always` so we won't miss their result changing; // - those queries are `eval_always` so we won't miss their result changing;
// - this write will have happened before these queries are called. // - this write will have happened before these queries are called.
let def_id = self.definitions.write().create_def(parent, data); let key = self.definitions.write().create_def(parent, data);
let feed = TyCtxtFeed { tcx: self.tcx, def_id }; let feed = TyCtxtFeed { tcx: self.tcx, key };
feed.def_span(self.span); feed.def_span(self.span);
feed feed
} }

View file

@ -330,11 +330,11 @@ macro_rules! define_callbacks {
macro_rules! define_feedable { macro_rules! define_feedable {
($($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => { ($($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
impl<'tcx> TyCtxtFeed<'tcx> { $(impl<'tcx, K: IntoQueryParam<$($K)*> + Copy> TyCtxtFeed<'tcx, K> {
$($(#[$attr])* $(#[$attr])*
#[inline(always)] #[inline(always)]
pub fn $name(self, value: $V) -> query_stored::$name<'tcx> { pub fn $name(self, value: $V) -> query_stored::$name<'tcx> {
let key = self.def_id().into_query_param(); let key = self.key().into_query_param();
opt_remap_env_constness!([$($modifiers)*][key]); opt_remap_env_constness!([$($modifiers)*][key]);
let tcx = self.tcx; let tcx = self.tcx;
@ -361,8 +361,8 @@ macro_rules! define_feedable {
dep_graph::hash_result, dep_graph::hash_result,
); );
cache.complete(key, value, dep_node_index) cache.complete(key, value, dep_node_index)
})* }
} })*
} }
} }