@@ -49,6 +49,38 @@ pub(crate) struct FixupContext {
49
49
/// No parentheses required.
50
50
leftmost_subexpression_in_stmt : bool ,
51
51
52
+ /// Print expression such that it can be parsed as a match arm.
53
+ ///
54
+ /// This is almost equivalent to `stmt`, but the grammar diverges a tiny bit
55
+ /// between statements and match arms when it comes to braced macro calls.
56
+ /// Macro calls with brace delimiter terminate a statement without a
57
+ /// semicolon, but do not terminate a match-arm without comma.
58
+ ///
59
+ /// ```ignore (illustrative)
60
+ /// m! {} - 1; // two statements: a macro call followed by -1 literal
61
+ ///
62
+ /// match () {
63
+ /// _ => m! {} - 1, // binary subtraction operator
64
+ /// }
65
+ /// ```
66
+ match_arm : bool ,
67
+
68
+ /// This is almost equivalent to `leftmost_subexpression_in_stmt`, other
69
+ /// than for braced macro calls.
70
+ ///
71
+ /// If we have `m! {} - 1` as an expression, the leftmost subexpression
72
+ /// `m! {}` will need to be parenthesized in the statement case but not the
73
+ /// match-arm case.
74
+ ///
75
+ /// ```ignore (illustrative)
76
+ /// (m! {}) - 1; // subexpression needs parens
77
+ ///
78
+ /// match () {
79
+ /// _ => m! {} - 1, // no parens
80
+ /// }
81
+ /// ```
82
+ leftmost_subexpression_in_match_arm : bool ,
83
+
52
84
/// This is the difference between:
53
85
///
54
86
/// ```ignore (illustrative)
@@ -68,6 +100,8 @@ impl Default for FixupContext {
68
100
FixupContext {
69
101
stmt : false ,
70
102
leftmost_subexpression_in_stmt : false ,
103
+ match_arm : false ,
104
+ leftmost_subexpression_in_match_arm : false ,
71
105
parenthesize_exterior_struct_lit : false ,
72
106
}
73
107
}
@@ -83,6 +117,10 @@ impl FixupContext {
83
117
FixupContext { stmt : true , ..FixupContext :: default ( ) }
84
118
}
85
119
120
+ pub fn new_match_arm ( ) -> Self {
121
+ FixupContext { match_arm : true , ..FixupContext :: default ( ) }
122
+ }
123
+
86
124
/// Create the initial fixup for printing an expression as the "condition"
87
125
/// of an `if` or `while`. There are a few other positions which are
88
126
/// grammatically equivalent and also use this, such as the iterator
@@ -106,6 +144,9 @@ impl FixupContext {
106
144
FixupContext {
107
145
stmt : false ,
108
146
leftmost_subexpression_in_stmt : self . stmt || self . leftmost_subexpression_in_stmt ,
147
+ match_arm : false ,
148
+ leftmost_subexpression_in_match_arm : self . match_arm
149
+ || self . leftmost_subexpression_in_match_arm ,
109
150
..self
110
151
}
111
152
}
@@ -119,7 +160,13 @@ impl FixupContext {
119
160
/// example the `$b` in `$a + $b` and `-$b`, but not the one in `[$b]` or
120
161
/// `$a.f($b)`.
121
162
pub fn subsequent_subexpression ( self ) -> Self {
122
- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..self }
163
+ FixupContext {
164
+ stmt : false ,
165
+ leftmost_subexpression_in_stmt : false ,
166
+ match_arm : false ,
167
+ leftmost_subexpression_in_match_arm : false ,
168
+ ..self
169
+ }
123
170
}
124
171
125
172
/// Determine whether parentheses are needed around the given expression to
@@ -128,7 +175,9 @@ impl FixupContext {
128
175
/// The documentation on `FixupContext::leftmost_subexpression_in_stmt` has
129
176
/// examples.
130
177
pub fn would_cause_statement_boundary ( self , expr : & Expr ) -> bool {
131
- self . leftmost_subexpression_in_stmt && !classify:: expr_requires_semi_to_be_stmt ( expr)
178
+ ( self . leftmost_subexpression_in_stmt && !classify:: expr_requires_semi_to_be_stmt ( expr) )
179
+ || ( self . leftmost_subexpression_in_match_arm
180
+ && !classify:: expr_requires_comma_to_be_match_arm ( expr) )
132
181
}
133
182
134
183
/// Determine whether parentheses are needed around the given `let`
0 commit comments