about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2025-03-30 11:19:07 +0200
committerMara Bos <m-ou.se@m-ou.se>2025-03-30 11:21:51 +0200
commitd035ca7db384e125d1a5110cefb3872386fa692f (patch)
treee56d65113c81fa35dbbe2f71e91599fdb7a40267
parent5cc60728e7ee10eb2ae5f61f7d412d9805b22f0c (diff)
downloadrust-d035ca7db384e125d1a5110cefb3872386fa692f.tar.gz
rust-d035ca7db384e125d1a5110cefb3872386fa692f.zip
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 };
-rw-r--r--compiler/rustc_hir_pretty/src/lib.rs14
-rw-r--r--tests/pretty/hir-lifetimes.pp6
-rw-r--r--tests/pretty/hir-struct-expr.pp28
-rw-r--r--tests/pretty/hir-struct-expr.rs24
4 files changed, 59 insertions, 13 deletions
diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs
index ddaca89ccf8..1c23761b2e5 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 e545b0c8f57..4d1ab9d383b 100644
--- a/tests/pretty/hir-lifetimes.pp
+++ b/tests/pretty/hir-lifetimes.pp
@@ -30,10 +30,10 @@ impl  Foo<'_> {
 
     // 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 @@ struct St<'a> {
     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 00000000000..f85d17542df
--- /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 00000000000..9580f5d557d
--- /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 };
+}