Fix review comment

This commit is contained in:
bjorn3 2021-10-12 13:36:01 +02:00
parent f0b0040bf8
commit 07ee9bba15

View File

@ -12,16 +12,8 @@ pub(crate) fn inside_proc_macro() -> bool {
_ => {}
}
#[cfg(feature = "is_available")]
{
proc_macro::is_available()
}
#[cfg(not(feature = "is_available"))]
{
INIT.call_once(initialize);
inside_proc_macro()
}
INIT.call_once(initialize);
inside_proc_macro()
}
pub(crate) fn force_fallback() {
@ -29,7 +21,7 @@ pub(crate) fn force_fallback() {
}
pub(crate) fn unforce_fallback() {
WORKS.store(0, Ordering::SeqCst);
initialize();
}
// Swap in a null panic hook to avoid printing "thread panicked" to stderr,
@ -57,19 +49,27 @@ pub(crate) fn unforce_fallback() {
// not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
// the main thread before launching any other threads.
fn initialize() {
type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static;
#[cfg(feature = "is_available")]
{
WORKS.store(proc_macro::is_available() as usize + 1, Ordering::SeqCst);
}
let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
let sanity_check = &*null_hook as *const PanicHook;
let original_hook = panic::take_hook();
panic::set_hook(null_hook);
#[cfg(not(feature = "is_available"))]
{
type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static;
let works = panic::catch_unwind(proc_macro::Span::call_site).is_ok();
WORKS.store(works as usize + 1, Ordering::SeqCst);
let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
let sanity_check = &*null_hook as *const PanicHook;
let original_hook = panic::take_hook();
panic::set_hook(null_hook);
let hopefully_null_hook = panic::take_hook();
panic::set_hook(original_hook);
if sanity_check != &*hopefully_null_hook {
panic!("observed race condition in proc_macro2::inside_proc_macro");
let works = panic::catch_unwind(proc_macro::Span::call_site).is_ok();
WORKS.store(works as usize + 1, Ordering::SeqCst);
let hopefully_null_hook = panic::take_hook();
panic::set_hook(original_hook);
if sanity_check != &*hopefully_null_hook {
panic!("observed race condition in proc_macro2::inside_proc_macro");
}
}
}