Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Make proc-macro-nested empty crate at Rust 1.45+
Browse files Browse the repository at this point in the history
proc-macro-hack does nothing in these versions, so proc-macro-nested
doesn't need to do anything either.
  • Loading branch information
taiki-e committed Oct 27, 2020
1 parent 52a4b90 commit 1e609ab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
30 changes: 30 additions & 0 deletions nested/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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";
Expand Down Expand Up @@ -46,3 +65,14 @@ fn main() {
fs::write(dest_path, content).unwrap();
}
}

fn rustc_minor_version() -> Option<u32> {
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()
}
2 changes: 2 additions & 0 deletions nested/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1e609ab

Please sign in to comment.