Skip to content

Commit

Permalink
Improve token stream pretty printing.
Browse files Browse the repository at this point in the history
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 ;`.
  • Loading branch information
nnethercote committed May 24, 2022
1 parent b2eed72 commit 20d4424
Show file tree
Hide file tree
Showing 54 changed files with 181 additions and 179 deletions.
17 changes: 13 additions & 4 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,29 @@ pub fn print_crate<'a>(
/// and also addresses some specific regressions described in #63896 and #73345.
fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
if let TokenTree::Token(token) = prev {
if matches!(token.kind, token::Dot | token::Dollar) {
// No space after these tokens, e.g. `x.y`, `$e`, `a::b`
// (The carets point to `prev`) ^ ^ ^^
if matches!(token.kind, token::Dot | token::Dollar | token::ModSep) {
return false;
}
if let token::DocComment(comment_kind, ..) = token.kind {
return comment_kind != CommentKind::Line;
}
}
match tt {
TokenTree::Token(token) => !matches!(token.kind, token::Comma | token::Not | token::Dot),
// No space before these tokens, e.g. `x,`, `m!`, `x.y`, `a::b`, `s;`, `x:`
// (The carets point to `token`) ^ ^ ^ ^^ ^ ^
TokenTree::Token(token) => !matches!(
token.kind,
token::Comma | token::Not | token::Dot | token::ModSep | token::Semi | token::Colon
),
// No space before parentheses if preceded by these tokens, e.g. `foo(...)`, `foo!(...).
TokenTree::Delimited(_, Delimiter::Parenthesis, _) => {
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..), .. }))
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..) | token::Not, .. }))
}
// No space before brackets if preceded by these tokens, e.g. e.g. `#[...]`, `#![...]`.
TokenTree::Delimited(_, Delimiter::Bracket, _) => {
!matches!(prev, TokenTree::Token(Token { kind: token::Pound, .. }))
!matches!(prev, TokenTree::Token(Token { kind: token::Pound | token::Not, .. }))
}
TokenTree::Delimited(..) => true,
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/pretty/ast-stmt-expr-attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ fn syntax() {
let _ = #[attr] continue;
let _ = #[attr] return;
let _ = #[attr] foo!();
let _ = #[attr] foo!(#! [attr]);
let _ = #[attr] foo!(#![attr]);
let _ = #[attr] foo![];
let _ = #[attr] foo![#! [attr]];
let _ = #[attr] foo![#![attr]];
let _ = #[attr] foo! {};
let _ = #[attr] foo! { #! [attr] };
let _ = #[attr] foo! { #![attr] };
let _ = #[attr] Foo { bar: baz };
let _ = #[attr] Foo { ..foo };
let _ = #[attr] Foo { bar: baz, ..foo };
Expand Down
2 changes: 1 addition & 1 deletion src/test/pretty/cast-lt.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
// pretty-mode:expanded
// pp-exact:cast-lt.pp

macro_rules! negative { ($e : expr) => { $e < 0 } }
macro_rules! negative { ($e: expr) => { $e < 0 } }

fn main() { (1 as i32) < 0; }
6 changes: 3 additions & 3 deletions src/test/pretty/delimited-token-groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

#![feature(rustc_attrs)]

macro_rules! mac { ($($tt : tt) *) => () }
macro_rules! mac { ($($tt: tt) *) => () }

mac! {
struct S { field1 : u8, field2 : u16, } impl Clone for S
struct S { field1: u8, field2: u16, } impl Clone for S
{
fn clone() -> S
{
panic! () ;
panic!();

}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/pretty/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

#![feature(decl_macro)]

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

fn main() {}
14 changes: 7 additions & 7 deletions src/test/pretty/macro_rules.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// pp-exact

macro_rules! brace { () => {} ; }
macro_rules! brace { () => {}; }

macro_rules! bracket[() => {} ;];
macro_rules! bracket[() => {};];

macro_rules! paren(() => {} ;);
macro_rules! paren(() => {};);

macro_rules! matcher_brackets {
(paren) => {} ; (bracket) => {} ; (brace) => {} ;
(paren) => {}; (bracket) => {}; (brace) => {};
}

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

fn main() {}
2 changes: 1 addition & 1 deletion src/test/pretty/stmt_expr_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn _8() {
}

fn _9() {
macro_rules! stmt_mac { () => { let _ = () ; } }
macro_rules! stmt_mac { () => { let _ = (); } }

#[rustc_dummy]
stmt_mac!();
Expand Down
2 changes: 1 addition & 1 deletion src/test/run-make/rustc-macro-dep-files/foo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use proc_macro::TokenStream;
#[proc_macro_derive(A)]
pub fn derive(input: TokenStream) -> TokenStream {
let input = input.to_string();
assert!(input.contains("struct A ;"));
assert!(input.contains("struct A;"));
"struct B;".parse().unwrap()
}
6 changes: 3 additions & 3 deletions src/test/ui/async-await/issues/issue-60674.stdout
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
async fn f(mut x : u8) {}
async fn g((mut x, y, mut z) : (u8, u8, u8)) {}
async fn g(mut x : u8, (a, mut b, c) : (u8, u8, u8), y : u8) {}
async fn f(mut x: u8) {}
async fn g((mut x, y, mut z): (u8, u8, u8)) {}
async fn g(mut x: u8, (a, mut b, c): (u8, u8, u8), y: u8) {}
2 changes: 1 addition & 1 deletion src/test/ui/hygiene/unpretty-debug.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![feature /* 0#0 */(no_core)]
#![no_core /* 0#0 */]

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

fn bar /* 0#0 */() {
let x /* 0#0 */ = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/macros/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ fn test_item() {
() => {};
}
),
"macro_rules! stringify { () => {} ; }", // FIXME
"macro_rules! stringify { () => {}; }", // FIXME
);
assert_eq!(
stringify_item!(
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/macros/trace-macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ LL | println!("Hello, World!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: expanding `println! { "Hello, World!" }`
= note: to `{ $crate :: io :: _print($crate :: format_args_nl! ("Hello, World!")) ; }`
= note: to `{ $crate::io::_print($crate::format_args_nl!("Hello, World!")); }`

14 changes: 7 additions & 7 deletions src/test/ui/macros/trace_faulty_macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LL | my_faulty_macro!();
| ^^^^^^^^^^^^^^^^^^
|
= note: expanding `my_faulty_macro! { }`
= note: to `my_faulty_macro! (bcd) ;`
= note: to `my_faulty_macro!(bcd);`
= note: expanding `my_faulty_macro! { bcd }`

error: recursion limit reached while expanding `my_recursive_macro!`
Expand All @@ -41,13 +41,13 @@ LL | my_recursive_macro!();
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: expanding `my_recursive_macro! { }`
= note: to `my_recursive_macro! () ;`
= note: to `my_recursive_macro!();`
= note: expanding `my_recursive_macro! { }`
= note: to `my_recursive_macro! () ;`
= note: to `my_recursive_macro!();`
= note: expanding `my_recursive_macro! { }`
= note: to `my_recursive_macro! () ;`
= note: to `my_recursive_macro!();`
= note: expanding `my_recursive_macro! { }`
= note: to `my_recursive_macro! () ;`
= note: to `my_recursive_macro!();`

error: expected expression, found `A { a: a, b: 0, c: _, .. }`
--> $DIR/trace_faulty_macros.rs:16:9
Expand Down Expand Up @@ -75,8 +75,8 @@ LL | let a = pat_macro!();
| ^^^^^^^^^^^^
|
= note: expanding `pat_macro! { }`
= note: to `pat_macro! (A { a : a, b : 0, c : _, .. }) ;`
= note: expanding `pat_macro! { A { a : a, b : 0, c : _, .. } }`
= note: to `pat_macro!(A { a: a, b: 0, c: _, .. });`
= note: expanding `pat_macro! { A { a: a, b: 0, c: _, .. } }`
= note: to `A { a: a, b: 0, c: _, .. }`

error: aborting due to 4 previous errors
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/proc-macro/allowed-attr-stmt-expr.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PRINT-ATTR INPUT (DISPLAY): struct ItemWithSemi ;
PRINT-ATTR INPUT (DISPLAY): struct ItemWithSemi;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -45,7 +45,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/allowed-attr-stmt-expr.rs:53:27: 53:29 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!" ;
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!";
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down Expand Up @@ -87,7 +87,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/allowed-attr-stmt-expr.rs:57:33: 57:34 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string) ;
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro!("{}", string);
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down Expand Up @@ -140,7 +140,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/allowed-attr-stmt-expr.rs:61:28: 61:29 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {}) ;
PRINT-ATTR INPUT (DISPLAY): second_make_stmt!(#[allow(dead_code)] struct Bar {});
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "second_make_stmt",
Expand Down Expand Up @@ -288,7 +288,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/allowed-attr-stmt-expr.rs:68:18: 68:20 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct NonBracedStruct ;
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct NonBracedStruct;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/attr-complex-fn.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PRINT-ATTR INPUT (DISPLAY): fn foo < T : MyTrait < MyStruct < { true } >> > () {}
PRINT-ATTR INPUT (DISPLAY): fn foo < T: MyTrait < MyStruct < { true } >> > () {}
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "fn",
Expand Down Expand Up @@ -76,7 +76,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/attr-complex-fn.rs:19:42: 19:44 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): impl < T > MyTrait < T > for MyStruct < { true } > { #! [rustc_dummy] }
PRINT-ATTR INPUT (DISPLAY): impl < T > MyTrait < T > for MyStruct < { true } > { #![rustc_dummy] }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "impl",
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/proc-macro/attr-stmt-expr.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/attr-stmt-expr.rs:45:27: 45:29 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!" ;
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!";
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down Expand Up @@ -71,7 +71,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/attr-stmt-expr.rs:49:33: 49:34 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string) ;
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro!("{}", string);
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down Expand Up @@ -124,7 +124,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
span: $DIR/attr-stmt-expr.rs:53:28: 53:29 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {}) ;
PRINT-ATTR INPUT (DISPLAY): second_make_stmt!(#[allow(dead_code)] struct Bar {});
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "second_make_stmt",
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/attribute-after-derive.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PRINT-ATTR INPUT (DISPLAY): #[derive(Print)] struct AttributeDerive { #[cfg(FALSE)] field : u8, }
PRINT-ATTR INPUT (DISPLAY): #[derive(Print)] struct AttributeDerive { #[cfg(FALSE)] field: u8, }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down Expand Up @@ -130,7 +130,7 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
span: $DIR/attribute-after-derive.rs:23:24: 26:2 (#0),
},
]
PRINT-ATTR INPUT (DISPLAY): struct DeriveAttribute { #[cfg(FALSE)] field : u8, }
PRINT-ATTR INPUT (DISPLAY): struct DeriveAttribute { #[cfg(FALSE)] field: u8, }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/attribute-spans-preserved.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fn main() { let y : u32 = "z" ; { let x : u32 = "y" ; } }
fn main() { let y: u32 = "z"; { let x: u32 = "y"; } }
6 changes: 3 additions & 3 deletions src/test/ui/proc-macro/auxiliary/attr-stmt-expr-rpass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use proc_macro::TokenStream;
#[proc_macro_attribute]
pub fn expect_let(attr: TokenStream, item: TokenStream) -> TokenStream {
assert!(attr.to_string().is_empty());
assert_eq!(item.to_string(), "let string = \"Hello, world!\" ;");
assert_eq!(item.to_string(), "let string = \"Hello, world!\";");
item
}

#[proc_macro_attribute]
pub fn expect_print_stmt(attr: TokenStream, item: TokenStream) -> TokenStream {
assert!(attr.to_string().is_empty());
assert_eq!(item.to_string(), "println! (\"{}\", string) ;");
assert_eq!(item.to_string(), "println!(\"{}\", string);");
item
}

Expand All @@ -31,7 +31,7 @@ pub fn expect_expr(attr: TokenStream, item: TokenStream) -> TokenStream {
#[proc_macro_attribute]
pub fn expect_print_expr(attr: TokenStream, item: TokenStream) -> TokenStream {
assert!(attr.to_string().is_empty());
assert_eq!(item.to_string(), "println! (\"{}\", string)");
assert_eq!(item.to_string(), "println!(\"{}\", string)");
item
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/proc-macro/auxiliary/attr-stmt-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use proc_macro::TokenStream;
#[proc_macro_attribute]
pub fn expect_let(attr: TokenStream, item: TokenStream) -> TokenStream {
assert!(attr.to_string().is_empty());
assert_eq!(item.to_string(), "let string = \"Hello, world!\" ;");
assert_eq!(item.to_string(), "let string = \"Hello, world!\";");
item
}

#[proc_macro_attribute]
pub fn expect_my_macro_stmt(attr: TokenStream, item: TokenStream) -> TokenStream {
assert!(attr.to_string().is_empty());
assert_eq!(item.to_string(), "my_macro! (\"{}\", string) ;");
assert_eq!(item.to_string(), "my_macro!(\"{}\", string);");
item
}

Expand All @@ -31,7 +31,7 @@ pub fn expect_expr(attr: TokenStream, item: TokenStream) -> TokenStream {
#[proc_macro_attribute]
pub fn expect_my_macro_expr(attr: TokenStream, item: TokenStream) -> TokenStream {
assert!(attr.to_string().is_empty());
assert_eq!(item.to_string(), "my_macro! (\"{}\", string)");
assert_eq!(item.to_string(), "my_macro!(\"{}\", string)");
item
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/auxiliary/derive-a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ use proc_macro::TokenStream;
#[proc_macro_derive(A)]
pub fn derive(input: TokenStream) -> TokenStream {
let input = input.to_string();
assert!(input.contains("struct A ;"));
assert!(input.contains("struct A;"));
"".parse().unwrap()
}
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/auxiliary/derive-atob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ use proc_macro::TokenStream;
#[proc_macro_derive(AToB)]
pub fn derive(input: TokenStream) -> TokenStream {
let input = input.to_string();
assert_eq!(input, "struct A ;");
assert_eq!(input, "struct A;");
"struct B;".parse().unwrap()
}
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/auxiliary/derive-ctod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ use proc_macro::TokenStream;
#[proc_macro_derive(CToD)]
pub fn derive(input: TokenStream) -> TokenStream {
let input = input.to_string();
assert_eq!(input, "struct C ;");
assert_eq!(input, "struct C;");
"struct D;".parse().unwrap()
}
4 changes: 2 additions & 2 deletions src/test/ui/proc-macro/auxiliary/derive-same-struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use proc_macro::TokenStream;
#[proc_macro_derive(AToB)]
pub fn derive1(input: TokenStream) -> TokenStream {
println!("input1: {:?}", input.to_string());
assert_eq!(input.to_string(), "struct A ;");
assert_eq!(input.to_string(), "struct A;");
"#[derive(BToC)] struct B;".parse().unwrap()
}

#[proc_macro_derive(BToC)]
pub fn derive2(input: TokenStream) -> TokenStream {
assert_eq!(input.to_string(), "struct B ;");
assert_eq!(input.to_string(), "struct B;");
"struct C;".parse().unwrap()
}
2 changes: 1 addition & 1 deletion src/test/ui/proc-macro/auxiliary/derive-union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn derive(input: TokenStream) -> TokenStream {
let input = input.to_string();
assert!(input.contains("#[repr(C)]"));
assert!(input.contains("union Test {"));
assert!(input.contains("a : u8,"));
assert!(input.contains("a: u8,"));
assert!(input.contains("}"));
"".parse().unwrap()
}
Loading

0 comments on commit 20d4424

Please sign in to comment.