Skip to content

Commit

Permalink
Add build script to check for nightly
Browse files Browse the repository at this point in the history
The rustdoc_missing_doc_code_examples lint has been sending
warnings and causing CI issues due to being an unstable feature.

This change introduces a small build script that detects whether
the current toolchain is nightly and, if so, sets the config option
"nightly_features". This config option then sets the feature gate
for missing_doc_code_examples and turns on 'warn'.

It expands the existing code for parsing minor version
to parse the rest of the rust --version.

This change also introduces a toolchain test that uses
rust_version to double-check that the config option was enabled
IFF the nightly toolchain is being used.
  • Loading branch information
thenorili committed Nov 6, 2023
1 parent 0b24ae5 commit b01a5af
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
6 changes: 5 additions & 1 deletion color-spantrace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@
//! [`tracing_error::SpanTrace`]: https://docs.rs/tracing-error/*/tracing_error/struct.SpanTrace.html
//! [`color-backtrace`]: https://github.com/athre0z/color-backtrace
#![doc(html_root_url = "https://docs.rs/color-spantrace/0.2.0")]
#![cfg_attr(
nightly_features,
feature(rustdoc_missing_doc_code_examples),
warn(rustdoc::missing_doc_code_examples)
)]
#![warn(
missing_debug_implementations,
missing_docs,
rustdoc::missing_doc_code_examples,
rust_2018_idioms,
unreachable_pub,
bad_style,
Expand Down
33 changes: 24 additions & 9 deletions eyre/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::process::{Command, ExitStatus};
Expand Down Expand Up @@ -55,16 +56,20 @@ fn main() {
_ => {}
}

let rustc = match rustc_minor_version() {
Some(rustc) => rustc,
let version = match rustc_version_info() {
Some(version) => version,
None => return,
};

if rustc < 52 {
if version.is_nightly {
println!("cargo:rustc-cfg=nightly_features");
}

if version.minor < 52 {
println!("cargo:rustc-cfg=eyre_no_fmt_arguments_as_str");
}

if rustc < 58 {
if version.minor < 58 {
println!("cargo:rustc-cfg=eyre_no_fmt_args_capture");
}
}
Expand All @@ -86,13 +91,23 @@ fn compile_probe(probe: &str) -> Option<ExitStatus> {
.ok()
}

fn rustc_minor_version() -> Option<u32> {
let rustc = env::var_os("RUSTC")?;
struct VersionInfo {
minor: u32,
is_nightly: bool,
}

fn rustc_version_info() -> Option<VersionInfo> {
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 1") {
let mut pieces = version.split(['.', ' ', '-']);
if pieces.next() != Some("rustc") {
return None;
}
pieces.next()?.parse().ok()
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 };
return Some(version);

Check failure on line 112 in eyre/build.rs

View workflow job for this annotation

GitHub Actions / Clippy (stable)

unneeded `return` statement
}
10 changes: 8 additions & 2 deletions eyre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,17 @@
//! [`color-spantrace`]: https://github.com/eyre-rs/color-spantrace
//! [`color-backtrace`]: https://github.com/athre0z/color-backtrace
#![doc(html_root_url = "https://docs.rs/eyre/0.6.8")]
// #![cfg_attr(feature = "nightly",
// feature(rustdoc_missing_doc_code_examples),
// warn(rustdoc::missing_doc_code_examples))]
#![cfg_attr(
nightly_features,
feature(rustdoc_missing_doc_code_examples),
warn(rustdoc::missing_doc_code_examples)
)]
#![warn(
missing_debug_implementations,
missing_docs,
// FIXME: this lint is currently nightly only
rustdoc::missing_doc_code_examples,
unsafe_op_in_unsafe_fn,
rust_2018_idioms,
unreachable_pub,
Expand Down
17 changes: 17 additions & 0 deletions eyre/tests/test_toolchain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[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");
}
}

#[rustversion::attr(nightly, ignore)]
//#[cfg_attr(miri, ignore)]
#[test]
fn stabletest() {
if cfg!(nightly_features) {
panic!("nightly feature is set when the toolchain isn't nightly");
}
}

0 comments on commit b01a5af

Please sign in to comment.