Skip to content

Commit 20d4424

Browse files
committed
Improve token stream pretty printing.
Specifically: - No space before or after `::`, e.g. `a::b` instead of `a :: b`. - No space between `!` and `(`, e.g. `foo!()` instead of `foo! ()`. - No space between `!` and `[`, e.g. `#![...]` instead of `#! [...]`. - No space before `:`, e.g. `a: u32` instead of `a : u32`. - No space before `;`, e.g. `struct A;` instead of `struct A ;`.
1 parent b2eed72 commit 20d4424

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+181
-179
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,29 @@ pub fn print_crate<'a>(
146146
/// and also addresses some specific regressions described in #63896 and #73345.
147147
fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
148148
if let TokenTree::Token(token) = prev {
149-
if matches!(token.kind, token::Dot | token::Dollar) {
149+
// No space after these tokens, e.g. `x.y`, `$e`, `a::b`
150+
// (The carets point to `prev`) ^ ^ ^^
151+
if matches!(token.kind, token::Dot | token::Dollar | token::ModSep) {
150152
return false;
151153
}
152154
if let token::DocComment(comment_kind, ..) = token.kind {
153155
return comment_kind != CommentKind::Line;
154156
}
155157
}
156158
match tt {
157-
TokenTree::Token(token) => !matches!(token.kind, token::Comma | token::Not | token::Dot),
159+
// No space before these tokens, e.g. `x,`, `m!`, `x.y`, `a::b`, `s;`, `x:`
160+
// (The carets point to `token`) ^ ^ ^ ^^ ^ ^
161+
TokenTree::Token(token) => !matches!(
162+
token.kind,
163+
token::Comma | token::Not | token::Dot | token::ModSep | token::Semi | token::Colon
164+
),
165+
// No space before parentheses if preceded by these tokens, e.g. `foo(...)`, `foo!(...).
158166
TokenTree::Delimited(_, Delimiter::Parenthesis, _) => {
159-
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..), .. }))
167+
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..) | token::Not, .. }))
160168
}
169+
// No space before brackets if preceded by these tokens, e.g. e.g. `#[...]`, `#![...]`.
161170
TokenTree::Delimited(_, Delimiter::Bracket, _) => {
162-
!matches!(prev, TokenTree::Token(Token { kind: token::Pound, .. }))
171+
!matches!(prev, TokenTree::Token(Token { kind: token::Pound | token::Not, .. }))
163172
}
164173
TokenTree::Delimited(..) => true,
165174
}

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

+3-3
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/cast-lt.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
// pretty-mode:expanded
99
// pp-exact:cast-lt.pp
1010

11-
macro_rules! negative { ($e : expr) => { $e < 0 } }
11+
macro_rules! negative { ($e: expr) => { $e < 0 } }
1212

1313
fn main() { (1 as i32) < 0; }

src/test/pretty/delimited-token-groups.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
#![feature(rustc_attrs)]
44

5-
macro_rules! mac { ($($tt : tt) *) => () }
5+
macro_rules! mac { ($($tt: tt) *) => () }
66

77
mac! {
8-
struct S { field1 : u8, field2 : u16, } impl Clone for S
8+
struct S { field1: u8, field2: u16, } impl Clone for S
99
{
1010
fn clone() -> S
1111
{
12-
panic! () ;
12+
panic!();
1313

1414
}
1515
}

src/test/pretty/macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
#![feature(decl_macro)]
44

5-
pub(crate) macro mac { ($arg : expr) => { $arg + $arg } }
5+
pub(crate) macro mac { ($arg: expr) => { $arg + $arg } }
66

77
fn main() {}

src/test/pretty/macro_rules.rs

+7-7
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 {
14-
($b : block, $e : expr, $i : ident, $it : item, $l : lifetime, $lit :
15-
literal, $m : meta, $p : pat, $pth : path, $s : stmt, $tt : tt, $ty : ty,
16-
$vis : vis) => {} ;
14+
($b: block, $e: expr, $i: ident, $it: item, $l: lifetime, $lit: literal,
15+
$m: meta, $p: pat, $pth: path, $s: stmt, $tt: tt, $ty: ty, $vis: vis) =>
16+
{};
1717
}
1818

1919
fn main() {}

src/test/pretty/stmt_expr_attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn _8() {
114114
}
115115

116116
fn _9() {
117-
macro_rules! stmt_mac { () => { let _ = () ; } }
117+
macro_rules! stmt_mac { () => { let _ = (); } }
118118

119119
#[rustc_dummy]
120120
stmt_mac!();

src/test/run-make/rustc-macro-dep-files/foo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ use proc_macro::TokenStream;
77
#[proc_macro_derive(A)]
88
pub fn derive(input: TokenStream) -> TokenStream {
99
let input = input.to_string();
10-
assert!(input.contains("struct A ;"));
10+
assert!(input.contains("struct A;"));
1111
"struct B;".parse().unwrap()
1212
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
async fn f(mut x : u8) {}
2-
async fn g((mut x, y, mut z) : (u8, u8, u8)) {}
3-
async fn g(mut x : u8, (a, mut b, c) : (u8, u8, u8), y : u8) {}
1+
async fn f(mut x: u8) {}
2+
async fn g((mut x, y, mut z): (u8, u8, u8)) {}
3+
async fn g(mut x: u8, (a, mut b, c): (u8, u8, u8), y: u8) {}

src/test/ui/hygiene/unpretty-debug.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![feature /* 0#0 */(no_core)]
99
#![no_core /* 0#0 */]
1010

11-
macro_rules! foo /* 0#0 */ { ($x : ident) => { y + $x } }
11+
macro_rules! foo /* 0#0 */ { ($x: ident) => { y + $x } }
1212

1313
fn bar /* 0#0 */() {
1414
let x /* 0#0 */ = 1;

src/test/ui/macros/stringify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ fn test_item() {
636636
() => {};
637637
}
638638
),
639-
"macro_rules! stringify { () => {} ; }", // FIXME
639+
"macro_rules! stringify { () => {}; }", // FIXME
640640
);
641641
assert_eq!(
642642
stringify_item!(

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

+1-1
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

+7-7
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
@@ -75,8 +75,8 @@ LL | let a = pat_macro!();
7575
| ^^^^^^^^^^^^
7676
|
7777
= note: expanding `pat_macro! { }`
78-
= note: to `pat_macro! (A { a : a, b : 0, c : _, .. }) ;`
79-
= note: expanding `pat_macro! { A { a : a, b : 0, c : _, .. } }`
78+
= note: to `pat_macro!(A { a: a, b: 0, c: _, .. });`
79+
= note: expanding `pat_macro! { A { a: a, b: 0, c: _, .. } }`
8080
= note: to `A { a: a, b: 0, c: _, .. }`
8181

8282
error: aborting due to 4 previous errors

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): struct ItemWithSemi ;
1+
PRINT-ATTR INPUT (DISPLAY): struct ItemWithSemi;
22
PRINT-ATTR INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "struct",
@@ -45,7 +45,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
4545
span: $DIR/allowed-attr-stmt-expr.rs:53:27: 53:29 (#0),
4646
},
4747
]
48-
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!" ;
48+
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!";
4949
PRINT-ATTR INPUT (DEBUG): TokenStream [
5050
Punct {
5151
ch: '#',
@@ -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",
@@ -288,7 +288,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
288288
span: $DIR/allowed-attr-stmt-expr.rs:68:18: 68:20 (#0),
289289
},
290290
]
291-
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct NonBracedStruct ;
291+
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct NonBracedStruct;
292292
PRINT-ATTR INPUT (DEBUG): TokenStream [
293293
Punct {
294294
ch: '#',

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): fn foo < T : MyTrait < MyStruct < { true } >> > () {}
1+
PRINT-ATTR INPUT (DISPLAY): fn foo < T: MyTrait < MyStruct < { true } >> > () {}
22
PRINT-ATTR INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "fn",
@@ -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

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
2929
span: $DIR/attr-stmt-expr.rs:45:27: 45:29 (#0),
3030
},
3131
]
32-
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!" ;
32+
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!";
3333
PRINT-ATTR INPUT (DEBUG): TokenStream [
3434
Punct {
3535
ch: '#',
@@ -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/attribute-after-derive.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): #[derive(Print)] struct AttributeDerive { #[cfg(FALSE)] field : u8, }
1+
PRINT-ATTR INPUT (DISPLAY): #[derive(Print)] struct AttributeDerive { #[cfg(FALSE)] field: u8, }
22
PRINT-ATTR INPUT (DEBUG): TokenStream [
33
Punct {
44
ch: '#',
@@ -130,7 +130,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
130130
span: $DIR/attribute-after-derive.rs:23:24: 26:2 (#0),
131131
},
132132
]
133-
PRINT-ATTR INPUT (DISPLAY): struct DeriveAttribute { #[cfg(FALSE)] field : u8, }
133+
PRINT-ATTR INPUT (DISPLAY): struct DeriveAttribute { #[cfg(FALSE)] field: u8, }
134134
PRINT-ATTR INPUT (DEBUG): TokenStream [
135135
Ident {
136136
ident: "struct",
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"; } }

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

+3-3
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_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

+3-3
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

@@ -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

src/test/ui/proc-macro/auxiliary/derive-a.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ use proc_macro::TokenStream;
1010
#[proc_macro_derive(A)]
1111
pub fn derive(input: TokenStream) -> TokenStream {
1212
let input = input.to_string();
13-
assert!(input.contains("struct A ;"));
13+
assert!(input.contains("struct A;"));
1414
"".parse().unwrap()
1515
}

src/test/ui/proc-macro/auxiliary/derive-atob.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ use proc_macro::TokenStream;
1010
#[proc_macro_derive(AToB)]
1111
pub fn derive(input: TokenStream) -> TokenStream {
1212
let input = input.to_string();
13-
assert_eq!(input, "struct A ;");
13+
assert_eq!(input, "struct A;");
1414
"struct B;".parse().unwrap()
1515
}

src/test/ui/proc-macro/auxiliary/derive-ctod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ use proc_macro::TokenStream;
1010
#[proc_macro_derive(CToD)]
1111
pub fn derive(input: TokenStream) -> TokenStream {
1212
let input = input.to_string();
13-
assert_eq!(input, "struct C ;");
13+
assert_eq!(input, "struct C;");
1414
"struct D;".parse().unwrap()
1515
}

src/test/ui/proc-macro/auxiliary/derive-same-struct.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use proc_macro::TokenStream;
1010
#[proc_macro_derive(AToB)]
1111
pub fn derive1(input: TokenStream) -> TokenStream {
1212
println!("input1: {:?}", input.to_string());
13-
assert_eq!(input.to_string(), "struct A ;");
13+
assert_eq!(input.to_string(), "struct A;");
1414
"#[derive(BToC)] struct B;".parse().unwrap()
1515
}
1616

1717
#[proc_macro_derive(BToC)]
1818
pub fn derive2(input: TokenStream) -> TokenStream {
19-
assert_eq!(input.to_string(), "struct B ;");
19+
assert_eq!(input.to_string(), "struct B;");
2020
"struct C;".parse().unwrap()
2121
}

src/test/ui/proc-macro/auxiliary/derive-union.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn derive(input: TokenStream) -> TokenStream {
1212
let input = input.to_string();
1313
assert!(input.contains("#[repr(C)]"));
1414
assert!(input.contains("union Test {"));
15-
assert!(input.contains("a : u8,"));
15+
assert!(input.contains("a: u8,"));
1616
assert!(input.contains("}"));
1717
"".parse().unwrap()
1818
}

0 commit comments

Comments
 (0)