Skip to content

Commit

Permalink
Fix review comment
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Oct 12, 2021
1 parent f0b0040 commit 07ee9bb
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,16 @@ 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() {
WORKS.store(1, Ordering::SeqCst);
}

pub(crate) fn unforce_fallback() {
WORKS.store(0, Ordering::SeqCst);
initialize();
}

// Swap in a null panic hook to avoid printing "thread panicked" to stderr,
Expand Down Expand Up @@ -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);
}

#[cfg(not(feature = "is_available"))]
{
type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static;

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 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 works = panic::catch_unwind(proc_macro::Span::call_site).is_ok();
WORKS.store(works as usize + 1, Ordering::SeqCst);
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");
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");
}
}
}

0 comments on commit 07ee9bb

Please sign in to comment.