Automatically use rich API on 1.30.0+
This commit detects the rustc version in the build script of proc-macro2
to determine whether the compiler supports the necessary backend APIs.
This should hopefully allow the crate to compile on stable by default
but have a better implementation on 1.30.0+ compilers.
diff --git a/src/lib.rs b/src/lib.rs
index 39ab87a..9eac7d6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -45,7 +45,7 @@
// Proc-macro2 types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/proc-macro2/0.4.13")]
#![cfg_attr(
- feature = "nightly",
+ super_unstable,
feature(proc_macro_raw_ident, proc_macro_span)
)]
@@ -65,10 +65,10 @@
mod strnom;
mod stable;
-#[cfg(not(feature = "nightly"))]
+#[cfg(not(wrap_proc_macro))]
use stable as imp;
#[path = "unstable.rs"]
-#[cfg(feature = "nightly")]
+#[cfg(wrap_proc_macro)]
mod imp;
/// An abstract stream of tokens, or more concretely a sequence of token trees.
@@ -328,7 +328,7 @@
}
/// This method is only available when the `"nightly"` feature is enabled.
- #[cfg(all(feature = "nightly", use_proc_macro))]
+ #[cfg(super_unstable)]
pub fn unstable(self) -> proc_macro::Span {
self.inner.unstable()
}
diff --git a/src/unstable.rs b/src/unstable.rs
index 66158ec..e62c86a 100644
--- a/src/unstable.rs
+++ b/src/unstable.rs
@@ -1,4 +1,4 @@
-#![cfg_attr(not(procmacro2_semver_exempt), allow(dead_code))]
+#![cfg_attr(not(super_unstable), allow(dead_code))]
use std::fmt;
use std::iter;
@@ -292,11 +292,13 @@
// NOTE: We have to generate our own filename object here because we can't wrap
// the one provided by proc_macro.
#[derive(Clone, PartialEq, Eq)]
+#[cfg(super_unstable)]
pub enum SourceFile {
Nightly(proc_macro::SourceFile, FileName),
Stable(stable::SourceFile),
}
+#[cfg(super_unstable)]
impl SourceFile {
fn nightly(sf: proc_macro::SourceFile) -> Self {
let filename = stable::file_name(sf.path().display().to_string());
@@ -319,12 +321,14 @@
}
}
+#[cfg(super_unstable)]
impl AsRef<FileName> for SourceFile {
fn as_ref(&self) -> &FileName {
self.path()
}
}
+#[cfg(super_unstable)]
impl fmt::Debug for SourceFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
@@ -354,6 +358,7 @@
}
}
+ #[cfg(super_unstable)]
pub fn def_site() -> Span {
if nightly_works() {
Span::Nightly(proc_macro::Span::def_site())
@@ -362,6 +367,7 @@
}
}
+ #[cfg(super_unstable)]
pub fn resolved_at(&self, other: Span) -> Span {
match (self, other) {
(Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.resolved_at(b)),
@@ -370,6 +376,7 @@
}
}
+ #[cfg(super_unstable)]
pub fn located_at(&self, other: Span) -> Span {
match (self, other) {
(Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.located_at(b)),
@@ -385,7 +392,7 @@
}
}
- #[cfg(procmacro2_semver_exempt)]
+ #[cfg(super_unstable)]
pub fn source_file(&self) -> SourceFile {
match self {
Span::Nightly(s) => SourceFile::nightly(s.source_file()),
@@ -393,7 +400,7 @@
}
}
- #[cfg(procmacro2_semver_exempt)]
+ #[cfg(super_unstable)]
pub fn start(&self) -> LineColumn {
match self {
Span::Nightly(s) => {
@@ -407,7 +414,7 @@
}
}
- #[cfg(procmacro2_semver_exempt)]
+ #[cfg(super_unstable)]
pub fn end(&self) -> LineColumn {
match self {
Span::Nightly(s) => {
@@ -421,7 +428,7 @@
}
}
- #[cfg(procmacro2_semver_exempt)]
+ #[cfg(super_unstable)]
pub fn join(&self, other: Span) -> Option<Span> {
let ret = match (self, other) {
(Span::Nightly(a), Span::Nightly(b)) => Span::Nightly(a.join(b)?),
@@ -431,6 +438,7 @@
Some(ret)
}
+ #[cfg(super_unstable)]
pub fn eq(&self, other: &Span) -> bool {
match (self, other) {
(Span::Nightly(a), Span::Nightly(b)) => a.eq(b),
@@ -484,7 +492,17 @@
pub fn new_raw(string: &str, span: Span) -> Ident {
match span {
- Span::Nightly(s) => Ident::Nightly(proc_macro::Ident::new_raw(string, s)),
+ Span::Nightly(s) => {
+ let p: proc_macro::TokenStream = string.parse().unwrap();
+ let ident = match p.into_iter().next() {
+ Some(proc_macro::TokenTree::Ident(mut i)) => {
+ i.set_span(s);
+ i
+ }
+ _ => panic!(),
+ };
+ Ident::Nightly(ident)
+ }
Span::Stable(s) => Ident::Stable(stable::Ident::new_raw(string, s)),
}
}