detection.rs 2.7 KB

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