From 8d7cf1a4cae07489c375af7bd03923012774dadd Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 29 Dec 2021 12:37:13 -0800 Subject: [PATCH] Fix spacing in pretty printed PatKind::Struct with no fields --- compiler/rustc_ast_pretty/src/pprust/state.rs | 10 ++++++++-- compiler/rustc_hir_pretty/src/lib.rs | 10 ++++++++-- src/test/ui/macros/stringify.rs | 8 ++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index f0c1d017326..f0676caed46 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -2460,7 +2460,11 @@ impl<'a> State<'a> { self.print_path(path, true, 0); } self.nbsp(); - self.word_space("{"); + self.word("{"); + let empty = fields.is_empty() && !etc; + if !empty { + self.space(); + } self.commasep_cmnt( Consistent, &fields, @@ -2481,7 +2485,9 @@ impl<'a> State<'a> { } self.word(".."); } - self.space(); + if !empty { + self.space(); + } self.word("}"); } PatKind::Tuple(ref elts) => { diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 334fa6f4e5c..96754cc3c8f 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1874,7 +1874,11 @@ impl<'a> State<'a> { PatKind::Struct(ref qpath, ref fields, etc) => { self.print_qpath(qpath, true); self.nbsp(); - self.word_space("{"); + self.word("{"); + let empty = fields.is_empty() && !etc; + if !empty { + self.space(); + } self.commasep_cmnt( Consistent, &fields, @@ -1895,7 +1899,9 @@ impl<'a> State<'a> { } self.word(".."); } - self.space(); + if !empty { + self.space(); + } self.word("}"); } PatKind::Or(ref pats) => { diff --git a/src/test/ui/macros/stringify.rs b/src/test/ui/macros/stringify.rs index 90bc7dc1da2..c0a030a2ecb 100644 --- a/src/test/ui/macros/stringify.rs +++ b/src/test/ui/macros/stringify.rs @@ -661,9 +661,9 @@ fn test_pat() { assert_eq!(stringify_pat!(ref mut _x @ _), "ref mut _x @ _"); // PatKind::Struct - assert_eq!(stringify_pat!(Struct {}), "Struct { }"); // FIXME - assert_eq!(stringify_pat!(Struct:: {}), "Struct:: { }"); - assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> { }"); + assert_eq!(stringify_pat!(Struct {}), "Struct {}"); + assert_eq!(stringify_pat!(Struct:: {}), "Struct:: {}"); + assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> {}"); assert_eq!(stringify_pat!(Struct { x }), "Struct { x }"); assert_eq!(stringify_pat!(Struct { x: _x }), "Struct { x: _x }"); assert_eq!(stringify_pat!(Struct { .. }), "Struct { .. }"); @@ -672,7 +672,7 @@ fn test_pat() { #[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151 assert_eq!( stringify_pat!(::Type {}), - "::Type { }", + "::Type {}", ); // PatKind::TupleStruct