Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: conditionally allow unstable opts on stable/beta #4228

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ jobs:
matrix:
integration: [
bitflags,
chalk,
crater,
error-chain,
glob,
log,
mdbook,
packed_simd,
Expand All @@ -37,6 +34,15 @@ jobs:
# Instead, leverage `continue-on-error`
# https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idstepscontinue-on-error
#
# Failing due to breaking changes in rustfmt 2.0 where empty
# match blocks have trailing commas removed
# https://github.com/rust-lang/rustfmt/pull/4226
- integration: chalk
allow-failure: true
- integration: crater
allow-failure: true
- integration: glob
allow-failure: true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three integration test jobs are failing since rustfmt 2.0 is removing existing trailing commas from empty match blocks after #4226, so adding these to the allowed failure list til after the 2.0 release and the formatting changes are applied upstream:

 Diff in /tmp/tmp.hWkMNIst66/src/lib.rs:734:
 
                     // Empty match
                     match self.matches_from(follows_separator, file.clone(), i + ti + 1, options) {
-                        SubPatternDoesntMatch => {}, // keep trying
+                        SubPatternDoesntMatch => {} // keep trying
                         m => return m,
                     };

# Using old rustfmt configuration option
- integration: rand
allow-failure: true
Expand Down
9 changes: 6 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ matrix:
- env: CFG_RELEASE_CHANNEL=beta
- os: osx
- env: INTEGRATION=bitflags
- env: INTEGRATION=chalk
- env: INTEGRATION=crater
- env: INTEGRATION=error-chain
- env: INTEGRATION=glob
- env: INTEGRATION=log
- env: INTEGRATION=mdbook
- env: INTEGRATION=packed_simd
Expand All @@ -30,6 +27,12 @@ matrix:
- env: INTEGRATION=tempdir
- env: INTEGRATION=futures-rs
allow_failures:
# Failing due to breaking changes in rustfmt 2.0 where empty
# match blocks have trailing commas removed
# https://github.com/rust-lang/rustfmt/pull/4226
- env: INTEGRATION=chalk
- env: INTEGRATION=crater
- env: INTEGRATION=glob
# Using old configuration option
- env: INTEGRATION=rand
# Doesn't build - keep this in allow_failures as it's fragile to breaking changes of rustc.
Expand Down
11 changes: 9 additions & 2 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2395,11 +2395,18 @@ fn lorem<Ipsum: Dolor+Sit=Amet>() {

## `unstable_features`

Enable unstable features on the unstable channel.
Enable unstable features on stable and beta channels (unstable features are available by default on nightly).

- **Default value**: `false`
- **Possible values**: `true`, `false`
- **Stable**: No (tracking issue: [#3387](https://github.com/rust-lang/rustfmt/issues/3387))
- **Stable**: Yes

**Note** - if you are setting unstable configuration options via the CLI instead of the configuration file, then you should include a `unstable_features=true` item before any of the unstable items.

For example:
```bash
rustfmt src/lib.rs --config unstable_features=true merge_imports=true
```

## `use_field_init_shorthand`

Expand Down
37 changes: 32 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ create_config! {
// Control options (changes the operation of rustfmt, rather than the formatting)
required_version: String, env!("CARGO_PKG_VERSION").to_owned(), false,
"Require a specific version of rustfmt";
unstable_features: bool, false, false,
"Enables unstable features. Only available on nightly channel";
unstable_features: bool, false, true,
"Enables unstable features on stable and beta channels (unstable features are enabled \
by default on nightly channel)";
hide_parse_errors: bool, false, false, "Hide errors from the parser";
error_on_line_overflow: bool, false, false, "Error if unable to get all lines within max_width";
error_on_unformatted: bool, false, false,
Expand Down Expand Up @@ -437,6 +438,10 @@ mod test {
single_line_if_else_max_width: usize, 50, true, "Maximum line length for single \
line if-else expressions. A value of zero means always break if-else expressions.";

unstable_features: bool, false, true,
"Enables unstable features on stable and beta channels (unstable features are enabled \
by default on nightly channel)";

// Options that are used by the tests
stable_option: bool, false, true, "A stable option";
unstable_option: bool, false, false, "An unstable option";
Expand Down Expand Up @@ -693,13 +698,31 @@ ignore = []
}

#[test]
fn test_from_toml_not_nightly() {
fn test_from_toml_not_nightly_unstable_set() {
if is_nightly_channel!() {
// This test requires non-nightly
return;
}
let config = Config::from_toml("unstable_features = true", Path::new("")).unwrap();
assert_eq!(config.was_set().unstable_features(), false);
let toml = r#"
unstable_features = true
merge_imports = true
"#;
let config = Config::from_toml(toml, Path::new("")).unwrap();
assert_eq!(config.was_set().unstable_features(), true);
assert_eq!(config.was_set().merge_imports(), true);
assert_eq!(config.unstable_features(), true);
assert_eq!(config.merge_imports(), true);
}

#[test]
fn test_from_toml_not_nightly_unstable_not_set() {
if is_nightly_channel!() {
// This test requires non-nightly
return;
}
let config = Config::from_toml("merge_imports = true", Path::new("")).unwrap();
assert_eq!(config.was_set().merge_imports(), false);
assert_eq!(config.merge_imports(), false);
}

#[test]
Expand Down Expand Up @@ -744,8 +767,12 @@ ignore = []
}
let mut config = Config::default();
assert_eq!(config.unstable_features(), false);
config.override_value("merge_imports", "true");
assert_eq!(config.merge_imports(), false);
config.override_value("unstable_features", "true");
assert_eq!(config.unstable_features(), true);
config.override_value("merge_imports", "true");
assert_eq!(config.merge_imports(), true);
}

#[test]
Expand Down
21 changes: 16 additions & 5 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,14 @@ macro_rules! create_config {
if self.$i.3 {
update_config!(self, $i = val, dir);
} else {
if is_nightly_channel!() {
if parsed.unstable_features == Some(true) || is_nightly_channel!() {
update_config!(self, $i = val, dir);
} else {
eprintln!("Warning: can't set `{} = {:?}`, unstable features are only \
available in nightly channel.", stringify!($i), val);
eprintln!(
"Warning: can't set `{} = {:?}`, unstable features can only \
be used on stable or beta when `unstable_features` is also enabled.",
stringify!($i), val
);
}
}
}
Expand Down Expand Up @@ -237,12 +240,20 @@ macro_rules! create_config {
match key {
$(
stringify!($i) => {
self.$i.1 = true;
self.$i.2 = val.parse::<$Ty>()
if self.$i.3 || self.unstable_features() || is_nightly_channel!() {
self.$i.1 = true;
self.$i.2 = val.parse::<$Ty>()
.expect(&format!("Failed to parse override for {} (\"{}\") as a {}",
stringify!($i),
val,
stringify!($Ty)));
} else {
return eprintln!(
"Warning: can't set `{} = {:?}`, unstable features can only \
be used on stable or beta when `unstable_features` is also enabled.",
stringify!($i), val
);
}
}
)+
_ => panic!("Unknown config key in override: {}", key)
Expand Down
3 changes: 3 additions & 0 deletions src/test/configuration_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ impl ConfigCodeBlock {

fn get_block_config(&self) -> Config {
let mut config = Config::default();
if !crate::is_nightly_channel!() {
config.override_value("unstable_features", "true");
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Figured this would be better than just disabling the cfg snippet tests outright on beta

if self.config_name.is_some() && self.config_value.is_some() {
config.override_value(
self.config_name.as_ref().unwrap(),
Expand Down
4 changes: 4 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,10 @@ fn read_config(filename: &Path) -> (Config, OperationSetting, EmitterConfig) {
get_config(filename.with_extension("toml").file_name().map(Path::new))
};

if !config.was_set().unstable_features() && !is_nightly_channel!() {
config.override_value("unstable_features", "true");
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@topecongiro - know you've already approved the core changes here, but before I merge I want to make sure you are okay with this one check added here in 788eb5a to fix the tests on beta vs adding an inline // rustfmt-unstable-features: true comment into each of the hundreds of test files under tests/{source,target} that would need them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that sounds way better to me.


let mut operation_setting = OperationSetting::default();
let mut emitter_config = EmitterConfig::default();
for (key, val) in &sig_comments {
Expand Down
1 change: 1 addition & 0 deletions tests/config/small_tabs.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
unstable_features = true
max_width = 100
comment_width = 80
tab_spaces = 2
Expand Down