about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2024-01-22 15:58:36 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2024-01-22 20:19:17 +1100
commit1fbabeeb2e0e737301f6527ec3a22f1fa2f87676 (patch)
treec995acc17570741b2516df07776a2a29cefafda3
parent41e4a3e08649ed2a41cf60c6104795e0971cee67 (diff)
Fix some cases in `space_between`.
There are a number of cases where we erroneously omit the space between
two tokens, all involving an exception to a more general case. The
affected tokens are `$`, `!`, `.`, `,`, and `let` followed by a
parenthesis.

This fixes a lot of FIXME comments.
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/state.rs46
-rw-r--r--tests/ui/macros/stringify.rs33
-rw-r--r--tests/ui/proc-macro/issue-76182-leading-vert-pat.stdout2
3 files changed, 44 insertions, 37 deletions
diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs
index f7d8fee465d..a6f6f0b29a0 100644
--- a/compiler/rustc_ast_pretty/src/pprust/state.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/state.rs
@@ -171,27 +171,35 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
         // 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,
+        // `.` + NON-PUNCT: `x.y`, `tup.0`
+        (Tok(Token { kind: Dot, .. }, _), tt2) if !is_punct(tt2) => false,
 
-        // NON-PUNCT + `;`: `x = 3;`, `[T; 3]`
-        (tt1, Tok(Token { kind: Semi, .. }, _)) if !is_punct(tt1) => false,
+        // `$` + IDENT: `$e`
+        (Tok(Token { kind: Dollar, .. }, _), Tok(Token { kind: Ident(..), .. }, _)) => false,
 
-        // IDENT + `(`: `f(3)`
-        //
-        // FIXME: Incorrect cases:
-        // - Let: `let(a, b) = (1, 2)`
-        (Tok(Token { kind: Ident(..), .. }, _), Del(_, _, Parenthesis, _)) => 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,
diff --git a/tests/ui/macros/stringify.rs b/tests/ui/macros/stringify.rs
index 192e6e0cc98..8cef833f48d 100644
--- a/tests/ui/macros/stringify.rs
+++ b/tests/ui/macros/stringify.rs
@@ -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");
@@ -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,
@@ -212,7 +211,7 @@ fn test_expr() {
     c2_match_arm!(
         [ { 1 } - 1 ],
         "match () { _ => ({ 1 }) - 1, }",
-        "match() { _ => { 1 } - 1 }",
+        "match () { _ => { 1 } - 1 }",
     );
 
     // ExprKind::Closure
@@ -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 $(,)?) => {
@@ -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
@@ -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!([ :#!@|$=&*,+;*~? ], ":#!@|$=&*,+;*~?");
 }
diff --git a/tests/ui/proc-macro/issue-76182-leading-vert-pat.stdout b/tests/ui/proc-macro/issue-76182-leading-vert-pat.stdout
index 09eb33f7e31..fdbf9118cd3 100644
--- a/tests/ui/proc-macro/issue-76182-leading-vert-pat.stdout
+++ b/tests/ui/proc-macro/issue-76182-leading-vert-pat.stdout
@@ -1,4 +1,4 @@
-PRINT-ATTR INPUT (DISPLAY): fn main() { match() { | () => () } }
+PRINT-ATTR INPUT (DISPLAY): fn main() { match () { | () => () } }
 PRINT-ATTR INPUT (DEBUG): TokenStream [
     Ident {
         ident: "fn",