Skip to content

Commit 2db2776

Browse files
committed
Wrap TyCtxt inside a QueryCtxt for queries.
1 parent dab9b89 commit 2db2776

File tree

9 files changed

+82
-49
lines changed

9 files changed

+82
-49
lines changed

compiler/rustc_macros/src/query.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,10 @@ fn add_query_description_impl(
354354
quote! {
355355
#[inline]
356356
fn try_load_from_disk(
357-
#tcx: TyCtxt<'tcx>,
358-
#id: SerializedDepNodeIndex
357+
tcx: QueryCtxt<'tcx>,
358+
id: SerializedDepNodeIndex
359359
) -> Option<Self::Value> {
360+
let (#tcx, #id) = (*tcx, id);
360361
#block
361362
}
362363
}
@@ -365,10 +366,10 @@ fn add_query_description_impl(
365366
quote! {
366367
#[inline]
367368
fn try_load_from_disk(
368-
tcx: TyCtxt<'tcx>,
369+
tcx: QueryCtxt<'tcx>,
369370
id: SerializedDepNodeIndex
370371
) -> Option<Self::Value> {
371-
tcx.on_disk_cache.as_ref()?.try_load_query_result(tcx, id)
372+
tcx.on_disk_cache.as_ref()?.try_load_query_result(*tcx, id)
372373
}
373374
}
374375
};
@@ -393,10 +394,11 @@ fn add_query_description_impl(
393394
#[inline]
394395
#[allow(unused_variables, unused_braces)]
395396
fn cache_on_disk(
396-
#tcx: TyCtxt<'tcx>,
397-
#key: &Self::Key,
398-
#value: Option<&Self::Value>
397+
tcx: QueryCtxt<'tcx>,
398+
key: &Self::Key,
399+
value: Option<&Self::Value>
399400
) -> bool {
401+
let (#tcx, #key, #value) = (*tcx, key, value);
400402
#expr
401403
}
402404

@@ -414,16 +416,14 @@ fn add_query_description_impl(
414416

415417
let desc = quote! {
416418
#[allow(unused_variables)]
417-
fn describe(
418-
#tcx: TyCtxt<'tcx>,
419-
#key: #arg,
420-
) -> String {
421-
::rustc_middle::ty::print::with_no_trimmed_paths(|| format!(#desc))
419+
fn describe(tcx: QueryCtxt<'tcx>, key: #arg) -> String {
420+
let (#tcx, #key) = (*tcx, key);
421+
::rustc_middle::ty::print::with_no_trimmed_paths(|| format!(#desc).into())
422422
}
423423
};
424424

425425
impls.extend(quote! {
426-
impl<'tcx> QueryDescription<TyCtxt<'tcx>> for queries::#name<'tcx> {
426+
impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::#name<'tcx> {
427427
#desc
428428
#cache
429429
}

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
//!
5656
//! [dependency graph]: https://rustc-dev-guide.rust-lang.org/query.html
5757
58+
use crate::ty::query::QueryCtxt;
5859
use crate::ty::TyCtxt;
5960

6061
use rustc_data_structures::fingerprint::Fingerprint;
@@ -261,7 +262,7 @@ pub mod dep_kind {
261262

262263
if let Some(key) = recover(tcx, dep_node) {
263264
force_query::<queries::$variant<'_>, _>(
264-
tcx,
265+
QueryCtxt(tcx),
265266
key,
266267
DUMMY_SP,
267268
*dep_node
@@ -287,7 +288,7 @@ pub mod dep_kind {
287288
.unwrap_or(false));
288289

289290
let key = recover(tcx, dep_node).unwrap_or_else(|| panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash));
290-
if queries::$variant::cache_on_disk(tcx, &key, None) {
291+
if queries::$variant::cache_on_disk(QueryCtxt(tcx), &key, None) {
291292
let _ = tcx.$variant(key);
292293
}
293294
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::traits::query::{
77
CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal,
88
};
99
use crate::ty::query::queries;
10+
use crate::ty::query::QueryCtxt;
1011
use crate::ty::subst::{GenericArg, SubstsRef};
1112
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
1213
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::ty::query::QueryCtxt;
12
use crate::ty::tls;
23

34
use rustc_query_system::query::deadlock;
@@ -20,7 +21,7 @@ pub unsafe fn handle_deadlock() {
2021
thread::spawn(move || {
2122
tls::enter_context(icx, |_| {
2223
rustc_span::SESSION_GLOBALS
23-
.set(session_globals, || tls::with(|tcx| deadlock(tcx, &registry)))
24-
})
24+
.set(session_globals, || tls::with(|tcx| deadlock(QueryCtxt(tcx), &registry)))
25+
});
2526
});
2627
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use std::sync::Arc;
6060

6161
#[macro_use]
6262
mod plumbing;
63+
pub use plumbing::QueryCtxt;
6364
pub(crate) use rustc_query_system::query::CycleError;
6465
use rustc_query_system::query::*;
6566

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState};
33
use crate::mir::{self, interpret};
44
use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder};
55
use crate::ty::context::TyCtxt;
6+
use crate::ty::query::QueryCtxt;
67
use crate::ty::{self, Ty};
78
use rustc_data_structures::fingerprint::{Fingerprint, FingerprintDecoder, FingerprintEncoder};
89
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
@@ -312,7 +313,7 @@ impl<'sess> OnDiskCache<'sess> {
312313
($($query:ident,)*) => {
313314
$(
314315
encode_query_results::<ty::query::queries::$query<'_>>(
315-
tcx,
316+
QueryCtxt(tcx),
316317
enc,
317318
qri
318319
)?;
@@ -1230,12 +1231,12 @@ impl<'a> Decodable<opaque::Decoder<'a>> for IntEncodedWithFixedSize {
12301231
}
12311232

12321233
fn encode_query_results<'a, 'tcx, Q>(
1233-
tcx: TyCtxt<'tcx>,
1234+
tcx: QueryCtxt<'tcx>,
12341235
encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>,
12351236
query_result_index: &mut EncodedQueryResultIndex,
12361237
) -> FileEncodeResult
12371238
where
1238-
Q: super::QueryDescription<TyCtxt<'tcx>> + super::QueryAccessors<TyCtxt<'tcx>>,
1239+
Q: super::QueryDescription<QueryCtxt<'tcx>> + super::QueryAccessors<QueryCtxt<'tcx>>,
12391240
Q::Value: Encodable<CacheEncoder<'a, 'tcx, FileEncoder>>,
12401241
{
12411242
let _timer = tcx

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

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use crate::ty::query::Query;
66
use crate::ty::tls::{self, ImplicitCtxt};
77
use crate::ty::{self, TyCtxt};
8+
use rustc_query_system::dep_graph::HasDepContext;
89
use rustc_query_system::query::QueryContext;
910
use rustc_query_system::query::{CycleError, QueryJobId, QueryJobInfo};
1011

@@ -15,7 +16,29 @@ use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, Handler, Leve
1516
use rustc_span::def_id::DefId;
1617
use rustc_span::Span;
1718

18-
impl QueryContext for TyCtxt<'tcx> {
19+
#[derive(Copy, Clone)]
20+
pub struct QueryCtxt<'tcx>(pub TyCtxt<'tcx>);
21+
22+
impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
23+
type Target = TyCtxt<'tcx>;
24+
25+
fn deref(&self) -> &Self::Target {
26+
&self.0
27+
}
28+
}
29+
30+
impl HasDepContext for QueryCtxt<'tcx> {
31+
type DepKind = crate::dep_graph::DepKind;
32+
type StableHashingContext = crate::ich::StableHashingContext<'tcx>;
33+
type DepContext = TyCtxt<'tcx>;
34+
35+
#[inline]
36+
fn dep_context(&self) -> &Self::DepContext {
37+
&self.0
38+
}
39+
}
40+
41+
impl QueryContext for QueryCtxt<'tcx> {
1942
type Query = Query<'tcx>;
2043

2144
fn incremental_verify_ich(&self) -> bool {
@@ -26,11 +49,11 @@ impl QueryContext for TyCtxt<'tcx> {
2649
}
2750

2851
fn def_path_str(&self, def_id: DefId) -> String {
29-
TyCtxt::def_path_str(*self, def_id)
52+
self.0.def_path_str(def_id)
3053
}
3154

3255
fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
33-
tls::with_related_context(*self, |icx| icx.query)
56+
tls::with_related_context(**self, |icx| icx.query)
3457
}
3558

3659
fn try_collect_active_jobs(
@@ -53,10 +76,10 @@ impl QueryContext for TyCtxt<'tcx> {
5376
// The `TyCtxt` stored in TLS has the same global interner lifetime
5477
// as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
5578
// when accessing the `ImplicitCtxt`.
56-
tls::with_related_context(*self, move |current_icx| {
79+
tls::with_related_context(**self, move |current_icx| {
5780
// Update the `ImplicitCtxt` to point to our new query job.
5881
let new_icx = ImplicitCtxt {
59-
tcx: *self,
82+
tcx: **self,
6083
query: Some(token),
6184
diagnostics,
6285
layout_depth: current_icx.layout_depth,
@@ -71,7 +94,7 @@ impl QueryContext for TyCtxt<'tcx> {
7194
}
7295
}
7396

74-
impl<'tcx> TyCtxt<'tcx> {
97+
impl<'tcx> QueryCtxt<'tcx> {
7598
#[inline(never)]
7699
#[cold]
77100
pub(super) fn report_cycle(
@@ -81,7 +104,7 @@ impl<'tcx> TyCtxt<'tcx> {
81104
assert!(!stack.is_empty());
82105

83106
let fix_span = |span: Span, query: &Query<'tcx>| {
84-
self.sess.source_map().guess_head_span(query.default_span(self, span))
107+
self.sess.source_map().guess_head_span(query.default_span(*self, span))
85108
};
86109

87110
// Disable naming impls with types in this path, since that
@@ -119,7 +142,9 @@ impl<'tcx> TyCtxt<'tcx> {
119142
err
120143
})
121144
}
145+
}
122146

147+
impl<'tcx> TyCtxt<'tcx> {
123148
pub fn try_print_query_stack(handler: &Handler, num_frames: Option<usize>) {
124149
eprintln!("query stack during panic:");
125150

@@ -149,7 +174,7 @@ impl<'tcx> TyCtxt<'tcx> {
149174
"#{} [{}] {}",
150175
i,
151176
query_info.info.query.name(),
152-
query_info.info.query.describe(icx.tcx)
177+
query_info.info.query.describe(QueryCtxt(icx.tcx))
153178
),
154179
);
155180
diag.span =
@@ -272,7 +297,7 @@ macro_rules! define_queries {
272297
}
273298
}
274299

275-
pub fn describe(&self, tcx: TyCtxt<$tcx>) -> String {
300+
pub(crate) fn describe(&self, tcx: QueryCtxt<$tcx>) -> String {
276301
let (r, name) = match *self {
277302
$(Query::$name(key) => {
278303
(queries::$name::describe(tcx, key), stringify!($name))
@@ -362,35 +387,35 @@ macro_rules! define_queries {
362387
const NAME: &'static str = stringify!($name);
363388
}
364389

365-
impl<$tcx> QueryAccessors<TyCtxt<$tcx>> for queries::$name<$tcx> {
390+
impl<$tcx> QueryAccessors<QueryCtxt<$tcx>> for queries::$name<$tcx> {
366391
const ANON: bool = is_anon!([$($modifiers)*]);
367392
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
368393
const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$name;
369394

370395
type Cache = query_storage::$name<$tcx>;
371396

372397
#[inline(always)]
373-
fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Query<$tcx>, Self::Key> {
398+
fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Query<$tcx>, Self::Key> {
374399
&tcx.queries.$name
375400
}
376401

377402
#[inline(always)]
378-
fn query_cache<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache>
403+
fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache>
379404
where 'tcx:'a
380405
{
381406
&tcx.query_caches.$name
382407
}
383408

384409
#[inline]
385-
fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
410+
fn compute(tcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
386411
let provider = tcx.queries.providers.get(key.query_crate())
387412
// HACK(eddyb) it's possible crates may be loaded after
388413
// the query engine is created, and because crate loading
389414
// is not yet integrated with the query engine, such crates
390415
// would be missing appropriate entries in `providers`.
391416
.unwrap_or(&tcx.queries.fallback_extern_providers)
392417
.$name;
393-
provider(tcx, key)
418+
provider(*tcx, key)
394419
}
395420

396421
fn hash_result(
@@ -401,7 +426,7 @@ macro_rules! define_queries {
401426
}
402427

403428
fn handle_cycle_error(
404-
tcx: TyCtxt<'tcx>,
429+
tcx: QueryCtxt<'tcx>,
405430
error: CycleError<Query<'tcx>>
406431
) -> Self::Value {
407432
handle_cycle_error!([$($modifiers)*][tcx, error])
@@ -425,7 +450,8 @@ macro_rules! define_queries {
425450
Err(lookup) => lookup,
426451
};
427452

428-
get_query::<queries::$name<'_>, _>(self.tcx, DUMMY_SP, key, lookup, QueryMode::Ensure);
453+
let qcx = QueryCtxt(self.tcx);
454+
get_query::<queries::$name<'_>, _>(qcx, DUMMY_SP, key, lookup, QueryMode::Ensure);
429455
})*
430456
}
431457

@@ -516,7 +542,8 @@ macro_rules! define_queries {
516542
Err(lookup) => lookup,
517543
};
518544

519-
get_query::<queries::$name<'_>, _>(self.tcx, self.span, key, lookup, QueryMode::Get).unwrap()
545+
let qcx = QueryCtxt(self.tcx);
546+
get_query::<queries::$name<'_>, _>(qcx, self.span, key, lookup, QueryMode::Get).unwrap()
520547
})*
521548
}
522549

@@ -558,12 +585,12 @@ macro_rules! define_queries_struct {
558585

559586
pub(crate) fn try_collect_active_jobs(
560587
&self
561-
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, <TyCtxt<$tcx> as QueryContext>::Query>>> {
588+
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, Query<$tcx>>>> {
562589
let mut jobs = FxHashMap::default();
563590

564591
$(
565592
self.$name.try_collect_active_jobs(
566-
<queries::$name<'tcx> as QueryAccessors<TyCtxt<'tcx>>>::DEP_KIND,
593+
<queries::$name<'tcx> as QueryAccessors<QueryCtxt<'tcx>>>::DEP_KIND,
567594
Query::$name,
568595
&mut jobs,
569596
)?;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::ty::query::queries;
1+
use crate::ty::query::query_storage;
22
use crate::ty::TyCtxt;
33
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
4-
use rustc_query_system::query::{QueryAccessors, QueryCache, QueryCacheStore};
4+
use rustc_query_system::query::{QueryCache, QueryCacheStore};
55

66
use std::any::type_name;
77
use std::mem;
@@ -125,7 +125,7 @@ macro_rules! print_stats {
125125

126126
$(
127127
queries.push(stats::<
128-
<queries::$name<'_> as QueryAccessors<TyCtxt<'_>>>::Cache,
128+
query_storage::$name<'_>,
129129
>(
130130
stringify!($name),
131131
&tcx.query_caches.$name,

0 commit comments

Comments
 (0)