Skip to content

Commit 53ef93c

Browse files
feat: implement Style Edition support
Adds the 'style_edition' configuration option along with documentation, 'version' option deprecation, and mapping of 'edition' and 'version' values for backwards compatibility and adherence to the style evolution RFC
1 parent ea02de2 commit 53ef93c

File tree

5 files changed

+97
-8
lines changed

5 files changed

+97
-8
lines changed

Diff for: Configurations.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ Note that this option may be soft-deprecated in the future once the [ignore](#ig
534534
Specifies which edition is used by the parser.
535535

536536
- **Default value**: `"2015"`
537-
- **Possible values**: `"2015"`, `"2018"`, `"2021"`
537+
- **Possible values**: `"2015"`, `"2018"`, `"2021"`, `"2024"`
538538
- **Stable**: Yes
539539

540540
Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if executed
@@ -2692,6 +2692,17 @@ By default this option is set as a percentage of [`max_width`](#max_width) provi
26922692

26932693
See also [`max_width`](#max_width) and [`use_small_heuristics`](#use_small_heuristics)
26942694

2695+
## `style_edition`
2696+
2697+
Controls the edition of the [Rust Style Guide] to use for formatting ([RFC 3338])
2698+
2699+
- **Default value**: `"2015"`
2700+
- **Possible values**: `"2015"`, `"2018"`, `"2021"`, `"2024"` (unstable variant)
2701+
- **Stable**: Yes
2702+
2703+
[Rust Style Guide]: https://doc.rust-lang.org/nightly/style-guide/
2704+
[RFC 3338]: https://rust-lang.github.io/rfcs/3338-style-evolution.html
2705+
26952706
## `tab_spaces`
26962707

26972708
Number of spaces per tab
@@ -3051,9 +3062,7 @@ fn main() {
30513062

30523063
## `version`
30533064

3054-
Which version of the formatting rules to use. `Version::One` is backwards-compatible
3055-
with Rustfmt 1.0. Other versions are only backwards compatible within a major
3056-
version number.
3065+
This option is deprecated and has been replaced by [`style_edition`](#style_edition)
30573066

30583067
- **Default value**: `One`
30593068
- **Possible values**: `One`, `Two`

Diff for: rustfmt.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
error_on_line_overflow = true
22
error_on_unformatted = true
3-
version = "Two"
3+
style_edition = "2024"

Diff for: src/config/config_type.rs

+44
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ macro_rules! create_config {
130130
"merge_imports" => self.0.set_merge_imports(),
131131
"fn_args_layout" => self.0.set_fn_args_layout(),
132132
"hide_parse_errors" => self.0.set_hide_parse_errors(),
133+
"version" => self.0.set_version(),
134+
"edition" => self.0.set_edition(),
133135
&_ => (),
134136
}
135137
}
@@ -202,6 +204,8 @@ macro_rules! create_config {
202204
self.set_merge_imports();
203205
self.set_fn_args_layout();
204206
self.set_hide_parse_errors();
207+
self.set_version();
208+
self.set_edition();
205209
self
206210
}
207211

@@ -303,6 +307,8 @@ macro_rules! create_config {
303307
"merge_imports" => self.set_merge_imports(),
304308
"fn_args_layout" => self.set_fn_args_layout(),
305309
"hide_parse_errors" => self.set_hide_parse_errors(),
310+
"version" => self.set_version(),
311+
"edition" => self.set_edition(),
306312
&_ => (),
307313
}
308314
}
@@ -503,6 +509,44 @@ macro_rules! create_config {
503509
}
504510
}
505511

512+
fn set_version(&mut self) {
513+
if !self.was_set().version() {
514+
return;
515+
}
516+
517+
eprintln!(
518+
"Warning: the `version` option is deprecated. \
519+
Use `style_edition` instead."
520+
);
521+
522+
if self.was_set().style_edition() {
523+
eprintln!(
524+
"Warning: the deprecated `version` option was \
525+
used in conjunction with the `edition` or \
526+
`style_edition` options which take precedence. \
527+
The value of the `version` option will be ignored."
528+
);
529+
} else if matches!(self.version(), Version::Two) {
530+
self.style_edition.2 = StyleEdition::Edition2024;
531+
} else {
532+
self.style_edition.2 = StyleEdition::Edition2015;
533+
}
534+
}
535+
536+
fn set_edition(&mut self) {
537+
if self.was_set().style_edition() || self.was_set().version() {
538+
return;
539+
}
540+
541+
// User has explicitly specified an Edition value without
542+
// explicitly specifying a Style Edition value, so the Style Edition
543+
// must default to whatever value was provided for Edition
544+
// as per: https://rust-lang.github.io/rfcs/3338-style-evolution.html#explanation
545+
self.style_edition.2 = self.edition().into();
546+
}
547+
548+
549+
506550
#[allow(unreachable_pub)]
507551
/// Returns `true` if the config key was explicitly set and is the default value.
508552
pub fn is_default(&self, key: &str) -> bool {

Diff for: src/config/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ create_config! {
149149
blank_lines_lower_bound: BlankLinesLowerBound, false,
150150
"Minimum number of blank lines which must be put between items";
151151
edition: EditionConfig, true, "The edition of the parser (RFC 2052)";
152+
style_edition: StyleEditionConfig, false, "The edition of the Style Guide (RFC 3338)";
152153
version: VersionConfig, false, "Version of formatting rules";
153154
inline_attribute_width: InlineAttributeWidth, false,
154155
"Write an item and its attribute on the same line \
@@ -502,6 +503,9 @@ mod test {
502503
stable_option: StableOption, true, "A stable option";
503504
unstable_option: UnstableOption, false, "An unstable option";
504505
partially_unstable_option: PartiallyUnstable, true, "A partially unstable option";
506+
edition: EditionConfig, true, "blah";
507+
style_edition: StyleEditionConfig, true, "blah";
508+
version: VersionConfig, false, "blah blah"
505509
}
506510

507511
#[cfg(test)]
@@ -685,6 +689,7 @@ match_block_trailing_comma = false
685689
blank_lines_upper_bound = 1
686690
blank_lines_lower_bound = 0
687691
edition = "2015"
692+
style_edition = "2015"
688693
version = "One"
689694
inline_attribute_width = 0
690695
format_generated_files = true

Diff for: src/config/options.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,17 @@ impl From<Edition> for rustc_span::edition::Edition {
456456
}
457457
}
458458

459+
impl From<Edition> for StyleEdition {
460+
fn from(edition: Edition) -> Self {
461+
match edition {
462+
Edition::Edition2015 => StyleEdition::Edition2015,
463+
Edition::Edition2018 => StyleEdition::Edition2018,
464+
Edition::Edition2021 => StyleEdition::Edition2021,
465+
Edition::Edition2024 => StyleEdition::Edition2024,
466+
}
467+
}
468+
}
469+
459470
impl PartialOrd for Edition {
460471
fn partial_cmp(&self, other: &Edition) -> Option<std::cmp::Ordering> {
461472
rustc_span::edition::Edition::partial_cmp(&(*self).into(), &(*other).into())
@@ -473,10 +484,11 @@ pub enum MatchArmLeadingPipe {
473484
Preserve,
474485
}
475486

476-
/// Defines the default values for each config according to [the style guide].
477-
/// rustfmt output may differ between style editions.
487+
/// Defines the default values for each config according to the edition of the
488+
/// [Style Guide] as per [RFC 3338]. Rustfmt output may differ between Style editions.
478489
///
479-
/// [the style guide]: https://doc.rust-lang.org/nightly/style-guide/
490+
/// [Style Guide]: https://doc.rust-lang.org/nightly/style-guide/
491+
/// [RFC 3338]: https://rust-lang.github.io/rfcs/3338-style-evolution.html
480492
#[config_type]
481493
pub enum StyleEdition {
482494
#[value = "2015"]
@@ -493,10 +505,28 @@ pub enum StyleEdition {
493505
Edition2021,
494506
#[value = "2024"]
495507
#[doc_hint = "2024"]
508+
#[unstable_variant]
496509
/// [Edition 2024]().
497510
Edition2024,
498511
}
499512

513+
impl From<StyleEdition> for rustc_span::edition::Edition {
514+
fn from(edition: StyleEdition) -> Self {
515+
match edition {
516+
StyleEdition::Edition2015 => Self::Edition2015,
517+
StyleEdition::Edition2018 => Self::Edition2018,
518+
StyleEdition::Edition2021 => Self::Edition2021,
519+
StyleEdition::Edition2024 => Self::Edition2024,
520+
}
521+
}
522+
}
523+
524+
impl PartialOrd for StyleEdition {
525+
fn partial_cmp(&self, other: &StyleEdition) -> Option<std::cmp::Ordering> {
526+
rustc_span::edition::Edition::partial_cmp(&(*self).into(), &(*other).into())
527+
}
528+
}
529+
500530
/// Defines unit structs to implement `StyleEditionDefault` for.
501531
#[macro_export]
502532
macro_rules! config_option_with_style_edition_default {
@@ -608,6 +638,7 @@ config_option_with_style_edition_default!(
608638
BlankLinesUpperBound, usize, _ => 1;
609639
BlankLinesLowerBound, usize, _ => 0;
610640
EditionConfig, Edition, _ => Edition::Edition2015;
641+
StyleEditionConfig, StyleEdition, _ => StyleEdition::Edition2015;
611642
VersionConfig, Version, Edition2024 => Version::Two, _ => Version::One;
612643
InlineAttributeWidth, usize, _ => 0;
613644
FormatGeneratedFiles, bool, _ => true;

0 commit comments

Comments
 (0)