Use proc_macro::is_available() on rust 1.57+

This avoids the need for catching a panic, which is incompatible with
projects using panic=abort or cg_clif.
This commit is contained in:
bjorn3 2021-10-11 18:02:21 +02:00
parent 0253a0b7cd
commit f0b0040bf8
2 changed files with 21 additions and 3 deletions

View File

@ -33,6 +33,12 @@
// location of a token. Enabled by procmacro2_semver_exempt or the
// "span-locations" Cargo cfg. This is behind a cfg because tracking
// location inside spans is a performance hit.
//
// "is_available"
// Use `proc_macro::is_available()` to detect if the proc macro API is
// available or needs to be polyfilled instead of trying to use the proc
// macro API and catching a panic if it isn't available.
// Enabled on Rust 1.57+
use std::env;
use std::iter;
@ -82,6 +88,10 @@ fn main() {
println!("cargo:rustc-cfg=literal_from_str");
}
if version.minor >= 57 {
println!("cargo:rustc-cfg=is_available");
}
let target = env::var("TARGET").unwrap();
if !enable_use_proc_macro(&target) {
return;

View File

@ -12,8 +12,16 @@ pub(crate) fn inside_proc_macro() -> bool {
_ => {}
}
INIT.call_once(initialize);
inside_proc_macro()
#[cfg(feature = "is_available")]
{
proc_macro::is_available()
}
#[cfg(not(feature = "is_available"))]
{
INIT.call_once(initialize);
inside_proc_macro()
}
}
pub(crate) fn force_fallback() {
@ -21,7 +29,7 @@ pub(crate) fn force_fallback() {
}
pub(crate) fn unforce_fallback() {
initialize();
WORKS.store(0, Ordering::SeqCst);
}
// Swap in a null panic hook to avoid printing "thread panicked" to stderr,