From 1dffeeaac7e784bd6e4159eda2ea6945fcdec5e4 Mon Sep 17 00:00:00 2001 From: Adam Perry Date: Sat, 2 Nov 2019 07:56:55 -0700 Subject: [PATCH 1/2] De-dupe release channel check between library and binary. --- src/bin/main.rs | 8 ++------ src/config/config_type.rs | 2 +- src/config/mod.rs | 2 +- src/lib.rs | 2 +- src/release_channel.rs | 7 ++----- src/test/mod.rs | 8 ++++---- 6 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index ccbfb4052e2..7850f5cd01a 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -16,8 +16,8 @@ use ansi_term::Colour::Red; use getopts::{Matches, Options}; use crate::rustfmt::{ - load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, - FormatReportFormatterBuilder, Input, Session, Verbosity, + load_config, release_channel::is_nightly, CliOptions, Color, Config, Edition, EmitMode, + FileLines, FileName, FormatReportFormatterBuilder, Input, Session, Verbosity, }; fn main() { @@ -189,10 +189,6 @@ fn make_opts() -> Options { opts } -fn is_nightly() -> bool { - option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev") -} - // Returned i32 is an exit code fn execute(opts: &Options) -> Result { let matches = opts.parse(env::args().skip(1))?; diff --git a/src/config/config_type.rs b/src/config/config_type.rs index b8f0691a7b9..3a62b9f0295 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -155,7 +155,7 @@ macro_rules! create_config { self.$i.1 = true; self.$i.2 = val; } else { - if crate::is_nightly_channel!() { + if crate::release_channel::is_nightly() { self.$i.1 = true; self.$i.2 = val; } else { diff --git a/src/config/mod.rs b/src/config/mod.rs index 94f390ceef4..3f4ec1df0c0 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -476,7 +476,7 @@ mod test { #[test] fn test_valid_license_template_path() { - if !crate::is_nightly_channel!() { + if !crate::release_channel::is_nightly() { return; } let toml = r#"license_template_path = "tests/license-template/lt.txt""#; diff --git a/src/lib.rs b/src/lib.rs index dbd9469908e..c4e7ddfbf63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,7 +63,7 @@ pub(crate) mod modules; mod overflow; mod pairs; mod patterns; -mod release_channel; +pub mod release_channel; mod reorder; mod rewrite; pub(crate) mod rustfmt_diff; diff --git a/src/release_channel.rs b/src/release_channel.rs index 948247b3c97..05a7e91ce5c 100644 --- a/src/release_channel.rs +++ b/src/release_channel.rs @@ -8,9 +8,6 @@ /// 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") - }; +pub fn is_nightly() -> bool { + 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 6b7a9365c6f..42477298801 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -11,7 +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::release_channel::is_nightly; use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter}; use crate::source_file; use crate::{FormatReport, FormatReportFormatterBuilder, Input, Session}; @@ -312,7 +312,7 @@ fn idempotence_tests() { init_log(); run_test_with(&TestSetting::default(), || { // these tests require nightly - if !is_nightly_channel!() { + if !is_nightly() { return; } // Get all files in the tests/target directory. @@ -336,7 +336,7 @@ fn idempotence_tests() { fn self_tests() { init_log(); // Issue-3443: these tests require nightly - if !is_nightly_channel!() { + if !is_nightly() { return; } let mut files = get_test_files(Path::new("tests"), false); @@ -491,7 +491,7 @@ fn check_files(files: Vec, opt_config: &Option) -> (Vec Date: Sat, 2 Nov 2019 08:01:54 -0700 Subject: [PATCH 2/2] Allow RUSTFMT_BOOTSTRAP=1 to override nightly feature checks. This was recommended in https://github.com/rust-lang/rust/pull/65939#discussion_r341424553. --- src/release_channel.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/release_channel.rs b/src/release_channel.rs index 05a7e91ce5c..781c0a964b8 100644 --- a/src/release_channel.rs +++ b/src/release_channel.rs @@ -8,6 +8,10 @@ /// 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. +/// +/// Additionally, the RUSTFMT_BOOTSTRAP environment variable can be set to `1` to +/// allow for use of unstable features when used within the compiler's stage0. pub fn is_nightly() -> bool { option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev") + || std::env::var("RUSTFMT_BOOTSTRAP") == Ok("1".to_string()) }