diff --git a/Configurations.md b/Configurations.md index 3b993f3fd48..e9035c6ff24 100644 --- a/Configurations.md +++ b/Configurations.md @@ -497,8 +497,8 @@ Don't reformat anything Specifies which edition is used by the parser. -- **Default value**: `2015` -- **Possible values**: `2015`, `2018` +- **Default value**: `"2015"` +- **Possible values**: `"2015"`, `"2018"`, `"2021"` - **Stable**: Yes Rustfmt is able to pick up the edition used by reading the `Cargo.toml` file if executed diff --git a/src/config/options.rs b/src/config/options.rs index ae120a475f4..e7a6c414354 100644 --- a/src/config/options.rs +++ b/src/config/options.rs @@ -388,6 +388,10 @@ pub enum Edition { #[doc_hint = "2018"] /// Edition 2018. Edition2018, + #[value = "2021"] + #[doc_hint = "2021"] + /// Edition 2021. + Edition2021, } impl Default for Edition { @@ -396,15 +400,22 @@ impl Default for Edition { } } -impl Edition { - pub(crate) fn to_libsyntax_pos_edition(self) -> rustc_span::edition::Edition { - match self { - Edition::Edition2015 => rustc_span::edition::Edition::Edition2015, - Edition::Edition2018 => rustc_span::edition::Edition::Edition2018, +impl From for rustc_span::edition::Edition { + fn from(edition: Edition) -> Self { + match edition { + Edition::Edition2015 => Self::Edition2015, + Edition::Edition2018 => Self::Edition2018, + Edition::Edition2021 => Self::Edition2021, } } } +impl PartialOrd for Edition { + fn partial_cmp(&self, other: &Edition) -> Option { + rustc_span::edition::Edition::partial_cmp(&(*self).into(), &(*other).into()) + } +} + /// Controls how rustfmt should handle leading pipes on match arms. #[config_type] pub enum MatchArmLeadingPipe { diff --git a/src/formatting.rs b/src/formatting.rs index 26ae494227d..289e58cf693 100644 --- a/src/formatting.rs +++ b/src/formatting.rs @@ -34,7 +34,7 @@ impl<'b, T: Write + 'b> Session<'b, T> { return Err(ErrorKind::VersionMismatch); } - rustc_span::with_session_globals(self.config.edition().to_libsyntax_pos_edition(), || { + rustc_span::with_session_globals(self.config.edition().into(), || { if self.config.disable_all_formatting() { // When the input is from stdin, echo back the input. if let Input::Text(ref buf) = input { diff --git a/src/imports.rs b/src/imports.rs index 0b3d844ea14..d7082d50f88 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -331,7 +331,7 @@ impl UseTree { }; let leading_modsep = - context.config.edition() == Edition::Edition2018 && a.prefix.is_global(); + context.config.edition() >= Edition::Edition2018 && a.prefix.is_global(); let mut modsep = leading_modsep;