Skip to content

Commit 527e2ea

Browse files
committedDec 19, 2023
Test parenthesization of leftmost subexprs containing stmt boundaries
1 parent e999d8b commit 527e2ea

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
 

‎tests/ui/macros/stringify.rs

+66
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ fn test_expr() {
204204
} ],
205205
"match self { Ok => 1, Err => 0, }"
206206
);
207+
macro_rules! c2_match_arm {
208+
([ $expr:expr ], $expr_expected:expr, $tokens_expected:expr $(,)?) => {
209+
c2!(expr, [ match () { _ => $expr } ], $expr_expected, $tokens_expected);
210+
};
211+
}
212+
c2_match_arm!(
213+
[ { 1 } - 1 ],
214+
// FIXME(dtolnay): this is invalid syntax, needs parens.
215+
"match () { _ => { 1 } - 1, }",
216+
"match() { _ => { 1 } - 1 }",
217+
);
207218

208219
// ExprKind::Closure
209220
c1!(expr, [ || {} ], "|| {}");
@@ -651,6 +662,17 @@ fn test_stmt() {
651662
"let (a, b): (u32, u32) = (1, 2);",
652663
"let(a, b): (u32, u32) = (1, 2)" // FIXME
653664
);
665+
macro_rules! c2_let_expr_minus_one {
666+
([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
667+
c2!(stmt, [ let _ = $expr - 1 ], $stmt_expected, $tokens_expected);
668+
};
669+
}
670+
c2_let_expr_minus_one!(
671+
[ match void {} ],
672+
// FIXME(dtolnay): no parens needed.
673+
"let _ = (match void {}) - 1;",
674+
"let _ = match void {} - 1",
675+
);
654676

655677
// StmtKind::Item
656678
c1!(stmt, [ struct S; ], "struct S;");
@@ -661,6 +683,50 @@ fn test_stmt() {
661683

662684
// StmtKind::Semi
663685
c2!(stmt, [ 1 + 1 ], "1 + 1;", "1 + 1");
686+
macro_rules! c2_expr_as_stmt {
687+
// Parse as expr, then reparse as stmt.
688+
//
689+
// The c2_minus_one macro below can't directly call `c2!(stmt, ...)`
690+
// because `$expr - 1` cannot be parsed directly as a stmt. A statement
691+
// boundary occurs after the `match void {}`, after which the `-` token
692+
// hits "no rules expected this token in macro call".
693+
//
694+
// The unwanted statement boundary is exactly why the pretty-printer is
695+
// injecting parentheses around the subexpression, which is the behavior
696+
// we are interested in testing.
697+
([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
698+
c2!(stmt, [ $expr ], $stmt_expected, $tokens_expected);
699+
};
700+
}
701+
macro_rules! c2_minus_one {
702+
([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
703+
c2_expr_as_stmt!([ $expr - 1 ], $stmt_expected, $tokens_expected);
704+
};
705+
}
706+
c2_minus_one!(
707+
[ match void {} ],
708+
"(match void {}) - 1;",
709+
// FIXME(dtolnay): no parens expected.
710+
"(match void {}) - 1",
711+
);
712+
c2_minus_one!(
713+
[ match void {}() ],
714+
// FIXME(dtolnay): needs parens around match.
715+
"match void {}() - 1;",
716+
"match void {}() - 1",
717+
);
718+
c2_minus_one!(
719+
[ match void {}[0] ],
720+
// FIXME(dtolnay): needs parens around match.
721+
"match void {}[0] - 1;",
722+
"match void {}[0] - 1",
723+
);
724+
c2_minus_one!(
725+
[ loop { break 1; } ],
726+
// FIXME(dtolnay): needs parens around loop.
727+
"loop { break 1; } - 1;",
728+
"loop { break 1; } - 1",
729+
);
664730

665731
// StmtKind::Empty
666732
c1!(stmt, [ ; ], ";");

0 commit comments

Comments
 (0)
Please sign in to comment.