17ac06127Sopenharmony_ciuse core::sync::atomic::{AtomicUsize, Ordering};
27ac06127Sopenharmony_ciuse std::sync::Once;
37ac06127Sopenharmony_ci
47ac06127Sopenharmony_cistatic WORKS: AtomicUsize = AtomicUsize::new(0);
57ac06127Sopenharmony_cistatic INIT: Once = Once::new();
67ac06127Sopenharmony_ci
77ac06127Sopenharmony_cipub(crate) fn inside_proc_macro() -> bool {
87ac06127Sopenharmony_ci    match WORKS.load(Ordering::Relaxed) {
97ac06127Sopenharmony_ci        1 => return false,
107ac06127Sopenharmony_ci        2 => return true,
117ac06127Sopenharmony_ci        _ => {}
127ac06127Sopenharmony_ci    }
137ac06127Sopenharmony_ci
147ac06127Sopenharmony_ci    INIT.call_once(initialize);
157ac06127Sopenharmony_ci    inside_proc_macro()
167ac06127Sopenharmony_ci}
177ac06127Sopenharmony_ci
187ac06127Sopenharmony_cipub(crate) fn force_fallback() {
197ac06127Sopenharmony_ci    WORKS.store(1, Ordering::Relaxed);
207ac06127Sopenharmony_ci}
217ac06127Sopenharmony_ci
227ac06127Sopenharmony_cipub(crate) fn unforce_fallback() {
237ac06127Sopenharmony_ci    initialize();
247ac06127Sopenharmony_ci}
257ac06127Sopenharmony_ci
267ac06127Sopenharmony_ci#[cfg(not(no_is_available))]
277ac06127Sopenharmony_cifn initialize() {
287ac06127Sopenharmony_ci    let available = proc_macro::is_available();
297ac06127Sopenharmony_ci    WORKS.store(available as usize + 1, Ordering::Relaxed);
307ac06127Sopenharmony_ci}
317ac06127Sopenharmony_ci
327ac06127Sopenharmony_ci// Swap in a null panic hook to avoid printing "thread panicked" to stderr,
337ac06127Sopenharmony_ci// then use catch_unwind to determine whether the compiler's proc_macro is
347ac06127Sopenharmony_ci// working. When proc-macro2 is used from outside of a procedural macro all
357ac06127Sopenharmony_ci// of the proc_macro crate's APIs currently panic.
367ac06127Sopenharmony_ci//
377ac06127Sopenharmony_ci// The Once is to prevent the possibility of this ordering:
387ac06127Sopenharmony_ci//
397ac06127Sopenharmony_ci//     thread 1 calls take_hook, gets the user's original hook
407ac06127Sopenharmony_ci//     thread 1 calls set_hook with the null hook
417ac06127Sopenharmony_ci//     thread 2 calls take_hook, thinks null hook is the original hook
427ac06127Sopenharmony_ci//     thread 2 calls set_hook with the null hook
437ac06127Sopenharmony_ci//     thread 1 calls set_hook with the actual original hook
447ac06127Sopenharmony_ci//     thread 2 calls set_hook with what it thinks is the original hook
457ac06127Sopenharmony_ci//
467ac06127Sopenharmony_ci// in which the user's hook has been lost.
477ac06127Sopenharmony_ci//
487ac06127Sopenharmony_ci// There is still a race condition where a panic in a different thread can
497ac06127Sopenharmony_ci// happen during the interval that the user's original panic hook is
507ac06127Sopenharmony_ci// unregistered such that their hook is incorrectly not called. This is
517ac06127Sopenharmony_ci// sufficiently unlikely and less bad than printing panic messages to stderr
527ac06127Sopenharmony_ci// on correct use of this crate. Maybe there is a libstd feature request
537ac06127Sopenharmony_ci// here. For now, if a user needs to guarantee that this failure mode does
547ac06127Sopenharmony_ci// not occur, they need to call e.g. `proc_macro2::Span::call_site()` from
557ac06127Sopenharmony_ci// the main thread before launching any other threads.
567ac06127Sopenharmony_ci#[cfg(no_is_available)]
577ac06127Sopenharmony_cifn initialize() {
587ac06127Sopenharmony_ci    use std::panic::{self, PanicInfo};
597ac06127Sopenharmony_ci
607ac06127Sopenharmony_ci    type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static;
617ac06127Sopenharmony_ci
627ac06127Sopenharmony_ci    let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ });
637ac06127Sopenharmony_ci    let sanity_check = &*null_hook as *const PanicHook;
647ac06127Sopenharmony_ci    let original_hook = panic::take_hook();
657ac06127Sopenharmony_ci    panic::set_hook(null_hook);
667ac06127Sopenharmony_ci
677ac06127Sopenharmony_ci    let works = panic::catch_unwind(proc_macro::Span::call_site).is_ok();
687ac06127Sopenharmony_ci    WORKS.store(works as usize + 1, Ordering::Relaxed);
697ac06127Sopenharmony_ci
707ac06127Sopenharmony_ci    let hopefully_null_hook = panic::take_hook();
717ac06127Sopenharmony_ci    panic::set_hook(original_hook);
727ac06127Sopenharmony_ci    if sanity_check != &*hopefully_null_hook {
737ac06127Sopenharmony_ci        panic!("observed race condition in proc_macro2::inside_proc_macro");
747ac06127Sopenharmony_ci    }
757ac06127Sopenharmony_ci}
76