Skip to content

Commit 6ece803

Browse files
committed
Get rid of of the global_ctxt query
1 parent ed14192 commit 6ece803

File tree

6 files changed

+50
-72
lines changed

6 files changed

+50
-72
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use rustc_errors::registry::Registry;
4545
use rustc_errors::{ColorConfig, DiagCtxt, ErrCode, FatalError, PResult, markdown};
4646
use rustc_feature::find_gated_cfg;
4747
use rustc_interface::util::{self, get_codegen_backend};
48-
use rustc_interface::{Linker, interface, passes};
48+
use rustc_interface::{Linker, create_and_enter_global_ctxt, interface, passes};
4949
use rustc_lint::unerased_lint_store;
5050
use rustc_metadata::creader::MetadataLoader;
5151
use rustc_metadata::locator;
@@ -400,7 +400,9 @@ fn run_compiler(
400400
// If pretty printing is requested: Figure out the representation, print it and exit
401401
if let Some(pp_mode) = sess.opts.pretty {
402402
if pp_mode.needs_ast_map() {
403-
queries.global_ctxt().enter(|tcx| {
403+
let krate = queries.parse().steal();
404+
405+
create_and_enter_global_ctxt(&compiler, krate, |tcx| {
404406
tcx.ensure().early_lint_checks(());
405407
pretty::print(sess, pp_mode, pretty::PrintExtra::NeedsAstMap { tcx });
406408
passes::write_dep_info(tcx);
@@ -425,7 +427,9 @@ fn run_compiler(
425427
return early_exit();
426428
}
427429

428-
queries.global_ctxt().enter(|tcx| {
430+
let krate = queries.parse().steal();
431+
432+
create_and_enter_global_ctxt(&compiler, krate, |tcx| {
429433
// Make sure name resolution and macro expansion is run.
430434
let _ = tcx.resolver_for_lowering();
431435

compiler/rustc_interface/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// tidy-alphabetical-end
99

1010
mod callbacks;
11-
mod errors;
11+
pub mod errors;
1212
pub mod interface;
1313
pub mod passes;
1414
mod proc_macro_decls;
@@ -17,7 +17,7 @@ pub mod util;
1717

1818
pub use callbacks::setup_callbacks;
1919
pub use interface::{Config, run_compiler};
20-
pub use passes::DEFAULT_QUERY_PROVIDERS;
20+
pub use passes::{DEFAULT_QUERY_PROVIDERS, create_and_enter_global_ctxt};
2121
pub use queries::{Linker, Queries};
2222

2323
#[cfg(test)]

compiler/rustc_interface/src/passes.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use tracing::{info, instrument};
4141
use crate::interface::Compiler;
4242
use crate::{errors, proc_macro_decls, util};
4343

44-
pub(crate) fn parse<'a>(sess: &'a Session) -> ast::Crate {
44+
pub fn parse<'a>(sess: &'a Session) -> ast::Crate {
4545
let krate = sess
4646
.time("parse_crate", || {
4747
let mut parser = unwrap_or_emit_fatal(match &sess.io.input {
@@ -709,7 +709,22 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
709709
*providers
710710
});
711711

712-
pub(crate) fn create_global_ctxt<'tcx>(
712+
pub fn create_and_enter_global_ctxt<'tcx, T>(
713+
compiler: &'tcx Compiler,
714+
krate: rustc_ast::Crate,
715+
f: impl for<'a> FnOnce(TyCtxt<'a>) -> T,
716+
) -> T {
717+
let gcx_cell = OnceLock::new();
718+
let arena = WorkerLocal::new(|_| Arena::default());
719+
let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());
720+
721+
let gcx = create_global_ctxt(compiler, krate, &gcx_cell, &arena, &hir_arena);
722+
let ret = gcx.enter(f);
723+
gcx.finish();
724+
ret
725+
}
726+
727+
fn create_global_ctxt<'tcx>(
713728
compiler: &'tcx Compiler,
714729
mut krate: rustc_ast::Crate,
715730
gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,

compiler/rustc_interface/src/queries.rs

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use rustc_codegen_ssa::CodegenResults;
77
use rustc_codegen_ssa::traits::CodegenBackend;
88
use rustc_data_structures::steal::Steal;
99
use rustc_data_structures::svh::Svh;
10-
use rustc_data_structures::sync::{OnceLock, WorkerLocal};
1110
use rustc_hir::def_id::LOCAL_CRATE;
12-
use rustc_middle::arena::Arena;
1311
use rustc_middle::dep_graph::DepGraph;
1412
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
1513
use rustc_session::Session;
@@ -65,51 +63,18 @@ impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
6563

6664
pub struct Queries<'tcx> {
6765
compiler: &'tcx Compiler,
68-
gcx_cell: OnceLock<GlobalCtxt<'tcx>>,
69-
70-
arena: WorkerLocal<Arena<'tcx>>,
71-
hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>,
7266

7367
parse: Query<ast::Crate>,
74-
// This just points to what's in `gcx_cell`.
75-
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
7668
}
7769

7870
impl<'tcx> Queries<'tcx> {
7971
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
80-
Queries {
81-
compiler,
82-
gcx_cell: OnceLock::new(),
83-
arena: WorkerLocal::new(|_| Arena::default()),
84-
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
85-
parse: Query { result: RefCell::new(None) },
86-
gcx: Query { result: RefCell::new(None) },
87-
}
88-
}
89-
90-
pub fn finish(&'tcx self) {
91-
if let Some(gcx) = self.gcx_cell.get() {
92-
gcx.finish();
93-
}
72+
Queries { compiler, parse: Query { result: RefCell::new(None) } }
9473
}
9574

9675
pub fn parse(&self) -> QueryResult<'_, ast::Crate> {
9776
self.parse.compute(|| passes::parse(&self.compiler.sess))
9877
}
99-
100-
pub fn global_ctxt(&'tcx self) -> QueryResult<'tcx, &'tcx GlobalCtxt<'tcx>> {
101-
self.gcx.compute(|| {
102-
let krate = self.parse().steal();
103-
104-
passes::create_global_ctxt(
105-
self.compiler,
106-
krate,
107-
&self.gcx_cell,
108-
&self.arena,
109-
&self.hir_arena,
110-
)
111-
})
112-
}
11378
}
11479

11580
pub struct Linker {
@@ -192,16 +157,7 @@ impl Compiler {
192157
where
193158
F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
194159
{
195-
// Must declare `_timer` first so that it is dropped after `queries`.
196-
let _timer;
197160
let queries = Queries::new(self);
198-
let ret = f(&queries);
199-
200-
// The timer's lifetime spans the dropping of `queries`, which contains
201-
// the global context.
202-
_timer = self.sess.timer("free_global_ctxt");
203-
queries.finish();
204-
205-
ret
161+
f(&queries)
206162
}
207163
}

src/librustdoc/doctest.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -175,23 +175,26 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
175175
..
176176
} = interface::run_compiler(config, |compiler| {
177177
compiler.enter(|queries| {
178-
let collector = queries.global_ctxt().enter(|tcx| {
179-
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
180-
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
181-
let opts = scrape_test_config(crate_name, crate_attrs, args_path);
182-
let enable_per_target_ignores = options.enable_per_target_ignores;
183-
184-
let mut collector = CreateRunnableDocTests::new(options, opts);
185-
let hir_collector = HirCollector::new(
186-
ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()),
187-
enable_per_target_ignores,
188-
tcx,
189-
);
190-
let tests = hir_collector.collect_crate();
191-
tests.into_iter().for_each(|t| collector.add_test(t));
192-
193-
collector
194-
});
178+
let krate = queries.parse().steal();
179+
180+
let collector =
181+
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
182+
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
183+
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
184+
let opts = scrape_test_config(crate_name, crate_attrs, args_path);
185+
let enable_per_target_ignores = options.enable_per_target_ignores;
186+
187+
let mut collector = CreateRunnableDocTests::new(options, opts);
188+
let hir_collector = HirCollector::new(
189+
ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()),
190+
enable_per_target_ignores,
191+
tcx,
192+
);
193+
let tests = hir_collector.collect_crate();
194+
tests.into_iter().for_each(|t| collector.add_test(t));
195+
196+
collector
197+
});
195198
compiler.sess.dcx().abort_if_errors();
196199

197200
collector

src/librustdoc/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,12 +857,12 @@ fn main_args(
857857
}
858858

859859
compiler.enter(|queries| {
860-
let mut gcx = queries.global_ctxt();
860+
let krate = queries.parse().steal();
861861
if sess.dcx().has_errors().is_some() {
862862
sess.dcx().fatal("Compilation failed, aborting rustdoc");
863863
}
864864

865-
gcx.enter(|tcx| {
865+
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
866866
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
867867
core::run_global_ctxt(tcx, show_coverage, render_options, output_format)
868868
});

0 commit comments

Comments
 (0)