Skip to content

Commit 41e4a3e

Browse files
committed
Don't insert spaces before most semicolons in print_tts.
This gives better output for code produced by proc macros.
1 parent ef71f10 commit 41e4a3e

26 files changed

+59
-73
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
160160
use TokenTree::Delimited as Del;
161161
use TokenTree::Token as Tok;
162162

163+
fn is_punct(tt: &TokenTree) -> bool {
164+
matches!(tt, TokenTree::Token(tok, _) if tok.is_punct())
165+
}
166+
163167
// Each match arm has one or more examples in comments. The default is to
164168
// insert space between adjacent tokens, except for the cases listed in
165169
// this match.
@@ -180,6 +184,9 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
180184
// - Never type: `Fn() ->!`
181185
(_, Tok(Token { kind: Comma | Dot | Not, .. }, _)) => false,
182186

187+
// NON-PUNCT + `;`: `x = 3;`, `[T; 3]`
188+
(tt1, Tok(Token { kind: Semi, .. }, _)) if !is_punct(tt1) => false,
189+
183190
// IDENT + `(`: `f(3)`
184191
//
185192
// FIXME: Incorrect cases:

tests/pretty/delimited-token-groups.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mac! {
99
{
1010
fn clone() -> S
1111
{
12-
panic! () ;
12+
panic! ();
1313

1414
}
1515
}

tests/pretty/macro_rules.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
// pp-exact
22

3-
macro_rules! brace { () => {} ; }
3+
macro_rules! brace { () => {}; }
44

5-
macro_rules! bracket[() => {} ;];
5+
macro_rules! bracket[() => {};];
66

7-
macro_rules! paren(() => {} ;);
7+
macro_rules! paren(() => {};);
88

99
macro_rules! matcher_brackets {
10-
(paren) => {} ; (bracket) => {} ; (brace) => {} ;
10+
(paren) => {}; (bracket) => {}; (brace) => {};
1111
}
1212

1313
macro_rules! all_fragments {
1414
($b : block, $e : expr, $i : ident, $it : item, $l : lifetime, $lit :
1515
literal, $m : meta, $p : pat, $pth : path, $s : stmt, $tt : tt, $ty : ty,
16-
$vis : vis) => {} ;
16+
$vis : vis) => {};
1717
}
1818

1919
fn main() {}

tests/pretty/stmt_expr_attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn _8() {
113113
}
114114

115115
fn _9() {
116-
macro_rules! stmt_mac { () => { let _ = () ; } }
116+
macro_rules! stmt_mac { () => { let _ = (); } }
117117

118118
#[rustc_dummy]
119119
stmt_mac!();

tests/ui/macros/trace_faulty_macros.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ LL | test!(let x = 1+1);
111111
= note: expanding `test! { let x = 1+1 }`
112112
= note: to `test! ((x, 1 + 1))`
113113
= note: expanding `test! { (x, 1 + 1) }`
114-
= note: to `let x = 1 + 1 ;`
114+
= note: to `let x = 1 + 1;`
115115

116116
error: aborting due to 5 previous errors
117117

tests/ui/proc-macro/allowed-attr-stmt-expr.stdout

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
PRINT-ATTR INPUT (DISPLAY): struct ItemWithSemi;
2-
PRINT-ATTR RE-COLLECTED (DISPLAY): struct ItemWithSemi ;
32
PRINT-ATTR INPUT (DEBUG): TokenStream [
43
Ident {
54
ident: "struct",
@@ -47,7 +46,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
4746
},
4847
]
4948
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!";
50-
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_let] let string = "Hello, world!" ;
5149
PRINT-ATTR INPUT (DEBUG): TokenStream [
5250
Punct {
5351
ch: '#',
@@ -90,7 +88,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
9088
},
9189
]
9290
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro!("{}", string);
93-
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string) ;
91+
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string);
9492
PRINT-ATTR INPUT (DEBUG): TokenStream [
9593
Punct {
9694
ch: '#',
@@ -144,7 +142,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
144142
},
145143
]
146144
PRINT-ATTR INPUT (DISPLAY): second_make_stmt!(#[allow(dead_code)] struct Bar {});
147-
PRINT-ATTR RE-COLLECTED (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {}) ;
145+
PRINT-ATTR RE-COLLECTED (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {});
148146
PRINT-ATTR INPUT (DEBUG): TokenStream [
149147
Ident {
150148
ident: "second_make_stmt",
@@ -293,7 +291,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
293291
},
294292
]
295293
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct NonBracedStruct;
296-
PRINT-ATTR RE-COLLECTED (DISPLAY): #[rustc_dummy] struct NonBracedStruct ;
297294
PRINT-ATTR INPUT (DEBUG): TokenStream [
298295
Punct {
299296
ch: '#',

tests/ui/proc-macro/attr-stmt-expr.stdout

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
3030
},
3131
]
3232
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!";
33-
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_let] let string = "Hello, world!" ;
3433
PRINT-ATTR INPUT (DEBUG): TokenStream [
3534
Punct {
3635
ch: '#',
@@ -73,7 +72,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
7372
},
7473
]
7574
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro!("{}", string);
76-
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string) ;
75+
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string);
7776
PRINT-ATTR INPUT (DEBUG): TokenStream [
7877
Punct {
7978
ch: '#',
@@ -127,7 +126,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
127126
},
128127
]
129128
PRINT-ATTR INPUT (DISPLAY): second_make_stmt!(#[allow(dead_code)] struct Bar {});
130-
PRINT-ATTR RE-COLLECTED (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {}) ;
129+
PRINT-ATTR RE-COLLECTED (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {});
131130
PRINT-ATTR INPUT (DEBUG): TokenStream [
132131
Ident {
133132
ident: "second_make_stmt",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fn main() { let y : u32 = "z" ; { let x: u32 = "y"; } }
1+
fn main() { let y : u32 = "z"; { let x: u32 = "y"; } }

tests/ui/proc-macro/auxiliary/attr-stmt-expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ use proc_macro::TokenStream;
1010
#[proc_macro_attribute]
1111
pub fn expect_let(attr: TokenStream, item: TokenStream) -> TokenStream {
1212
assert!(attr.to_string().is_empty());
13-
assert_eq!(item.to_string(), "let string = \"Hello, world!\" ;");
13+
assert_eq!(item.to_string(), "let string = \"Hello, world!\";");
1414
item
1515
}
1616

1717
#[proc_macro_attribute]
1818
pub fn expect_my_macro_stmt(attr: TokenStream, item: TokenStream) -> TokenStream {
1919
assert!(attr.to_string().is_empty());
20-
assert_eq!(item.to_string(), "my_macro! (\"{}\", string) ;");
20+
assert_eq!(item.to_string(), "my_macro! (\"{}\", string);");
2121
item
2222
}
2323

tests/ui/proc-macro/cfg-eval-inner.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ PRINT-ATTR RE-COLLECTED (DISPLAY): impl Foo <
1111
{ field: [u8; { #![rustc_dummy(another_cursed_inner)] 1 }] } 0
1212
}] > { #![rustc_dummy(evaluated_attr)] fn bar() {} }
1313
PRINT-ATTR DEEP-RE-COLLECTED (DISPLAY): impl Foo <
14-
[u8 ;
14+
[u8;
1515
{
1616
#! [rustc_dummy(cursed_inner)] #! [allow(unused)] struct Inner
17-
{ field : [u8 ; { #! [rustc_dummy(another_cursed_inner)] 1 }] } 0
17+
{ field : [u8; { #! [rustc_dummy(another_cursed_inner)] 1 }] } 0
1818
}] > { #! [rustc_dummy(evaluated_attr)] fn bar() {} }
1919
PRINT-ATTR INPUT (DEBUG): TokenStream [
2020
Ident {

0 commit comments

Comments
 (0)