Skip to content

Commit 357c013

Browse files
committed
Remove some unncessary spaces from pretty-printed tokenstream output
In addition to making the output look nicer for all crates, this also aligns the pretty-printing output with what the `rental` crate expects. This will allow us to eventually disable a backwards-compat hack in a follow-up PR.
1 parent 1025db8 commit 357c013

25 files changed

+176
-53
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,15 @@ pub fn print_crate<'a>(
140140
// and also addresses some specific regressions described in #63896 and #73345.
141141
fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
142142
if let TokenTree::Token(token) = prev {
143+
if matches!(token.kind, token::Dot) {
144+
return false;
145+
}
143146
if let token::DocComment(comment_kind, ..) = token.kind {
144147
return comment_kind != CommentKind::Line;
145148
}
146149
}
147150
match tt {
148-
TokenTree::Token(token) => token.kind != token::Comma,
151+
TokenTree::Token(token) => !matches!(token.kind, token::Comma | token::Not | token::Dot),
149152
TokenTree::Delimited(_, DelimToken::Paren, _) => {
150153
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..), .. }))
151154
}

src/test/pretty/ast-stmt-expr-attr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ fn syntax() {
114114
let _ = #[attr] continue ;
115115
let _ = #[attr] return;
116116
let _ = #[attr] foo!();
117-
let _ = #[attr] foo!(# ! [attr]);
117+
let _ = #[attr] foo!(#! [attr]);
118118
let _ = #[attr] foo![];
119-
let _ = #[attr] foo![# ! [attr]];
119+
let _ = #[attr] foo![#! [attr]];
120120
let _ = #[attr] foo! { };
121-
let _ = #[attr] foo! { # ! [attr] };
121+
let _ = #[attr] foo! { #! [attr] };
122122
let _ = #[attr] Foo{bar: baz,};
123123
let _ = #[attr] Foo{..foo};
124124
let _ = #[attr] Foo{bar: baz, ..foo};

src/test/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
}

src/test/ui/macros/trace-macro.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ LL | println!("Hello, World!");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: expanding `println! { "Hello, World!" }`
8-
= note: to `{ $crate :: io :: _print($crate :: format_args_nl ! ("Hello, World!")) ; }`
8+
= note: to `{ $crate :: io :: _print($crate :: format_args_nl! ("Hello, World!")) ; }`
99

src/test/ui/macros/trace_faulty_macros.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | my_faulty_macro!();
1919
| ^^^^^^^^^^^^^^^^^^^
2020
|
2121
= note: expanding `my_faulty_macro! { }`
22-
= note: to `my_faulty_macro ! (bcd) ;`
22+
= note: to `my_faulty_macro! (bcd) ;`
2323
= note: expanding `my_faulty_macro! { bcd }`
2424

2525
error: recursion limit reached while expanding `my_recursive_macro!`
@@ -41,13 +41,13 @@ LL | my_recursive_macro!();
4141
| ^^^^^^^^^^^^^^^^^^^^^^
4242
|
4343
= note: expanding `my_recursive_macro! { }`
44-
= note: to `my_recursive_macro ! () ;`
44+
= note: to `my_recursive_macro! () ;`
4545
= note: expanding `my_recursive_macro! { }`
46-
= note: to `my_recursive_macro ! () ;`
46+
= note: to `my_recursive_macro! () ;`
4747
= note: expanding `my_recursive_macro! { }`
48-
= note: to `my_recursive_macro ! () ;`
48+
= note: to `my_recursive_macro! () ;`
4949
= note: expanding `my_recursive_macro! { }`
50-
= note: to `my_recursive_macro ! () ;`
50+
= note: to `my_recursive_macro! () ;`
5151

5252
error: expected expression, found `A { a: a, b: 0, c: _, .. }`
5353
--> $DIR/trace_faulty_macros.rs:16:9
@@ -73,7 +73,7 @@ LL | let a = pat_macro!();
7373
| ^^^^^^^^^^^^
7474
|
7575
= note: expanding `pat_macro! { }`
76-
= note: to `pat_macro ! (A { a : a, b : 0, c : _, .. }) ;`
76+
= note: to `pat_macro! (A { a : a, b : 0, c : _, .. }) ;`
7777
= note: expanding `pat_macro! { A { a : a, b : 0, c : _, .. } }`
7878
= note: to `A { a: a, b: 0, c: _, .. }`
7979

src/test/ui/proc-macro/allowed-attr-stmt-expr.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
8787
span: $DIR/allowed-attr-stmt-expr.rs:57:33: 57:34 (#0),
8888
},
8989
]
90-
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro ! ("{}", string) ;
90+
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string) ;
9191
PRINT-ATTR INPUT (DEBUG): TokenStream [
9292
Punct {
9393
ch: '#',
@@ -140,7 +140,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
140140
span: $DIR/allowed-attr-stmt-expr.rs:61:28: 61:29 (#0),
141141
},
142142
]
143-
PRINT-ATTR INPUT (DISPLAY): second_make_stmt ! (#[allow(dead_code)] struct Bar { }) ;
143+
PRINT-ATTR INPUT (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar { }) ;
144144
PRINT-ATTR INPUT (DEBUG): TokenStream [
145145
Ident {
146146
ident: "second_make_stmt",

src/test/ui/proc-macro/attr-complex-fn.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
7676
span: $DIR/attr-complex-fn.rs:19:42: 19:44 (#0),
7777
},
7878
]
79-
PRINT-ATTR INPUT (DISPLAY): impl < T > MyTrait < T > for MyStruct < { true } > { # ! [rustc_dummy] }
79+
PRINT-ATTR INPUT (DISPLAY): impl < T > MyTrait < T > for MyStruct < { true } > { #! [rustc_dummy] }
8080
PRINT-ATTR INPUT (DEBUG): TokenStream [
8181
Ident {
8282
ident: "impl",

src/test/ui/proc-macro/attr-stmt-expr.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
7171
span: $DIR/attr-stmt-expr.rs:49:33: 49:34 (#0),
7272
},
7373
]
74-
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro ! ("{}", string) ;
74+
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string) ;
7575
PRINT-ATTR INPUT (DEBUG): TokenStream [
7676
Punct {
7777
ch: '#',
@@ -124,7 +124,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
124124
span: $DIR/attr-stmt-expr.rs:53:28: 53:29 (#0),
125125
},
126126
]
127-
PRINT-ATTR INPUT (DISPLAY): second_make_stmt ! (#[allow(dead_code)] struct Bar { }) ;
127+
PRINT-ATTR INPUT (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar { }) ;
128128
PRINT-ATTR INPUT (DEBUG): TokenStream [
129129
Ident {
130130
ident: "second_make_stmt",

src/test/ui/proc-macro/auxiliary/attr-stmt-expr-rpass.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn expect_let(attr: TokenStream, item: TokenStream) -> TokenStream {
1717
#[proc_macro_attribute]
1818
pub fn expect_print_stmt(attr: TokenStream, item: TokenStream) -> TokenStream {
1919
assert!(attr.to_string().is_empty());
20-
assert_eq!(item.to_string(), "println ! (\"{}\", string) ;");
20+
assert_eq!(item.to_string(), "println! (\"{}\", string) ;");
2121
item
2222
}
2323

@@ -31,7 +31,7 @@ pub fn expect_expr(attr: TokenStream, item: TokenStream) -> TokenStream {
3131
#[proc_macro_attribute]
3232
pub fn expect_print_expr(attr: TokenStream, item: TokenStream) -> TokenStream {
3333
assert!(attr.to_string().is_empty());
34-
assert_eq!(item.to_string(), "println ! (\"{}\", string)");
34+
assert_eq!(item.to_string(), "println! (\"{}\", string)");
3535
item
3636
}
3737

src/test/ui/proc-macro/auxiliary/attr-stmt-expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn expect_let(attr: TokenStream, item: TokenStream) -> TokenStream {
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

@@ -31,7 +31,7 @@ pub fn expect_expr(attr: TokenStream, item: TokenStream) -> TokenStream {
3131
#[proc_macro_attribute]
3232
pub fn expect_my_macro_expr(attr: TokenStream, item: TokenStream) -> TokenStream {
3333
assert!(attr.to_string().is_empty());
34-
assert_eq!(item.to_string(), "my_macro ! (\"{}\", string)");
34+
assert_eq!(item.to_string(), "my_macro! (\"{}\", string)");
3535
item
3636
}
3737

0 commit comments

Comments
 (0)