@@ -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 }
@@ -76,13 +110,16 @@ impl Default for FixupContext {
76110impl FixupContext {
77111 /// Create the initial fixup for printing an expression in statement
78112 /// position.
79- ///
80- /// This is currently also used for printing an expression as a match-arm,
81- /// but this is incorrect and leads to over-parenthesizing.
82113 pub fn new_stmt ( ) -> Self {
83114 FixupContext { stmt : true , ..FixupContext :: default ( ) }
84115 }
85116
117+ /// Create the initial fixup for printing an expression as the right-hand
118+ /// side of a match arm.
119+ pub fn new_match_arm ( ) -> Self {
120+ FixupContext { match_arm : true , ..FixupContext :: default ( ) }
121+ }
122+
86123 /// Create the initial fixup for printing an expression as the "condition"
87124 /// of an `if` or `while`. There are a few other positions which are
88125 /// grammatically equivalent and also use this, such as the iterator
@@ -106,6 +143,9 @@ impl FixupContext {
106143 FixupContext {
107144 stmt : false ,
108145 leftmost_subexpression_in_stmt : self . stmt || self . leftmost_subexpression_in_stmt ,
146+ match_arm : false ,
147+ leftmost_subexpression_in_match_arm : self . match_arm
148+ || self . leftmost_subexpression_in_match_arm ,
109149 ..self
110150 }
111151 }
@@ -119,7 +159,13 @@ impl FixupContext {
119159 /// example the `$b` in `$a + $b` and `-$b`, but not the one in `[$b]` or
120160 /// `$a.f($b)`.
121161 pub fn subsequent_subexpression ( self ) -> Self {
122- FixupContext { stmt : false , leftmost_subexpression_in_stmt : false , ..self }
162+ FixupContext {
163+ stmt : false ,
164+ leftmost_subexpression_in_stmt : false ,
165+ match_arm : false ,
166+ leftmost_subexpression_in_match_arm : false ,
167+ ..self
168+ }
123169 }
124170
125171 /// Determine whether parentheses are needed around the given expression to
@@ -128,7 +174,8 @@ impl FixupContext {
128174 /// The documentation on `FixupContext::leftmost_subexpression_in_stmt` has
129175 /// examples.
130176 pub fn would_cause_statement_boundary ( self , expr : & Expr ) -> bool {
131- self . leftmost_subexpression_in_stmt && !classify:: expr_requires_semi_to_be_stmt ( expr)
177+ ( self . leftmost_subexpression_in_stmt && !classify:: expr_requires_semi_to_be_stmt ( expr) )
178+ || ( self . leftmost_subexpression_in_match_arm && classify:: expr_is_complete ( expr) )
132179 }
133180
134181 /// Determine whether parentheses are needed around the given `let`
0 commit comments