Skip to content

Commit 0a93cf6

Browse files
committed
Mark rustc_driver::install_ice_hook unsafe
It modifies the environment.
1 parent 89fd614 commit 0a93cf6

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

compiler/rustc_driver_impl/src/lib.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,11 @@ fn ice_path() -> &'static Option<PathBuf> {
13071307
/// internal features.
13081308
///
13091309
/// A custom rustc driver can skip calling this to set up a custom ICE hook.
1310-
pub fn install_ice_hook(
1310+
///
1311+
/// # Unsafety
1312+
///
1313+
/// This function modifies the environment and has the same safety as [`std::env::set_var`].
1314+
pub unsafe fn install_ice_hook(
13111315
bug_report_url: &'static str,
13121316
extra_info: fn(&DiagCtxt),
13131317
) -> Arc<AtomicBool> {
@@ -1520,7 +1524,8 @@ pub fn main() -> ! {
15201524
init_rustc_env_logger(&early_dcx);
15211525
signal_handler::install();
15221526
let mut callbacks = TimePassesCallbacks::default();
1523-
let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
1527+
// We're single-threaded at this point, so it's okay to call `install_ice_hook`.
1528+
let using_internal_features = unsafe { install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ()) };
15241529
install_ctrlc_handler();
15251530

15261531
let exit_code = catch_with_exit_code(|| {

src/tools/clippy/src/driver.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,16 @@ pub fn main() {
181181

182182
rustc_driver::init_rustc_env_logger(&early_dcx);
183183

184-
let using_internal_features = rustc_driver::install_ice_hook(BUG_REPORT_URL, |handler| {
185-
// FIXME: this macro calls unwrap internally but is called in a panicking context! It's not
186-
// as simple as moving the call from the hook to main, because `install_ice_hook` doesn't
187-
// accept a generic closure.
188-
let version_info = rustc_tools_util::get_version_info!();
189-
handler.note(format!("Clippy version: {version_info}"));
190-
});
184+
// We're single-threaded at this point, so it's okay to call `install_ice_hook`.
185+
let using_internal_features = unsafe {
186+
rustc_driver::install_ice_hook(BUG_REPORT_URL, |handler| {
187+
// FIXME: this macro calls unwrap internally but is called in a panicking context! It's not
188+
// as simple as moving the call from the hook to main, because `install_ice_hook` doesn't
189+
// accept a generic closure.
190+
let version_info = rustc_tools_util::get_version_info!();
191+
handler.note(format!("Clippy version: {version_info}"));
192+
})
193+
};
191194

192195
exit(rustc_driver::catch_with_exit_code(move || {
193196
let mut orig_args = rustc_driver::args::raw_args(&early_dcx)?;

src/tools/miri/src/bin/miri.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,11 @@ fn main() {
371371
// If the environment asks us to actually be rustc, then do that.
372372
if let Some(crate_kind) = env::var_os("MIRI_BE_RUSTC") {
373373
// Earliest rustc setup.
374-
let using_internal_features =
375-
rustc_driver::install_ice_hook(rustc_driver::DEFAULT_BUG_REPORT_URL, |_| ());
374+
//
375+
// We're single-threaded at this point, so it's okay to call `install_ice_hook`.
376+
let using_internal_features = unsafe {
377+
rustc_driver::install_ice_hook(rustc_driver::DEFAULT_BUG_REPORT_URL, |_| ())
378+
};
376379
rustc_driver::init_rustc_env_logger(&early_dcx);
377380

378381
let target_crate = if crate_kind == "target" {
@@ -393,8 +396,11 @@ fn main() {
393396
}
394397

395398
// Add an ICE bug report hook.
396-
let using_internal_features =
397-
rustc_driver::install_ice_hook("https://github.com/rust-lang/miri/issues/new", |_| ());
399+
//
400+
// We're single-threaded at this point, so it's okay to call `install_ice_hook`.
401+
let using_internal_features = unsafe {
402+
rustc_driver::install_ice_hook("https://github.com/rust-lang/miri/issues/new", |_| ())
403+
};
398404

399405
// Init loggers the Miri way.
400406
init_early_loggers(&early_dcx);

src/tools/rustfmt/src/bin/main.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rustfmt/issues/new?la
2828
extern crate rustc_driver;
2929

3030
fn main() {
31-
rustc_driver::install_ice_hook(BUG_REPORT_URL, |_| ());
31+
// We're single-threaded at this point, so it's okay to call `install_ice_hook`.
32+
unsafe {
33+
rustc_driver::install_ice_hook(BUG_REPORT_URL, |_| ());
34+
}
3235

3336
tracing_subscriber::fmt()
3437
.with_env_filter(EnvFilter::from_env("RUSTFMT_LOG"))

0 commit comments

Comments
 (0)