From 8aee35e2ed51a9f782c1bff3f8a165b5a85f0c43 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 17 Nov 2023 11:04:38 +1100 Subject: [PATCH] Merge `interface::run_compiler` calls. `rustc_driver_impl::run_compiler` currently has two `interface::run_compiler` calls: one for the "no input" case, and one for the normal case. This commit merges the former into the latter, which makes the control flow easier to read and avoids some duplication. It also makes it clearer that the "no input" case will describe lints before printing crate info, while the normal case does it in the reverse order. Possibly a bug? --- compiler/rustc_driver_impl/src/lib.rs | 48 ++++++++++----------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index c24d74d1b9b..f74eb028364 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -338,41 +338,14 @@ fn run_compiler( expanded_args: args, }; - match make_input(&default_handler, &matches.free) { + let has_input = match make_input(&default_handler, &matches.free) { Err(reported) => return Err(reported), Ok(Some(input)) => { config.input = input; - - callbacks.config(&mut config); + true // has input: normal compilation } Ok(None) => match matches.free.len() { - 0 => { - callbacks.config(&mut config); - - default_handler.abort_if_errors(); - - interface::run_compiler(config, |compiler| { - let sopts = &compiler.session().opts; - let handler = EarlyErrorHandler::new(sopts.error_format); - - if sopts.describe_lints { - describe_lints(compiler.session()); - return; - } - let should_stop = print_crate_info( - &handler, - compiler.codegen_backend(), - compiler.session(), - false, - ); - - if should_stop == Compilation::Stop { - return; - } - handler.early_error("no input filename given") - }); - return Ok(()); - } + 0 => false, // no input: we will exit early 1 => panic!("make_input should have provided valid inputs"), _ => default_handler.early_error(format!( "multiple input filenames provided (first two filenames are `{}` and `{}`)", @@ -381,13 +354,28 @@ fn run_compiler( }, }; + callbacks.config(&mut config); + default_handler.abort_if_errors(); + drop(default_handler); interface::run_compiler(config, |compiler| { let sess = compiler.session(); let codegen_backend = compiler.codegen_backend(); let handler = EarlyErrorHandler::new(sess.opts.error_format); + if !has_input { + if sess.opts.describe_lints { + describe_lints(sess); + return sess.compile_status(); + } + let should_stop = print_crate_info(&handler, codegen_backend, sess, false); + if should_stop == Compilation::Continue { + handler.early_error("no input filename given") + } + return sess.compile_status(); + } + let should_stop = print_crate_info(&handler, codegen_backend, sess, true) .and_then(|| list_metadata(&handler, sess, &*codegen_backend.metadata_loader())) .and_then(|| try_process_rlink(sess, compiler));