Skip to content

Commit

Permalink
Auto merge of #120227 - nnethercote:further-improve-space_between, r=…
Browse files Browse the repository at this point in the history
…<try>

Further improve `space_between`

`space_between` is used by `print_tts` to decide when spaces should be put between  tokens. This PR improves it in two ways:
- avoid unnecessary spaces before semicolons, and
- don't omit some necessary spaces before/after some punctuation symbols.

r? `@petrochenkov`
  • Loading branch information
bors committed Jan 22, 2024
2 parents 3066253 + 1fbabee commit ad2e388
Show file tree
Hide file tree
Showing 28 changed files with 102 additions and 109 deletions.
51 changes: 33 additions & 18 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,31 +160,46 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
use TokenTree::Delimited as Del;
use TokenTree::Token as Tok;

fn is_punct(tt: &TokenTree) -> bool {
matches!(tt, TokenTree::Token(tok, _) if tok.is_punct())
}

// Each match arm has one or more examples in comments. The default is to
// insert space between adjacent tokens, except for the cases listed in
// this match.
match (tt1, tt2) {
// No space after line doc comments.
(Tok(Token { kind: DocComment(CommentKind::Line, ..), .. }, _), _) => false,

// `.` + ANYTHING: `x.y`, `tup.0`
// `$` + ANYTHING: `$e`
(Tok(Token { kind: Dot | Dollar, .. }, _), _) => false,

// ANYTHING + `,`: `foo,`
// ANYTHING + `.`: `x.y`, `tup.0`
// ANYTHING + `!`: `foo! { ... }`
//
// FIXME: Incorrect cases:
// - Logical not: `x =! y`, `if! x { f(); }`
// - Never type: `Fn() ->!`
(_, Tok(Token { kind: Comma | Dot | Not, .. }, _)) => false,

// IDENT + `(`: `f(3)`
//
// FIXME: Incorrect cases:
// - Let: `let(a, b) = (1, 2)`
(Tok(Token { kind: Ident(..), .. }, _), Del(_, _, Parenthesis, _)) => false,
// `.` + NON-PUNCT: `x.y`, `tup.0`
(Tok(Token { kind: Dot, .. }, _), tt2) if !is_punct(tt2) => false,

// `$` + IDENT: `$e`
(Tok(Token { kind: Dollar, .. }, _), Tok(Token { kind: Ident(..), .. }, _)) => false,

// NON-PUNCT + `,`: `foo,`
// NON-PUNCT + `;`: `x = 3;`, `[T; 3]`
// NON-PUNCT + `.`: `x.y`, `tup.0`
(tt1, Tok(Token { kind: Comma | Semi | Dot, .. }, _)) if !is_punct(tt1) => false,

// IDENT + `!`: `println!()`, but `if !x { ... }` needs a space after the `if`
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Tok(Token { kind: Not, .. }, _))
if !Ident::new(*sym, *span).is_reserved() || *is_raw =>
{
false
}

// IDENT|`fn`|`Self`|`pub` + `(`: `f(3)`, `fn(x: u8)`, `Self()`, `pub(crate)`,
// but `let (a, b) = (1, 2)` needs a space after the `let`
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Del(_, _, Parenthesis, _))
if !Ident::new(*sym, *span).is_reserved()
|| *sym == kw::Fn
|| *sym == kw::SelfUpper
|| *sym == kw::Pub
|| *is_raw =>
{
false
}

// `#` + `[`: `#[attr]`
(Tok(Token { kind: Pound, .. }, _), Del(_, _, Bracket, _)) => false,
Expand Down
2 changes: 1 addition & 1 deletion tests/pretty/delimited-token-groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mac! {
{
fn clone() -> S
{
panic! () ;
panic! ();

}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/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) => {} ;
$vis : vis) => {};
}

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

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

#[rustc_dummy]
stmt_mac!();
Expand Down
33 changes: 16 additions & 17 deletions tests/ui/macros/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ fn test_expr() {
c1!(expr, [ true || false ], "true || false");
c1!(expr, [ true || false && false ], "true || false && false");
c1!(expr, [ a < 1 && 2 < b && c > 3 && 4 > d ], "a < 1 && 2 < b && c > 3 && 4 > d");
c2!(expr, [ a & b & !c ], "a & b & !c", "a & b &!c"); // FIXME
c1!(expr, [ a & b & !c ], "a & b & !c");
c1!(expr, [ a + b * c - d + -1 * -2 - -3], "a + b * c - d + -1 * -2 - -3");
c2!(expr, [ x = !y ], "x = !y", "x =!y"); // FIXME
c1!(expr, [ x = !y ], "x = !y");

// ExprKind::Unary
c1!(expr, [ *expr ], "*expr");
Expand Down Expand Up @@ -141,15 +141,14 @@ fn test_expr() {
"if let _ = (true && false) {}",
"if let _ = true && false {}",
);
c2!(expr,
c1!(expr,
[ match () { _ if let _ = Struct {} => {} } ],
"match () { _ if let _ = Struct {} => {} }",
"match() { _ if let _ = Struct {} => {} }",
"match () { _ if let _ = Struct {} => {} }"
);

// ExprKind::If
c1!(expr, [ if true {} ], "if true {}");
c2!(expr, [ if !true {} ], "if !true {}", "if!true {}"); // FIXME
c1!(expr, [ if !true {} ], "if !true {}");
c1!(expr, [ if ::std::blah() { } else { } ], "if ::std::blah() {} else {}");
c1!(expr, [ if let true = true {} else {} ], "if let true = true {} else {}");
c1!(expr,
Expand Down Expand Up @@ -212,7 +211,7 @@ fn test_expr() {
c2_match_arm!(
[ { 1 } - 1 ],
"match () { _ => ({ 1 }) - 1, }",
"match() { _ => { 1 } - 1 }",
"match () { _ => { 1 } - 1 }",
);

// ExprKind::Closure
Expand Down Expand Up @@ -655,11 +654,11 @@ fn test_stmt() {
c2!(stmt, [ let _ ], "let _;", "let _");
c2!(stmt, [ let x = true ], "let x = true;", "let x = true");
c2!(stmt, [ let x: bool = true ], "let x: bool = true;", "let x: bool = true");
c2!(stmt, [ let (a, b) = (1, 2) ], "let (a, b) = (1, 2);", "let(a, b) = (1, 2)"); // FIXME
c2!(stmt, [ let (a, b) = (1, 2) ], "let (a, b) = (1, 2);", "let (a, b) = (1, 2)");
c2!(stmt,
[ let (a, b): (u32, u32) = (1, 2) ],
"let (a, b): (u32, u32) = (1, 2);",
"let(a, b): (u32, u32) = (1, 2)" // FIXME
"let (a, b): (u32, u32) = (1, 2)"
);
macro_rules! c2_let_expr_minus_one {
([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
Expand Down Expand Up @@ -776,8 +775,8 @@ fn test_ty() {
c1!(ty, [ Ref<'a> ], "Ref<'a>");
c1!(ty, [ PhantomData<T> ], "PhantomData<T>");
c2!(ty, [ PhantomData::<T> ], "PhantomData<T>", "PhantomData::<T>");
c2!(ty, [ Fn() -> ! ], "Fn() -> !", "Fn() ->!");
c2!(ty, [ Fn(u8) -> ! ], "Fn(u8) -> !", "Fn(u8) ->!"); // FIXME
c1!(ty, [ Fn() -> ! ], "Fn() -> !");
c1!(ty, [ Fn(u8) -> ! ], "Fn(u8) -> !");
c1!(ty, [ <Struct as Trait>::Type ], "<Struct as Trait>::Type");

// TyKind::TraitObject
Expand Down Expand Up @@ -857,16 +856,16 @@ fn test_punct() {
// Otherwise, any old proc macro that parses pretty-printed code might glue
// together tokens that shouldn't be glued.
p!([ = = < < <= <= == == != != >= >= > > ], "= = < < <= <= == == != != >= >= > >");
p!([ && && & & || || | | ! ! ], "&& && & & || || | |!!"); // FIXME
p!([ && && & & || || | | ! ! ], "&& && & & || || | | ! !");
p!([ ~ ~ @ @ # # ], "~ ~ @ @ # #");
p!([ . . .. .. ... ... ..= ..=], ".... .. ... ... ..= ..="); // FIXME
p!([ , , ; ; : : :: :: ], ",, ; ; : : :: ::"); // FIXME
p!([ . . .. .. ... ... ..= ..=], ". . .. .. ... ... ..= ..=");
p!([ , , ; ; : : :: :: ], ", , ; ; : : :: ::");
p!([ -> -> <- <- => =>], "-> -> <- <- => =>");
p!([ $ $ ? ? ' ' ], "$$? ? ' '"); // FIXME
p!([ $ $ ? ? ' ' ], "$ $ ? ? ' '");
p!([ + + += += - - -= -= * * *= *= / / /= /= ], "+ + += += - - -= -= * * *= *= / / /= /=");
p!([ % % %= %= ^ ^ ^= ^= << << <<= <<= >> >> >>= >>= ],
"% % %= %= ^ ^ ^= ^= << << <<= <<= >> >> >>= >>=");
p!([ +! ?= |> >>@ --> <-- $$ =====> ], "+! ?= |> >>@ --> <-- $$=====>");
p!([ ,; ;, ** @@ $+$ >< <> ?? +== ], ",; ;, ** @@ $+$>< <> ?? +=="); // FIXME: `$ >` -> `$>`
p!([ +! ?= |> >>@ --> <-- $$ =====> ], "+! ?= |> >>@ --> <-- $$ =====>");
p!([ ,; ;, ** @@ $+$ >< <> ?? +== ], ",; ;, ** @@ $+$ >< <> ?? +==");
p!([ :#!@|$=&*,+;*~? ], ":#!@|$=&*,+;*~?");
}
2 changes: 1 addition & 1 deletion tests/ui/macros/trace_faulty_macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ LL | test!(let x = 1+1);
= note: expanding `test! { let x = 1+1 }`
= note: to `test! ((x, 1 + 1))`
= note: expanding `test! { (x, 1 + 1) }`
= note: to `let x = 1 + 1 ;`
= note: to `let x = 1 + 1;`

error: aborting due to 5 previous errors

Expand Down
7 changes: 2 additions & 5 deletions tests/ui/proc-macro/allowed-attr-stmt-expr.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
PRINT-ATTR INPUT (DISPLAY): struct ItemWithSemi;
PRINT-ATTR RE-COLLECTED (DISPLAY): struct ItemWithSemi ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -47,7 +46,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!";
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_let] let string = "Hello, world!" ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down Expand Up @@ -90,7 +88,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro!("{}", string);
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string);
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down Expand Up @@ -144,7 +142,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): second_make_stmt!(#[allow(dead_code)] struct Bar {});
PRINT-ATTR RE-COLLECTED (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {}) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {});
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "second_make_stmt",
Expand Down Expand Up @@ -293,7 +291,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): #[rustc_dummy] struct NonBracedStruct;
PRINT-ATTR RE-COLLECTED (DISPLAY): #[rustc_dummy] struct NonBracedStruct ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/proc-macro/attr-stmt-expr.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): #[expect_let] let string = "Hello, world!";
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_let] let string = "Hello, world!" ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down Expand Up @@ -73,7 +72,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): #[expect_my_macro_stmt] my_macro!("{}", string);
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): #[expect_my_macro_stmt] my_macro! ("{}", string);
PRINT-ATTR INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down Expand Up @@ -127,7 +126,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): second_make_stmt!(#[allow(dead_code)] struct Bar {});
PRINT-ATTR RE-COLLECTED (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {}) ;
PRINT-ATTR RE-COLLECTED (DISPLAY): second_make_stmt! (#[allow(dead_code)] struct Bar {});
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "second_make_stmt",
Expand Down
2 changes: 1 addition & 1 deletion tests/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"; } }
4 changes: 2 additions & 2 deletions tests/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 Down
4 changes: 2 additions & 2 deletions tests/ui/proc-macro/cfg-eval-inner.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ PRINT-ATTR RE-COLLECTED (DISPLAY): impl Foo <
{ field: [u8; { #![rustc_dummy(another_cursed_inner)] 1 }] } 0
}] > { #![rustc_dummy(evaluated_attr)] fn bar() {} }
PRINT-ATTR DEEP-RE-COLLECTED (DISPLAY): impl Foo <
[u8 ;
[u8;
{
#! [rustc_dummy(cursed_inner)] #! [allow(unused)] struct Inner
{ field : [u8 ; { #! [rustc_dummy(another_cursed_inner)] 1 }] } 0
{ field : [u8; { #! [rustc_dummy(another_cursed_inner)] 1 }] } 0
}] > { #! [rustc_dummy(evaluated_attr)] fn bar() {} }
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/proc-macro/doc-comment-preserved.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PRINT-BANG INPUT (DISPLAY): /**
*******
*/
pub struct S;
PRINT-BANG RE-COLLECTED (DISPLAY): #[doc = "\n*******\n* DOC *\n* DOC *\n* DOC *\n*******\n"] pub struct S ;
PRINT-BANG RE-COLLECTED (DISPLAY): #[doc = "\n*******\n* DOC *\n* DOC *\n* DOC *\n*******\n"] pub struct S;
PRINT-BANG INPUT (DEBUG): TokenStream [
Punct {
ch: '#',
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/proc-macro/dollar-crate-issue-57089.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
PRINT-BANG INPUT (DISPLAY): struct M($crate :: S);
PRINT-BANG RE-COLLECTED (DISPLAY): struct M($crate :: S) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -40,7 +39,6 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct A($crate :: S);
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A($crate :: S) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/proc-macro/dollar-crate-issue-62325.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
PRINT-ATTR INPUT (DISPLAY): struct A(identity! ($crate :: S));
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A(identity! ($crate :: S)) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -55,7 +54,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct B(identity! ($crate :: S));
PRINT-ATTR RE-COLLECTED (DISPLAY): struct B(identity! ($crate :: S)) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down
6 changes: 0 additions & 6 deletions tests/ui/proc-macro/dollar-crate.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
PRINT-BANG INPUT (DISPLAY): struct M($crate :: S);
PRINT-BANG RE-COLLECTED (DISPLAY): struct M($crate :: S) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -40,7 +39,6 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct A($crate :: S);
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A($crate :: S) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -81,7 +79,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-DERIVE INPUT (DISPLAY): struct D($crate :: S);
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D($crate :: S) ;
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -122,7 +119,6 @@ PRINT-DERIVE INPUT (DEBUG): TokenStream [
},
]
PRINT-BANG INPUT (DISPLAY): struct M($crate :: S);
PRINT-BANG RE-COLLECTED (DISPLAY): struct M($crate :: S) ;
PRINT-BANG INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -163,7 +159,6 @@ PRINT-BANG INPUT (DEBUG): TokenStream [
},
]
PRINT-ATTR INPUT (DISPLAY): struct A($crate :: S);
PRINT-ATTR RE-COLLECTED (DISPLAY): struct A($crate :: S) ;
PRINT-ATTR INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down Expand Up @@ -204,7 +199,6 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
},
]
PRINT-DERIVE INPUT (DISPLAY): struct D($crate :: S);
PRINT-DERIVE RE-COLLECTED (DISPLAY): struct D($crate :: S) ;
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "struct",
Expand Down
Loading

0 comments on commit ad2e388

Please sign in to comment.