Skip to content

Commit c2c9be5

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 d8ae6a9 commit c2c9be5

File tree

5 files changed

+97
-8
lines changed

5 files changed

+97
-8
lines changed

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`

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"

src/config/config_type.rs

+44
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ macro_rules! create_config {
133133
"merge_imports" => self.0.set_merge_imports(),
134134
"fn_args_layout" => self.0.set_fn_args_layout(),
135135
"hide_parse_errors" => self.0.set_hide_parse_errors(),
136+
"version" => self.0.set_version(),
137+
"edition" => self.0.set_edition(),
136138
&_ => (),
137139
}
138140
}
@@ -259,6 +261,8 @@ macro_rules! create_config {
259261
self.set_merge_imports();
260262
self.set_fn_args_layout();
261263
self.set_hide_parse_errors();
264+
self.set_version();
265+
self.set_edition();
262266
self
263267
}
264268

@@ -360,6 +364,8 @@ macro_rules! create_config {
360364
"merge_imports" => self.set_merge_imports(),
361365
"fn_args_layout" => self.set_fn_args_layout(),
362366
"hide_parse_errors" => self.set_hide_parse_errors(),
367+
"version" => self.set_version(),
368+
"edition" => self.set_edition(),
363369
&_ => (),
364370
}
365371
}
@@ -560,6 +566,44 @@ macro_rules! create_config {
560566
}
561567
}
562568

569+
fn set_version(&mut self) {
570+
if !self.was_set().version() {
571+
return;
572+
}
573+
574+
eprintln!(
575+
"Warning: the `version` option is deprecated. \
576+
Use `style_edition` instead."
577+
);
578+
579+
if self.was_set().style_edition() {
580+
eprintln!(
581+
"Warning: the deprecated `version` option was \
582+
used in conjunction with the `edition` or \
583+
`style_edition` options which take precedence. \
584+
The value of the `version` option will be ignored."
585+
);
586+
} else if matches!(self.version(), Version::Two) {
587+
self.style_edition.2 = StyleEdition::Edition2024;
588+
} else {
589+
self.style_edition.2 = StyleEdition::Edition2015;
590+
}
591+
}
592+
593+
fn set_edition(&mut self) {
594+
if self.was_set().style_edition() || self.was_set().version() {
595+
return;
596+
}
597+
598+
// User has explicitly specified an Edition value without
599+
// explicitly specifying a Style Edition value, so the Style Edition
600+
// must default to whatever value was provided for Edition
601+
// as per: https://rust-lang.github.io/rfcs/3338-style-evolution.html#explanation
602+
self.style_edition.2 = self.edition().into();
603+
}
604+
605+
606+
563607
#[allow(unreachable_pub)]
564608
/// Returns `true` if the config key was explicitly set and is the default value.
565609
pub fn is_default(&self, key: &str) -> bool {

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

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)