Skip to content

Improve hir_pretty for struct expressions. #139132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions tests/pretty/hir-lifetimes.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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<'_>) { }
Expand Down Expand Up @@ -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);

Expand Down
28 changes: 28 additions & 0 deletions tests/pretty/hir-struct-expr.pp
Original file line number Diff line number Diff line change
@@ -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 };
}
24 changes: 24 additions & 0 deletions tests/pretty/hir-struct-expr.rs
Original file line number Diff line number Diff line change
@@ -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 };
}
Loading