Skip to content

Commit

Permalink
add a cargo feature to automatically detect usage of a nightly compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
Alextopher committed Oct 22, 2024
1 parent b02d79c commit bec8429
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ keywords = ["proc-macro", "error", "errors"]
categories = ["development-tools::procedural-macro-helpers"]
license = "MIT OR Apache-2.0"
edition = "2021"
build = "build.rs"

[dependencies]
quote = "1"
proc-macro2 = "1"
proc-macro-error-attr2 = { path = "./proc-macro-error-attr", version = "=2.0.0" }

[dependencies.syn]
version = "2"
optional = true
default-features = false
syn = { version = "2", optional = true, default-features = false }

[dev-dependencies]
test-crate = { path = "./test-crate" }
Expand All @@ -32,9 +29,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 }
Expand Down
18 changes: 18 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[cfg(all(feature = "detect-nightly", not(feature = "nightly")))]
pub fn main() {
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
}
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit bec8429

Please sign in to comment.