Skip to content

Commit 77eb2be

Browse files
committed
Move query accessor code into functions
# Conflicts: # compiler/rustc_middle/src/ty/query.rs
1 parent 575d424 commit 77eb2be

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

compiler/rustc_middle/src/ty/query.rs

+72-18
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,55 @@ impl<'tcx> TyCtxt<'tcx> {
106106
}
107107
}
108108

109+
#[inline(always)]
110+
fn query_get_at<'tcx, Cache, K>(
111+
tcx: TyCtxt<'tcx>,
112+
execute_query: fn(
113+
&'tcx dyn QueryEngine<'tcx>,
114+
TyCtxt<'tcx>,
115+
Span,
116+
Cache::Key,
117+
QueryMode,
118+
) -> Option<Cache::Stored>,
119+
query_cache: &Cache,
120+
span: Span,
121+
key: K,
122+
) -> Cache::Stored
123+
where
124+
K: IntoQueryParam<Cache::Key>,
125+
Cache::Stored: Copy,
126+
Cache: QueryCache,
127+
{
128+
let key = key.into_query_param();
129+
match try_get_cached(tcx, query_cache, &key) {
130+
Some(value) => value,
131+
None => execute_query(tcx.queries, tcx, span, key, QueryMode::Get).unwrap(),
132+
}
133+
}
134+
135+
#[inline(always)]
136+
fn query_ensure<'tcx, Cache, K>(
137+
tcx: TyCtxt<'tcx>,
138+
execute_query: fn(
139+
&'tcx dyn QueryEngine<'tcx>,
140+
TyCtxt<'tcx>,
141+
Span,
142+
Cache::Key,
143+
QueryMode,
144+
) -> Option<Cache::Stored>,
145+
query_cache: &Cache,
146+
key: K,
147+
) where
148+
K: IntoQueryParam<Cache::Key>,
149+
Cache::Stored: Copy,
150+
Cache: QueryCache,
151+
{
152+
let key = key.into_query_param();
153+
if try_get_cached(tcx, query_cache, &key).is_none() {
154+
execute_query(tcx.queries, tcx, DUMMY_SP, key, QueryMode::Ensure);
155+
}
156+
}
157+
109158
macro_rules! query_helper_param_ty {
110159
(DefId) => { impl IntoQueryParam<DefId> };
111160
(LocalDefId) => { impl IntoQueryParam<LocalDefId> };
@@ -158,9 +207,9 @@ macro_rules! separate_provide_extern_default {
158207
}
159208

160209
macro_rules! opt_remap_env_constness {
161-
([][$name:ident]) => {};
210+
([][$name:ident]) => { $name };
162211
([(remap_env_constness) $($rest:tt)*][$name:ident]) => {
163-
let $name = $name.without_const();
212+
$name.without_const()
164213
};
165214
([$other:tt $($modifiers:tt)*][$name:ident]) => {
166215
opt_remap_env_constness!([$($modifiers)*][$name])
@@ -219,13 +268,12 @@ macro_rules! define_callbacks {
219268
$($(#[$attr])*
220269
#[inline(always)]
221270
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
222-
let key = key.into_query_param();
223-
opt_remap_env_constness!([$($modifiers)*][key]);
224-
225-
match try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key) {
226-
Some(_) => return,
227-
None => self.tcx.queries.$name(self.tcx, DUMMY_SP, key, QueryMode::Ensure),
228-
};
271+
query_ensure(
272+
self.tcx,
273+
QueryEngine::$name,
274+
&self.tcx.query_caches.$name,
275+
opt_remap_env_constness!([$($modifiers)*][key]),
276+
);
229277
})*
230278
}
231279

@@ -235,7 +283,13 @@ macro_rules! define_callbacks {
235283
#[must_use]
236284
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V
237285
{
238-
self.at(DUMMY_SP).$name(key)
286+
query_get_at(
287+
self,
288+
QueryEngine::$name,
289+
&self.query_caches.$name,
290+
DUMMY_SP,
291+
opt_remap_env_constness!([$($modifiers)*][key]),
292+
)
239293
})*
240294
}
241295

@@ -244,13 +298,13 @@ macro_rules! define_callbacks {
244298
#[inline(always)]
245299
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V
246300
{
247-
let key = key.into_query_param();
248-
opt_remap_env_constness!([$($modifiers)*][key]);
249-
250-
match try_get_cached(self.tcx, &self.tcx.query_caches.$name, &key) {
251-
Some(value) => value,
252-
None => self.tcx.queries.$name(self.tcx, self.span, key, QueryMode::Get).unwrap(),
253-
}
301+
query_get_at(
302+
self.tcx,
303+
QueryEngine::$name,
304+
&self.tcx.query_caches.$name,
305+
self.span,
306+
opt_remap_env_constness!([$($modifiers)*][key]),
307+
)
254308
})*
255309
}
256310

@@ -337,7 +391,7 @@ macro_rules! define_feedable {
337391
#[inline(always)]
338392
pub fn $name(self, value: query_values::$name<'tcx>) -> $V {
339393
let key = self.key().into_query_param();
340-
opt_remap_env_constness!([$($modifiers)*][key]);
394+
let key = opt_remap_env_constness!([$($modifiers)*][key]);
341395

342396
let tcx = self.tcx;
343397
let cache = &tcx.query_caches.$name;

compiler/rustc_query_system/src/query/plumbing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ where
335335
/// It returns the shard index and a lock guard to the shard,
336336
/// which will be used if the query is not in the cache and we need
337337
/// to compute it.
338-
#[inline]
338+
#[inline(always)]
339339
pub fn try_get_cached<Tcx, C>(tcx: Tcx, cache: &C, key: &C::Key) -> Option<C::Stored>
340340
where
341341
C: QueryCache,

0 commit comments

Comments
 (0)