Skip to content

Commit

Permalink
Rollup merge of rust-lang#92418 - dtolnay:emptystructpat, r=michaelwo…
Browse files Browse the repository at this point in the history
…erister

Fix spacing in pretty printed PatKind::Struct with no fields

Follow-up to rust-lang#92238 fixing one of the FIXMEs.

```rust
macro_rules! repro {
    ($pat:pat) => {
        stringify!($pat)
    };
}

fn main() {
    println!("{}", repro!(Struct {}));
}
```

Before:&ensp;<code>Struct&nbsp;{&nbsp;&nbsp;}</code>
After:&ensp;<code>Struct&nbsp;{}</code>
  • Loading branch information
matthiaskrgr committed Jan 3, 2022
2 parents 92f28bd + 8d7cf1a commit df92119
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
10 changes: 8 additions & 2 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2461,7 +2461,11 @@ impl<'a> State<'a> {
self.print_path(path, true, 0);
}
self.nbsp();
self.word_space("{");
self.word("{");
let empty = fields.is_empty() && !etc;
if !empty {
self.space();
}
self.commasep_cmnt(
Consistent,
&fields,
Expand All @@ -2482,7 +2486,9 @@ impl<'a> State<'a> {
}
self.word("..");
}
self.space();
if !empty {
self.space();
}
self.word("}");
}
PatKind::Tuple(ref elts) => {
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,11 @@ impl<'a> State<'a> {
PatKind::Struct(ref qpath, ref fields, etc) => {
self.print_qpath(qpath, true);
self.nbsp();
self.word_space("{");
self.word("{");
let empty = fields.is_empty() && !etc;
if !empty {
self.space();
}
self.commasep_cmnt(
Consistent,
&fields,
Expand All @@ -1895,7 +1899,9 @@ impl<'a> State<'a> {
}
self.word("..");
}
self.space();
if !empty {
self.space();
}
self.word("}");
}
PatKind::Or(ref pats) => {
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/macros/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,9 @@ fn test_pat() {
assert_eq!(stringify_pat!(ref mut _x @ _), "ref mut _x @ _");

// PatKind::Struct
assert_eq!(stringify_pat!(Struct {}), "Struct { }"); // FIXME
assert_eq!(stringify_pat!(Struct::<u8> {}), "Struct::<u8> { }");
assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> { }");
assert_eq!(stringify_pat!(Struct {}), "Struct {}");
assert_eq!(stringify_pat!(Struct::<u8> {}), "Struct::<u8> {}");
assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> {}");
assert_eq!(stringify_pat!(Struct { x }), "Struct { x }");
assert_eq!(stringify_pat!(Struct { x: _x }), "Struct { x: _x }");
assert_eq!(stringify_pat!(Struct { .. }), "Struct { .. }");
Expand All @@ -672,7 +672,7 @@ fn test_pat() {
#[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
assert_eq!(
stringify_pat!(<Struct as Trait>::Type {}),
"<Struct as Trait>::Type { }",
"<Struct as Trait>::Type {}",
);

// PatKind::TupleStruct
Expand Down

0 comments on commit df92119

Please sign in to comment.