1 | use core::sync::atomic::{AtomicUsize, Ordering}; |
2 | use std::sync::Once; |
3 | |
4 | static WORKS: AtomicUsize = AtomicUsize::new(0); |
5 | static INIT: Once = Once::new(); |
6 | |
7 | pub(crate) fn inside_proc_macro() -> bool { |
8 | match WORKS.load(order:Ordering::Relaxed) { |
9 | 1 => return false, |
10 | 2 => return true, |
11 | _ => {} |
12 | } |
13 | |
14 | INIT.call_once(initialize); |
15 | inside_proc_macro() |
16 | } |
17 | |
18 | pub(crate) fn force_fallback() { |
19 | WORKS.store(val:1, order:Ordering::Relaxed); |
20 | } |
21 | |
22 | pub(crate) fn unforce_fallback() { |
23 | initialize(); |
24 | } |
25 | |
26 | #[cfg (not(no_is_available))] |
27 | fn initialize() { |
28 | let available: bool = proc_macro::is_available(); |
29 | WORKS.store(val:available as usize + 1, order:Ordering::Relaxed); |
30 | } |
31 | |
32 | // Swap in a null panic hook to avoid printing "thread panicked" to stderr, |
33 | // then use catch_unwind to determine whether the compiler's proc_macro is |
34 | // working. When proc-macro2 is used from outside of a procedural macro all |
35 | // of the proc_macro crate's APIs currently panic. |
36 | // |
37 | // The Once is to prevent the possibility of this ordering: |
38 | // |
39 | // thread 1 calls take_hook, gets the user's original hook |
40 | // thread 1 calls set_hook with the null hook |
41 | // thread 2 calls take_hook, thinks null hook is the original hook |
42 | // thread 2 calls set_hook with the null hook |
43 | // thread 1 calls set_hook with the actual original hook |
44 | // thread 2 calls set_hook with what it thinks is the original hook |
45 | // |
46 | // in which the user's hook has been lost. |
47 | // |
48 | // There is still a race condition where a panic in a different thread can |
49 | // happen during the interval that the user's original panic hook is |
50 | // unregistered such that their hook is incorrectly not called. This is |
51 | // sufficiently unlikely and less bad than printing panic messages to stderr |
52 | // on correct use of this crate. Maybe there is a libstd feature request |
53 | // here. For now, if a user needs to guarantee that this failure mode does |
54 | // not occur, they need to call e.g. `proc_macro2::Span::call_site()` from |
55 | // the main thread before launching any other threads. |
56 | #[cfg (no_is_available)] |
57 | fn initialize() { |
58 | use std::panic::{self, PanicInfo}; |
59 | |
60 | type PanicHook = dyn Fn(&PanicInfo) + Sync + Send + 'static; |
61 | |
62 | let null_hook: Box<PanicHook> = Box::new(|_panic_info| { /* ignore */ }); |
63 | let sanity_check = &*null_hook as *const PanicHook; |
64 | let original_hook = panic::take_hook(); |
65 | panic::set_hook(null_hook); |
66 | |
67 | let works = panic::catch_unwind(proc_macro::Span::call_site).is_ok(); |
68 | WORKS.store(works as usize + 1, Ordering::Relaxed); |
69 | |
70 | let hopefully_null_hook = panic::take_hook(); |
71 | panic::set_hook(original_hook); |
72 | if sanity_check != &*hopefully_null_hook { |
73 | panic!("observed race condition in proc_macro2::inside_proc_macro" ); |
74 | } |
75 | } |
76 | |