Skip to content

Commit

Permalink
Touch up PR 300
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 12, 2021
1 parent 321a711 commit 3268f28
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
6 changes: 3 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
// location inside spans is a performance hit.
//
// "is_available"
// Use `proc_macro::is_available()` to detect if the proc macro API is
// Use proc_macro::is_available() to detect if the proc macro API is
// available or needs to be polyfilled instead of trying to use the proc
// macro API and catching a panic if it isn't available.
// Enabled on Rust 1.57+
// macro API and catching a panic if it isn't available. Enabled on Rust
// 1.57+.

use std::env;
use std::iter;
Expand Down
37 changes: 18 additions & 19 deletions src/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ pub(crate) fn unforce_fallback() {
initialize();
}

#[cfg(feature = "is_available")]
fn initialize() {
let available = proc_macro::is_available();
WORKS.store(available as usize + 1, Ordering::SeqCst);
}

// Swap in a null panic hook to avoid printing "thread panicked" to stderr,
// then use catch_unwind to determine whether the compiler's proc_macro is
// working. When proc-macro2 is used from outside of a procedural macro all
Expand All @@ -48,28 +54,21 @@ pub(crate) fn unforce_fallback() {
// here. For now, if a user needs to guarantee that this failure mode does
// not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
// the main thread before launching any other threads.
#[cfg(not(feature = "is_available"))]
fn initialize() {
#[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;
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 3268f28

Please sign in to comment.