Skip to content

Commit b97bdcb

Browse files
committed
Extract query-method inner functions to query::inner
1 parent c33b667 commit b97bdcb

File tree

3 files changed

+90
-75
lines changed

3 files changed

+90
-75
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//! Helper functions that serve as the immediate implementation of
2+
//! `tcx.$query(..)` and its variations.
3+
4+
use rustc_query_system::query::{QueryCache, QueryMode, try_get_cached};
5+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
6+
7+
use crate::query::IntoQueryParam;
8+
use crate::query::erase::{self, Erase, EraseType};
9+
use crate::ty::TyCtxt;
10+
11+
/// Shared implementation of `tcx.$query(..)` and `tcx.at(span).$query(..)`
12+
/// for all queries.
13+
#[inline(always)]
14+
pub(crate) fn query_get_at<'tcx, Cache>(
15+
tcx: TyCtxt<'tcx>,
16+
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
17+
query_cache: &Cache,
18+
span: Span,
19+
key: Cache::Key,
20+
) -> Cache::Value
21+
where
22+
Cache: QueryCache,
23+
{
24+
let key = key.into_query_param();
25+
match try_get_cached(tcx, query_cache, &key) {
26+
Some(value) => value,
27+
None => execute_query(tcx, span, key, QueryMode::Get).unwrap(),
28+
}
29+
}
30+
31+
/// Shared implementation of `tcx.ensure_ok().$query(..)` for most queries,
32+
/// and `tcx.ensure_done().$query(..)` for all queries.
33+
#[inline]
34+
pub(crate) fn query_ensure<'tcx, Cache>(
35+
tcx: TyCtxt<'tcx>,
36+
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
37+
query_cache: &Cache,
38+
key: Cache::Key,
39+
check_cache: bool,
40+
) where
41+
Cache: QueryCache,
42+
{
43+
let key = key.into_query_param();
44+
if try_get_cached(tcx, query_cache, &key).is_none() {
45+
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { check_cache });
46+
}
47+
}
48+
49+
/// Shared implementation of `tcx.ensure_ok().$query(..)` for queries that
50+
/// have the `return_result_from_ensure_ok` modifier.
51+
#[inline]
52+
pub(crate) fn query_ensure_error_guaranteed<'tcx, Cache, T>(
53+
tcx: TyCtxt<'tcx>,
54+
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
55+
query_cache: &Cache,
56+
key: Cache::Key,
57+
check_cache: bool,
58+
) -> Result<(), ErrorGuaranteed>
59+
where
60+
Cache: QueryCache<Value = Erase<Result<T, ErrorGuaranteed>>>,
61+
Result<T, ErrorGuaranteed>: EraseType,
62+
{
63+
let key = key.into_query_param();
64+
if let Some(res) = try_get_cached(tcx, query_cache, &key) {
65+
erase::restore(res).map(drop)
66+
} else {
67+
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { check_cache })
68+
.map(erase::restore)
69+
.map(|res| res.map(drop))
70+
// Either we actually executed the query, which means we got a full `Result`,
71+
// or we can just assume the query succeeded, because it was green in the
72+
// incremental cache. If it is green, that means that the previous compilation
73+
// that wrote to the incremental cache compiles successfully. That is only
74+
// possible if the cache entry was `Ok(())`, so we emit that here, without
75+
// actually encoding the `Result` in the cache or loading it from there.
76+
.unwrap_or(Ok(()))
77+
}
78+
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ use crate::mir::interpret::{
121121
};
122122
use crate::mir::mono::{CodegenUnit, CollectionMode, MonoItem, MonoItemPartitions};
123123
use crate::query::erase::{Erase, erase, restore};
124-
use crate::query::plumbing::{
125-
CyclePlaceholder, DynamicQuery, query_ensure, query_ensure_error_guaranteed, query_get_at,
126-
};
124+
use crate::query::plumbing::{CyclePlaceholder, DynamicQuery};
127125
use crate::traits::query::{
128126
CanonicalAliasGoal, CanonicalDropckOutlivesGoal, CanonicalImpliedOutlivesBoundsGoal,
129127
CanonicalMethodAutoderefStepsGoal, CanonicalPredicateGoal, CanonicalTypeOpAscribeUserTypeGoal,
@@ -147,6 +145,7 @@ use crate::{dep_graph, mir, thir};
147145

148146
mod arena_cached;
149147
pub mod erase;
148+
pub(crate) mod inner;
150149
mod keys;
151150
pub mod on_disk_cache;
152151
#[macro_use]

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ use rustc_query_system::HandleCycleError;
88
use rustc_query_system::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
99
pub(crate) use rustc_query_system::query::QueryJobId;
1010
use rustc_query_system::query::*;
11-
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
11+
use rustc_span::{ErrorGuaranteed, Span};
1212
pub use sealed::IntoQueryParam;
1313

14-
use super::erase::EraseType;
1514
use crate::dep_graph;
1615
use crate::dep_graph::DepKind;
1716
use crate::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
@@ -167,78 +166,17 @@ impl<'tcx> TyCtxt<'tcx> {
167166
}
168167
}
169168

170-
#[inline(always)]
171-
pub fn query_get_at<'tcx, Cache>(
172-
tcx: TyCtxt<'tcx>,
173-
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
174-
query_cache: &Cache,
175-
span: Span,
176-
key: Cache::Key,
177-
) -> Cache::Value
178-
where
179-
Cache: QueryCache,
180-
{
181-
let key = key.into_query_param();
182-
match try_get_cached(tcx, query_cache, &key) {
183-
Some(value) => value,
184-
None => execute_query(tcx, span, key, QueryMode::Get).unwrap(),
185-
}
186-
}
187-
188-
#[inline]
189-
pub fn query_ensure<'tcx, Cache>(
190-
tcx: TyCtxt<'tcx>,
191-
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
192-
query_cache: &Cache,
193-
key: Cache::Key,
194-
check_cache: bool,
195-
) where
196-
Cache: QueryCache,
197-
{
198-
let key = key.into_query_param();
199-
if try_get_cached(tcx, query_cache, &key).is_none() {
200-
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { check_cache });
201-
}
202-
}
203-
204-
#[inline]
205-
pub fn query_ensure_error_guaranteed<'tcx, Cache, T>(
206-
tcx: TyCtxt<'tcx>,
207-
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
208-
query_cache: &Cache,
209-
key: Cache::Key,
210-
check_cache: bool,
211-
) -> Result<(), ErrorGuaranteed>
212-
where
213-
Cache: QueryCache<Value = super::erase::Erase<Result<T, ErrorGuaranteed>>>,
214-
Result<T, ErrorGuaranteed>: EraseType,
215-
{
216-
let key = key.into_query_param();
217-
if let Some(res) = try_get_cached(tcx, query_cache, &key) {
218-
super::erase::restore(res).map(drop)
219-
} else {
220-
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { check_cache })
221-
.map(super::erase::restore)
222-
.map(|res| res.map(drop))
223-
// Either we actually executed the query, which means we got a full `Result`,
224-
// or we can just assume the query succeeded, because it was green in the
225-
// incremental cache. If it is green, that means that the previous compilation
226-
// that wrote to the incremental cache compiles successfully. That is only
227-
// possible if the cache entry was `Ok(())`, so we emit that here, without
228-
// actually encoding the `Result` in the cache or loading it from there.
229-
.unwrap_or(Ok(()))
230-
}
231-
}
232-
233-
macro_rules! query_ensure {
169+
/// Calls either `query_ensure` or `query_ensure_error_guaranteed`, depending
170+
/// on whether the list of modifiers contains `return_result_from_ensure_ok`.
171+
macro_rules! query_ensure_select {
234172
([]$($args:tt)*) => {
235-
query_ensure($($args)*)
173+
crate::query::inner::query_ensure($($args)*)
236174
};
237175
([(return_result_from_ensure_ok) $($rest:tt)*]$($args:tt)*) => {
238-
query_ensure_error_guaranteed($($args)*).map(|_| ())
176+
crate::query::inner::query_ensure_error_guaranteed($($args)*)
239177
};
240178
([$other:tt $($modifiers:tt)*]$($args:tt)*) => {
241-
query_ensure!([$($modifiers)*]$($args)*)
179+
query_ensure_select!([$($modifiers)*]$($args)*)
242180
};
243181
}
244182

@@ -434,7 +372,7 @@ macro_rules! define_callbacks {
434372
self,
435373
key: query_helper_param_ty!($($K)*),
436374
) -> ensure_ok_result!([$($modifiers)*]) {
437-
query_ensure!(
375+
query_ensure_select!(
438376
[$($modifiers)*]
439377
self.tcx,
440378
self.tcx.query_system.fns.engine.$name,
@@ -449,7 +387,7 @@ macro_rules! define_callbacks {
449387
$($(#[$attr])*
450388
#[inline(always)]
451389
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
452-
query_ensure(
390+
crate::query::inner::query_ensure(
453391
self.tcx,
454392
self.tcx.query_system.fns.engine.$name,
455393
&self.tcx.query_system.caches.$name,
@@ -474,7 +412,7 @@ macro_rules! define_callbacks {
474412
#[inline(always)]
475413
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V
476414
{
477-
restore::<$V>(query_get_at(
415+
restore::<$V>(crate::query::inner::query_get_at(
478416
self.tcx,
479417
self.tcx.query_system.fns.engine.$name,
480418
&self.tcx.query_system.caches.$name,

0 commit comments

Comments
 (0)