Skip to content

Commit 5354848

Browse files
committed
Consolidate cfg checks for wasm/proc_macro
1 parent 5130ca9 commit 5354848

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

build.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::env;
2+
3+
fn main() {
4+
println!("cargo:rerun-if-changed=build.rs");
5+
6+
let target = env::var("TARGET").unwrap();
7+
8+
maybe_enable_use_proc_macro(&target);
9+
}
10+
11+
fn maybe_enable_use_proc_macro(target: &str) {
12+
// wasm targets don't have the `proc_macro` crate, disable this feature.
13+
if target.contains("wasm32") {
14+
return
15+
}
16+
17+
// Otherwise, only enable it if our feature is actually enabled.
18+
if cfg!(feature = "proc-macro") {
19+
println!("cargo:rustc-cfg=use_proc_macro");
20+
}
21+
}

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#![doc(html_root_url = "https://docs.rs/proc-macro2/0.4.9")]
4747
#![cfg_attr(feature = "nightly", feature(proc_macro_raw_ident, proc_macro_span))]
4848

49-
#[cfg(all(not(all(target_arch = "wasm32", target_os = "unknown")), feature = "proc-macro"))]
49+
#[cfg(use_proc_macro)]
5050
extern crate proc_macro;
5151
extern crate unicode_xid;
5252

@@ -146,14 +146,14 @@ impl FromStr for TokenStream {
146146
}
147147
}
148148

149-
#[cfg(all(not(all(target_arch = "wasm32", target_os = "unknown")), feature = "proc-macro"))]
149+
#[cfg(use_proc_macro)]
150150
impl From<proc_macro::TokenStream> for TokenStream {
151151
fn from(inner: proc_macro::TokenStream) -> TokenStream {
152152
TokenStream::_new(inner.into())
153153
}
154154
}
155155

156-
#[cfg(all(not(all(target_arch = "wasm32", target_os = "unknown")), feature = "proc-macro"))]
156+
#[cfg(use_proc_macro)]
157157
impl From<TokenStream> for proc_macro::TokenStream {
158158
fn from(inner: TokenStream) -> proc_macro::TokenStream {
159159
inner.inner.into()
@@ -318,7 +318,7 @@ impl Span {
318318
}
319319

320320
/// This method is only available when the `"nightly"` feature is enabled.
321-
#[cfg(all(not(all(target_arch = "wasm32", target_os = "unknown")), feature = "nightly", feature = "proc-macro"))]
321+
#[cfg(all(feature = "nightly", use_proc_macro))]
322322
pub fn unstable(self) -> proc_macro::Span {
323323
self.inner.unstable()
324324
}

src/stable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl fmt::Debug for TokenStream {
116116
}
117117
}
118118

119-
#[cfg(all(not(all(target_arch = "wasm32", target_os = "unknown")), feature = "proc-macro"))]
119+
#[cfg(use_proc_macro)]
120120
impl From<::proc_macro::TokenStream> for TokenStream {
121121
fn from(inner: ::proc_macro::TokenStream) -> TokenStream {
122122
inner
@@ -126,7 +126,7 @@ impl From<::proc_macro::TokenStream> for TokenStream {
126126
}
127127
}
128128

129-
#[cfg(all(not(all(target_arch = "wasm32", target_os = "unknown")), feature = "proc-macro"))]
129+
#[cfg(use_proc_macro)]
130130
impl From<TokenStream> for ::proc_macro::TokenStream {
131131
fn from(inner: TokenStream) -> ::proc_macro::TokenStream {
132132
inner

0 commit comments

Comments
 (0)