Skip to content

Commit 321a711

Browse files
authored
Merge pull request #300 from bjorn3/proc_macro_is_available
Use proc_macro::is_available() on rust 1.57+
2 parents 0253a0b + 07ee9bb commit 321a711

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
// location of a token. Enabled by procmacro2_semver_exempt or the
3434
// "span-locations" Cargo cfg. This is behind a cfg because tracking
3535
// location inside spans is a performance hit.
36+
//
37+
// "is_available"
38+
// Use `proc_macro::is_available()` to detect if the proc macro API is
39+
// available or needs to be polyfilled instead of trying to use the proc
40+
// macro API and catching a panic if it isn't available.
41+
// Enabled on Rust 1.57+
3642

3743
use std::env;
3844
use std::iter;
@@ -82,6 +88,10 @@ fn main() {
8288
println!("cargo:rustc-cfg=literal_from_str");
8389
}
8490

91+
if version.minor >= 57 {
92+
println!("cargo:rustc-cfg=is_available");
93+
}
94+
8595
let target = env::var("TARGET").unwrap();
8696
if !enable_use_proc_macro(&target) {
8797
return;

src/detection.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,27 @@ pub(crate) fn unforce_fallback() {
4949
// not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
5050
// the main thread before launching any other threads.
5151
fn initialize() {
52-
type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static;
52+
#[cfg(feature = "is_available")]
53+
{
54+
WORKS.store(proc_macro::is_available() as usize + 1, Ordering::SeqCst);
55+
}
56+
57+
#[cfg(not(feature = "is_available"))]
58+
{
59+
type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static;
5360

54-
let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
55-
let sanity_check = &*null_hook as *const PanicHook;
56-
let original_hook = panic::take_hook();
57-
panic::set_hook(null_hook);
61+
let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
62+
let sanity_check = &*null_hook as *const PanicHook;
63+
let original_hook = panic::take_hook();
64+
panic::set_hook(null_hook);
5865

59-
let works = panic::catch_unwind(proc_macro::Span::call_site).is_ok();
60-
WORKS.store(works as usize + 1, Ordering::SeqCst);
66+
let works = panic::catch_unwind(proc_macro::Span::call_site).is_ok();
67+
WORKS.store(works as usize + 1, Ordering::SeqCst);
6168

62-
let hopefully_null_hook = panic::take_hook();
63-
panic::set_hook(original_hook);
64-
if sanity_check != &*hopefully_null_hook {
65-
panic!("observed race condition in proc_macro2::inside_proc_macro");
69+
let hopefully_null_hook = panic::take_hook();
70+
panic::set_hook(original_hook);
71+
if sanity_check != &*hopefully_null_hook {
72+
panic!("observed race condition in proc_macro2::inside_proc_macro");
73+
}
6674
}
6775
}

0 commit comments

Comments
 (0)