Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion build/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() {
};

let version = match rustc::parse(&string) {
Some(version) => format!("{:#?}\n", version),
Some(version) => version,
None => {
eprintln!(
"Error: unexpected output from `rustc --version`: {:?}\n\n\
Expand All @@ -47,6 +47,12 @@ fn main() {
}
};

if version.minor < 38 {
// Prior to 1.38, a #[proc_macro] is not allowed to be named `cfg`.
println!("cargo:rustc-cfg=cfg_macro_not_allowed");
}

let version = format!("{:#?}\n", version);
let out_dir = env::var_os("OUT_DIR").expect("OUT_DIR not set");
let out_file = Path::new(&out_dir).join("version.rs");
fs::write(out_file, version).expect("failed to write version.rs");
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,18 @@ pub fn attr(args: TokenStream, input: TokenStream) -> TokenStream {
.and_then(|args| expand::try_attr(args, input))
.unwrap_or_else(Error::into_compile_error)
}

#[cfg(not(cfg_macro_not_allowed))]
#[proc_macro]
pub fn cfg(input: TokenStream) -> TokenStream {
use proc_macro::{Ident, Span, TokenTree};
(|| {
let ref mut args = iter::new(input);
let expr = expr::parse(args)?;
token::parse_end(args)?;
let boolean = expr.eval(RUSTVERSION);
let ident = Ident::new(&boolean.to_string(), Span::call_site());
Ok(TokenStream::from(TokenTree::Ident(ident)))
})()
.unwrap_or_else(Error::into_compile_error)
}