Skip to content

Commit 2d5b8e6

Browse files
committed
Get the default panic hook for ICEs, from std::panic::set_hook.
1 parent ee3d5af commit 2d5b8e6

File tree

1 file changed

+33
-1
lines changed
  • crates/rustc_codegen_spirv/src

1 file changed

+33
-1
lines changed

crates/rustc_codegen_spirv/src/lib.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,39 @@ fn get_env_dump_dir(env_var: &str) -> Option<PathBuf> {
623623
pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
624624
// Override rustc's panic hook with our own to override the ICE error
625625
// message, and direct people to `rust-gpu`.
626-
std::panic::set_hook(Box::new(|panic_info| {
626+
let _rustc_hook = std::panic::take_hook();
627+
let default_hook = std::panic::take_hook();
628+
{
629+
// NOTE(eddyb) the reason we can get access to the default panic hook,
630+
// is that `std::panic::take_hook` has this phrase in its documentation:
631+
//
632+
// > If no custom hook is registered, the default hook will be returned.
633+
//
634+
// But just in case (races with other threads?), we can do it a few more
635+
// times, and require that we get the same "boxed" ZST every time.
636+
let more_hooks = [
637+
std::panic::take_hook(),
638+
std::panic::take_hook(),
639+
std::panic::take_hook(),
640+
std::panic::take_hook(),
641+
];
642+
assert_eq!(
643+
std::mem::size_of_val(&*default_hook),
644+
0,
645+
"failed to acquire default panic hook using `std::panic::take_hook`, \
646+
or default panic hook not a ZST anymore"
647+
);
648+
#[allow(clippy::vtable_address_comparisons)]
649+
for other_hook in more_hooks {
650+
assert!(
651+
std::ptr::eq(&*default_hook, &*other_hook),
652+
"failed to acquire default panic hook using `std::panic::take_hook`, \
653+
or `std::panic::set_hook` was used on another thread"
654+
);
655+
}
656+
}
657+
std::panic::set_hook(Box::new(move |panic_info| {
658+
default_hook(panic_info);
627659
rustc_driver::report_ice(
628660
panic_info,
629661
"https://github.com/EmbarkStudios/rust-gpu/issues/new",

0 commit comments

Comments
 (0)