Skip to content

Commit d72c5b9

Browse files
committed
Use an extension trait to define query methods
1 parent ff9fadb commit d72c5b9

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ pub struct QuerySystem<'tcx> {
8383
pub jobs: AtomicU64,
8484
}
8585

86+
#[derive(Copy, Clone)]
87+
pub struct ExplicitQuery<'tcx> {
88+
pub tcx: TyCtxt<'tcx>,
89+
}
90+
8691
#[derive(Copy, Clone)]
8792
pub struct TyCtxtAt<'tcx> {
8893
pub tcx: TyCtxt<'tcx>,
@@ -169,6 +174,12 @@ impl<'tcx> TyCtxt<'tcx> {
169174
TyCtxtAt { tcx: self, span }
170175
}
171176

177+
/// Explicitly call a query.
178+
#[inline(always)]
179+
pub fn query(self) -> ExplicitQuery<'tcx> {
180+
ExplicitQuery { tcx: self }
181+
}
182+
172183
pub fn try_mark_green(self, dep_node: &dep_graph::DepNode) -> bool {
173184
(self.query_system.fns.try_mark_green)(self, dep_node)
174185
}
@@ -406,11 +417,27 @@ macro_rules! define_callbacks {
406417
})*
407418
}
408419

409-
impl<'tcx> TyCtxt<'tcx> {
420+
/// Extension trait allowing calling queries directly on `TyCtxt`.
421+
pub trait Queries<'tcx> {
422+
$(
423+
#[must_use]
424+
fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V;
425+
)*
426+
}
427+
428+
impl<'tcx> Queries<'tcx> for ExplicitQuery<'tcx> {
429+
$($(#[$attr])*
430+
#[inline(always)]
431+
fn $name(self, key: query_helper_param_ty!($($K)*) ) -> $V
432+
{
433+
self.tcx.at(DUMMY_SP).$name(key)
434+
})*
435+
}
436+
437+
impl<'tcx> Queries<'tcx> for TyCtxt<'tcx> {
410438
$($(#[$attr])*
411439
#[inline(always)]
412-
#[must_use]
413-
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V
440+
fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V
414441
{
415442
self.at(DUMMY_SP).$name(key)
416443
})*

0 commit comments

Comments
 (0)