Skip to content

Commit b34d561

Browse files
committed
add a cargo feature to automatically detect usage of a nightly compiler
1 parent b02d79c commit b34d561

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@ trybuild = { version = "1.0.99", features = ["diff"] }
3232
default = ["syn-error"]
3333
syn-error = ["dep:syn"]
3434
nightly = []
35+
detect-nightly = []
3536

3637
[lints.rust]
37-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(run_ui_tests)'] }
38+
unexpected_cfgs = { level = "warn", check-cfg = [
39+
'cfg(run_ui_tests)',
40+
'cfg(detected_nightly)',
41+
] }
3842

3943
[lints.clippy]
4044
pedantic = { level = "warn", priority = -1 }

build.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#[cfg(all(feature = "detect-nightly", not(feature = "nightly")))]
2+
pub fn main() {
3+
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());
4+
5+
let rustc_version = std::process::Command::new(rustc)
6+
.arg("--version")
7+
.output()
8+
.expect("Failed to get rustc version")
9+
.stdout;
10+
11+
let rustc_version = String::from_utf8(rustc_version).expect("Failed to parse rustc version");
12+
if rustc_version.contains("nightly") {
13+
println!("cargo:rustc-cfg=detected_nightly");
14+
}
15+
}
16+
17+
#[cfg(not(feature = "detect-nightly"))]
18+
pub fn main() {
19+
// do nothing if the 'detect-nightly' feature is not enabled
20+
}

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@
270270
//! [`proc-macro2::Span`]: https://docs.rs/proc-macro2/1.0.10/proc_macro2/struct.Span.html
271271
//! [`ToTokens`]: https://docs.rs/quote/1.0.3/quote/trait.ToTokens.html
272272
//!
273-
274-
#![cfg_attr(feature = "nightly", feature(proc_macro_diagnostic))]
273+
#![cfg_attr(
274+
any(feature = "nightly", detected_nightly),
275+
feature(proc_macro_diagnostic)
276+
)]
275277
#![forbid(unsafe_code)]
276278

277279
extern crate proc_macro;
@@ -294,11 +296,11 @@ mod diagnostic;
294296
mod macros;
295297
mod sealed;
296298

297-
#[cfg(not(feature = "nightly"))]
299+
#[cfg(not(any(feature = "nightly", detected_nightly)))]
298300
#[path = "imp/fallback.rs"]
299301
mod imp;
300302

301-
#[cfg(feature = "nightly")]
303+
#[cfg(any(feature = "nightly", detected_nightly))]
302304
#[path = "imp/delegate.rs"]
303305
mod imp;
304306

0 commit comments

Comments
 (0)