From d438815a3c40187edd54add405b05f07edc0c9be Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Fri, 1 Jan 2021 10:46:20 -0600 Subject: [PATCH 01/10] ci: fix linking issue on windows gnu jobs --- .github/workflows/windows.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index 0b78d63aaf7..156848c1650 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -14,7 +14,7 @@ jobs: # There's a limit of 60 concurrent jobs across all repos in the rust-lang organization. # In order to prevent overusing too much of that 60 limit, we throttle the # number of rustfmt jobs that will run concurrently. - max-parallel: 1 + max-parallel: 2 fail-fast: false matrix: target: [ @@ -50,6 +50,17 @@ jobs: profile: minimal default: true + - name: Add mingw32 to path for i686-gnu + run: | + echo "C:\msys64\mingw32\bin" >> $GITHUB_PATH + if: matrix.target == 'i686-pc-windows-gnu' && matrix.channel == 'nightly' + shell: bash + + - name: Add mingw64 to path for x86_64-gnu + run: echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH + if: matrix.target == 'x86_64-pc-windows-gnu' && matrix.channel == 'nightly' + shell: bash + - name: cargo-make run: cargo install --force cargo-make From 0934150a5378b740c1586df37b410b3d726756f0 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 1 Jan 2021 17:00:40 +0100 Subject: [PATCH 02/10] Add support for edition 2021. --- Configurations.md | 4 ++-- src/config/options.rs | 21 ++++++++++++++++----- src/formatting.rs | 2 +- src/imports.rs | 2 +- 4 files changed, 20 insertions(+), 9 deletions(-) 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; From d0265490c10287646419c64254adede14bf93044 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 1 Jan 2021 17:00:52 +0100 Subject: [PATCH 03/10] Fixes for new rustc changes. --- src/closures.rs | 1 - src/items.rs | 2 +- src/macros.rs | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/closures.rs b/src/closures.rs index 71b07e4b9ee..3d65077ddc2 100644 --- a/src/closures.rs +++ b/src/closures.rs @@ -151,7 +151,6 @@ fn rewrite_closure_with_block( id: ast::NodeId::root(), kind: ast::StmtKind::Expr(ptr::P(body.clone())), span: body.span, - tokens: None, }], id: ast::NodeId::root(), rules: ast::BlockCheckMode::Default, diff --git a/src/items.rs b/src/items.rs index 7b1bb6ce7be..9f079f15c15 100644 --- a/src/items.rs +++ b/src/items.rs @@ -2118,7 +2118,7 @@ pub(crate) fn span_hi_for_param(context: &RewriteContext<'_>, param: &ast::Param pub(crate) fn is_named_param(param: &ast::Param) -> bool { if let ast::PatKind::Ident(_, ident, _) = param.pat.kind { - ident.name != symbol::kw::Invalid + ident.name != symbol::kw::Empty } else { true } diff --git a/src/macros.rs b/src/macros.rs index e5a28751988..119ae61d8dd 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -158,7 +158,7 @@ fn rewrite_macro_name( format!("{}!", pprust::path_to_string(path)) }; match extra_ident { - Some(ident) if ident.name != kw::Invalid => format!("{} {}", name, ident), + Some(ident) if ident.name != kw::Empty => format!("{} {}", name, ident), _ => name, } } From 76f504c873a7a7b9bc650fa6750514b3ce867226 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 1 Jan 2021 20:45:03 +0100 Subject: [PATCH 04/10] Bump rustc-ap to v697. --- Cargo.toml | 16 ++++++++-------- rust-toolchain | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 rust-toolchain diff --git a/Cargo.toml b/Cargo.toml index 2b14df5dcd5..e9d2776276e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,11 +66,11 @@ rustc-workspace-hack = "1.0.0" [dependencies.rustc_ast] package = "rustc-ap-rustc_ast" -version = "691.0.0" +version = "697.0.0" [dependencies.rustc_ast_pretty] package = "rustc-ap-rustc_ast_pretty" -version = "691.0.0" +version = "697.0.0" [dependencies.rustc_attr] package = "rustc-ap-rustc_attr" @@ -78,24 +78,24 @@ version = "691.0.0" [dependencies.rustc_data_structures] package = "rustc-ap-rustc_data_structures" -version = "691.0.0" +version = "697.0.0" [dependencies.rustc_errors] package = "rustc-ap-rustc_errors" -version = "691.0.0" +version = "697.0.0" [dependencies.rustc_expand] package = "rustc-ap-rustc_expand" -version = "691.0.0" +version = "697.0.0" [dependencies.rustc_parse] package = "rustc-ap-rustc_parse" -version = "691.0.0" +version = "697.0.0" [dependencies.rustc_session] package = "rustc-ap-rustc_session" -version = "691.0.0" +version = "697.0.0" [dependencies.rustc_span] package = "rustc-ap-rustc_span" -version = "691.0.0" +version = "697.0.0" diff --git a/rust-toolchain b/rust-toolchain new file mode 100644 index 00000000000..32429dab56e --- /dev/null +++ b/rust-toolchain @@ -0,0 +1 @@ +nightly-2020-12-31 From 8c81dd38668bd5e5edd4fe6aca51684848730121 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 1 Jan 2021 20:47:30 +0100 Subject: [PATCH 05/10] Account for new ast::GenericParamKind::Const::default in rust_ast. --- src/spanned.rs | 9 ++++++++- src/types.rs | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/spanned.rs b/src/spanned.rs index 7bf370c131e..9e3658dd22f 100644 --- a/src/spanned.rs +++ b/src/spanned.rs @@ -113,7 +113,14 @@ impl Spanned for ast::Param { impl Spanned for ast::GenericParam { fn span(&self) -> Span { - let lo = if self.attrs.is_empty() { + let lo = if let ast::GenericParamKind::Const { + ty: _, + kw_span, + default: _, + } = self.kind + { + kw_span.lo() + } else if self.attrs.is_empty() { self.ident.span.lo() } else { self.attrs[0].span.lo() diff --git a/src/types.rs b/src/types.rs index 6e1fc8fc387..bedc29fbc1a 100644 --- a/src/types.rs +++ b/src/types.rs @@ -563,7 +563,12 @@ impl Rewrite for ast::GenericParam { _ => (), } - if let rustc_ast::ast::GenericParamKind::Const { ref ty, .. } = &self.kind { + if let ast::GenericParamKind::Const { + ref ty, + kw_span: _, + default: _, + } = &self.kind + { result.push_str("const "); result.push_str(rewrite_ident(context, self.ident)); result.push_str(": "); From 1f764128be5e9b41ada4ad6cb6b5439607fc2711 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 1 Jan 2021 23:57:19 +0100 Subject: [PATCH 06/10] Fix expected macro formatting test output. --- tests/target/macro_rules.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/target/macro_rules.rs b/tests/target/macro_rules.rs index 68052b25f48..97444aef404 100644 --- a/tests/target/macro_rules.rs +++ b/tests/target/macro_rules.rs @@ -172,7 +172,7 @@ macro_rules! m [ ]; // #2470 -macro foo($type_name: ident, $docs: expr) { +macro foo($type_name:ident, $docs:expr) { #[allow(non_camel_case_types)] #[doc=$docs] #[derive(Debug, Clone, Copy)] From f5e4da77eab7aae59ab39c80db332530bac52a7c Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sat, 2 Jan 2021 02:21:22 +0100 Subject: [PATCH 07/10] Add 2021 test. --- tests/target/imports_2021_edition.rs | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tests/target/imports_2021_edition.rs diff --git a/tests/target/imports_2021_edition.rs b/tests/target/imports_2021_edition.rs new file mode 100644 index 00000000000..34dcc866a0b --- /dev/null +++ b/tests/target/imports_2021_edition.rs @@ -0,0 +1,3 @@ +// rustfmt-edition: 2021 + +use ::happy::new::year; From 1365f6235965f00298799364a0151e0c40836b7f Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sat, 2 Jan 2021 02:21:48 +0100 Subject: [PATCH 08/10] Update Cargo.lock. --- Cargo.lock | 80 +++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69b8214ed8a..d379fec561c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -878,18 +878,18 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_arena" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81f7b9bc5a6f79b1f230833cb4c8f8928d48c129b21df5b372c202fb826c0b5e" +checksum = "fb953bea2006184c8f01a6fd3ed51658c73380992a9aefc113e8d32ece6b7516" dependencies = [ "smallvec", ] [[package]] name = "rustc-ap-rustc_ast" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d77f313e9f30af93f2737f1a99d6552e26b702c5cef3bb65e35f5b4fe5191f1" +checksum = "94da60fa49b2f60d2539e8823cf2b4d4e61583ba4ee796b8289e12f017d3dc5b" dependencies = [ "bitflags", "rustc-ap-rustc_data_structures", @@ -904,9 +904,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_ast_passes" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30408fbf42fa6fbeb383d3fce0f24d2490c3d12527beb2f48e6e728765bc8695" +checksum = "3e9f9eaaee223832187a398abe0f9cb8bc4e5cd538322d8f3864aea65239c79e" dependencies = [ "itertools 0.9.0", "rustc-ap-rustc_ast", @@ -923,9 +923,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_ast_pretty" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d47b8a3adcccc204578b0ee9cd2f9952921fa43977f58343913cca04cce87043" +checksum = "302b43429c62efc43b159b1f8ab94c8b517fb553cbae854c3fcf34e01c36accb" dependencies = [ "rustc-ap-rustc_ast", "rustc-ap-rustc_span", @@ -935,9 +935,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_attr" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66f5f53ecdbf7d8b47905936f93eb1fdae496137e94b7e4023a0b866b0e1a92d" +checksum = "2cbd78cb6f7ca0991478d7f1bc5646b6eca58c37ccbdf70b5d83c490a7c47be7" dependencies = [ "rustc-ap-rustc_ast", "rustc-ap-rustc_ast_pretty", @@ -954,9 +954,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_data_structures" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3aa913fa40b90157067b17dd7ddfd5df0d8566e339ffa8351a638bdf3fc7ee81" +checksum = "7b9ebd359b0f21086a88595a25d92dc7e8e5f7b111e41c52bb6c97e2d95fd0bb" dependencies = [ "arrayvec 0.5.1", "bitflags", @@ -985,9 +985,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_errors" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d4b4956287d7c4996409b8362aa69c0c9a6853751ff00ee0a6f78223c5ef3ad" +checksum = "3b810fcac4d738c47d7793afe3e0f2e03d5193c36c698b0fbcebfb64e468c06b" dependencies = [ "annotate-snippets 0.8.0", "atty", @@ -1005,9 +1005,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_expand" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fa908bb1b67230dd4309e93edefc6a6c2f3d8b6a195f77c47743c882114a22e" +checksum = "f5b44aadd09c05a42a21a063e9f2241fd3d9c00c3dd6e474e22c3a3e8274c959" dependencies = [ "rustc-ap-rustc_ast", "rustc-ap-rustc_ast_passes", @@ -1028,9 +1028,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_feature" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b7a1db115893ed7ed0db80f70d2246c1709de7854238acde76471495930f2a" +checksum = "a2f4121cb9718c8c1c6350a3aaea619fbbae38fb1aadd3d7305586460babb531" dependencies = [ "rustc-ap-rustc_data_structures", "rustc-ap-rustc_span", @@ -1038,21 +1038,21 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_fs_util" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55937887cb606cc72193ea3c5feb8bbbb810d812aa233b9a1e7749155c4a3501" +checksum = "fdb0f36e34fafb725795bef3ec6f414cac34e7ca98e6d25927be36d95ae1c6ac" [[package]] name = "rustc-ap-rustc_graphviz" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e39e179e616356927f0c4eda43e3a35d88476f91e1ac8e4a0a09661dbab44a6e" +checksum = "a98402e20e2913016ed54f12aead5c987fe227a0fb31cc720e17ff51c6f32466" [[package]] name = "rustc-ap-rustc_index" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "572d3962d6999f3b1a71d335308e939e204339d4ad36e6ebe7a591c9d4329f5d" +checksum = "ec91408d727f73f682cd8ae836d762c8dab0ed4e81994ced03aa1edcee3b99a4" dependencies = [ "arrayvec 0.5.1", "rustc-ap-rustc_macros", @@ -1061,18 +1061,18 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_lexer" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44bc89d9ca7a78fb82e103b389362c55f03800745f8ba14e068b805cfaf783ec" +checksum = "67adbe260a0a11910624d6d28c0304fcf7b063e666682111005c83b09f73429d" dependencies = [ "unicode-xid", ] [[package]] name = "rustc-ap-rustc_lint_defs" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d39bda92aabd77e49ac8ad5e24fccf9d7245b8ff2bf1249ab98733e2e5a2863" +checksum = "6bf11d0646da7bd136fbca53834afcc3760fbfc20fa4875e139b3ada41ec53a5" dependencies = [ "rustc-ap-rustc_ast", "rustc-ap-rustc_data_structures", @@ -1084,9 +1084,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_macros" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3295fbc9625197494e356e92d8ac08370eddafa60189861c7b2f084b3b5a6b8" +checksum = "c454b10b66750ffd9bfd7d53b0f30eaba1462356e9ac91f0d037cb0556dc7681" dependencies = [ "proc-macro2", "quote", @@ -1096,9 +1096,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_parse" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ff5d0094396844efead43303a6eb25b8a4962e2c80fb0ea4a86e4101fbfd404" +checksum = "3d03f423948137a8370a88447382a18015d47a273268e3ead2d0a987c3b14070" dependencies = [ "bitflags", "rustc-ap-rustc_ast", @@ -1116,9 +1116,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_serialize" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d5cff6709a8b51a3730288a9ead17cabe8146b1c787db52298447ef7890140a" +checksum = "e7ed5df71bd37d1e179b4bbedf77db76c9e0eb2e03159c58a691adbf29f74682" dependencies = [ "indexmap", "smallvec", @@ -1126,9 +1126,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_session" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36bb15ef12174b5ed6419a7e4260a899ce8927e8c8fd1f0cddf178818737dcdf" +checksum = "3e3b92b51fad25a897b23ec98961126aea038abeab8d47989f774f7727016b5e" dependencies = [ "bitflags", "getopts", @@ -1148,9 +1148,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_span" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "104d349a32be9cfd3d39a5a70ad6c5e682ce262fc5cc8717d35a01e980c0d8b2" +checksum = "d98273206d8a571c780f233f3391ea30e29c5e75ecdc60335ccef346046e1953" dependencies = [ "cfg-if 0.1.10", "md-5", @@ -1168,9 +1168,9 @@ dependencies = [ [[package]] name = "rustc-ap-rustc_target" -version = "691.0.0" +version = "697.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d7ac4ded9a6aecb534744c836a160497985f0d53b272581e95e7890d31b9e17" +checksum = "08ce81fe0130e61112db5f3b2db6b21d407e4b14ae467ab9637f4696cc340ad1" dependencies = [ "bitflags", "rustc-ap-rustc_data_structures", From 07371081d5095e42ec46f71f3767f3d8958b6fd9 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sat, 9 Jan 2021 11:23:36 -0600 Subject: [PATCH 09/10] fix: maintain redundant semis on items in statement pos --- src/visitor.rs | 57 +++++++++++++++++++++++++++++--------- tests/source/statements.rs | 43 ++++++++++++++++++++++++++++ tests/target/statements.rs | 42 ++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 13 deletions(-) create mode 100644 tests/source/statements.rs create mode 100644 tests/target/statements.rs diff --git a/src/visitor.rs b/src/visitor.rs index fefeffac95c..15055d46d30 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -112,7 +112,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { mk_sp(self.last_pos, hi) } - fn visit_stmt(&mut self, stmt: &Stmt<'_>) { + fn visit_stmt(&mut self, stmt: &Stmt<'_>, include_empty_semi: bool) { debug!( "visit_stmt: {}", self.parse_sess.span_to_debug_info(stmt.span()) @@ -127,13 +127,23 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { .map_or(false, |i| starts_with_newline(&snippet[i..])); let snippet = snippet.trim(); if !snippet.is_empty() { - if original_starts_with_newline { - self.push_str("\n"); + // FIXME(calebcartwright 2021-01-03) - This exists strictly to maintain legacy + // formatting where rustfmt would preserve redundant semicolons on Items in a + // statement position. + // See comment within `walk_stmts` for more info + if include_empty_semi { + self.format_missing(stmt.span().hi()); + } else { + if original_starts_with_newline { + self.push_str("\n"); + } + + self.push_str(&self.block_indent.to_string(self.config)); + self.push_str(snippet); } - self.push_str(&self.block_indent.to_string(self.config)); - self.push_str(snippet); + } else if include_empty_semi { + self.push_str(";"); } - self.last_pos = stmt.span().hi(); return; } @@ -141,8 +151,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { match stmt.as_ast_node().kind { ast::StmtKind::Item(ref item) => { self.visit_item(item); - // Handle potential `;` after the item. - self.format_missing(stmt.span().hi()); + self.last_pos = stmt.span().hi(); } ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => { let attrs = get_attrs_from_stmt(stmt.as_ast_node()); @@ -899,7 +908,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { self.visit_items_with_reordering(&ptr_vec_to_ref_vec(&m.items)); } - fn walk_stmts(&mut self, stmts: &[Stmt<'_>]) { + fn walk_stmts(&mut self, stmts: &[Stmt<'_>], include_current_empty_semi: bool) { if stmts.is_empty() { return; } @@ -912,16 +921,38 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { .collect(); if items.is_empty() { - self.visit_stmt(&stmts[0]); - self.walk_stmts(&stmts[1..]); + self.visit_stmt(&stmts[0], include_current_empty_semi); + + // FIXME(calebcartwright 2021-01-03) - This exists strictly to maintain legacy + // formatting where rustfmt would preserve redundant semicolons on Items in a + // statement position. + // + // Starting in rustc-ap-* v692 (~2020-12-01) the rustc parser now parses this as + // two separate statements (Item and Empty kinds), whereas before it was parsed as + // a single statement with the statement's span including the redundant semicolon. + // + // rustfmt typically tosses unnecessary/redundant semicolons, and eventually we + // should toss these as well, but doing so at this time would + // break the Stability Guarantee + // N.B. This could be updated to utilize the version gates. + let include_next_empty = if stmts.len() > 1 { + match (&stmts[0].as_ast_node().kind, &stmts[1].as_ast_node().kind) { + (ast::StmtKind::Item(_), ast::StmtKind::Empty) => true, + _ => false, + } + } else { + false + }; + + self.walk_stmts(&stmts[1..], include_next_empty); } else { self.visit_items_with_reordering(&items); - self.walk_stmts(&stmts[items.len()..]); + self.walk_stmts(&stmts[items.len()..], false); } } fn walk_block_stmts(&mut self, b: &ast::Block) { - self.walk_stmts(&Stmt::from_ast_nodes(b.stmts.iter())) + self.walk_stmts(&Stmt::from_ast_nodes(b.stmts.iter()), false) } fn format_mod( diff --git a/tests/source/statements.rs b/tests/source/statements.rs new file mode 100644 index 00000000000..c840b8ce105 --- /dev/null +++ b/tests/source/statements.rs @@ -0,0 +1,43 @@ +// FIXME(calebcartwright) - Hopefully one day we can +// elide these redundant semis like we do in other contexts. +fn redundant_item_semis() { + impl Foo { + fn get(&self) -> usize { + 5 + } + }; + + impl Bar { + fn get(&self) -> usize { + 5 + } + } /*asdfsf*/; + + + impl Baz { + fn get(&self) -> usize { + 5 + } + } /*asdfsf*/ + + // why would someone do this + ; + + + impl Qux { + fn get(&self) -> usize { + 5 + } + } + + // why + ; + + impl Lorem { + fn get(&self) -> usize { + 5 + } + } + // oh why + ; +} \ No newline at end of file diff --git a/tests/target/statements.rs b/tests/target/statements.rs new file mode 100644 index 00000000000..c1e7dc464c2 --- /dev/null +++ b/tests/target/statements.rs @@ -0,0 +1,42 @@ +// FIXME(calebcartwright) - Hopefully one day we can +// elide these redundant semis like we do in other contexts. +fn redundant_item_semis() { + impl Foo { + fn get(&self) -> usize { + 5 + } + }; + + impl Bar { + fn get(&self) -> usize { + 5 + } + } /*asdfsf*/ + ; + + impl Baz { + fn get(&self) -> usize { + 5 + } + } /*asdfsf*/ + + // why would someone do this + ; + + impl Qux { + fn get(&self) -> usize { + 5 + } + } + + // why + ; + + impl Lorem { + fn get(&self) -> usize { + 5 + } + } + // oh why + ; +} From ed647cee307e09241be91ae035ea9f7293900d05 Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Sat, 9 Jan 2021 11:24:46 -0600 Subject: [PATCH 10/10] meta: bump to v1.4.31 --- CHANGELOG.md | 17 ++++++++++++++++- Cargo.lock | 2 +- Cargo.toml | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e31518813bd..9c04042a320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,21 @@ ## [Unreleased] +## [1.4.31] 2021-01-09 + +### Changed + +- `rustc-ap-*` crates updated to v697.0.0 + +### Added +- Support for 2021 Edition [#4618](https://github.com/rust-lang/rustfmt/pull/4618)) + +### Install/Download Options +- **crates.io package** - *pending* +- **rustup (nightly)** - *pending* +- **GitHub Release Binaries** - [Release v1.4.31](https://github.com/rust-lang/rustfmt/releases/tag/v1.4.31) +- **Build from source** - [Tag v1.4.31](https://github.com/rust-lang/rustfmt/tree/v1.4.31), see instructions for how to [install rustfmt from source][install-from-source] + ## [1.4.30] 2020-12-20 ### Fixed @@ -11,7 +26,7 @@ ### Install/Download Options - **crates.io package** - *pending* -- **rustup (nightly)** - *pending* +- **rustup (nightly)** - Starting in `2020-12-25` - **GitHub Release Binaries** - [Release v1.4.30](https://github.com/rust-lang/rustfmt/releases/tag/v1.4.30) - **Build from source** - [Tag v1.4.30](https://github.com/rust-lang/rustfmt/tree/v1.4.30), see instructions for how to [install rustfmt from source][install-from-source] diff --git a/Cargo.lock b/Cargo.lock index d379fec561c..9728daf1493 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1243,7 +1243,7 @@ dependencies = [ [[package]] name = "rustfmt-nightly" -version = "1.4.30" +version = "1.4.31" dependencies = [ "annotate-snippets 0.6.1", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index e9d2776276e..61a73d29af7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rustfmt-nightly" -version = "1.4.30" +version = "1.4.31" authors = ["Nicholas Cameron ", "The Rustfmt developers"] description = "Tool to find and fix Rust formatting issues" repository = "https://github.com/rust-lang/rustfmt" @@ -74,7 +74,7 @@ version = "697.0.0" [dependencies.rustc_attr] package = "rustc-ap-rustc_attr" -version = "691.0.0" +version = "697.0.0" [dependencies.rustc_data_structures] package = "rustc-ap-rustc_data_structures"