Skip to content

Commit df92119

Browse files
authored
Rollup merge of rust-lang#92418 - dtolnay:emptystructpat, r=michaelwoerister
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>
2 parents 92f28bd + 8d7cf1a commit df92119

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,11 @@ impl<'a> State<'a> {
24612461
self.print_path(path, true, 0);
24622462
}
24632463
self.nbsp();
2464-
self.word_space("{");
2464+
self.word("{");
2465+
let empty = fields.is_empty() && !etc;
2466+
if !empty {
2467+
self.space();
2468+
}
24652469
self.commasep_cmnt(
24662470
Consistent,
24672471
&fields,
@@ -2482,7 +2486,9 @@ impl<'a> State<'a> {
24822486
}
24832487
self.word("..");
24842488
}
2485-
self.space();
2489+
if !empty {
2490+
self.space();
2491+
}
24862492
self.word("}");
24872493
}
24882494
PatKind::Tuple(ref elts) => {

compiler/rustc_hir_pretty/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,11 @@ impl<'a> State<'a> {
18741874
PatKind::Struct(ref qpath, ref fields, etc) => {
18751875
self.print_qpath(qpath, true);
18761876
self.nbsp();
1877-
self.word_space("{");
1877+
self.word("{");
1878+
let empty = fields.is_empty() && !etc;
1879+
if !empty {
1880+
self.space();
1881+
}
18781882
self.commasep_cmnt(
18791883
Consistent,
18801884
&fields,
@@ -1895,7 +1899,9 @@ impl<'a> State<'a> {
18951899
}
18961900
self.word("..");
18971901
}
1898-
self.space();
1902+
if !empty {
1903+
self.space();
1904+
}
18991905
self.word("}");
19001906
}
19011907
PatKind::Or(ref pats) => {

src/test/ui/macros/stringify.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,9 @@ fn test_pat() {
661661
assert_eq!(stringify_pat!(ref mut _x @ _), "ref mut _x @ _");
662662

663663
// PatKind::Struct
664-
assert_eq!(stringify_pat!(Struct {}), "Struct { }"); // FIXME
665-
assert_eq!(stringify_pat!(Struct::<u8> {}), "Struct::<u8> { }");
666-
assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> { }");
664+
assert_eq!(stringify_pat!(Struct {}), "Struct {}");
665+
assert_eq!(stringify_pat!(Struct::<u8> {}), "Struct::<u8> {}");
666+
assert_eq!(stringify_pat!(Struct::<'static> {}), "Struct::<'static> {}");
667667
assert_eq!(stringify_pat!(Struct { x }), "Struct { x }");
668668
assert_eq!(stringify_pat!(Struct { x: _x }), "Struct { x: _x }");
669669
assert_eq!(stringify_pat!(Struct { .. }), "Struct { .. }");
@@ -672,7 +672,7 @@ fn test_pat() {
672672
#[rustfmt::skip] // https://github.com/rust-lang/rustfmt/issues/5151
673673
assert_eq!(
674674
stringify_pat!(<Struct as Trait>::Type {}),
675-
"<Struct as Trait>::Type { }",
675+
"<Struct as Trait>::Type {}",
676676
);
677677

678678
// PatKind::TupleStruct

0 commit comments

Comments
 (0)