Skip to content

Commit

Permalink
Merge pull request #300 from bjorn3/proc_macro_is_available
Browse files Browse the repository at this point in the history
Use proc_macro::is_available() on rust 1.57+
  • Loading branch information
dtolnay committed Oct 12, 2021
2 parents 0253a0b + 07ee9bb commit 321a711
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
10 changes: 10 additions & 0 deletions build.rs
Expand Up @@ -33,6 +33,12 @@
// location of a token. Enabled by procmacro2_semver_exempt or the
// "span-locations" Cargo cfg. This is behind a cfg because tracking
// location inside spans is a performance hit.
//
// "is_available"
// 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+

use std::env;
use std::iter;
Expand Down Expand Up @@ -82,6 +88,10 @@ fn main() {
println!("cargo:rustc-cfg=literal_from_str");
}

if version.minor >= 57 {
println!("cargo:rustc-cfg=is_available");
}

let target = env::var("TARGET").unwrap();
if !enable_use_proc_macro(&target) {
return;
Expand Down
30 changes: 19 additions & 11 deletions src/detection.rs
Expand Up @@ -49,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 321a711

Please sign in to comment.