From 1e609ab5dd10a94ef3754ba39426d46e254f625e Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Mon, 26 Oct 2020 05:53:03 +0900 Subject: [PATCH] Make proc-macro-nested empty crate at Rust 1.45+ proc-macro-hack does nothing in these versions, so proc-macro-nested doesn't need to do anything either. --- nested/build.rs | 30 ++++++++++++++++++++++++++++++ nested/src/lib.rs | 2 ++ 2 files changed, 32 insertions(+) diff --git a/nested/build.rs b/nested/build.rs index d451b74..a2659dd 100644 --- a/nested/build.rs +++ b/nested/build.rs @@ -2,6 +2,8 @@ use std::env; use std::fs; use std::iter; use std::path::Path; +use std::process::Command; +use std::str; /* #[doc(hidden)] @@ -14,10 +16,27 @@ macro_rules! count { } */ +// The rustc-cfg strings below are *not* public API. Please let us know by +// opening a GitHub issue if your build environment requires some way to enable +// these cfgs other than by executing our build script. fn main() { // Tell Cargo not to rerun on src/lib.rs changes. println!("cargo:rerun-if-changed=build.rs"); + let minor = match rustc_minor_version() { + Some(minor) => minor, + None => return, + }; + + // Function like procedural macros in expressions patterns statements stabilized in Rust 1.45: + // https://blog.rust-lang.org/2020/07/16/Rust-1.45.0.html#stabilizing-function-like-procedural-macros-in-expressions-patterns-and-statements + if minor >= 45 { + println!("cargo:rustc-cfg=fn_like_proc_macro"); + // proc-macro-hack does nothing in these versions, so proc-macro-nested + // doesn't need to do anything either. + return; + } + let mut content = String::new(); content += "#[doc(hidden)]\n"; content += "#[macro_export]\n"; @@ -46,3 +65,14 @@ fn main() { fs::write(dest_path, content).unwrap(); } } + +fn rustc_minor_version() -> Option { + let rustc = env::var_os("RUSTC")?; + let output = Command::new(rustc).arg("--version").output().ok()?; + let version = str::from_utf8(&output.stdout).ok()?; + let mut pieces = version.split('.'); + if pieces.next() != Some("rustc 1") { + return None; + } + pieces.next()?.parse().ok() +} diff --git a/nested/src/lib.rs b/nested/src/lib.rs index 0cd8302..66d838b 100644 --- a/nested/src/lib.rs +++ b/nested/src/lib.rs @@ -40,8 +40,10 @@ #![no_std] +#[cfg(not(fn_like_proc_macro))] include!(concat!(env!("OUT_DIR"), "/count.rs")); +#[cfg(not(fn_like_proc_macro))] #[doc(hidden)] #[macro_export] macro_rules! dispatch {