Skip to content

Commit 243944c

Browse files
committed
Remove QueryContext.
There is a type `QueryCtxt`, which impls the trait `QueryContext`. Confusingly, there is another type `QueryContext`. The latter is (like `TyCtxt`) just a pointer to a `GlobalContext`. It's not used much, e.g. its `impl` block has a single method. This commit removes `QueryContext`, replacing its use with direct `GlobalCtxt` use.
1 parent afbe167 commit 243944c

File tree

4 files changed

+33
-36
lines changed

4 files changed

+33
-36
lines changed

compiler/rustc_interface/src/passes.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -738,30 +738,16 @@ pub static DEFAULT_EXTERN_QUERY_PROVIDERS: LazyLock<ExternProviders> = LazyLock:
738738
extern_providers
739739
});
740740

741-
pub struct QueryContext<'tcx> {
742-
gcx: &'tcx GlobalCtxt<'tcx>,
743-
}
744-
745-
impl<'tcx> QueryContext<'tcx> {
746-
pub fn enter<F, R>(&mut self, f: F) -> R
747-
where
748-
F: FnOnce(TyCtxt<'tcx>) -> R,
749-
{
750-
let icx = ty::tls::ImplicitCtxt::new(self.gcx);
751-
ty::tls::enter_context(&icx, || f(icx.tcx))
752-
}
753-
}
754-
755741
pub fn create_global_ctxt<'tcx>(
756742
compiler: &'tcx Compiler,
757743
lint_store: Lrc<LintStore>,
758744
dep_graph: DepGraph,
759745
untracked: Untracked,
760746
queries: &'tcx OnceCell<TcxQueries<'tcx>>,
761-
global_ctxt: &'tcx OnceCell<GlobalCtxt<'tcx>>,
747+
gcx_cell: &'tcx OnceCell<GlobalCtxt<'tcx>>,
762748
arena: &'tcx WorkerLocal<Arena<'tcx>>,
763749
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
764-
) -> QueryContext<'tcx> {
750+
) -> &'tcx GlobalCtxt<'tcx> {
765751
// We're constructing the HIR here; we don't care what we will
766752
// read, since we haven't even constructed the *input* to
767753
// incr. comp. yet.
@@ -785,8 +771,8 @@ pub fn create_global_ctxt<'tcx>(
785771
TcxQueries::new(local_providers, extern_providers, query_result_on_disk_cache)
786772
});
787773

788-
let gcx = sess.time("setup_global_ctxt", || {
789-
global_ctxt.get_or_init(move || {
774+
sess.time("setup_global_ctxt", || {
775+
gcx_cell.get_or_init(move || {
790776
TyCtxt::create_global_ctxt(
791777
sess,
792778
lint_store,
@@ -799,9 +785,7 @@ pub fn create_global_ctxt<'tcx>(
799785
rustc_query_impl::query_callbacks(arena),
800786
)
801787
})
802-
});
803-
804-
QueryContext { gcx }
788+
})
805789
}
806790

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

compiler/rustc_interface/src/queries.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
22
use crate::interface::{Compiler, Result};
3-
use crate::passes::{self, BoxedResolver, QueryContext};
3+
use crate::passes::{self, BoxedResolver};
44

55
use rustc_ast as ast;
66
use rustc_codegen_ssa::traits::CodegenBackend;
@@ -64,7 +64,7 @@ impl<'a, T> std::ops::DerefMut for QueryResult<'a, T> {
6464
}
6565
}
6666

67-
impl<'a, 'tcx> QueryResult<'a, QueryContext<'tcx>> {
67+
impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
6868
pub fn enter<T>(&mut self, f: impl FnOnce(TyCtxt<'tcx>) -> T) -> T {
6969
(*self.0).get_mut().enter(f)
7070
}
@@ -78,7 +78,7 @@ impl<T> Default for Query<T> {
7878

7979
pub struct Queries<'tcx> {
8080
compiler: &'tcx Compiler,
81-
gcx: OnceCell<GlobalCtxt<'tcx>>,
81+
gcx_cell: OnceCell<GlobalCtxt<'tcx>>,
8282
queries: OnceCell<TcxQueries<'tcx>>,
8383

8484
arena: WorkerLocal<Arena<'tcx>>,
@@ -90,15 +90,16 @@ pub struct Queries<'tcx> {
9090
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
9191
expansion: Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>,
9292
dep_graph: Query<DepGraph>,
93-
global_ctxt: Query<QueryContext<'tcx>>,
93+
// This just points to what's in `gcx_cell`.
94+
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
9495
ongoing_codegen: Query<Box<dyn Any>>,
9596
}
9697

9798
impl<'tcx> Queries<'tcx> {
9899
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
99100
Queries {
100101
compiler,
101-
gcx: OnceCell::new(),
102+
gcx_cell: OnceCell::new(),
102103
queries: OnceCell::new(),
103104
arena: WorkerLocal::new(|_| Arena::default()),
104105
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
@@ -108,7 +109,7 @@ impl<'tcx> Queries<'tcx> {
108109
register_plugins: Default::default(),
109110
expansion: Default::default(),
110111
dep_graph: Default::default(),
111-
global_ctxt: Default::default(),
112+
gcx: Default::default(),
112113
ongoing_codegen: Default::default(),
113114
}
114115
}
@@ -207,8 +208,8 @@ impl<'tcx> Queries<'tcx> {
207208
})
208209
}
209210

210-
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, QueryContext<'tcx>>> {
211-
self.global_ctxt.compute(|| {
211+
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
212+
self.gcx.compute(|| {
212213
let crate_name = *self.crate_name()?.borrow();
213214
let (krate, resolver, lint_store) = self.expansion()?.steal();
214215

@@ -218,18 +219,18 @@ impl<'tcx> Queries<'tcx> {
218219
ast_lowering: untracked_resolver_for_lowering,
219220
} = BoxedResolver::to_resolver_outputs(resolver);
220221

221-
let mut qcx = passes::create_global_ctxt(
222+
let gcx = passes::create_global_ctxt(
222223
self.compiler,
223224
lint_store,
224225
self.dep_graph()?.steal(),
225226
untracked,
226227
&self.queries,
227-
&self.gcx,
228+
&self.gcx_cell,
228229
&self.arena,
229230
&self.hir_arena,
230231
);
231232

232-
qcx.enter(|tcx| {
233+
gcx.enter(|tcx| {
233234
let feed = tcx.feed_unit_query();
234235
feed.resolver_for_lowering(
235236
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))),
@@ -239,7 +240,7 @@ impl<'tcx> Queries<'tcx> {
239240
let feed = tcx.feed_local_crate();
240241
feed.crate_name(crate_name);
241242
});
242-
Ok(qcx)
243+
Ok(gcx)
243244
})
244245
}
245246

@@ -387,7 +388,7 @@ impl Compiler {
387388

388389
// NOTE: intentionally does not compute the global context if it hasn't been built yet,
389390
// since that likely means there was a parse error.
390-
if let Some(Ok(gcx)) = &mut *queries.global_ctxt.result.borrow_mut() {
391+
if let Some(Ok(gcx)) = &mut *queries.gcx.result.borrow_mut() {
391392
let gcx = gcx.get_mut();
392393
// We assume that no queries are run past here. If there are new queries
393394
// after this point, they'll show up as "<unknown>" in self-profiling data.

compiler/rustc_middle/src/ty/context.rs

+12
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ pub struct GlobalCtxt<'tcx> {
468468
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
469469
}
470470

471+
impl<'tcx> GlobalCtxt<'tcx> {
472+
/// Installs `self` in a `TyCtxt` and `ImplicitCtxt` for the duration of
473+
/// `f`.
474+
pub fn enter<'a: 'tcx, F, R>(&'a self, f: F) -> R
475+
where
476+
F: FnOnce(TyCtxt<'tcx>) -> R,
477+
{
478+
let icx = tls::ImplicitCtxt::new(self);
479+
tls::enter_context(&icx, || f(icx.tcx))
480+
}
481+
}
482+
471483
impl<'tcx> TyCtxt<'tcx> {
472484
/// Expects a body and returns its codegen attributes.
473485
///

src/librustdoc/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,9 @@ fn main_args(at_args: &[String]) -> MainResult {
814814
sess.fatal("Compilation failed, aborting rustdoc");
815815
}
816816

817-
let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess);
817+
let mut gcx = abort_on_err(queries.global_ctxt(), sess);
818818

819-
global_ctxt.enter(|tcx| {
819+
gcx.enter(|tcx| {
820820
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
821821
core::run_global_ctxt(
822822
tcx,

0 commit comments

Comments
 (0)