Skip to content

Commit 6e4af4a

Browse files
committed
Move definition of callbacks to parent module.
1 parent a4b1158 commit 6e4af4a

File tree

2 files changed

+88
-83
lines changed

2 files changed

+88
-83
lines changed

compiler/rustc_middle/src/ty/query/mod.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,93 @@ pub use self::on_disk_cache::OnDiskCache;
8686
mod profiling_support;
8787
pub use self::profiling_support::alloc_self_profile_query_strings;
8888

89+
#[derive(Copy, Clone)]
90+
pub struct TyCtxtAt<'tcx> {
91+
pub tcx: TyCtxt<'tcx>,
92+
pub span: Span,
93+
}
94+
95+
impl Deref for TyCtxtAt<'tcx> {
96+
type Target = TyCtxt<'tcx>;
97+
#[inline(always)]
98+
fn deref(&self) -> &Self::Target {
99+
&self.tcx
100+
}
101+
}
102+
103+
#[derive(Copy, Clone)]
104+
pub struct TyCtxtEnsure<'tcx> {
105+
pub tcx: TyCtxt<'tcx>,
106+
}
107+
108+
impl TyCtxt<'tcx> {
109+
/// Returns a transparent wrapper for `TyCtxt`, which ensures queries
110+
/// are executed instead of just returning their results.
111+
#[inline(always)]
112+
pub fn ensure(self) -> TyCtxtEnsure<'tcx> {
113+
TyCtxtEnsure { tcx: self }
114+
}
115+
116+
/// Returns a transparent wrapper for `TyCtxt` which uses
117+
/// `span` as the location of queries performed through it.
118+
#[inline(always)]
119+
pub fn at(self, span: Span) -> TyCtxtAt<'tcx> {
120+
TyCtxtAt { tcx: self, span }
121+
}
122+
}
123+
124+
macro_rules! define_callbacks {
125+
(<$tcx:tt>
126+
$($(#[$attr:meta])*
127+
[$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
128+
129+
impl TyCtxtEnsure<$tcx> {
130+
$($(#[$attr])*
131+
#[inline(always)]
132+
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
133+
let key = key.into_query_param();
134+
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, |_| {});
135+
136+
let lookup = match cached {
137+
Ok(()) => return,
138+
Err(lookup) => lookup,
139+
};
140+
141+
self.tcx.queries.$name(self.tcx, DUMMY_SP, key, lookup, QueryMode::Ensure);
142+
})*
143+
}
144+
145+
impl TyCtxt<$tcx> {
146+
$($(#[$attr])*
147+
#[inline(always)]
148+
#[must_use]
149+
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
150+
{
151+
self.at(DUMMY_SP).$name(key)
152+
})*
153+
}
154+
155+
impl TyCtxtAt<$tcx> {
156+
$($(#[$attr])*
157+
#[inline(always)]
158+
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
159+
{
160+
let key = key.into_query_param();
161+
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, |value| {
162+
value.clone()
163+
});
164+
165+
let lookup = match cached {
166+
Ok(value) => return value,
167+
Err(lookup) => lookup,
168+
};
169+
170+
self.tcx.queries.$name(self.tcx, self.span, key, lookup, QueryMode::Get).unwrap()
171+
})*
172+
}
173+
}
174+
}
175+
89176
// Each of these queries corresponds to a function pointer field in the
90177
// `Providers` struct for requesting a value of that type, and a method
91178
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way
@@ -99,6 +186,7 @@ pub use self::profiling_support::alloc_self_profile_query_strings;
99186
// as they will raise an fatal error on query cycles instead.
100187

101188
rustc_query_append! { [define_queries!][<'tcx>] }
189+
rustc_query_append! { [define_callbacks!][<'tcx>] }
102190

103191
mod sealed {
104192
use super::{DefId, LocalDefId};

compiler/rustc_middle/src/ty/query/plumbing.rs

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -456,89 +456,6 @@ macro_rules! define_queries {
456456
}
457457
})*
458458

459-
#[derive(Copy, Clone)]
460-
pub struct TyCtxtEnsure<'tcx> {
461-
pub tcx: TyCtxt<'tcx>,
462-
}
463-
464-
impl TyCtxtEnsure<$tcx> {
465-
$($(#[$attr])*
466-
#[inline(always)]
467-
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
468-
let key = key.into_query_param();
469-
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, |_| {});
470-
471-
let lookup = match cached {
472-
Ok(()) => return,
473-
Err(lookup) => lookup,
474-
};
475-
476-
self.tcx.queries.$name(self.tcx, DUMMY_SP, key, lookup, QueryMode::Ensure);
477-
})*
478-
}
479-
480-
#[derive(Copy, Clone)]
481-
pub struct TyCtxtAt<'tcx> {
482-
pub tcx: TyCtxt<'tcx>,
483-
pub span: Span,
484-
}
485-
486-
impl Deref for TyCtxtAt<'tcx> {
487-
type Target = TyCtxt<'tcx>;
488-
#[inline(always)]
489-
fn deref(&self) -> &Self::Target {
490-
&self.tcx
491-
}
492-
}
493-
494-
impl TyCtxt<$tcx> {
495-
/// Returns a transparent wrapper for `TyCtxt`, which ensures queries
496-
/// are executed instead of just returning their results.
497-
#[inline(always)]
498-
pub fn ensure(self) -> TyCtxtEnsure<$tcx> {
499-
TyCtxtEnsure {
500-
tcx: self,
501-
}
502-
}
503-
504-
/// Returns a transparent wrapper for `TyCtxt` which uses
505-
/// `span` as the location of queries performed through it.
506-
#[inline(always)]
507-
pub fn at(self, span: Span) -> TyCtxtAt<$tcx> {
508-
TyCtxtAt {
509-
tcx: self,
510-
span
511-
}
512-
}
513-
514-
$($(#[$attr])*
515-
#[inline(always)]
516-
#[must_use]
517-
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
518-
{
519-
self.at(DUMMY_SP).$name(key)
520-
})*
521-
}
522-
523-
impl TyCtxtAt<$tcx> {
524-
$($(#[$attr])*
525-
#[inline(always)]
526-
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
527-
{
528-
let key = key.into_query_param();
529-
let cached = try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key, |value| {
530-
value.clone()
531-
});
532-
533-
let lookup = match cached {
534-
Ok(value) => return value,
535-
Err(lookup) => lookup,
536-
};
537-
538-
self.tcx.queries.$name(self.tcx, self.span, key, lookup, QueryMode::Get).unwrap()
539-
})*
540-
}
541-
542459
define_provider_struct! {
543460
tcx: $tcx,
544461
input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)

0 commit comments

Comments
 (0)