Add build script to check for nightly (#112)

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.
This commit is contained in:
nori li 2023-11-07 13:35:25 -08:00 committed by GitHub
parent 0b24ae558f
commit 7fefca5981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 12 deletions

View File

@ -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,

View File

@ -1,4 +1,5 @@
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::process::{Command, ExitStatus};
@ -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");
}
}
@ -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 };
Some(version)
}

View File

@ -329,11 +329,14 @@
//! [`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(
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,

View File

@ -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");
}
}