Skip to content

Commit d035ca7

Browse files
committed
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 };
1 parent 5cc6072 commit d035ca7

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

Diff for: compiler/rustc_hir_pretty/src/lib.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,8 @@ impl<'a> State<'a> {
11931193
wth: hir::StructTailExpr<'_>,
11941194
) {
11951195
self.print_qpath(qpath, true);
1196-
self.word("{");
1196+
self.nbsp();
1197+
self.word_space("{");
11971198
self.commasep_cmnt(Consistent, fields, |s, field| s.print_expr_field(field), |f| f.span);
11981199
match wth {
11991200
hir::StructTailExpr::Base(expr) => {
@@ -1215,20 +1216,13 @@ impl<'a> State<'a> {
12151216
self.word("..");
12161217
self.end();
12171218
}
1218-
hir::StructTailExpr::None => {
1219-
if !fields.is_empty() {
1220-
self.word(",");
1221-
}
1222-
}
1219+
hir::StructTailExpr::None => {}
12231220
}
1224-
1221+
self.space();
12251222
self.word("}");
12261223
}
12271224

12281225
fn print_expr_field(&mut self, field: &hir::ExprField<'_>) {
1229-
if self.attrs(field.hir_id).is_empty() {
1230-
self.space();
1231-
}
12321226
self.cbox(INDENT_UNIT);
12331227
self.print_attrs_as_outer(self.attrs(field.hir_id));
12341228
if !field.is_shorthand {

Diff for: tests/pretty/hir-lifetimes.pp

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030

3131
// FIXME: impl Traits printed as just `/*impl Trait*/`, ugh
3232
fn iter1<'a>(&self)
33-
-> /*impl Trait*/ { #[lang = "Range"]{ start: 0, end: 1,} }
33+
-> /*impl Trait*/ { #[lang = "Range"] { start: 0, end: 1 } }
3434
3535
fn iter2(&self)
36-
-> /*impl Trait*/ { #[lang = "Range"]{ start: 0, end: 1,} }
36+
-> /*impl Trait*/ { #[lang = "Range"] { start: 0, end: 1 } }
3737
}
3838
3939
fn a(x: Foo<'_>) { }
@@ -82,7 +82,7 @@
8282
x: &'a u32,
8383
}
8484

85-
fn f() { { let _ = St{ x: &0,}; }; { let _ = St{ x: &0,}; }; }
85+
fn f() { { let _ = St { x: &0 }; }; { let _ = St { x: &0 }; }; }
8686

8787
struct Name<'a>(&'a str);
8888

Diff for: tests/pretty/hir-struct-expr.pp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#[prelude_import]
2+
use ::std::prelude::rust_2015::*;
3+
#[macro_use]
4+
extern crate std;
5+
//@ pretty-compare-only
6+
//@ pretty-mode:hir
7+
//@ pp-exact:hir-struct-expr.pp
8+
9+
struct StructWithSomeFields {
10+
field_1: i32,
11+
field_2: i32,
12+
field_3: i32,
13+
field_4: i32,
14+
field_5: i32,
15+
field_6: i32,
16+
}
17+
18+
fn main() {
19+
let a =
20+
StructWithSomeFields {
21+
field_1: 1,
22+
field_2: 2,
23+
field_3: 3,
24+
field_4: 4,
25+
field_5: 5,
26+
field_6: 6 };
27+
let a = StructWithSomeFields { field_1: 1, field_2: 2, ..a };
28+
}

Diff for: tests/pretty/hir-struct-expr.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ pretty-compare-only
2+
//@ pretty-mode:hir
3+
//@ pp-exact:hir-struct-expr.pp
4+
5+
struct StructWithSomeFields {
6+
field_1: i32,
7+
field_2: i32,
8+
field_3: i32,
9+
field_4: i32,
10+
field_5: i32,
11+
field_6: i32,
12+
}
13+
14+
fn main() {
15+
let a = StructWithSomeFields {
16+
field_1: 1,
17+
field_2: 2,
18+
field_3: 3,
19+
field_4: 4,
20+
field_5: 5,
21+
field_6: 6,
22+
};
23+
let a = StructWithSomeFields { field_1: 1, field_2: 2, ..a };
24+
}

0 commit comments

Comments
 (0)