diff --git a/color-spantrace/build.rs b/color-spantrace/build.rs new file mode 100644 index 0000000..aae2878 --- /dev/null +++ b/color-spantrace/build.rs @@ -0,0 +1,53 @@ +use std::env; +use std::ffi::OsString; +use std::process::Command; +use std::str; + +fn main() { + let version = match rustc_version_info() { + Some(version) => version, + None => return, + }; + version.toolchain.set_feature(); +} + +#[derive(PartialEq)] +enum Toolchain { + Stable, + Beta, + Nightly, +} + +impl Toolchain { + fn set_feature(self) { + match self { + Toolchain::Nightly => println!("cargo:rustc-cfg=nightly"), + Toolchain::Beta => println!("cargo:rustc-cfg=beta"), + Toolchain::Stable => println!("cargo:rustc-cfg=stable"), + } + } +} + +struct VersionInfo { + toolchain: Toolchain, +} + +fn rustc_version_info() -> Option { + let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc")); + let output = Command::new(rustc).arg("--version").output().ok()?; + let version = str::from_utf8(&output.stdout).ok()?; + let mut pieces = version.split(['.', ' ', '-']); + if pieces.next() != Some("rustc") { + return None; + } + let _major: u32 = pieces.next()?.parse().ok()?; + let _minor: u32 = pieces.next()?.parse().ok()?; + let _patch: u32 = pieces.next()?.parse().ok()?; + let toolchain = match pieces.next() { + Some("beta") => Toolchain::Beta, + Some("nightly") => Toolchain::Nightly, + _ => Toolchain::Stable, + }; + let version = VersionInfo { toolchain }; + Some(version) +} diff --git a/color-spantrace/src/lib.rs b/color-spantrace/src/lib.rs index 80a0cee..61fc923 100644 --- a/color-spantrace/src/lib.rs +++ b/color-spantrace/src/lib.rs @@ -61,10 +61,11 @@ //! [`color-backtrace`]: https://github.com/athre0z/color-backtrace #![doc(html_root_url = "https://docs.rs/color-spantrace/0.2.0")] #![cfg_attr( - nightly_features, + nightly, feature(rustdoc_missing_doc_code_examples), warn(rustdoc::missing_doc_code_examples) )] +#![cfg_attr(stable, warn(private_in_public))] #![warn( missing_debug_implementations, missing_docs, @@ -78,7 +79,6 @@ overflowing_literals, path_statements, patterns_in_fns_without_body, - private_in_public, unconditional_recursion, unused, unused_allocation, diff --git a/eyre/build.rs b/eyre/build.rs index bb63ea9..e0b2d29 100644 --- a/eyre/build.rs +++ b/eyre/build.rs @@ -61,9 +61,7 @@ fn main() { None => return, }; - if version.is_nightly { - println!("cargo:rustc-cfg=nightly_features"); - } + version.toolchain.set_feature(); if version.minor < 52 { println!("cargo:rustc-cfg=eyre_no_fmt_arguments_as_str"); @@ -91,9 +89,26 @@ fn compile_probe(probe: &str) -> Option { .ok() } +// TODO factor this toolchain parsing and related tests into its own file +#[derive(PartialEq)] +enum Toolchain { + Stable, + Beta, + Nightly, +} +impl Toolchain { + fn set_feature(self) { + match self { + Toolchain::Nightly => println!("cargo:rustc-cfg=nightly"), + Toolchain::Beta => println!("cargo:rustc-cfg=beta"), + Toolchain::Stable => println!("cargo:rustc-cfg=stable"), + } + } +} + struct VersionInfo { minor: u32, - is_nightly: bool, + toolchain: Toolchain, } fn rustc_version_info() -> Option { @@ -107,7 +122,11 @@ fn rustc_version_info() -> Option { let _major: u32 = pieces.next()?.parse().ok()?; let minor = pieces.next()?.parse().ok()?; let _patch: u32 = pieces.next()?.parse().ok()?; - let is_nightly = pieces.next() == Some("nightly"); - let version = VersionInfo { minor, is_nightly }; + let toolchain = match pieces.next() { + Some("beta") => Toolchain::Beta, + Some("nightly") => Toolchain::Nightly, + _ => Toolchain::Stable, + }; + let version = VersionInfo { minor, toolchain }; Some(version) } diff --git a/eyre/src/lib.rs b/eyre/src/lib.rs index 39d4316..bce3b23 100644 --- a/eyre/src/lib.rs +++ b/eyre/src/lib.rs @@ -316,10 +316,11 @@ //! [`color-backtrace`]: https://github.com/athre0z/color-backtrace #![doc(html_root_url = "https://docs.rs/eyre/0.6.8")] #![cfg_attr( - nightly_features, + nightly, feature(rustdoc_missing_doc_code_examples), warn(rustdoc::missing_doc_code_examples) )] +#![cfg_attr(stable, warn(private_in_public))] #![warn( missing_debug_implementations, missing_docs, @@ -334,7 +335,6 @@ overflowing_literals, path_statements, patterns_in_fns_without_body, - private_in_public, unconditional_recursion, unused, unused_allocation, diff --git a/eyre/tests/test_toolchain.rs b/eyre/tests/test_toolchain.rs index fc54cd6..0309398 100644 --- a/eyre/tests/test_toolchain.rs +++ b/eyre/tests/test_toolchain.rs @@ -1,17 +1,34 @@ +// These tests check our build script against rustversion. + #[rustversion::attr(not(nightly), ignore)] -//#[cfg_attr(miri, ignore)] #[test] fn nightlytest() { - if !cfg!(nightly_features) { - panic!("nightly feature isn't set when the toolchain is nightly"); + if !cfg!(nightly) { + panic!("nightly feature isn't set when the toolchain is nightly."); + } + if cfg!(any(beta, stable)) { + panic!("beta, stable, and nightly are mutually exclusive features.") + } +} + +#[rustversion::attr(not(beta), ignore)] +#[test] +fn betatest() { + if !cfg!(beta) { + panic!("beta feature is not set when the toolchain is beta."); + } + if cfg!(any(nightly, stable)) { + panic!("beta, stable, and nightly are mutually exclusive features.") } } -#[rustversion::attr(nightly, ignore)] -//#[cfg_attr(miri, ignore)] +#[rustversion::attr(not(stable), ignore)] #[test] fn stabletest() { - if cfg!(nightly_features) { - panic!("nightly feature is set when the toolchain isn't nightly"); + if !cfg!(stable) { + panic!("stable feature is not set when the toolchain is stable."); + } + if cfg!(any(nightly, beta)) { + panic!("beta, stable, and nightly are mutually exclusive features.") } }