Skip to content

Commit 78ef773

Browse files
committed
Implement sanity check for juggling panic hooks
1 parent 2ed15d5 commit 78ef773

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/unstable.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::fmt;
44
use std::iter;
5-
use std::panic;
5+
use std::panic::{self, PanicInfo};
66
use std::str::FromStr;
77

88
use proc_macro;
@@ -59,11 +59,21 @@ fn nightly_works() -> bool {
5959
// not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
6060
// the main thread before launching any other threads.
6161
INIT.call_once(|| {
62+
type PanicHook = Fn(&PanicInfo) + Sync + Send + 'static;
63+
64+
let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
65+
let sanity_check = &*null_hook as *const PanicHook;
6266
let original_hook = panic::take_hook();
63-
panic::set_hook(Box::new(|_panic_info| { /* ignore */ }));
67+
panic::set_hook(null_hook);
68+
6469
let works = panic::catch_unwind(|| proc_macro::Span::call_site()).is_ok();
6570
WORKS.store(works as usize + 1, Ordering::SeqCst);
71+
72+
let hopefully_null_hook = panic::take_hook();
6673
panic::set_hook(original_hook);
74+
if sanity_check != &*hopefully_null_hook {
75+
panic!("observed race condition in proc_macro2::nightly_works");
76+
}
6777
});
6878
nightly_works()
6979
}

0 commit comments

Comments
 (0)