From 1da13489247ed5b0b6694b2aabb0184f16b50afd Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 21 Jun 2023 11:26:49 +1000 Subject: [PATCH] Remove Queries::ongoing_codegen. There's no need to store it in `Queries`. We can just use a local variable, because it's always used shortly after it's produced. The commit also removes the `tcx.analysis()` call in `ongoing_codegen`, because it's easy to ensure that's done beforehand. All this makes the dataflow within `run_compiler` easier to follow, at the cost of making one test slightly more verbose, which I think is a good tradeoff. --- compiler/rustc_driver_impl/src/lib.rs | 4 +-- compiler/rustc_interface/src/passes.rs | 4 +-- compiler/rustc_interface/src/queries.rs | 29 ++++++++-------------- tests/run-make-fulldeps/issue-19371/foo.rs | 11 ++++---- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 2f1c7819727..0db96d4e735 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -424,7 +424,7 @@ fn run_compiler( return early_exit(); } - queries.ongoing_codegen()?; + let ongoing_codegen = queries.ongoing_codegen()?; if sess.opts.unstable_opts.print_type_sizes { sess.code_stats.print_type_sizes(); @@ -437,7 +437,7 @@ fn run_compiler( sess.code_stats.print_vtable_sizes(crate_name); } - let linker = queries.linker()?; + let linker = queries.linker(ongoing_codegen)?; Ok(Some(linker)) })?; diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index be2af94961f..6b3facd041c 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -740,8 +740,8 @@ pub fn create_global_ctxt<'tcx>( }) } -/// Runs the resolution, type-checking, region checking and other -/// miscellaneous analysis passes on the crate. +/// Runs the type-checking, region checking and other miscellaneous analysis +/// passes on the crate. fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> { rustc_passes::hir_id_validator::check_crate(tcx); diff --git a/compiler/rustc_interface/src/queries.rs b/compiler/rustc_interface/src/queries.rs index 455a8129656..d1ba748d7af 100644 --- a/compiler/rustc_interface/src/queries.rs +++ b/compiler/rustc_interface/src/queries.rs @@ -93,7 +93,6 @@ pub struct Queries<'tcx> { dep_graph: Query, // This just points to what's in `gcx_cell`. gcx: Query<&'tcx GlobalCtxt<'tcx>>, - ongoing_codegen: Query>, } impl<'tcx> Queries<'tcx> { @@ -110,7 +109,6 @@ pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> { register_plugins: Default::default(), dep_graph: Default::default(), gcx: Default::default(), - ongoing_codegen: Default::default(), } } @@ -249,23 +247,19 @@ pub fn global_ctxt(&'tcx self) -> Result> }) } - pub fn ongoing_codegen(&'tcx self) -> Result>> { - self.ongoing_codegen.compute(|| { - self.global_ctxt()?.enter(|tcx| { - tcx.analysis(()).ok(); + pub fn ongoing_codegen(&'tcx self) -> Result> { + self.global_ctxt()?.enter(|tcx| { + // Don't do code generation if there were any errors + self.session().compile_status()?; - // Don't do code generation if there were any errors - self.session().compile_status()?; + // If we have any delayed bugs, for example because we created TyKind::Error earlier, + // it's likely that codegen will only cause more ICEs, obscuring the original problem + self.session().diagnostic().flush_delayed(); - // If we have any delayed bugs, for example because we created TyKind::Error earlier, - // it's likely that codegen will only cause more ICEs, obscuring the original problem - self.session().diagnostic().flush_delayed(); + // Hook for UI tests. + Self::check_for_rustc_errors_attr(tcx); - // Hook for UI tests. - Self::check_for_rustc_errors_attr(tcx); - - Ok(passes::start_codegen(&***self.codegen_backend(), tcx)) - }) + Ok(passes::start_codegen(&***self.codegen_backend(), tcx)) }) } @@ -303,7 +297,7 @@ fn check_for_rustc_errors_attr(tcx: TyCtxt<'_>) { } } - pub fn linker(&'tcx self) -> Result { + pub fn linker(&'tcx self, ongoing_codegen: Box) -> Result { let sess = self.session().clone(); let codegen_backend = self.codegen_backend().clone(); @@ -314,7 +308,6 @@ pub fn linker(&'tcx self) -> Result { tcx.dep_graph.clone(), ) }); - let ongoing_codegen = self.ongoing_codegen()?.steal(); Ok(Linker { sess, diff --git a/tests/run-make-fulldeps/issue-19371/foo.rs b/tests/run-make-fulldeps/issue-19371/foo.rs index 6d08cfd07f8..9cca6200050 100644 --- a/tests/run-make-fulldeps/issue-19371/foo.rs +++ b/tests/run-make-fulldeps/issue-19371/foo.rs @@ -63,10 +63,11 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf) { }; interface::run_compiler(config, |compiler| { - // This runs all the passes prior to linking, too. - let linker = compiler.enter(|queries| queries.linker()); - if let Ok(linker) = linker { - linker.link(); - } + let linker = compiler.enter(|queries| { + queries.global_ctxt()?.enter(|tcx| tcx.analysis(()))?; + let ongoing_codegen = queries.ongoing_codegen()?; + queries.linker(ongoing_codegen) + }); + linker.unwrap().link(); }); }