From b34d561f64258580fdc2e30bc7d38ec9f31ca368 Mon Sep 17 00:00:00 2001 From: Alextopher Date: Fri, 25 Oct 2024 20:53:02 -0400 Subject: [PATCH 1/2] add a cargo feature to automatically detect usage of a nightly compiler --- Cargo.toml | 6 +++++- build.rs | 20 ++++++++++++++++++++ src/lib.rs | 10 ++++++---- 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 build.rs diff --git a/Cargo.toml b/Cargo.toml index fcc6161..c92bdb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,9 +32,13 @@ trybuild = { version = "1.0.99", features = ["diff"] } default = ["syn-error"] syn-error = ["dep:syn"] nightly = [] +detect-nightly = [] [lints.rust] -unexpected_cfgs = { level = "warn", check-cfg = ['cfg(run_ui_tests)'] } +unexpected_cfgs = { level = "warn", check-cfg = [ + 'cfg(run_ui_tests)', + 'cfg(detected_nightly)', +] } [lints.clippy] pedantic = { level = "warn", priority = -1 } diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..bdeb1dc --- /dev/null +++ b/build.rs @@ -0,0 +1,20 @@ +#[cfg(all(feature = "detect-nightly", not(feature = "nightly")))] +pub fn main() { + let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string()); + + let rustc_version = std::process::Command::new(rustc) + .arg("--version") + .output() + .expect("Failed to get rustc version") + .stdout; + + let rustc_version = String::from_utf8(rustc_version).expect("Failed to parse rustc version"); + if rustc_version.contains("nightly") { + println!("cargo:rustc-cfg=detected_nightly"); + } +} + +#[cfg(not(feature = "detect-nightly"))] +pub fn main() { + // do nothing if the 'detect-nightly' feature is not enabled +} diff --git a/src/lib.rs b/src/lib.rs index a00c924..1ce31c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -270,8 +270,10 @@ //! [`proc-macro2::Span`]: https://docs.rs/proc-macro2/1.0.10/proc_macro2/struct.Span.html //! [`ToTokens`]: https://docs.rs/quote/1.0.3/quote/trait.ToTokens.html //! - -#![cfg_attr(feature = "nightly", feature(proc_macro_diagnostic))] +#![cfg_attr( + any(feature = "nightly", detected_nightly), + feature(proc_macro_diagnostic) +)] #![forbid(unsafe_code)] extern crate proc_macro; @@ -294,11 +296,11 @@ mod diagnostic; mod macros; mod sealed; -#[cfg(not(feature = "nightly"))] +#[cfg(not(any(feature = "nightly", detected_nightly)))] #[path = "imp/fallback.rs"] mod imp; -#[cfg(feature = "nightly")] +#[cfg(any(feature = "nightly", detected_nightly))] #[path = "imp/delegate.rs"] mod imp; From 0e1a1cac9ec497772f55b41984d81304c4235cbf Mon Sep 17 00:00:00 2001 From: Alextopher Date: Fri, 13 Dec 2024 05:00:55 -0500 Subject: [PATCH 2/2] fail into a successful build --- build.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.rs b/build.rs index bdeb1dc..80701bb 100644 --- a/build.rs +++ b/build.rs @@ -5,10 +5,10 @@ pub fn main() { let rustc_version = std::process::Command::new(rustc) .arg("--version") .output() - .expect("Failed to get rustc version") - .stdout; + .map(|c| c.stdout) + .unwrap_or_default(); - let rustc_version = String::from_utf8(rustc_version).expect("Failed to parse rustc version"); + let rustc_version = String::from_utf8(rustc_version).unwrap_or_default(); if rustc_version.contains("nightly") { println!("cargo:rustc-cfg=detected_nightly"); }