diff --git a/src/config/config_type.rs b/src/config/config_type.rs index d14969418ad17..326d81b2113b4 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -50,22 +50,6 @@ impl ConfigType for IgnoreList { } } -/// Checks if we're in a nightly build. -/// -/// The environment variable `CFG_RELEASE_CHANNEL` is set during the rustc bootstrap -/// to "stable", "beta", or "nightly" depending on what toolchain is being built. -/// If we are being built as part of the stable or beta toolchains, we want -/// to disable unstable configuration options. -/// -/// If we're being built by cargo (e.g., `cargo +nightly install rustfmt-nightly`), -/// `CFG_RELEASE_CHANNEL` is not set. As we only support being built against the -/// nightly compiler when installed from crates.io, default to nightly mode. -macro_rules! is_nightly_channel { - () => { - option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev") - }; -} - macro_rules! create_config { ($($i:ident: $ty:ty, $def:expr, $stb:expr, $( $dstring:expr ),+ );+ $(;)*) => ( #[cfg(test)] @@ -159,7 +143,7 @@ macro_rules! create_config { self.$i.1 = true; self.$i.2 = val; } else { - if is_nightly_channel!() { + if crate::is_nightly_channel!() { self.$i.1 = true; self.$i.2 = val; } else { diff --git a/src/lib.rs b/src/lib.rs index efd88357088ba..88605079b7eec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,9 @@ pub use crate::rustfmt_diff::{ModifiedChunk, ModifiedLines}; #[macro_use] mod utils; +#[macro_use] +mod release_channel; + mod attr; mod chains; pub(crate) mod checkstyle; diff --git a/src/release_channel.rs b/src/release_channel.rs new file mode 100644 index 0000000000000..948247b3c9700 --- /dev/null +++ b/src/release_channel.rs @@ -0,0 +1,16 @@ +/// Checks if we're in a nightly build. +/// +/// The environment variable `CFG_RELEASE_CHANNEL` is set during the rustc bootstrap +/// to "stable", "beta", or "nightly" depending on what toolchain is being built. +/// If we are being built as part of the stable or beta toolchains, we want +/// to disable unstable configuration options. +/// +/// If we're being built by cargo (e.g., `cargo +nightly install rustfmt-nightly`), +/// `CFG_RELEASE_CHANNEL` is not set. As we only support being built against the +/// nightly compiler when installed from crates.io, default to nightly mode. +#[macro_export] +macro_rules! is_nightly_channel { + () => { + option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev") + }; +} diff --git a/src/test/mod.rs b/src/test/mod.rs index 23f6a69b8d187..68fd90a01cfd3 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -11,6 +11,7 @@ use std::thread; use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle, ReportTactic}; use crate::formatting::{ReportedErrors, SourceFile}; +use crate::is_nightly_channel; use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter}; use crate::source_file; use crate::{FormatReport, FormatReportFormatterBuilder, Input, Session}; @@ -259,9 +260,9 @@ fn assert_output(source: &Path, expected_filename: &Path) { #[test] fn idempotence_tests() { run_test_with(&TestSetting::default(), || { - match option_env!("CFG_RELEASE_CHANNEL") { - None | Some("nightly") => {} - _ => return, // these tests require nightly + // these tests require nightly + if !is_nightly_channel!() { + return; } // Get all files in the tests/target directory. let files = get_test_files(Path::new("tests/target"), true); @@ -277,9 +278,9 @@ fn idempotence_tests() { // no warnings are emitted. #[test] fn self_tests() { - match option_env!("CFG_RELEASE_CHANNEL") { - None | Some("nightly") => {} - _ => return, // Issue-3443: these tests require nightly + // Issue-3443: these tests require nightly + if !is_nightly_channel!() { + return; } let mut files = get_test_files(Path::new("tests"), false); let bin_directories = vec!["cargo-fmt", "git-rustfmt", "bin", "format-diff"]; @@ -426,6 +427,16 @@ fn check_files(files: Vec, opt_config: &Option) -> (Vec Config { }; for (key, val) in &sig_comments { - if key != "target" && key != "config" { + if key != "target" && key != "config" && key != "unstable" { config.override_value(key, val); if config.is_default(key) { warn!("Default value {} used explicitly for {}", val, key);