@@ -204,6 +204,17 @@ fn test_expr() {
204
204
} ] ,
205
205
"match self { Ok => 1, Err => 0, }"
206
206
) ;
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
+ ) ;
207
218
208
219
// ExprKind::Closure
209
220
c1 ! ( expr, [ || { } ] , "|| {}" ) ;
@@ -651,6 +662,17 @@ fn test_stmt() {
651
662
"let (a, b): (u32, u32) = (1, 2);" ,
652
663
"let(a, b): (u32, u32) = (1, 2)" // FIXME
653
664
) ;
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
+ ) ;
654
676
655
677
// StmtKind::Item
656
678
c1 ! ( stmt, [ struct S ; ] , "struct S;" ) ;
@@ -661,6 +683,50 @@ fn test_stmt() {
661
683
662
684
// StmtKind::Semi
663
685
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
+ ) ;
664
730
665
731
// StmtKind::Empty
666
732
c1 ! ( stmt, [ ; ] , ";" ) ;
0 commit comments