From b7df49895ce9868f7de5ce00d804131f7aff9426 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 29 Dec 2021 11:35:50 -0800 Subject: [PATCH 1/2] Fix spacing of pretty printed const item without body --- compiler/rustc_ast_pretty/src/pprust/state.rs | 2 +- .../issue-68710-field-attr-proc-mac-lost.rs | 3 ++- .../pretty/nested-item-vis-defaultness.rs | 20 +++++++++---------- src/test/ui/macros/stringify.rs | 6 +++--- src/test/ui/proc-macro/quote-debug.stdout | 3 ++- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index f0c1d017326cd..6c4f38e9f63cd 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -1116,9 +1116,9 @@ impl<'a> State<'a> { self.print_ident(ident); self.word_space(":"); self.print_type(ty); - self.space(); self.end(); // end the head-ibox if let Some(body) = body { + self.space(); self.word_space("="); self.print_expr(body); } diff --git a/src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs b/src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs index ed7879001d559..5dd04a569e743 100644 --- a/src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs +++ b/src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs @@ -7,7 +7,8 @@ struct C { } #[allow()] -const C: C = +const C: C + = C{ #[cfg(debug_assertions)] field: 0, diff --git a/src/test/pretty/nested-item-vis-defaultness.rs b/src/test/pretty/nested-item-vis-defaultness.rs index f46c0e3f1bc62..b094ba577db2d 100644 --- a/src/test/pretty/nested-item-vis-defaultness.rs +++ b/src/test/pretty/nested-item-vis-defaultness.rs @@ -6,42 +6,42 @@ fn main() {} #[cfg(FALSE)] extern "C" { - static X: u8 ; + static X: u8; type X; fn foo(); - pub static X: u8 ; + pub static X: u8; pub type X; pub fn foo(); } #[cfg(FALSE)] trait T { - const X: u8 ; + const X: u8; type X; fn foo(); - default const X: u8 ; + default const X: u8; default type X; default fn foo(); - pub const X: u8 ; + pub const X: u8; pub type X; pub fn foo(); - pub default const X: u8 ; + pub default const X: u8; pub default type X; pub default fn foo(); } #[cfg(FALSE)] impl T for S { - const X: u8 ; + const X: u8; type X; fn foo(); - default const X: u8 ; + default const X: u8; default type X; default fn foo(); - pub const X: u8 ; + pub const X: u8; pub type X; pub fn foo(); - pub default const X: u8 ; + pub default const X: u8; pub default type X; pub default fn foo(); } diff --git a/src/test/ui/macros/stringify.rs b/src/test/ui/macros/stringify.rs index 90bc7dc1da239..7d1c05a85bcf5 100644 --- a/src/test/ui/macros/stringify.rs +++ b/src/test/ui/macros/stringify.rs @@ -382,13 +382,13 @@ fn test_item() { stringify_item!( static S: (); ), - "static S: () ;", // FIXME + "static S: ();", ); assert_eq!( stringify_item!( static mut S: (); ), - "static mut S: () ;", + "static mut S: ();", ); // ItemKind::Const @@ -402,7 +402,7 @@ fn test_item() { stringify_item!( const S: (); ), - "const S: () ;", // FIXME + "const S: ();", ); // ItemKind::Fn diff --git a/src/test/ui/proc-macro/quote-debug.stdout b/src/test/ui/proc-macro/quote-debug.stdout index 4bdc04b9ac430..d806d7c9aadd5 100644 --- a/src/test/ui/proc-macro/quote-debug.stdout +++ b/src/test/ui/proc-macro/quote-debug.stdout @@ -43,7 +43,8 @@ fn main() { crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new('\u{3b}', crate::Spacing::Alone)))].iter().cloned().collect::() } -const _: () = +const _: () + = { extern crate proc_macro; #[rustc_proc_macro_decls] From b62163515ad109dad05985b958c98a001dbcd89b Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 29 Dec 2021 11:53:03 -0800 Subject: [PATCH 2/2] Move equal sign back into head ibox --- compiler/rustc_ast_pretty/src/pprust/state.rs | 4 +++- src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs | 3 +-- src/test/ui/proc-macro/quote-debug.stdout | 3 +-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs index 6c4f38e9f63cd..0d5ce07c6ca11 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state.rs @@ -1116,9 +1116,11 @@ impl<'a> State<'a> { self.print_ident(ident); self.word_space(":"); self.print_type(ty); + if body.is_some() { + self.space(); + } self.end(); // end the head-ibox if let Some(body) = body { - self.space(); self.word_space("="); self.print_expr(body); } diff --git a/src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs b/src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs index 5dd04a569e743..ed7879001d559 100644 --- a/src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs +++ b/src/test/pretty/issue-68710-field-attr-proc-mac-lost.rs @@ -7,8 +7,7 @@ struct C { } #[allow()] -const C: C - = +const C: C = C{ #[cfg(debug_assertions)] field: 0, diff --git a/src/test/ui/proc-macro/quote-debug.stdout b/src/test/ui/proc-macro/quote-debug.stdout index d806d7c9aadd5..4bdc04b9ac430 100644 --- a/src/test/ui/proc-macro/quote-debug.stdout +++ b/src/test/ui/proc-macro/quote-debug.stdout @@ -43,8 +43,7 @@ fn main() { crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new('\u{3b}', crate::Spacing::Alone)))].iter().cloned().collect::() } -const _: () - = +const _: () = { extern crate proc_macro; #[rustc_proc_macro_decls]