Skip to content

Commit

Permalink
Use proc_macro::is_available() on rust 1.57+
Browse files Browse the repository at this point in the history
This avoids the need for catching a panic, which is incompatible with
projects using panic=abort or cg_clif.
  • Loading branch information
bjorn3 committed Oct 11, 2021
1 parent 0253a0b commit f0b0040
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 10 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 11 additions & 3 deletions src/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ 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() {
WORKS.store(1, Ordering::SeqCst);
}

pub(crate) fn unforce_fallback() {
initialize();
WORKS.store(0, Ordering::SeqCst);
}

// Swap in a null panic hook to avoid printing "thread panicked" to stderr,
Expand Down

0 comments on commit f0b0040

Please sign in to comment.