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

Lines changed: 13 additions & 4 deletions
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

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 7 additions & 7 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 {
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
}
Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)