From 9fd99a797c975d59209a35c5a99736bb4caaa939 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Sun, 10 Mar 2024 14:36:35 -0400 Subject: [PATCH] Rearrange where components live --- .../src/const_eval/eval_queries.rs | 2 +- compiler/rustc_const_eval/src/lib.rs | 7 +++++ compiler/rustc_data_structures/src/lib.rs | 6 ---- compiler/rustc_driver_impl/src/lib.rs | 29 +++++++++++-------- src/tools/miri/src/bin/miri.rs | 3 ++ src/tools/miri/src/concurrency/thread.rs | 17 ++--------- 6 files changed, 30 insertions(+), 34 deletions(-) diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index 22d959deb3ab9..c440be454cf7c 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -2,7 +2,6 @@ use std::sync::atomic::Ordering::Relaxed; use either::{Left, Right}; -use rustc_data_structures::CTRL_C_RECEIVED; use rustc_hir::def::DefKind; use rustc_middle::mir::interpret::{AllocId, ErrorHandled, InterpErrorInfo}; use rustc_middle::mir::{self, ConstAlloc, ConstValue}; @@ -25,6 +24,7 @@ use crate::interpret::{ GlobalId, Immediate, InternKind, InterpCx, InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, StackPopCleanup, }; +use crate::CTRL_C_RECEIVED; // Returns a pointer to where the result lives #[instrument(level = "trace", skip(ecx, body), ret)] diff --git a/compiler/rustc_const_eval/src/lib.rs b/compiler/rustc_const_eval/src/lib.rs index 51836063945e4..2079b41e70965 100644 --- a/compiler/rustc_const_eval/src/lib.rs +++ b/compiler/rustc_const_eval/src/lib.rs @@ -31,6 +31,8 @@ pub mod interpret; pub mod transform; pub mod util; +use std::sync::atomic::AtomicBool; + pub use errors::ReportErrorExt; use rustc_middle::{ty, util::Providers}; @@ -56,3 +58,8 @@ pub fn provide(providers: &mut Providers) { util::check_validity_requirement(tcx, init_kind, param_env_and_ty) }; } + +/// `rustc_driver::main` installs a handler that will set this to `true` if +/// the compiler has been sent a request to shut down, such as by a Ctrl-C. +/// This static lives here because it is only read by the interpreter. +pub static CTRL_C_RECEIVED: AtomicBool = AtomicBool::new(false); diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs index aeeaa392a7048..b82a9a909e6d0 100644 --- a/compiler/rustc_data_structures/src/lib.rs +++ b/compiler/rustc_data_structures/src/lib.rs @@ -46,7 +46,6 @@ extern crate tracing; extern crate rustc_macros; use std::fmt; -use std::sync::atomic::AtomicBool; pub use rustc_index::static_assert_size; @@ -162,8 +161,3 @@ macro_rules! external_bitflags_debug { } }; } - -/// `rustc_driver::main` installs a handler that will set this to `true` if -/// the compiler has been sent a request to shut down, such as by a Ctrl-C. -/// This static is placed here so that it is available to all parts of the compiler. -pub static CTRL_C_RECEIVED: AtomicBool = AtomicBool::new(false); diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index d6effb605356c..fb168aa312334 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -19,10 +19,10 @@ extern crate tracing; use rustc_ast as ast; use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults}; +use rustc_const_eval::CTRL_C_RECEIVED; use rustc_data_structures::profiling::{ get_resident_set_size, print_time_passes_entry, TimePassesFormat, }; -use rustc_data_structures::CTRL_C_RECEIVED; use rustc_errors::emitter::stderr_destination; use rustc_errors::registry::Registry; use rustc_errors::{ @@ -1506,17 +1506,9 @@ pub fn init_logger(early_dcx: &EarlyDiagCtxt, cfg: rustc_log::LoggerConfig) { } } -pub fn main() -> ! { - let start_time = Instant::now(); - let start_rss = get_resident_set_size(); - - let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default()); - - init_rustc_env_logger(&early_dcx); - signal_handler::install(); - let mut callbacks = TimePassesCallbacks::default(); - let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ()); - +/// Install our usual `ctrlc` handler, which sets [`rustc_const_eval::CTRL_C_RECEIVED`]. +/// Makign this handler optional lets tools can install a different handler, if they wish. +pub fn install_ctrlc_handler() { ctrlc::set_handler(move || { // Indicate that we have been signaled to stop. If we were already signaled, exit // immediately. In our interpreter loop we try to consult this value often, but if for @@ -1528,6 +1520,19 @@ pub fn main() -> ! { } }) .expect("Unable to install ctrlc handler"); +} + +pub fn main() -> ! { + let start_time = Instant::now(); + let start_rss = get_resident_set_size(); + + let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default()); + + init_rustc_env_logger(&early_dcx); + signal_handler::install(); + let mut callbacks = TimePassesCallbacks::default(); + let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ()); + install_ctrlc_handler(); let exit_code = catch_with_exit_code(|| { RunCompiler::new(&args::raw_args(&early_dcx)?, &mut callbacks) diff --git a/src/tools/miri/src/bin/miri.rs b/src/tools/miri/src/bin/miri.rs index 6955e649b4d17..9b62741f41e02 100644 --- a/src/tools/miri/src/bin/miri.rs +++ b/src/tools/miri/src/bin/miri.rs @@ -372,6 +372,9 @@ fn main() { let using_internal_features = rustc_driver::install_ice_hook("https://github.com/rust-lang/miri/issues/new", |_| ()); + // Install the ctrlc handler that sets `rustc_const_eval::CTRL_C_RECEIVED`. + rustc_driver::install_ctrlc_handler(); + // Init loggers the Miri way. init_early_loggers(&early_dcx); diff --git a/src/tools/miri/src/concurrency/thread.rs b/src/tools/miri/src/concurrency/thread.rs index 9c26a7b28ccd7..e2e18d3a7344f 100644 --- a/src/tools/miri/src/concurrency/thread.rs +++ b/src/tools/miri/src/concurrency/thread.rs @@ -9,8 +9,8 @@ use std::time::{Duration, SystemTime}; use either::Either; +use rustc_const_eval::CTRL_C_RECEIVED; use rustc_data_structures::fx::FxHashMap; -use rustc_data_structures::CTRL_C_RECEIVED; use rustc_hir::def_id::DefId; use rustc_index::{Idx, IndexVec}; use rustc_middle::mir::Mutability; @@ -1046,20 +1046,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { /// Run the core interpreter loop. Returns only when an interrupt occurs (an error or program /// termination). fn run_threads(&mut self) -> InterpResult<'tcx, !> { - // In normal rustc, rustc_driver::main installs this handler. But we don't use that - // function, see src/bin/miri.rs. - ctrlc::set_handler(move || { - // Indicate that we have been signaled to stop. If we were already signaled, exit - // immediately. In our interpreter loop we try to consult this value often, but if for - // whatever reason we don't get to that check or the cleanup we do upon finding that - // this bool has become true takes a long time, the exit here will promptly exit the - // process on the second Ctrl-C. - if CTRL_C_RECEIVED.swap(true, Relaxed) { - std::process::exit(1); - } - }) - .expect("Unable to install ctrlc handler"); - let this = self.eval_context_mut(); + let this = self.eval_context_mut(); loop { if CTRL_C_RECEIVED.load(Relaxed) { this.machine.handle_abnormal_termination();