From 0e8e7f4fedf45eb6f0e3bb29a5697137b9bfb9ea Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 22 Feb 2018 06:15:13 -0800 Subject: [PATCH 1/2] Add a feature for linking to `proc-macro` This commit adds a feature to this crate which enables linking to the upstream `proc_macro` crate. This should help this compile on targets which don't have `proc_macro` and allow it to also be suitable for embedding in Rust binaries. This feature is turned on by default for backwards compatibility right now. --- .travis.yml | 1 + Cargo.toml | 3 +++ src/lib.rs | 5 ++++- src/stable.rs | 11 ++++++----- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index a524c016..78942c40 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ matrix: script: - cargo test - cargo build --features nightly + - cargo build --no-default-features - RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test - RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo build --features nightly - RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo doc --no-deps diff --git a/Cargo.toml b/Cargo.toml index 4630c184..8c242eba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,3 +27,6 @@ unicode-xid = "0.1" # When disabled: emulate the same API as the nightly compiler's proc_macro crate # but in a way that works on all stable compilers >=1.15.0. nightly = [] + +proc-macro = [] +default = ["proc-macro"] diff --git a/src/lib.rs b/src/lib.rs index d08988c0..31ba7654 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,6 +28,7 @@ #![cfg_attr(feature = "nightly", feature(proc_macro))] +#[cfg(feature = "proc-macro")] extern crate proc_macro; #[cfg(not(feature = "nightly"))] @@ -67,12 +68,14 @@ impl FromStr for TokenStream { } } +#[cfg(feature = "proc-macro")] impl From for TokenStream { fn from(inner: proc_macro::TokenStream) -> TokenStream { TokenStream(inner.into()) } } +#[cfg(feature = "proc-macro")] impl From for proc_macro::TokenStream { fn from(inner: TokenStream) -> proc_macro::TokenStream { inner.0.into() @@ -175,7 +178,7 @@ impl Span { } /// This method is only available when the `"nightly"` feature is enabled. - #[cfg(feature = "nightly")] + #[cfg(all(feature = "nightly", feature = "proc-macro"))] pub fn unstable(self) -> proc_macro::Span { self.0.unstable() } diff --git a/src/stable.rs b/src/stable.rs index 41a7c4b3..54210600 100644 --- a/src/stable.rs +++ b/src/stable.rs @@ -11,7 +11,6 @@ use std::rc::Rc; use std::str::FromStr; use std::vec; -use proc_macro; use unicode_xid::UnicodeXID; use strnom::{Cursor, PResult, skip_whitespace, block_comment, whitespace, word_break}; @@ -120,14 +119,16 @@ impl fmt::Display for TokenStream { } } -impl From for TokenStream { - fn from(inner: proc_macro::TokenStream) -> TokenStream { +#[cfg(feature = "proc-macro")] +impl From<::proc_macro::TokenStream> for TokenStream { + fn from(inner: ::proc_macro::TokenStream) -> TokenStream { inner.to_string().parse().expect("compiler token stream parse failed") } } -impl From for proc_macro::TokenStream { - fn from(inner: TokenStream) -> proc_macro::TokenStream { +#[cfg(feature = "proc-macro")] +impl From for ::proc_macro::TokenStream { + fn from(inner: TokenStream) -> ::proc_macro::TokenStream { inner.to_string().parse().expect("failed to parse to compiler tokens") } } From 724687bc7b956c966f0eb5168b3943ec04ecb2e1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 22 Feb 2018 10:17:48 -0800 Subject: [PATCH 2/2] Fix default-features = false, features = ["nightly"] --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8c242eba..5ede4780 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ unicode-xid = "0.1" # # When disabled: emulate the same API as the nightly compiler's proc_macro crate # but in a way that works on all stable compilers >=1.15.0. -nightly = [] +nightly = ["proc-macro"] proc-macro = [] default = ["proc-macro"]