1
Fork 0

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.
This commit is contained in:
Nicholas Nethercote 2023-02-07 16:59:50 +11:00
parent afbe167fbb
commit 243944c653
4 changed files with 33 additions and 36 deletions

View file

@ -738,30 +738,16 @@ pub static DEFAULT_EXTERN_QUERY_PROVIDERS: LazyLock<ExternProviders> = LazyLock:
extern_providers extern_providers
}); });
pub struct QueryContext<'tcx> {
gcx: &'tcx GlobalCtxt<'tcx>,
}
impl<'tcx> QueryContext<'tcx> {
pub fn enter<F, R>(&mut self, f: F) -> R
where
F: FnOnce(TyCtxt<'tcx>) -> R,
{
let icx = ty::tls::ImplicitCtxt::new(self.gcx);
ty::tls::enter_context(&icx, || f(icx.tcx))
}
}
pub fn create_global_ctxt<'tcx>( pub fn create_global_ctxt<'tcx>(
compiler: &'tcx Compiler, compiler: &'tcx Compiler,
lint_store: Lrc<LintStore>, lint_store: Lrc<LintStore>,
dep_graph: DepGraph, dep_graph: DepGraph,
untracked: Untracked, untracked: Untracked,
queries: &'tcx OnceCell<TcxQueries<'tcx>>, queries: &'tcx OnceCell<TcxQueries<'tcx>>,
global_ctxt: &'tcx OnceCell<GlobalCtxt<'tcx>>, gcx_cell: &'tcx OnceCell<GlobalCtxt<'tcx>>,
arena: &'tcx WorkerLocal<Arena<'tcx>>, arena: &'tcx WorkerLocal<Arena<'tcx>>,
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>, hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
) -> QueryContext<'tcx> { ) -> &'tcx GlobalCtxt<'tcx> {
// We're constructing the HIR here; we don't care what we will // We're constructing the HIR here; we don't care what we will
// read, since we haven't even constructed the *input* to // read, since we haven't even constructed the *input* to
// incr. comp. yet. // incr. comp. yet.
@ -785,8 +771,8 @@ pub fn create_global_ctxt<'tcx>(
TcxQueries::new(local_providers, extern_providers, query_result_on_disk_cache) TcxQueries::new(local_providers, extern_providers, query_result_on_disk_cache)
}); });
let gcx = sess.time("setup_global_ctxt", || { sess.time("setup_global_ctxt", || {
global_ctxt.get_or_init(move || { gcx_cell.get_or_init(move || {
TyCtxt::create_global_ctxt( TyCtxt::create_global_ctxt(
sess, sess,
lint_store, lint_store,
@ -799,9 +785,7 @@ pub fn create_global_ctxt<'tcx>(
rustc_query_impl::query_callbacks(arena), rustc_query_impl::query_callbacks(arena),
) )
}) })
}); })
QueryContext { gcx }
} }
/// Runs the resolution, type-checking, region checking and other /// Runs the resolution, type-checking, region checking and other

View file

@ -1,6 +1,6 @@
use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation}; use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
use crate::interface::{Compiler, Result}; use crate::interface::{Compiler, Result};
use crate::passes::{self, BoxedResolver, QueryContext}; use crate::passes::{self, BoxedResolver};
use rustc_ast as ast; use rustc_ast as ast;
use rustc_codegen_ssa::traits::CodegenBackend; use rustc_codegen_ssa::traits::CodegenBackend;
@ -64,7 +64,7 @@ impl<'a, T> std::ops::DerefMut for QueryResult<'a, T> {
} }
} }
impl<'a, 'tcx> QueryResult<'a, QueryContext<'tcx>> { impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
pub fn enter<T>(&mut self, f: impl FnOnce(TyCtxt<'tcx>) -> T) -> T { pub fn enter<T>(&mut self, f: impl FnOnce(TyCtxt<'tcx>) -> T) -> T {
(*self.0).get_mut().enter(f) (*self.0).get_mut().enter(f)
} }
@ -78,7 +78,7 @@ impl<T> Default for Query<T> {
pub struct Queries<'tcx> { pub struct Queries<'tcx> {
compiler: &'tcx Compiler, compiler: &'tcx Compiler,
gcx: OnceCell<GlobalCtxt<'tcx>>, gcx_cell: OnceCell<GlobalCtxt<'tcx>>,
queries: OnceCell<TcxQueries<'tcx>>, queries: OnceCell<TcxQueries<'tcx>>,
arena: WorkerLocal<Arena<'tcx>>, arena: WorkerLocal<Arena<'tcx>>,
@ -90,7 +90,8 @@ pub struct Queries<'tcx> {
register_plugins: Query<(ast::Crate, Lrc<LintStore>)>, register_plugins: Query<(ast::Crate, Lrc<LintStore>)>,
expansion: Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>, expansion: Query<(Lrc<ast::Crate>, Rc<RefCell<BoxedResolver>>, Lrc<LintStore>)>,
dep_graph: Query<DepGraph>, dep_graph: Query<DepGraph>,
global_ctxt: Query<QueryContext<'tcx>>, // This just points to what's in `gcx_cell`.
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
ongoing_codegen: Query<Box<dyn Any>>, ongoing_codegen: Query<Box<dyn Any>>,
} }
@ -98,7 +99,7 @@ impl<'tcx> Queries<'tcx> {
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> { pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
Queries { Queries {
compiler, compiler,
gcx: OnceCell::new(), gcx_cell: OnceCell::new(),
queries: OnceCell::new(), queries: OnceCell::new(),
arena: WorkerLocal::new(|_| Arena::default()), arena: WorkerLocal::new(|_| Arena::default()),
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()), hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
@ -108,7 +109,7 @@ impl<'tcx> Queries<'tcx> {
register_plugins: Default::default(), register_plugins: Default::default(),
expansion: Default::default(), expansion: Default::default(),
dep_graph: Default::default(), dep_graph: Default::default(),
global_ctxt: Default::default(), gcx: Default::default(),
ongoing_codegen: Default::default(), ongoing_codegen: Default::default(),
} }
} }
@ -207,8 +208,8 @@ impl<'tcx> Queries<'tcx> {
}) })
} }
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, QueryContext<'tcx>>> { pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
self.global_ctxt.compute(|| { self.gcx.compute(|| {
let crate_name = *self.crate_name()?.borrow(); let crate_name = *self.crate_name()?.borrow();
let (krate, resolver, lint_store) = self.expansion()?.steal(); let (krate, resolver, lint_store) = self.expansion()?.steal();
@ -218,18 +219,18 @@ impl<'tcx> Queries<'tcx> {
ast_lowering: untracked_resolver_for_lowering, ast_lowering: untracked_resolver_for_lowering,
} = BoxedResolver::to_resolver_outputs(resolver); } = BoxedResolver::to_resolver_outputs(resolver);
let mut qcx = passes::create_global_ctxt( let gcx = passes::create_global_ctxt(
self.compiler, self.compiler,
lint_store, lint_store,
self.dep_graph()?.steal(), self.dep_graph()?.steal(),
untracked, untracked,
&self.queries, &self.queries,
&self.gcx, &self.gcx_cell,
&self.arena, &self.arena,
&self.hir_arena, &self.hir_arena,
); );
qcx.enter(|tcx| { gcx.enter(|tcx| {
let feed = tcx.feed_unit_query(); let feed = tcx.feed_unit_query();
feed.resolver_for_lowering( feed.resolver_for_lowering(
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))), tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))),
@ -239,7 +240,7 @@ impl<'tcx> Queries<'tcx> {
let feed = tcx.feed_local_crate(); let feed = tcx.feed_local_crate();
feed.crate_name(crate_name); feed.crate_name(crate_name);
}); });
Ok(qcx) Ok(gcx)
}) })
} }
@ -387,7 +388,7 @@ impl Compiler {
// NOTE: intentionally does not compute the global context if it hasn't been built yet, // NOTE: intentionally does not compute the global context if it hasn't been built yet,
// since that likely means there was a parse error. // since that likely means there was a parse error.
if let Some(Ok(gcx)) = &mut *queries.global_ctxt.result.borrow_mut() { if let Some(Ok(gcx)) = &mut *queries.gcx.result.borrow_mut() {
let gcx = gcx.get_mut(); let gcx = gcx.get_mut();
// We assume that no queries are run past here. If there are new queries // We assume that no queries are run past here. If there are new queries
// after this point, they'll show up as "<unknown>" in self-profiling data. // after this point, they'll show up as "<unknown>" in self-profiling data.

View file

@ -468,6 +468,18 @@ pub struct GlobalCtxt<'tcx> {
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>, pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
} }
impl<'tcx> GlobalCtxt<'tcx> {
/// Installs `self` in a `TyCtxt` and `ImplicitCtxt` for the duration of
/// `f`.
pub fn enter<'a: 'tcx, F, R>(&'a self, f: F) -> R
where
F: FnOnce(TyCtxt<'tcx>) -> R,
{
let icx = tls::ImplicitCtxt::new(self);
tls::enter_context(&icx, || f(icx.tcx))
}
}
impl<'tcx> TyCtxt<'tcx> { impl<'tcx> TyCtxt<'tcx> {
/// Expects a body and returns its codegen attributes. /// Expects a body and returns its codegen attributes.
/// ///

View file

@ -814,9 +814,9 @@ fn main_args(at_args: &[String]) -> MainResult {
sess.fatal("Compilation failed, aborting rustdoc"); sess.fatal("Compilation failed, aborting rustdoc");
} }
let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess); let mut gcx = abort_on_err(queries.global_ctxt(), sess);
global_ctxt.enter(|tcx| { gcx.enter(|tcx| {
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || { let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
core::run_global_ctxt( core::run_global_ctxt(
tcx, tcx,