Skip to content

Commit bae356b

Browse files
committed
Improve token stream pretty-printing even more.
With one more token of context, we can handle some interesting cases better, most notably `foo!()` and `foo![]` macro calls.
1 parent daeb4f0 commit bae356b

21 files changed

+81
-54
lines changed

compiler/rustc_ast_pretty/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#![deny(rustc::diagnostic_outside_of_impl)]
66
#![feature(associated_type_bounds)]
77
#![feature(box_patterns)]
8+
#![feature(if_let_guard)]
9+
#![feature(let_chains)]
810
#![feature(with_negative_coherence)]
911
#![recursion_limit = "256"]
1012

compiler/rustc_ast_pretty/src/pprust/state.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub fn print_crate<'a>(
153153
/// - #63896: `#[allow(unused,` must be printed rather than `#[allow(unused ,`
154154
/// - #73345: `#[allow(unused)] must be printed rather than `# [allow(unused)]
155155
///
156-
fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
156+
fn space_between(prev: Option<&TokenTree>, tt1: &TokenTree, tt2: &TokenTree) -> bool {
157157
use token::*;
158158
use Delimiter::*;
159159
use TokenTree::Delimited as Del;
@@ -235,6 +235,16 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
235235
false
236236
}
237237

238+
// `fn` + IDENT + `<`: `fn f<T>(t: T) { ... }`
239+
(Tok(Token { kind: Ident(..), .. }, _), Tok(Token { kind: Lt, .. }, _))
240+
if let Some(prev) = prev
241+
&& let Tok(Token { kind: Ident(sym, is_raw), .. }, _) = prev
242+
&& *sym == kw::Fn
243+
&& !is_raw =>
244+
{
245+
false
246+
}
247+
238248
// `>` + `(`: `f::<u8>()`
239249
// `>>` + `(`: `collect::<Vec<_>>()`
240250
(Tok(Token { kind: Gt | BinOp(Shr), .. }, _), Del(_, Parenthesis, _)) => false,
@@ -249,6 +259,20 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
249259
false
250260
}
251261

262+
// ANYTHING-BUT-`macro_rules` + `!` + NON-BRACE: `foo!()`, `vec![]`,
263+
// `if !cond { ... }`, but not `macro_rules! m { ... }`
264+
(Tok(Token { kind: Not, .. }, _), Del(_, Brace, _)) => true,
265+
(Tok(Token { kind: Not, .. }, _), _) =>
266+
if let Some(prev) = prev
267+
&& let Tok(Token { kind: Ident(sym, is_raw), .. }, _) = prev
268+
&& *sym == sym::macro_rules
269+
&& !is_raw
270+
{
271+
true
272+
} else {
273+
false
274+
}
275+
252276
// `~` + `const`: `impl ~const Clone`
253277
(Tok(Token { kind: Tilde, .. }, _), Tok(Token { kind: Ident(sym, is_raw), .. }, _))
254278
if *sym == kw::Const && !is_raw =>
@@ -658,14 +682,16 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
658682
}
659683

660684
fn print_tts(&mut self, tts: &TokenStream, convert_dollar_crate: bool) {
685+
let mut prev = None;
661686
let mut iter = tts.trees().peekable();
662687
while let Some(tt) = iter.next() {
663688
self.print_tt(tt, convert_dollar_crate);
664689
if let Some(next) = iter.peek() {
665-
if space_between(tt, next) {
690+
if space_between(prev, tt, next) {
666691
self.space();
667692
}
668693
}
694+
prev = Some(tt);
669695
}
670696
}
671697

tests/pretty/delimited-token-groups.rs

+1-1
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/ui/infinite/issue-41731-infinite-macro-print.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ LL | stack!("overflow");
1414
| ^^^^^^^^^^^^^^^^^^
1515
|
1616
= note: expanding `stack! { "overflow" }`
17-
= note: to `print! (stack! ("overflow"));`
18-
= note: expanding `print! { stack! ("overflow") }`
19-
= note: to `{ $crate::io::_print($crate::format_args! (stack! ("overflow"))); }`
17+
= note: to `print!(stack!("overflow"));`
18+
= note: expanding `print! { stack!("overflow") }`
19+
= note: to `{ $crate::io::_print($crate::format_args!(stack!("overflow"))); }`
2020
= note: expanding `stack! { "overflow" }`
21-
= note: to `print! (stack! ("overflow"));`
22-
= note: expanding `print! { stack! ("overflow") }`
23-
= note: to `{ $crate::io::_print($crate::format_args! (stack! ("overflow"))); }`
21+
= note: to `print!(stack!("overflow"));`
22+
= note: expanding `print! { stack!("overflow") }`
23+
= note: to `{ $crate::io::_print($crate::format_args!(stack!("overflow"))); }`
2424

2525
error: format argument must be a string literal
2626
--> $DIR/issue-41731-infinite-macro-print.rs:14:5

tests/ui/infinite/issue-41731-infinite-macro-println.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ LL | stack!("overflow");
1414
| ^^^^^^^^^^^^^^^^^^
1515
|
1616
= note: expanding `stack! { "overflow" }`
17-
= note: to `println! (stack! ("overflow"));`
18-
= note: expanding `println! { stack! ("overflow") }`
19-
= note: to `{ $crate::io::_print($crate::format_args_nl! (stack! ("overflow"))); }`
17+
= note: to `println!(stack!("overflow"));`
18+
= note: expanding `println! { stack!("overflow") }`
19+
= note: to `{ $crate::io::_print($crate::format_args_nl!(stack!("overflow"))); }`
2020
= note: expanding `stack! { "overflow" }`
21-
= note: to `println! (stack! ("overflow"));`
22-
= note: expanding `println! { stack! ("overflow") }`
23-
= note: to `{ $crate::io::_print($crate::format_args_nl! (stack! ("overflow"))); }`
21+
= note: to `println!(stack!("overflow"));`
22+
= note: expanding `println! { stack!("overflow") }`
23+
= note: to `{ $crate::io::_print($crate::format_args_nl!(stack!("overflow"))); }`
2424

2525
error: format argument must be a string literal
2626
--> $DIR/issue-41731-infinite-macro-println.rs:14:5

tests/ui/macros/stringify.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,17 @@ fn test_expr() {
103103
c1!(expr, [ true || false ], "true || false");
104104
c1!(expr, [ true || false && false ], "true || false && false");
105105
c1!(expr, [ a < 1 && 2 < b && c > 3 && 4 > d ], "a < 1 && 2 < b && c > 3 && 4 > d");
106-
c2!(expr, [ a & b & !c ], "a & b & !c", "a & b & ! c");
106+
c1!(expr, [ a & b & !c ], "a & b & !c");
107107
c2!(expr,
108108
[ a + b * c - d + -1 * -2 - -3],
109109
"a + b * c - d + -1 * -2 - -3",
110110
"a + b * c - d + - 1 * - 2 - - 3"
111111
);
112-
c2!(expr, [ x = !y ], "x = !y", "x = ! y");
112+
c1!(expr, [ x = !y ], "x = !y");
113113

114114
// ExprKind::Unary
115115
c2!(expr, [ *expr ], "*expr", "* expr");
116-
c2!(expr, [ !expr ], "!expr", "! expr");
116+
c1!(expr, [ !expr ], "!expr");
117117
c2!(expr, [ -expr ], "-expr", "- expr");
118118

119119
// ExprKind::Lit
@@ -132,7 +132,7 @@ fn test_expr() {
132132

133133
// ExprKind::If
134134
c1!(expr, [ if true {} ], "if true {}");
135-
c2!(expr, [ if !true {} ], "if !true {}", "if ! true {}");
135+
c1!(expr, [ if !true {} ], "if !true {}");
136136
c1!(expr,
137137
[ if ::std::blah() { } else { } ],
138138
"if ::std::blah() {} else {}"
@@ -292,7 +292,7 @@ fn test_expr() {
292292
// ExprKind::OffsetOf: untestable because this test works pre-expansion.
293293

294294
// ExprKind::MacCall
295-
c2!(expr, [ mac!(...) ], "mac!(...)", "mac! (...)");
295+
c1!(expr, [ mac!(...) ], "mac!(...)");
296296
c1!(expr, [ mac![...] ], "mac![...]");
297297
c1!(expr, [ mac! { ... } ], "mac! { ... }");
298298

@@ -379,12 +379,12 @@ fn test_item() {
379379
c2!(item,
380380
[ fn g<T>(t: Vec<Vec<Vec<T>>>) {} ],
381381
"fn g<T>(t: Vec<Vec<Vec<T>>>) {}",
382-
"fn g < T >(t: Vec < Vec < Vec < T >> >) {}"
382+
"fn g< T >(t: Vec < Vec < Vec < T >> >) {}"
383383
);
384384
c2!(item,
385385
[ fn h<'a>(t: &'a Vec<Cell<dyn D>>) {} ],
386386
"fn h<'a>(t: &'a Vec<Cell<dyn D>>) {}",
387-
"fn h < 'a >(t: & 'a Vec < Cell < dyn D >>) {}"
387+
"fn h< 'a >(t: & 'a Vec < Cell < dyn D >>) {}"
388388
);
389389

390390
// ItemKind::Mod
@@ -516,7 +516,7 @@ fn test_item() {
516516
c1!(item, [ impl ~const Struct {} ], "impl ~const Struct {}");
517517

518518
// ItemKind::MacCall
519-
c2!(item, [ mac!(...); ], "mac!(...);", "mac! (...);");
519+
c1!(item, [ mac!(...); ], "mac!(...);");
520520
c1!(item, [ mac![...]; ], "mac![...];");
521521
c1!(item, [ mac! { ... } ], "mac! { ... }");
522522

@@ -630,7 +630,7 @@ fn test_pat() {
630630
c1!(pat, [ (pat) ], "(pat)");
631631

632632
// PatKind::MacCall
633-
c2!(pat, [ mac!(...) ], "mac!(...)", "mac! (...)");
633+
c1!(pat, [ mac!(...) ], "mac!(...)");
634634
c1!(pat, [ mac![...] ], "mac![...]");
635635
c1!(pat, [ mac! { ... } ], "mac! { ... }");
636636
}
@@ -675,7 +675,7 @@ fn test_stmt() {
675675
c1!(stmt, [ ; ], ";");
676676

677677
// StmtKind::MacCall
678-
c2!(stmt, [ mac!(...) ], "mac!(...)", "mac! (...)");
678+
c1!(stmt, [ mac!(...) ], "mac!(...)");
679679
c1!(stmt, [ mac![...] ], "mac![...]");
680680
c1!(stmt, [ mac! { ... } ], "mac! { ... }");
681681
}
@@ -755,7 +755,7 @@ fn test_ty() {
755755
// TyKind::ImplicitSelf: there is no syntax for this.
756756

757757
// TyKind::MacCall
758-
c2!(ty, [ mac!(...) ], "mac!(...)", "mac! (...)");
758+
c1!(ty, [ mac!(...) ], "mac!(...)");
759759
c1!(ty, [ mac![...] ], "mac![...]");
760760
c1!(ty, [ mac! { ... } ], "mac! { ... }");
761761

tests/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

tests/ui/macros/trace_faulty_macros.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | my_faulty_macro!();
2020
| ^^^^^^^^^^^^^^^^^^
2121
|
2222
= note: expanding `my_faulty_macro! { }`
23-
= note: to `my_faulty_macro! (bcd);`
23+
= note: to `my_faulty_macro!(bcd);`
2424
= note: expanding `my_faulty_macro! { bcd }`
2525

2626
error: recursion limit reached while expanding `my_recursive_macro!`
@@ -42,13 +42,13 @@ LL | my_recursive_macro!();
4242
| ^^^^^^^^^^^^^^^^^^^^^
4343
|
4444
= note: expanding `my_recursive_macro! { }`
45-
= note: to `my_recursive_macro! ();`
45+
= note: to `my_recursive_macro!();`
4646
= note: expanding `my_recursive_macro! { }`
47-
= note: to `my_recursive_macro! ();`
47+
= note: to `my_recursive_macro!();`
4848
= note: expanding `my_recursive_macro! { }`
49-
= note: to `my_recursive_macro! ();`
49+
= note: to `my_recursive_macro!();`
5050
= note: expanding `my_recursive_macro! { }`
51-
= note: to `my_recursive_macro! ();`
51+
= note: to `my_recursive_macro!();`
5252

5353
error: expected expression, found `A { a: a, b: 0, c: _, .. }`
5454
--> $DIR/trace_faulty_macros.rs:16:9
@@ -76,7 +76,7 @@ LL | let a = pat_macro!();
7676
| ^^^^^^^^^^^^
7777
|
7878
= note: expanding `pat_macro! { }`
79-
= note: to `pat_macro! (A { a: a, b: 0, c: _, .. });`
79+
= note: to `pat_macro!(A { a: a, b: 0, c: _, .. });`
8080
= note: expanding `pat_macro! { A { a: a, b: 0, c: _, .. } }`
8181
= note: to `A { a: a, b: 0, c: _, .. }`
8282

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

+2-2
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",

tests/ui/proc-macro/attr-complex-fn.stdout

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

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

+2-2
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",

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

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

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

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

tests/ui/proc-macro/dollar-crate-issue-62325.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): struct A(identity! ($crate::S));
1+
PRINT-ATTR INPUT (DISPLAY): struct A(identity!($crate::S));
22
PRINT-ATTR INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "struct",
@@ -53,7 +53,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
5353
span: $DIR/dollar-crate-issue-62325.rs:19:35: 19:36 (#3),
5454
},
5555
]
56-
PRINT-ATTR INPUT (DISPLAY): struct B(identity! ($crate::S));
56+
PRINT-ATTR INPUT (DISPLAY): struct B(identity!($crate::S));
5757
PRINT-ATTR INPUT (DEBUG): TokenStream [
5858
Ident {
5959
ident: "struct",

tests/ui/proc-macro/issue-75734-pp-paren.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): fn main() { & | _: u8 | {}; mul_2! (1 + 1); }
1+
PRINT-ATTR INPUT (DISPLAY): fn main() { & | _: u8 | {}; mul_2!(1 + 1); }
22
PRINT-ATTR INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "fn",

tests/ui/proc-macro/meta-macro-hygiene.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ macro_rules! produce_it
3232
*/ {
3333
() =>
3434
{
35-
meta_macro::print_def_site! ($crate::dummy! ());
35+
meta_macro::print_def_site!($crate::dummy!());
3636
// `print_def_site!` will respan the `$crate` identifier
3737
// with `Span::def_site()`. This should cause it to resolve
3838
// relative to `meta_macro`, *not* `make_macro` (despite

tests/ui/proc-macro/nonterminal-expansion.stdout

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
PRINT-ATTR_ARGS INPUT (DISPLAY): a, line!(), b
2-
PRINT-ATTR_ARGS RE-COLLECTED (DISPLAY): a, line! (), b
32
PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
43
Ident {
54
ident: "a",

tests/ui/proc-macro/nonterminal-token-hygiene.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ macro_rules! outer
5252
*/ {
5353
($item: item) =>
5454
{
55-
macro inner() { print_bang! { $item } } inner! ();
55+
macro inner() { print_bang! { $item } } inner!();
5656

5757
};
5858
}

tests/ui/proc-macro/pretty-print-tts.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-BANG INPUT (DISPLAY): { #![rustc_dummy] let a = "hello".len(); matches! (a, 5); }
1+
PRINT-BANG INPUT (DISPLAY): { #![rustc_dummy] let a = "hello".len(); matches!(a, 5); }
22
PRINT-BANG INPUT (DEBUG): TokenStream [
33
Group {
44
delimiter: Brace,

tests/ui/proc-macro/trailing-plus.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): fn foo < T >() where T: Copy + {}
1+
PRINT-ATTR INPUT (DISPLAY): fn foo< T >() where T: Copy + {}
22
PRINT-ATTR INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "fn",

0 commit comments

Comments
 (0)