about summary refs log tree commit diff
path: root/tests/ui/macros/stringify.rs
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@gmail.com>2023-12-11 23:10:17 -0800
committerDavid Tolnay <dtolnay@gmail.com>2023-12-18 22:40:23 -0800
commit527e2eac17c5d3709e4e30e8b72ae33a6e8990b1 (patch)
tree61cccdf2c01a424206441db06f5a18763d879ca6 /tests/ui/macros/stringify.rs
parente999d8b6e137c0393470a2d846047ee86c177719 (diff)
downloadrust-527e2eac17c5d3709e4e30e8b72ae33a6e8990b1.tar.gz
rust-527e2eac17c5d3709e4e30e8b72ae33a6e8990b1.zip
Test parenthesization of leftmost subexprs containing stmt boundaries
Diffstat (limited to 'tests/ui/macros/stringify.rs')
-rw-r--r--tests/ui/macros/stringify.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/ui/macros/stringify.rs b/tests/ui/macros/stringify.rs
index 6fc12509aad..6cd64e3f430 100644
--- a/tests/ui/macros/stringify.rs
+++ b/tests/ui/macros/stringify.rs
@@ -204,6 +204,17 @@ fn test_expr() {
         } ],
         "match self { Ok => 1, Err => 0, }"
     );
+    macro_rules! c2_match_arm {
+        ([ $expr:expr ], $expr_expected:expr, $tokens_expected:expr $(,)?) => {
+            c2!(expr, [ match () { _ => $expr } ], $expr_expected, $tokens_expected);
+        };
+    }
+    c2_match_arm!(
+        [ { 1 } - 1 ],
+        // FIXME(dtolnay): this is invalid syntax, needs parens.
+        "match () { _ => { 1 } - 1, }",
+        "match() { _ => { 1 } - 1 }",
+    );
 
     // ExprKind::Closure
     c1!(expr, [ || {} ], "|| {}");
@@ -651,6 +662,17 @@ fn test_stmt() {
         "let (a, b): (u32, u32) = (1, 2);",
         "let(a, b): (u32, u32) = (1, 2)" // FIXME
     );
+    macro_rules! c2_let_expr_minus_one {
+        ([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
+            c2!(stmt, [ let _ = $expr - 1 ], $stmt_expected, $tokens_expected);
+        };
+    }
+    c2_let_expr_minus_one!(
+        [ match void {} ],
+        // FIXME(dtolnay): no parens needed.
+        "let _ = (match void {}) - 1;",
+        "let _ = match void {} - 1",
+    );
 
     // StmtKind::Item
     c1!(stmt, [ struct S; ], "struct S;");
@@ -661,6 +683,50 @@ fn test_stmt() {
 
     // StmtKind::Semi
     c2!(stmt, [ 1 + 1 ], "1 + 1;", "1 + 1");
+    macro_rules! c2_expr_as_stmt {
+        // Parse as expr, then reparse as stmt.
+        //
+        // The c2_minus_one macro below can't directly call `c2!(stmt, ...)`
+        // because `$expr - 1` cannot be parsed directly as a stmt. A statement
+        // boundary occurs after the `match void {}`, after which the `-` token
+        // hits "no rules expected this token in macro call".
+        //
+        // The unwanted statement boundary is exactly why the pretty-printer is
+        // injecting parentheses around the subexpression, which is the behavior
+        // we are interested in testing.
+        ([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
+            c2!(stmt, [ $expr ], $stmt_expected, $tokens_expected);
+        };
+    }
+    macro_rules! c2_minus_one {
+        ([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
+            c2_expr_as_stmt!([ $expr - 1 ], $stmt_expected, $tokens_expected);
+        };
+    }
+    c2_minus_one!(
+        [ match void {} ],
+        "(match void {}) - 1;",
+        // FIXME(dtolnay): no parens expected.
+        "(match void {}) - 1",
+    );
+    c2_minus_one!(
+        [ match void {}() ],
+        // FIXME(dtolnay): needs parens around match.
+        "match void {}() - 1;",
+        "match void {}() - 1",
+    );
+    c2_minus_one!(
+        [ match void {}[0] ],
+        // FIXME(dtolnay): needs parens around match.
+        "match void {}[0] - 1;",
+        "match void {}[0] - 1",
+    );
+    c2_minus_one!(
+        [ loop { break 1; } ],
+        // FIXME(dtolnay): needs parens around loop.
+        "loop { break 1; } - 1;",
+        "loop { break 1; } - 1",
+    );
 
     // StmtKind::Empty
     c1!(stmt, [ ; ], ";");