Skip to content

Commit 125b729

Browse files
committed
Allow arbitrary keys in feeding API
1 parent c38ff3b commit 125b729

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

compiler/rustc_middle/src/ty/context.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -1034,16 +1034,23 @@ pub struct FreeRegionInfo {
10341034

10351035
/// This struct should only be created by `create_def`.
10361036
#[derive(Copy, Clone)]
1037-
pub struct TyCtxtFeed<'tcx> {
1037+
pub struct TyCtxtFeed<'tcx, KEY: Copy> {
10381038
pub tcx: TyCtxt<'tcx>,
10391039
// Do not allow direct access, as downstream code must not mutate this field.
1040-
def_id: LocalDefId,
1040+
key: KEY,
10411041
}
10421042

1043-
impl<'tcx> TyCtxtFeed<'tcx> {
1043+
impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
1044+
#[inline(always)]
1045+
pub fn key(&self) -> KEY {
1046+
self.key
1047+
}
1048+
}
1049+
1050+
impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
10441051
#[inline(always)]
10451052
pub fn def_id(&self) -> LocalDefId {
1046-
self.def_id
1053+
self.key
10471054
}
10481055
}
10491056

@@ -1515,7 +1522,7 @@ impl<'tcx> TyCtxtAt<'tcx> {
15151522
self,
15161523
parent: LocalDefId,
15171524
data: hir::definitions::DefPathData,
1518-
) -> TyCtxtFeed<'tcx> {
1525+
) -> TyCtxtFeed<'tcx, LocalDefId> {
15191526
// This function modifies `self.definitions` using a side-effect.
15201527
// We need to ensure that these side effects are re-run by the incr. comp. engine.
15211528
// Depending on the forever-red node will tell the graph that the calling query
@@ -1536,9 +1543,9 @@ impl<'tcx> TyCtxtAt<'tcx> {
15361543
// This is fine because:
15371544
// - those queries are `eval_always` so we won't miss their result changing;
15381545
// - this write will have happened before these queries are called.
1539-
let def_id = self.definitions.write().create_def(parent, data);
1546+
let key = self.definitions.write().create_def(parent, data);
15401547

1541-
let feed = TyCtxtFeed { tcx: self.tcx, def_id };
1548+
let feed = TyCtxtFeed { tcx: self.tcx, key };
15421549
feed.def_span(self.span);
15431550
feed
15441551
}

compiler/rustc_middle/src/ty/query.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,11 @@ macro_rules! define_callbacks {
330330

331331
macro_rules! define_feedable {
332332
($($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
333-
impl<'tcx> TyCtxtFeed<'tcx> {
334-
$($(#[$attr])*
333+
$(impl<'tcx, K: IntoQueryParam<$($K)*> + Copy> TyCtxtFeed<'tcx, K> {
334+
$(#[$attr])*
335335
#[inline(always)]
336336
pub fn $name(self, value: $V) -> query_stored::$name<'tcx> {
337-
let key = self.def_id().into_query_param();
337+
let key = self.key().into_query_param();
338338
opt_remap_env_constness!([$($modifiers)*][key]);
339339

340340
let tcx = self.tcx;
@@ -361,8 +361,8 @@ macro_rules! define_feedable {
361361
dep_graph::hash_result,
362362
);
363363
cache.complete(key, value, dep_node_index)
364-
})*
365-
}
364+
}
365+
})*
366366
}
367367
}
368368

0 commit comments

Comments
 (0)