From d035ca7db384e125d1a5110cefb3872386fa692f Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 30 Mar 2025 11:19:07 +0200 Subject: [PATCH] Improve hir_pretty for struct expressions. Before: let a = StructWithSomeFields{ field_1: 1, field_2: 2, field_3: 3, field_4: 4, field_5: 5, field_6: 6,}; let a = StructWithSomeFields{ field_1: 1, field_2: 2, ..a}; After: let a = StructWithSomeFields { field_1: 1, field_2: 2, field_3: 3, field_4: 4, field_5: 5, field_6: 6 }; let a = StructWithSomeFields { field_1: 1, field_2: 2, ..a }; --- compiler/rustc_hir_pretty/src/lib.rs | 14 ++++---------- tests/pretty/hir-lifetimes.pp | 6 +++--- tests/pretty/hir-struct-expr.pp | 28 ++++++++++++++++++++++++++++ tests/pretty/hir-struct-expr.rs | 24 ++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 tests/pretty/hir-struct-expr.pp create mode 100644 tests/pretty/hir-struct-expr.rs diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index ddaca89ccf82d..1c23761b2e5bf 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -1193,7 +1193,8 @@ impl<'a> State<'a> { wth: hir::StructTailExpr<'_>, ) { self.print_qpath(qpath, true); - self.word("{"); + self.nbsp(); + self.word_space("{"); self.commasep_cmnt(Consistent, fields, |s, field| s.print_expr_field(field), |f| f.span); match wth { hir::StructTailExpr::Base(expr) => { @@ -1215,20 +1216,13 @@ impl<'a> State<'a> { self.word(".."); self.end(); } - hir::StructTailExpr::None => { - if !fields.is_empty() { - self.word(","); - } - } + hir::StructTailExpr::None => {} } - + self.space(); self.word("}"); } fn print_expr_field(&mut self, field: &hir::ExprField<'_>) { - if self.attrs(field.hir_id).is_empty() { - self.space(); - } self.cbox(INDENT_UNIT); self.print_attrs_as_outer(self.attrs(field.hir_id)); if !field.is_shorthand { diff --git a/tests/pretty/hir-lifetimes.pp b/tests/pretty/hir-lifetimes.pp index e545b0c8f5764..4d1ab9d383bf4 100644 --- a/tests/pretty/hir-lifetimes.pp +++ b/tests/pretty/hir-lifetimes.pp @@ -30,10 +30,10 @@ // FIXME: impl Traits printed as just `/*impl Trait*/`, ugh fn iter1<'a>(&self) - -> /*impl Trait*/ { #[lang = "Range"]{ start: 0, end: 1,} } + -> /*impl Trait*/ { #[lang = "Range"] { start: 0, end: 1 } } fn iter2(&self) - -> /*impl Trait*/ { #[lang = "Range"]{ start: 0, end: 1,} } + -> /*impl Trait*/ { #[lang = "Range"] { start: 0, end: 1 } } } fn a(x: Foo<'_>) { } @@ -82,7 +82,7 @@ x: &'a u32, } -fn f() { { let _ = St{ x: &0,}; }; { let _ = St{ x: &0,}; }; } +fn f() { { let _ = St { x: &0 }; }; { let _ = St { x: &0 }; }; } struct Name<'a>(&'a str); diff --git a/tests/pretty/hir-struct-expr.pp b/tests/pretty/hir-struct-expr.pp new file mode 100644 index 0000000000000..f85d17542dffb --- /dev/null +++ b/tests/pretty/hir-struct-expr.pp @@ -0,0 +1,28 @@ +#[prelude_import] +use ::std::prelude::rust_2015::*; +#[macro_use] +extern crate std; +//@ pretty-compare-only +//@ pretty-mode:hir +//@ pp-exact:hir-struct-expr.pp + +struct StructWithSomeFields { + field_1: i32, + field_2: i32, + field_3: i32, + field_4: i32, + field_5: i32, + field_6: i32, +} + +fn main() { + let a = + StructWithSomeFields { + field_1: 1, + field_2: 2, + field_3: 3, + field_4: 4, + field_5: 5, + field_6: 6 }; + let a = StructWithSomeFields { field_1: 1, field_2: 2, ..a }; +} diff --git a/tests/pretty/hir-struct-expr.rs b/tests/pretty/hir-struct-expr.rs new file mode 100644 index 0000000000000..9580f5d557d32 --- /dev/null +++ b/tests/pretty/hir-struct-expr.rs @@ -0,0 +1,24 @@ +//@ pretty-compare-only +//@ pretty-mode:hir +//@ pp-exact:hir-struct-expr.pp + +struct StructWithSomeFields { + field_1: i32, + field_2: i32, + field_3: i32, + field_4: i32, + field_5: i32, + field_6: i32, +} + +fn main() { + let a = StructWithSomeFields { + field_1: 1, + field_2: 2, + field_3: 3, + field_4: 4, + field_5: 5, + field_6: 6, + }; + let a = StructWithSomeFields { field_1: 1, field_2: 2, ..a }; +}