Fix the `nightly` feature outside of rustc
This commit is a stab at getting the `proc_macro2` crate to function outside the
context of the compiler, **even when the `nightly` feature is enabled**.
Previously when the `nightly` feature was enabled then `proc_macro2` would panic
at runtime because `proc_macro` itself would panic at runtime due to the lack of
the compiler being initialized.
In this commit the `unstable` module in `proc_macro2` no longer unconditionally
uses the `proc_macro` upstream crate but is rather an `enum` over the upstream
crate and the `stable` module. At runtime the appropriate implementation is
dynamically selected depending on which works.
This brings up some uncomfortable issues such as what happens when you try to
psas a "stable span" to a "nightly `Literal`", but I think we can paper over
these issues in time by further canonicalizing everything to nightly/stable if
it comes up.
One caveat this brings up is that `Span::unstable` unconditionally panics when
outside the compiler, but I think that's expected regardless.
diff --git a/src/lib.rs b/src/lib.rs
index 91b3add..11ec549 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -48,8 +48,6 @@
#[cfg(feature = "proc-macro")]
extern crate proc_macro;
-
-#[cfg(not(feature = "nightly"))]
extern crate unicode_xid;
use std::fmt;
@@ -59,12 +57,11 @@
use std::str::FromStr;
#[macro_use]
-#[cfg(not(feature = "nightly"))]
mod strnom;
+mod stable;
-#[path = "stable.rs"]
#[cfg(not(feature = "nightly"))]
-mod imp;
+use stable as imp;
#[path = "unstable.rs"]
#[cfg(feature = "nightly")]
mod imp;
@@ -88,6 +85,13 @@
}
}
+ fn _new_stable(inner: stable::TokenStream) -> TokenStream {
+ TokenStream {
+ inner: inner.into(),
+ _marker: marker::PhantomData,
+ }
+ }
+
pub fn empty() -> TokenStream {
TokenStream::_new(imp::TokenStream::empty())
}
@@ -201,6 +205,13 @@
}
}
+ fn _new_stable(inner: stable::Span) -> Span {
+ Span {
+ inner: inner.into(),
+ _marker: marker::PhantomData,
+ }
+ }
+
pub fn call_site() -> Span {
Span::_new(imp::Span::call_site())
}
@@ -525,6 +536,13 @@
}
}
+ fn _new_stable(inner: stable::Literal) -> Literal {
+ Literal {
+ inner: inner.into(),
+ _marker: marker::PhantomData,
+ }
+ }
+
int_literals! {
u8_suffixed => u8,
u16_suffixed => u16,