Skip to content

Commit 5d71b99

Browse files
committed
Make QueryEngine opaque to TyCtxt.
1 parent 3f868b1 commit 5d71b99

File tree

12 files changed

+99
-63
lines changed

12 files changed

+99
-63
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3890,6 +3890,7 @@ dependencies = [
38903890
"rustc_expand",
38913891
"rustc_hir",
38923892
"rustc_incremental",
3893+
"rustc_index",
38933894
"rustc_lint",
38943895
"rustc_metadata",
38953896
"rustc_middle",

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }
3030
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
3131
rustc_codegen_llvm = { path = "../rustc_codegen_llvm", optional = true }
3232
rustc_hir = { path = "../rustc_hir" }
33+
rustc_index = { path = "../rustc_index" }
3334
rustc_metadata = { path = "../rustc_metadata" }
3435
rustc_mir = { path = "../rustc_mir" }
3536
rustc_mir_build = { path = "../rustc_mir_build" }

compiler/rustc_interface/src/passes.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ use rustc_expand::base::ExtCtxt;
1515
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
1616
use rustc_hir::definitions::Definitions;
1717
use rustc_hir::Crate;
18+
use rustc_index::vec::IndexVec;
1819
use rustc_lint::LintStore;
1920
use rustc_middle::arena::Arena;
2021
use rustc_middle::dep_graph::DepGraph;
2122
use rustc_middle::middle;
2223
use rustc_middle::middle::cstore::{CrateStore, MetadataLoader, MetadataLoaderDyn};
24+
use rustc_middle::ty::query;
2325
use rustc_middle::ty::query::Providers;
2426
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
2527
use rustc_mir as mir;
@@ -738,20 +740,18 @@ pub static DEFAULT_EXTERN_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|
738740
extern_providers
739741
});
740742

741-
pub struct QueryContext<'tcx>(&'tcx GlobalCtxt<'tcx>);
743+
pub struct QueryContext<'tcx> {
744+
gcx: &'tcx GlobalCtxt<'tcx>,
745+
}
742746

743747
impl<'tcx> QueryContext<'tcx> {
744748
pub fn enter<F, R>(&mut self, f: F) -> R
745749
where
746750
F: FnOnce(TyCtxt<'tcx>) -> R,
747751
{
748-
let icx = ty::tls::ImplicitCtxt::new(self.0);
752+
let icx = ty::tls::ImplicitCtxt::new(self.gcx);
749753
ty::tls::enter_context(&icx, |_| f(icx.tcx))
750754
}
751-
752-
pub fn print_stats(&mut self) {
753-
self.enter(ty::query::print_stats)
754-
}
755755
}
756756

757757
pub fn create_global_ctxt<'tcx>(
@@ -762,6 +762,7 @@ pub fn create_global_ctxt<'tcx>(
762762
mut resolver_outputs: ResolverOutputs,
763763
outputs: OutputFilenames,
764764
crate_name: &str,
765+
queries: &'tcx OnceCell<query::Queries<'tcx>>,
765766
global_ctxt: &'tcx OnceCell<GlobalCtxt<'tcx>>,
766767
arena: &'tcx WorkerLocal<Arena<'tcx>>,
767768
) -> QueryContext<'tcx> {
@@ -785,26 +786,33 @@ pub fn create_global_ctxt<'tcx>(
785786
callback(sess, &mut local_providers, &mut extern_providers);
786787
}
787788

789+
let queries = {
790+
let crates = resolver_outputs.cstore.crates_untracked();
791+
let max_cnum = crates.iter().map(|c| c.as_usize()).max().unwrap_or(0);
792+
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
793+
providers[LOCAL_CRATE] = local_providers;
794+
queries.get_or_init(|| query::Queries::new(providers, extern_providers))
795+
};
796+
788797
let gcx = sess.time("setup_global_ctxt", || {
789798
global_ctxt.get_or_init(|| {
790799
TyCtxt::create_global_ctxt(
791800
sess,
792801
lint_store,
793-
local_providers,
794-
extern_providers,
795802
arena,
796803
resolver_outputs,
797804
krate,
798805
defs,
799806
dep_graph,
800807
query_result_on_disk_cache,
808+
queries,
801809
&crate_name,
802810
&outputs,
803811
)
804812
})
805813
});
806814

807-
QueryContext(gcx)
815+
QueryContext { gcx }
808816
}
809817

810818
/// Runs the resolution, type-checking, region checking and other

compiler/rustc_interface/src/queries.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_incremental::DepGraphFuture;
1313
use rustc_lint::LintStore;
1414
use rustc_middle::arena::Arena;
1515
use rustc_middle::dep_graph::DepGraph;
16+
use rustc_middle::ty::query;
1617
use rustc_middle::ty::{GlobalCtxt, ResolverOutputs, TyCtxt};
1718
use rustc_serialize::json;
1819
use rustc_session::config::{self, OutputFilenames, OutputType};
@@ -71,6 +72,7 @@ impl<T> Default for Query<T> {
7172
pub struct Queries<'tcx> {
7273
compiler: &'tcx Compiler,
7374
gcx: OnceCell<GlobalCtxt<'tcx>>,
75+
queries: OnceCell<query::Queries<'tcx>>,
7476

7577
arena: WorkerLocal<Arena<'tcx>>,
7678
hir_arena: WorkerLocal<rustc_ast_lowering::Arena<'tcx>>,
@@ -92,6 +94,7 @@ impl<'tcx> Queries<'tcx> {
9294
Queries {
9395
compiler,
9496
gcx: OnceCell::new(),
97+
queries: OnceCell::new(),
9598
arena: WorkerLocal::new(|_| Arena::default()),
9699
hir_arena: WorkerLocal::new(|_| rustc_ast_lowering::Arena::default()),
97100
dep_graph_future: Default::default(),
@@ -265,6 +268,7 @@ impl<'tcx> Queries<'tcx> {
265268
resolver_outputs.steal(),
266269
outputs,
267270
&crate_name,
271+
&self.queries,
268272
&self.gcx,
269273
&self.arena,
270274
))
@@ -429,7 +433,7 @@ impl Compiler {
429433
}
430434

431435
if self.session().opts.debugging_opts.query_stats {
432-
gcx.print_stats();
436+
gcx.enter(query::print_stats);
433437
}
434438
}
435439

compiler/rustc_macros/src/query.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,9 @@ fn add_query_description_impl(
354354
quote! {
355355
#[inline]
356356
fn try_load_from_disk(
357-
tcx: QueryCtxt<'tcx>,
358-
id: SerializedDepNodeIndex
357+
#tcx: QueryCtxt<'tcx>,
358+
#id: SerializedDepNodeIndex
359359
) -> Option<Self::Value> {
360-
let (#tcx, #id) = (*tcx, id);
361360
#block
362361
}
363362
}
@@ -394,11 +393,10 @@ fn add_query_description_impl(
394393
#[inline]
395394
#[allow(unused_variables, unused_braces)]
396395
fn cache_on_disk(
397-
tcx: QueryCtxt<'tcx>,
398-
key: &Self::Key,
399-
value: Option<&Self::Value>
396+
#tcx: QueryCtxt<'tcx>,
397+
#key: &Self::Key,
398+
#value: Option<&Self::Value>
400399
) -> bool {
401-
let (#tcx, #key, #value) = (*tcx, key, value);
402400
#expr
403401
}
404402

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ pub mod dep_kind {
262262

263263
if let Some(key) = recover(tcx, dep_node) {
264264
force_query::<queries::$variant<'_>, _>(
265-
QueryCtxt(tcx),
265+
QueryCtxt { tcx, queries: tcx.queries },
266266
key,
267267
DUMMY_SP,
268268
*dep_node
@@ -288,7 +288,8 @@ pub mod dep_kind {
288288
.unwrap_or(false));
289289

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

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ rustc_queries! {
699699
load_cached(tcx, id) {
700700
let typeck_results: Option<ty::TypeckResults<'tcx>> = tcx
701701
.on_disk_cache.as_ref()
702-
.and_then(|c| c.try_load_query_result(tcx, id));
702+
.and_then(|c| c.try_load_query_result(*tcx, id));
703703

704704
typeck_results.map(|x| &*tcx.arena.alloc(x))
705705
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::middle::stability;
1414
use crate::mir::interpret::{self, Allocation, ConstValue, Scalar};
1515
use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted};
1616
use crate::traits;
17-
use crate::ty::query::{self, OnDiskCache, TyCtxtAt};
17+
use crate::ty::query::{self, OnDiskCache, Queries, TyCtxtAt};
1818
use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts};
1919
use crate::ty::TyKind::*;
2020
use crate::ty::{
@@ -968,7 +968,7 @@ pub struct GlobalCtxt<'tcx> {
968968
/// This is `None` if we are not incremental compilation mode
969969
pub(crate) on_disk_cache: Option<OnDiskCache<'tcx>>,
970970

971-
pub queries: query::Queries<'tcx>,
971+
pub queries: &'tcx query::Queries<'tcx>,
972972
pub query_caches: query::QueryCaches<'tcx>,
973973

974974
maybe_unused_trait_imports: FxHashSet<LocalDefId>,
@@ -1109,14 +1109,13 @@ impl<'tcx> TyCtxt<'tcx> {
11091109
pub fn create_global_ctxt(
11101110
s: &'tcx Session,
11111111
lint_store: Lrc<dyn Any + sync::Send + sync::Sync>,
1112-
local_providers: ty::query::Providers,
1113-
extern_providers: ty::query::Providers,
11141112
arena: &'tcx WorkerLocal<Arena<'tcx>>,
11151113
resolutions: ty::ResolverOutputs,
11161114
krate: &'tcx hir::Crate<'tcx>,
11171115
definitions: &'tcx Definitions,
11181116
dep_graph: DepGraph,
11191117
on_disk_cache: Option<query::OnDiskCache<'tcx>>,
1118+
queries: &'tcx Queries<'tcx>,
11201119
crate_name: &str,
11211120
output_filenames: &OutputFilenames,
11221121
) -> GlobalCtxt<'tcx> {
@@ -1128,10 +1127,6 @@ impl<'tcx> TyCtxt<'tcx> {
11281127
let common_lifetimes = CommonLifetimes::new(&interners);
11291128
let common_consts = CommonConsts::new(&interners, &common_types);
11301129
let cstore = resolutions.cstore;
1131-
let crates = cstore.crates_untracked();
1132-
let max_cnum = crates.iter().map(|c| c.as_usize()).max().unwrap_or(0);
1133-
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
1134-
providers[LOCAL_CRATE] = local_providers;
11351130

11361131
let mut trait_map: FxHashMap<_, FxHashMap<_, _>> = FxHashMap::default();
11371132
for (hir_id, v) in krate.trait_map.iter() {
@@ -1161,7 +1156,7 @@ impl<'tcx> TyCtxt<'tcx> {
11611156
untracked_crate: krate,
11621157
definitions,
11631158
on_disk_cache,
1164-
queries: query::Queries::new(providers, extern_providers),
1159+
queries,
11651160
query_caches: query::QueryCaches::default(),
11661161
ty_rcache: Default::default(),
11671162
pred_rcache: Default::default(),

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::ty::query::QueryCtxt;
21
use crate::ty::tls;
3-
4-
use rustc_query_system::query::deadlock;
52
use rustc_rayon_core as rayon_core;
63
use std::thread;
74

@@ -21,7 +18,7 @@ pub unsafe fn handle_deadlock() {
2118
thread::spawn(move || {
2219
tls::enter_context(icx, |_| {
2320
rustc_span::SESSION_GLOBALS
24-
.set(session_globals, || tls::with(|tcx| deadlock(QueryCtxt(tcx), &registry)))
21+
.set(session_globals, || tls::with(|tcx| tcx.queries.deadlock(tcx, &registry)))
2522
});
2623
});
2724
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId};
4444
use rustc_hir::lang_items::{LangItem, LanguageItems};
4545
use rustc_hir::{Crate, ItemLocalId, TraitCandidate};
4646
use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
47+
use rustc_serialize::opaque;
4748
use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
4849
use rustc_session::utils::NativeLibKind;
4950
use rustc_session::CrateDisambiguator;

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ struct Footer {
133133
foreign_def_path_hashes: UnhashMap<DefPathHash, RawDefId>,
134134
}
135135

136-
type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
136+
pub type EncodedQueryResultIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
137137
type EncodedDiagnosticsIndex = Vec<(SerializedDepNodeIndex, AbsoluteBytePos)>;
138138
type EncodedDiagnostics = Vec<Diagnostic>;
139139

140140
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable)]
141141
struct SourceFileIndex(u32);
142142

143143
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Encodable, Decodable)]
144-
struct AbsoluteBytePos(u32);
144+
pub struct AbsoluteBytePos(u32);
145145

146146
impl AbsoluteBytePos {
147147
fn new(pos: usize) -> AbsoluteBytePos {
@@ -308,22 +308,7 @@ impl<'sess> OnDiskCache<'sess> {
308308
tcx.sess.time("encode_query_results", || -> FileEncodeResult {
309309
let enc = &mut encoder;
310310
let qri = &mut query_result_index;
311-
312-
macro_rules! encode_queries {
313-
($($query:ident,)*) => {
314-
$(
315-
encode_query_results::<ty::query::queries::$query<'_>>(
316-
QueryCtxt(tcx),
317-
enc,
318-
qri
319-
)?;
320-
)*
321-
}
322-
}
323-
324-
rustc_cached_queries!(encode_queries!);
325-
326-
Ok(())
311+
tcx.queries.encode_query_results(tcx, enc, qri)
327312
})?;
328313

329314
// Encode diagnostics.
@@ -973,7 +958,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [Span] {
973958

974959
//- ENCODING -------------------------------------------------------------------
975960

976-
trait OpaqueEncoder: Encoder {
961+
pub trait OpaqueEncoder: Encoder {
977962
fn position(&self) -> usize;
978963
}
979964

@@ -985,7 +970,7 @@ impl OpaqueEncoder for FileEncoder {
985970
}
986971

987972
/// An encoder that can write to the incremental compilation cache.
988-
struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
973+
pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> {
989974
tcx: TyCtxt<'tcx>,
990975
encoder: &'a mut E,
991976
type_shorthands: FxHashMap<Ty<'tcx>, usize>,
@@ -1230,7 +1215,7 @@ impl<'a> Decodable<opaque::Decoder<'a>> for IntEncodedWithFixedSize {
12301215
}
12311216
}
12321217

1233-
fn encode_query_results<'a, 'tcx, Q>(
1218+
pub fn encode_query_results<'a, 'tcx, Q>(
12341219
tcx: QueryCtxt<'tcx>,
12351220
encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>,
12361221
query_result_index: &mut EncodedQueryResultIndex,

0 commit comments

Comments
 (0)