Skip to content

Commit 978c659

Browse files
committed
Auto merge of #134302 - bjorn3:remove_driver_queries, r=oli-obk,jieyouxu
Remove queries from the driver interface All uses of driver queries in the public api of rustc_driver have been removed in #134130 already. This removes driver queries from rustc_interface and does a couple of cleanups around TyCtxt construction and entering enabled by this removal. Finishes the removal of driver queries started with #126834.
2 parents 52f4785 + b0cd37e commit 978c659

File tree

9 files changed

+206
-332
lines changed

9 files changed

+206
-332
lines changed

compiler/rustc_driver_impl/src/lib.rs

+47-54
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;
@@ -387,76 +387,69 @@ fn run_compiler(
387387
return early_exit();
388388
}
389389

390-
let linker = compiler.enter(|queries| {
390+
// Parse the crate root source code (doesn't parse submodules yet)
391+
// Everything else is parsed during macro expansion.
392+
let krate = passes::parse(sess);
393+
394+
// If pretty printing is requested: Figure out the representation, print it and exit
395+
if let Some(pp_mode) = sess.opts.pretty {
396+
if pp_mode.needs_ast_map() {
397+
create_and_enter_global_ctxt(compiler, krate, |tcx| {
398+
tcx.ensure().early_lint_checks(());
399+
pretty::print(sess, pp_mode, pretty::PrintExtra::NeedsAstMap { tcx });
400+
passes::write_dep_info(tcx);
401+
});
402+
} else {
403+
pretty::print(sess, pp_mode, pretty::PrintExtra::AfterParsing { krate: &krate });
404+
}
405+
trace!("finished pretty-printing");
406+
return early_exit();
407+
}
408+
409+
if callbacks.after_crate_root_parsing(compiler, &krate) == Compilation::Stop {
410+
return early_exit();
411+
}
412+
413+
if sess.opts.unstable_opts.parse_crate_root_only {
414+
return early_exit();
415+
}
416+
417+
let linker = create_and_enter_global_ctxt(compiler, krate, |tcx| {
391418
let early_exit = || {
392419
sess.dcx().abort_if_errors();
393420
None
394421
};
395422

396-
// Parse the crate root source code (doesn't parse submodules yet)
397-
// Everything else is parsed during macro expansion.
398-
queries.parse();
399-
400-
// If pretty printing is requested: Figure out the representation, print it and exit
401-
if let Some(pp_mode) = sess.opts.pretty {
402-
if pp_mode.needs_ast_map() {
403-
queries.global_ctxt().enter(|tcx| {
404-
tcx.ensure().early_lint_checks(());
405-
pretty::print(sess, pp_mode, pretty::PrintExtra::NeedsAstMap { tcx });
406-
passes::write_dep_info(tcx);
407-
});
408-
} else {
409-
let krate = queries.parse();
410-
pretty::print(sess, pp_mode, pretty::PrintExtra::AfterParsing {
411-
krate: &*krate.borrow(),
412-
});
413-
}
414-
trace!("finished pretty-printing");
423+
// Make sure name resolution and macro expansion is run.
424+
let _ = tcx.resolver_for_lowering();
425+
426+
if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir {
427+
dump_feature_usage_metrics(tcx, metrics_dir);
428+
}
429+
430+
if callbacks.after_expansion(compiler, tcx) == Compilation::Stop {
415431
return early_exit();
416432
}
417433

418-
if callbacks.after_crate_root_parsing(compiler, &*queries.parse().borrow())
419-
== Compilation::Stop
434+
passes::write_dep_info(tcx);
435+
436+
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
437+
&& sess.opts.output_types.len() == 1
420438
{
421439
return early_exit();
422440
}
423441

424-
if sess.opts.unstable_opts.parse_crate_root_only {
442+
if sess.opts.unstable_opts.no_analysis {
425443
return early_exit();
426444
}
427445

428-
queries.global_ctxt().enter(|tcx| {
429-
// Make sure name resolution and macro expansion is run.
430-
let _ = tcx.resolver_for_lowering();
431-
432-
if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir {
433-
dump_feature_usage_metrics(tcx, metrics_dir);
434-
}
435-
436-
if callbacks.after_expansion(compiler, tcx) == Compilation::Stop {
437-
return early_exit();
438-
}
439-
440-
passes::write_dep_info(tcx);
441-
442-
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
443-
&& sess.opts.output_types.len() == 1
444-
{
445-
return early_exit();
446-
}
447-
448-
if sess.opts.unstable_opts.no_analysis {
449-
return early_exit();
450-
}
446+
tcx.ensure().analysis(());
451447

452-
tcx.ensure().analysis(());
453-
454-
if callbacks.after_analysis(compiler, tcx) == Compilation::Stop {
455-
return early_exit();
456-
}
448+
if callbacks.after_analysis(compiler, tcx) == Compilation::Stop {
449+
return early_exit();
450+
}
457451

458-
Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend))
459-
})
452+
Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend))
460453
});
461454

462455
// Linking is done outside the `compiler.enter()` so that the

compiler/rustc_interface/src/lib.rs

+3-3
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,8 +17,8 @@ pub mod util;
1717

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

2323
#[cfg(test)]
2424
mod tests;

compiler/rustc_interface/src/passes.rs

+62-44
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 {
@@ -711,13 +711,11 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
711711
*providers
712712
});
713713

714-
pub(crate) fn create_global_ctxt<'tcx>(
715-
compiler: &'tcx Compiler,
714+
pub fn create_and_enter_global_ctxt<T, F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> T>(
715+
compiler: &Compiler,
716716
mut krate: rustc_ast::Crate,
717-
gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,
718-
arena: &'tcx WorkerLocal<Arena<'tcx>>,
719-
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
720-
) -> &'tcx GlobalCtxt<'tcx> {
717+
f: F,
718+
) -> T {
721719
let sess = &compiler.sess;
722720

723721
rustc_builtin_macros::cmdline_attrs::inject(
@@ -765,44 +763,64 @@ pub(crate) fn create_global_ctxt<'tcx>(
765763

766764
let incremental = dep_graph.is_fully_enabled();
767765

768-
sess.time("setup_global_ctxt", || {
769-
let qcx = gcx_cell.get_or_init(move || {
770-
TyCtxt::create_global_ctxt(
771-
sess,
772-
crate_types,
773-
stable_crate_id,
774-
arena,
775-
hir_arena,
776-
untracked,
777-
dep_graph,
778-
rustc_query_impl::query_callbacks(arena),
779-
rustc_query_impl::query_system(
780-
providers.queries,
781-
providers.extern_queries,
782-
query_result_on_disk_cache,
783-
incremental,
784-
),
785-
providers.hooks,
786-
compiler.current_gcx.clone(),
787-
)
788-
});
789-
790-
qcx.enter(|tcx| {
791-
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
792-
assert_eq!(feed.key(), LOCAL_CRATE);
793-
feed.crate_name(crate_name);
766+
let gcx_cell = OnceLock::new();
767+
let arena = WorkerLocal::new(|_| Arena::default());
768+
let hir_arena = WorkerLocal::new(|_| rustc_hir::Arena::default());
769+
770+
// This closure is necessary to force rustc to perform the correct lifetime
771+
// subtyping for GlobalCtxt::enter to be allowed.
772+
let inner: Box<
773+
dyn for<'tcx> FnOnce(
774+
&'tcx Compiler,
775+
&'tcx OnceLock<GlobalCtxt<'tcx>>,
776+
&'tcx WorkerLocal<Arena<'tcx>>,
777+
&'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
778+
F,
779+
) -> T,
780+
> = Box::new(move |compiler, gcx_cell, arena, hir_arena, f| {
781+
let sess = &compiler.sess;
782+
783+
TyCtxt::create_global_ctxt(
784+
gcx_cell,
785+
sess,
786+
crate_types,
787+
stable_crate_id,
788+
arena,
789+
hir_arena,
790+
untracked,
791+
dep_graph,
792+
rustc_query_impl::query_callbacks(arena),
793+
rustc_query_impl::query_system(
794+
providers.queries,
795+
providers.extern_queries,
796+
query_result_on_disk_cache,
797+
incremental,
798+
),
799+
providers.hooks,
800+
compiler.current_gcx.clone(),
801+
|tcx| {
802+
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
803+
assert_eq!(feed.key(), LOCAL_CRATE);
804+
feed.crate_name(crate_name);
805+
806+
let feed = tcx.feed_unit_query();
807+
feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
808+
sess,
809+
&pre_configured_attrs,
810+
crate_name,
811+
)));
812+
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
813+
feed.output_filenames(Arc::new(outputs));
814+
815+
let res = f(tcx);
816+
// FIXME maybe run finish even when a fatal error occured? or at least tcx.alloc_self_profile_query_strings()?
817+
tcx.finish();
818+
res
819+
},
820+
)
821+
});
794822

795-
let feed = tcx.feed_unit_query();
796-
feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
797-
sess,
798-
&pre_configured_attrs,
799-
crate_name,
800-
)));
801-
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
802-
feed.output_filenames(Arc::new(outputs));
803-
});
804-
qcx
805-
})
823+
inner(compiler, &gcx_cell, &arena, &hir_arena, f)
806824
}
807825

808826
/// Runs all analyses that we guarantee to run, even if errors were reported in earlier analyses.

0 commit comments

Comments
 (0)