@@ -49,6 +49,38 @@ pub(crate) struct FixupContext {
4949 /// No parentheses required.
5050 leftmost_subexpression_in_stmt : bool ,
5151
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+
5284 /// This is the difference between:
5385 ///
5486 /// ```ignore (illustrative)
@@ -68,6 +100,8 @@ impl Default for FixupContext {
68100 FixupContext {
69101 stmt : false ,
70102 leftmost_subexpression_in_stmt : false ,
103+ match_arm : false ,
104+ leftmost_subexpression_in_match_arm : false ,
71105 parenthesize_exterior_struct_lit : false ,
72106 }
73107 }
@@ -83,6 +117,10 @@ impl FixupContext {
83117 FixupContext { stmt : true , ..FixupContext :: default ( ) }
84118 }
85119
120+ pub fn new_match_arm ( ) -> Self {
121+ FixupContext { match_arm : true , ..FixupContext :: default ( ) }
122+ }
123+
86124 /// Create the initial fixup for printing an expression as the "condition"
87125 /// of an `if` or `while`. There are a few other positions which are
88126 /// grammatically equivalent and also use this, such as the iterator
@@ -106,6 +144,9 @@ impl FixupContext {
106144 FixupContext {
107145 stmt : false ,
108146 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 ,
109150 ..self
110151 }
111152 }
@@ -119,7 +160,13 @@ impl FixupContext {
119160 /// example the `$b` in `$a + $b` and `-$b`, but not the one in `[$b]` or
120161 /// `$a.f($b)`.
121162 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+ }
123170 }
124171
125172 /// Determine whether parentheses are needed around the given expression to
@@ -128,7 +175,9 @@ impl FixupContext {
128175 /// The documentation on `FixupContext::leftmost_subexpression_in_stmt` has
129176 /// examples.
130177 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) )
132181 }
133182
134183 /// Determine whether parentheses are needed around the given `let`
0 commit comments