Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore support for toolchains without $:literal matcher #202

Merged
merged 1 commit into from Dec 27, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions build.rs
@@ -0,0 +1,30 @@
use std::env;
use std::process::Command;
use std::str;

// 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() {
let minor = match rustc_minor_version() {
Some(minor) => minor,
None => return,
};

// The $:literal matcher in macro_rules stabilized in 1.32:
// https://blog.rust-lang.org/2019/01/17/Rust-1.32.0.html#macro-improvements
if minor < 32 {
println!("cargo:rustc-cfg=no_literal_matcher");
}
}

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()
}
44 changes: 40 additions & 4 deletions src/lib.rs
Expand Up @@ -1070,14 +1070,32 @@ macro_rules! quote_token {
$crate::__private::push_lifetime(&mut $tokens, stringify!($lifetime));
};

($tokens:ident _) => {
$crate::__private::push_underscore(&mut $tokens);
};

($tokens:ident $other:tt) => {
$crate::quote_literal_or_token!($tokens $other);
};
}

#[cfg(not(no_literal_matcher))]
#[macro_export]
#[doc(hidden)]
macro_rules! quote_literal_or_token {
($tokens:ident $literal:literal) => {
$crate::__private::push_literal(&mut $tokens, stringify!($literal));
};

($tokens:ident _) => {
$crate::__private::push_underscore(&mut $tokens);
($tokens:ident $other:tt) => {
$crate::__private::parse(&mut $tokens, stringify!($other));
};
}

#[cfg(no_literal_matcher)]
#[macro_export]
#[doc(hidden)]
macro_rules! quote_literal_or_token {
($tokens:ident $other:tt) => {
$crate::__private::parse(&mut $tokens, stringify!($other));
};
Expand Down Expand Up @@ -1297,14 +1315,32 @@ macro_rules! quote_token_spanned {
$crate::__private::push_lifetime_spanned(&mut $tokens, $span, stringify!($lifetime));
};

($tokens:ident $span:ident _) => {
$crate::__private::push_underscore_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident $other:tt) => {
$crate::quote_literal_or_token_spanned!($tokens $span $other);
};
}

#[cfg(not(no_literal_matcher))]
#[macro_export]
#[doc(hidden)]
macro_rules! quote_literal_or_token_spanned {
($tokens:ident $span:ident $literal:literal) => {
$crate::__private::push_literal_spanned(&mut $tokens, $span, stringify!($literal));
};

($tokens:ident $span:ident _) => {
$crate::__private::push_underscore_spanned(&mut $tokens, $span);
($tokens:ident $span:ident $other:tt) => {
$crate::__private::parse_spanned(&mut $tokens, $span, stringify!($other));
};
}

#[cfg(no_literal_matcher)]
#[macro_export]
#[doc(hidden)]
macro_rules! quote_literal_or_token_spanned {
($tokens:ident $span:ident $other:tt) => {
$crate::__private::parse_spanned(&mut $tokens, $span, stringify!($other));
};
Expand Down