Skip to content

Commit 46cb7d3

Browse files
refactor: improve mapping of legacy 'version' to 'style_edition'
1 parent 0652586 commit 46cb7d3

File tree

8 files changed

+82
-54
lines changed

8 files changed

+82
-54
lines changed

Diff for: src/bin/main.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use getopts::{Matches, Options};
1919

2020
use crate::rustfmt::{
2121
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName,
22-
FormatReportFormatterBuilder, Input, Session, StyleEdition, Verbosity,
22+
FormatReportFormatterBuilder, Input, Session, StyleEdition, Verbosity, Version,
2323
};
2424

2525
const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rustfmt/issues/new?labels=bug";
@@ -746,6 +746,13 @@ impl CliOptions for GetOptsOptions {
746746
.get("style_edition")
747747
.map_or(self.style_edition, |se| StyleEdition::from_str(se).ok())
748748
}
749+
750+
fn version(&self) -> Option<Version> {
751+
self.inline_config
752+
.get("version")
753+
.map(|version| Version::from_str(version).ok())
754+
.flatten()
755+
}
749756
}
750757

751758
fn edition_from_edition_str(edition_str: &str) -> Result<Edition> {
@@ -814,6 +821,17 @@ mod test {
814821
options.inline_config = HashMap::from([("version".to_owned(), "Two".to_owned())]);
815822
let config = get_config(None, Some(options));
816823
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
824+
assert_eq!(config.overflow_delimited_expr(), true);
825+
}
826+
827+
#[nightly_only_test]
828+
#[test]
829+
fn version_config_file_sets_style_edition_override_correctly() {
830+
let options = GetOptsOptions::default();
831+
let config_file = Some(Path::new("tests/config/style-edition/just-version"));
832+
let config = get_config(config_file, Some(options));
833+
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
834+
assert_eq!(config.overflow_delimited_expr(), true);
817835
}
818836

819837
#[nightly_only_test]
@@ -858,6 +876,7 @@ mod test {
858876
]);
859877
let config = get_config(None, Some(options));
860878
assert_eq!(config.style_edition(), StyleEdition::Edition2024);
879+
assert_eq!(config.overflow_delimited_expr(), true);
861880
}
862881

863882
#[nightly_only_test]

Diff for: src/config/config_type.rs

-25
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ macro_rules! create_config {
134134
"fn_args_layout" => self.0.set_fn_args_layout(),
135135
"hide_parse_errors" => self.0.set_hide_parse_errors(),
136136
"version" => self.0.set_version(),
137-
"edition" => self.0.set_edition(),
138137
&_ => (),
139138
}
140139
}
@@ -165,7 +164,6 @@ macro_rules! create_config {
165164
"fn_args_layout" => self.0.set_fn_args_layout(),
166165
"hide_parse_errors" => self.0.set_hide_parse_errors(),
167166
"version" => self.0.set_version(),
168-
"edition" => self.0.set_edition(),
169167
&_ => (),
170168
}
171169
}
@@ -264,7 +262,6 @@ macro_rules! create_config {
264262
self.set_fn_args_layout();
265263
self.set_hide_parse_errors();
266264
self.set_version();
267-
self.set_edition();
268265
self
269266
}
270267

@@ -367,7 +364,6 @@ macro_rules! create_config {
367364
"fn_args_layout" => self.set_fn_args_layout(),
368365
"hide_parse_errors" => self.set_hide_parse_errors(),
369366
"version" => self.set_version(),
370-
"edition" => self.set_edition(),
371367
&_ => (),
372368
}
373369
}
@@ -585,30 +581,9 @@ macro_rules! create_config {
585581
option which takes precedence. \
586582
The value of the `version` option will be ignored."
587583
);
588-
} else if matches!(self.version(), Version::Two) {
589-
self.style_edition.2 = StyleEdition::Edition2024;
590-
} else {
591-
self.style_edition.2 = StyleEdition::Edition2015;
592584
}
593585
}
594586

595-
fn set_edition(&mut self) {
596-
let style_edition_set = self.was_set().style_edition()
597-
|| self.was_set_cli().style_edition();
598-
599-
if style_edition_set || self.was_set().version() {
600-
return;
601-
}
602-
603-
// User has explicitly specified an Edition value without
604-
// explicitly specifying a Style Edition value, so the Style Edition
605-
// must default to whatever value was provided for Edition
606-
// as per: https://rust-lang.github.io/rfcs/3338-style-evolution.html#explanation
607-
self.style_edition.2 = self.edition().into();
608-
}
609-
610-
611-
612587
#[allow(unreachable_pub)]
613588
/// Returns `true` if the config key was explicitly set and is the default value.
614589
pub fn is_default(&self, key: &str) -> bool {

Diff for: src/config/mod.rs

+39-19
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,13 @@ impl PartialConfig {
222222
self,
223223
style_edition_override: Option<StyleEdition>,
224224
edition_override: Option<Edition>,
225+
version_override: Option<Version>,
225226
dir: &Path,
226227
) -> Config {
227228
Config::default_for_possible_style_edition(
228229
style_edition_override.or(self.style_edition),
229230
edition_override.or(self.edition),
231+
version_override.or(self.version),
230232
)
231233
.fill_from_parsed_config(self, dir)
232234
}
@@ -236,16 +238,25 @@ impl Config {
236238
pub fn default_for_possible_style_edition(
237239
style_edition: Option<StyleEdition>,
238240
edition: Option<Edition>,
241+
version: Option<Version>,
239242
) -> Config {
240-
style_edition.map_or_else(
241-
|| {
242-
edition.map_or_else(
243-
|| Config::default(),
244-
|e| Self::default_with_style_edition(e.into()),
245-
)
246-
},
247-
|se| Self::default_with_style_edition(se),
248-
)
243+
// Ensures the configuration defaults associated with Style Editions
244+
// follow the precedence set in
245+
// https://rust-lang.github.io/rfcs/3338-style-evolution.html
246+
// 'version' is a legacy alias for 'style_edition' that we'll support
247+
// for some period of time
248+
// FIXME(calebcartwright) - remove 'version' at some point
249+
match (style_edition, version, edition) {
250+
(Some(se), _, _) => Self::default_with_style_edition(se),
251+
(None, Some(Version::Two), _) => {
252+
Self::default_with_style_edition(StyleEdition::Edition2024)
253+
}
254+
(None, Some(Version::One), _) => {
255+
Self::default_with_style_edition(StyleEdition::Edition2015)
256+
}
257+
(None, None, Some(e)) => Self::default_with_style_edition(e.into()),
258+
(None, None, None) => Config::default(),
259+
}
249260
}
250261

251262
pub(crate) fn version_meets_requirement(&self) -> bool {
@@ -275,6 +286,7 @@ impl Config {
275286
file_path: &Path,
276287
edition: Option<Edition>,
277288
style_edition: Option<StyleEdition>,
289+
version: Option<Version>,
278290
) -> Result<Config, Error> {
279291
let mut file = File::open(&file_path)?;
280292
let mut toml = String::new();
@@ -284,6 +296,7 @@ impl Config {
284296
file_path.parent().unwrap(),
285297
edition,
286298
style_edition,
299+
version,
287300
)
288301
.map_err(|err| Error::new(ErrorKind::InvalidData, err))
289302
}
@@ -301,6 +314,7 @@ impl Config {
301314
dir: &Path,
302315
edition: Option<Edition>,
303316
style_edition: Option<StyleEdition>,
317+
version: Option<Version>,
304318
) -> Result<(Config, Option<PathBuf>), Error> {
305319
/// Try to find a project file in the given directory and its parents.
306320
/// Returns the path of the nearest project file if one exists,
@@ -347,24 +361,25 @@ impl Config {
347361

348362
match resolve_project_file(dir)? {
349363
None => Ok((
350-
Config::default_for_possible_style_edition(style_edition, edition),
364+
Config::default_for_possible_style_edition(style_edition, edition, version),
351365
None,
352366
)),
353-
Some(path) => Config::from_toml_path(&path, edition, style_edition)
367+
Some(path) => Config::from_toml_path(&path, edition, style_edition, version)
354368
.map(|config| (config, Some(path))),
355369
}
356370
}
357371

358372
#[allow(dead_code)]
359373
pub(super) fn from_toml(toml: &str, dir: &Path) -> Result<Config, String> {
360-
Self::from_toml_for_style_edition(toml, dir, None, None)
374+
Self::from_toml_for_style_edition(toml, dir, None, None, None)
361375
}
362376

363377
pub(crate) fn from_toml_for_style_edition(
364378
toml: &str,
365379
dir: &Path,
366380
edition: Option<Edition>,
367381
style_edition: Option<StyleEdition>,
382+
version: Option<Version>,
368383
) -> Result<Config, String> {
369384
let parsed: ::toml::Value = toml
370385
.parse()
@@ -385,7 +400,7 @@ impl Config {
385400
if !err.is_empty() {
386401
eprint!("{err}");
387402
}
388-
Ok(parsed_config.to_parsed_config(style_edition, edition, dir))
403+
Ok(parsed_config.to_parsed_config(style_edition, edition, version, dir))
389404
}
390405
Err(e) => {
391406
err.push_str("Error: Decoding config file failed:\n");
@@ -403,19 +418,24 @@ pub fn load_config<O: CliOptions>(
403418
file_path: Option<&Path>,
404419
options: Option<O>,
405420
) -> Result<(Config, Option<PathBuf>), Error> {
406-
let (over_ride, edition, style_edition) = match options {
407-
Some(ref opts) => (config_path(opts)?, opts.edition(), opts.style_edition()),
408-
None => (None, None, None),
421+
let (over_ride, edition, style_edition, version) = match options {
422+
Some(ref opts) => (
423+
config_path(opts)?,
424+
opts.edition(),
425+
opts.style_edition(),
426+
opts.version(),
427+
),
428+
None => (None, None, None, None),
409429
};
410430

411431
let result = if let Some(over_ride) = over_ride {
412-
Config::from_toml_path(over_ride.as_ref(), edition, style_edition)
432+
Config::from_toml_path(over_ride.as_ref(), edition, style_edition, version)
413433
.map(|p| (p, Some(over_ride.to_owned())))
414434
} else if let Some(file_path) = file_path {
415-
Config::from_resolved_toml_path(file_path, edition, style_edition)
435+
Config::from_resolved_toml_path(file_path, edition, style_edition, version)
416436
} else {
417437
Ok((
418-
Config::default_for_possible_style_edition(style_edition, edition),
438+
Config::default_for_possible_style_edition(style_edition, edition, version),
419439
None,
420440
))
421441
};

Diff for: src/config/options.rs

+1
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ pub trait CliOptions {
421421
fn config_path(&self) -> Option<&Path>;
422422
fn edition(&self) -> Option<Edition>;
423423
fn style_edition(&self) -> Option<StyleEdition>;
424+
fn version(&self) -> Option<Version>;
424425
}
425426

426427
/// The edition of the syntax and semantics of code (RFC 2052).

Diff for: src/git-rustfmt/main.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use getopts::{Matches, Options};
1515
use rustfmt_nightly as rustfmt;
1616
use tracing_subscriber::EnvFilter;
1717

18-
use crate::rustfmt::{load_config, CliOptions, FormatReportFormatterBuilder, Input, Session};
18+
use crate::rustfmt::{
19+
load_config, CliOptions, FormatReportFormatterBuilder, Input, Session, Version,
20+
};
1921

2022
fn prune_files(files: Vec<&str>) -> Vec<&str> {
2123
let prefixes: Vec<_> = files
@@ -95,6 +97,9 @@ impl CliOptions for NullOptions {
9597
fn style_edition(&self) -> Option<rustfmt_nightly::StyleEdition> {
9698
unreachable!();
9799
}
100+
fn version(&self) -> Option<Version> {
101+
unreachable!();
102+
}
98103
}
99104

100105
fn uncommitted_files() -> Vec<String> {

Diff for: src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use crate::utils::indent_next_line;
4848

4949
pub use crate::config::{
5050
load_config, CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle,
51-
Range, StyleEdition, Verbosity,
51+
Range, StyleEdition, Verbosity, Version,
5252
};
5353

5454
pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder};

Diff for: src/test/mod.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChu
1515
use crate::source_file;
1616
use crate::{
1717
is_nightly_channel, Edition, FormatReport, FormatReportFormatterBuilder, Input, Session,
18-
StyleEdition,
18+
StyleEdition, Version,
1919
};
2020

2121
use rustfmt_config_proc_macro::nightly_only_test;
@@ -713,7 +713,7 @@ fn print_mismatches<T: Fn(u32) -> String>(
713713

714714
fn read_config(filename: &Path) -> Config {
715715
let sig_comments = read_significant_comments(filename);
716-
let (edition, style_edition) = get_editions_from_comments(&sig_comments);
716+
let (edition, style_edition, version) = get_editions_from_comments(&sig_comments);
717717
// Look for a config file. If there is a 'config' property in the significant comments, use
718718
// that. Otherwise, if there are no significant comments at all, look for a config file with
719719
// the same name as the test file.
@@ -722,12 +722,14 @@ fn read_config(filename: &Path) -> Config {
722722
sig_comments.get("config").map(Path::new),
723723
edition,
724724
style_edition,
725+
version,
725726
)
726727
} else {
727728
get_config(
728729
filename.with_extension("toml").file_name().map(Path::new),
729730
edition,
730731
style_edition,
732+
version,
731733
)
732734
};
733735

@@ -761,14 +763,17 @@ enum IdempotentCheckError {
761763

762764
fn get_editions_from_comments(
763765
comments: &HashMap<String, String>,
764-
) -> (Option<Edition>, Option<StyleEdition>) {
766+
) -> (Option<Edition>, Option<StyleEdition>, Option<Version>) {
765767
(
766768
comments
767769
.get("edition")
768770
.map(|e| Edition::from_str(e).expect(&format!("invalid edition value: '{}'", e))),
769771
comments.get("style_edition").map(|se| {
770772
StyleEdition::from_str(se).expect(&format!("invalid style_edition value: '{}'", se))
771773
}),
774+
comments
775+
.get("version")
776+
.map(|v| Version::from_str(v).expect(&format!("invalid version value: '{}'", v))),
772777
)
773778
}
774779

@@ -778,8 +783,8 @@ fn idempotent_check(
778783
) -> Result<FormatReport, IdempotentCheckError> {
779784
let sig_comments = read_significant_comments(filename);
780785
let config = if let Some(ref config_file_path) = opt_config {
781-
let (edition, style_edition) = get_editions_from_comments(&sig_comments);
782-
Config::from_toml_path(config_file_path, edition, style_edition)
786+
let (edition, style_edition, version) = get_editions_from_comments(&sig_comments);
787+
Config::from_toml_path(config_file_path, edition, style_edition, version)
783788
.expect("`rustfmt.toml` not found")
784789
} else {
785790
read_config(filename)
@@ -808,14 +813,15 @@ fn get_config(
808813
config_file: Option<&Path>,
809814
edition: Option<Edition>,
810815
style_edition: Option<StyleEdition>,
816+
version: Option<Version>,
811817
) -> Config {
812818
let config_file_name = match config_file {
813-
None => return Config::default_for_possible_style_edition(style_edition, edition),
819+
None => return Config::default_for_possible_style_edition(style_edition, edition, version),
814820
Some(file_name) => {
815821
let mut full_path = PathBuf::from("tests/config/");
816822
full_path.push(file_name);
817823
if !full_path.exists() {
818-
return Config::default_for_possible_style_edition(style_edition, edition);
824+
return Config::default_for_possible_style_edition(style_edition, edition, version);
819825
};
820826
full_path
821827
}
@@ -832,6 +838,7 @@ fn get_config(
832838
Path::new("tests/config/"),
833839
edition,
834840
style_edition,
841+
version,
835842
)
836843
.expect("invalid TOML")
837844
}

Diff for: tests/config/style-edition/just-version/rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = "Two"

0 commit comments

Comments
 (0)