From b128cd6cfd5647da1cb2f74295ea076515eb8174 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 1 May 2020 14:46:29 -0700 Subject: [PATCH] Add a way to force fallback implementation --- src/detection.rs | 8 ++++++++ src/fallback.rs | 12 ++++++++++++ src/lib.rs | 6 +++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/detection.rs b/src/detection.rs index 3b86f2cb..c597bc99 100644 --- a/src/detection.rs +++ b/src/detection.rs @@ -16,6 +16,14 @@ pub(crate) fn inside_proc_macro() -> bool { inside_proc_macro() } +pub(crate) fn force_fallback() { + WORKS.store(1, Ordering::SeqCst); +} + +pub(crate) fn unforce_fallback() { + initialize(); +} + // Swap in a null panic hook to avoid printing "thread panicked" to stderr, // then use catch_unwind to determine whether the compiler's proc_macro is // working. When proc-macro2 is used from outside of a procedural macro all diff --git a/src/fallback.rs b/src/fallback.rs index c599f423..cb244af2 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -14,6 +14,18 @@ use std::str::FromStr; use std::vec; use unicode_xid::UnicodeXID; +/// Force use of proc-macro2's fallback implementation of the API for now, even +/// if the compiler's implementation is available. +pub fn force() { + crate::detection::force_fallback(); +} + +/// Resume using the compiler's implementation of the proc macro API if it is +/// available. +pub fn unforce() { + crate::detection::unforce_fallback(); +} + #[derive(Clone)] pub(crate) struct TokenStream { inner: Vec, diff --git a/src/lib.rs b/src/lib.rs index b95e6df9..a28eafca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,7 +99,11 @@ use std::str::FromStr; mod detection; #[macro_use] mod strnom; -mod fallback; + +// Public for proc_macro2::fallback::force() and unforce(), but those are quite +// a niche use case so we omit it from rustdoc. +#[doc(hidden)] +pub mod fallback; #[cfg(not(wrap_proc_macro))] use crate::fallback as imp;