rustc_ast_pretty/pprust/state/
fixup.rs

1use rustc_ast::util::classify;
2use rustc_ast::util::parser::{self, ExprPrecedence};
3use rustc_ast::{Expr, ExprKind, YieldKind};
4
5// The default amount of fixing is minimal fixing, so all fixups are set to `false` by `Default`.
6// Fixups should be turned on in a targeted fashion where needed.
7#[derive(Copy, Clone, Debug, Default)]
8pub(crate) struct FixupContext {
9    /// Print expression such that it can be parsed back as a statement
10    /// consisting of the original expression.
11    ///
12    /// The effect of this is for binary operators in statement position to set
13    /// `leftmost_subexpression_in_stmt` when printing their left-hand operand.
14    ///
15    /// ```ignore (illustrative)
16    /// (match x {}) - 1;  // match needs parens when LHS of binary operator
17    ///
18    /// match x {};  // not when its own statement
19    /// ```
20    stmt: bool,
21
22    /// This is the difference between:
23    ///
24    /// ```ignore (illustrative)
25    /// (match x {}) - 1;  // subexpression needs parens
26    ///
27    /// let _ = match x {} - 1;  // no parens
28    /// ```
29    ///
30    /// There are 3 distinguishable contexts in which `print_expr` might be
31    /// called with the expression `$match` as its argument, where `$match`
32    /// represents an expression of kind `ExprKind::Match`:
33    ///
34    ///   - stmt=false leftmost_subexpression_in_stmt=false
35    ///
36    ///     Example: `let _ = $match - 1;`
37    ///
38    ///     No parentheses required.
39    ///
40    ///   - stmt=false leftmost_subexpression_in_stmt=true
41    ///
42    ///     Example: `$match - 1;`
43    ///
44    ///     Must parenthesize `($match)`, otherwise parsing back the output as a
45    ///     statement would terminate the statement after the closing brace of
46    ///     the match, parsing `-1;` as a separate statement.
47    ///
48    ///   - stmt=true leftmost_subexpression_in_stmt=false
49    ///
50    ///     Example: `$match;`
51    ///
52    ///     No parentheses required.
53    leftmost_subexpression_in_stmt: bool,
54
55    /// Print expression such that it can be parsed as a match arm.
56    ///
57    /// This is almost equivalent to `stmt`, but the grammar diverges a tiny bit
58    /// between statements and match arms when it comes to braced macro calls.
59    /// Macro calls with brace delimiter terminate a statement without a
60    /// semicolon, but do not terminate a match-arm without comma.
61    ///
62    /// ```ignore (illustrative)
63    /// m! {} - 1;  // two statements: a macro call followed by -1 literal
64    ///
65    /// match () {
66    ///     _ => m! {} - 1,  // binary subtraction operator
67    /// }
68    /// ```
69    match_arm: bool,
70
71    /// This is almost equivalent to `leftmost_subexpression_in_stmt`, other
72    /// than for braced macro calls.
73    ///
74    /// If we have `m! {} - 1` as an expression, the leftmost subexpression
75    /// `m! {}` will need to be parenthesized in the statement case but not the
76    /// match-arm case.
77    ///
78    /// ```ignore (illustrative)
79    /// (m! {}) - 1;  // subexpression needs parens
80    ///
81    /// match () {
82    ///     _ => m! {} - 1,  // no parens
83    /// }
84    /// ```
85    leftmost_subexpression_in_match_arm: bool,
86
87    /// This is the difference between:
88    ///
89    /// ```ignore (illustrative)
90    /// if let _ = (Struct {}) {}  // needs parens
91    ///
92    /// match () {
93    ///     () if let _ = Struct {} => {}  // no parens
94    /// }
95    /// ```
96    parenthesize_exterior_struct_lit: bool,
97
98    /// This is the difference between:
99    ///
100    /// ```ignore (illustrative)
101    /// let _ = (return) - 1;  // without paren, this would return -1
102    ///
103    /// let _ = return + 1;  // no paren because '+' cannot begin expr
104    /// ```
105    next_operator_can_begin_expr: bool,
106
107    /// This is the difference between:
108    ///
109    /// ```ignore (illustrative)
110    /// let _ = 1 + return 1;  // no parens if rightmost subexpression
111    ///
112    /// let _ = 1 + (return 1) + 1;  // needs parens
113    /// ```
114    next_operator_can_continue_expr: bool,
115}
116
117impl FixupContext {
118    /// Create the initial fixup for printing an expression in statement
119    /// position.
120    pub(crate) fn new_stmt() -> Self {
121        FixupContext { stmt: true, ..FixupContext::default() }
122    }
123
124    /// Create the initial fixup for printing an expression as the right-hand
125    /// side of a match arm.
126    pub(crate) fn new_match_arm() -> Self {
127        FixupContext { match_arm: true, ..FixupContext::default() }
128    }
129
130    /// Create the initial fixup for printing an expression as the "condition"
131    /// of an `if` or `while`. There are a few other positions which are
132    /// grammatically equivalent and also use this, such as the iterator
133    /// expression in `for` and the scrutinee in `match`.
134    pub(crate) fn new_cond() -> Self {
135        FixupContext { parenthesize_exterior_struct_lit: true, ..FixupContext::default() }
136    }
137
138    /// Transform this fixup into the one that should apply when printing the
139    /// leftmost subexpression of the current expression.
140    ///
141    /// The leftmost subexpression is any subexpression that has the same first
142    /// token as the current expression, but has a different last token.
143    ///
144    /// For example in `$a + $b` and `$a.method()`, the subexpression `$a` is a
145    /// leftmost subexpression.
146    ///
147    /// Not every expression has a leftmost subexpression. For example neither
148    /// `-$a` nor `[$a]` have one.
149    pub(crate) fn leftmost_subexpression(self) -> Self {
150        FixupContext {
151            stmt: false,
152            leftmost_subexpression_in_stmt: self.stmt || self.leftmost_subexpression_in_stmt,
153            match_arm: false,
154            leftmost_subexpression_in_match_arm: self.match_arm
155                || self.leftmost_subexpression_in_match_arm,
156            next_operator_can_begin_expr: false,
157            next_operator_can_continue_expr: true,
158            ..self
159        }
160    }
161
162    /// Transform this fixup into the one that should apply when printing a
163    /// leftmost subexpression followed by a `.` or `?` token, which confer
164    /// different statement boundary rules compared to other leftmost
165    /// subexpressions.
166    pub(crate) fn leftmost_subexpression_with_dot(self) -> Self {
167        FixupContext {
168            stmt: self.stmt || self.leftmost_subexpression_in_stmt,
169            leftmost_subexpression_in_stmt: false,
170            match_arm: self.match_arm || self.leftmost_subexpression_in_match_arm,
171            leftmost_subexpression_in_match_arm: false,
172            next_operator_can_begin_expr: false,
173            next_operator_can_continue_expr: true,
174            ..self
175        }
176    }
177
178    /// Transform this fixup into the one that should apply when printing a
179    /// leftmost subexpression followed by punctuation that is legal as the
180    /// first token of an expression.
181    pub(crate) fn leftmost_subexpression_with_operator(
182        self,
183        next_operator_can_begin_expr: bool,
184    ) -> Self {
185        FixupContext { next_operator_can_begin_expr, ..self.leftmost_subexpression() }
186    }
187
188    /// Transform this fixup into the one that should apply when printing the
189    /// rightmost subexpression of the current expression.
190    ///
191    /// The rightmost subexpression is any subexpression that has a different
192    /// first token than the current expression, but has the same last token.
193    ///
194    /// For example in `$a + $b` and `-$b`, the subexpression `$b` is a
195    /// rightmost subexpression.
196    ///
197    /// Not every expression has a rightmost subexpression. For example neither
198    /// `[$b]` nor `$a.f($b)` have one.
199    pub(crate) fn rightmost_subexpression(self) -> Self {
200        FixupContext {
201            stmt: false,
202            leftmost_subexpression_in_stmt: false,
203            match_arm: false,
204            leftmost_subexpression_in_match_arm: false,
205            ..self
206        }
207    }
208
209    /// Determine whether parentheses are needed around the given expression to
210    /// head off an unintended statement boundary.
211    ///
212    /// The documentation on `FixupContext::leftmost_subexpression_in_stmt` has
213    /// examples.
214    pub(crate) fn would_cause_statement_boundary(self, expr: &Expr) -> bool {
215        (self.leftmost_subexpression_in_stmt && !classify::expr_requires_semi_to_be_stmt(expr))
216            || (self.leftmost_subexpression_in_match_arm && classify::expr_is_complete(expr))
217    }
218
219    /// Determine whether parentheses are needed around the given `let`
220    /// scrutinee.
221    ///
222    /// In `if let _ = $e {}`, some examples of `$e` that would need parentheses
223    /// are:
224    ///
225    ///   - `Struct {}.f()`, because otherwise the `{` would be misinterpreted
226    ///     as the opening of the if's then-block.
227    ///
228    ///   - `true && false`, because otherwise this would be misinterpreted as a
229    ///     "let chain".
230    pub(crate) fn needs_par_as_let_scrutinee(self, expr: &Expr) -> bool {
231        self.parenthesize_exterior_struct_lit && parser::contains_exterior_struct_lit(expr)
232            || parser::needs_par_as_let_scrutinee(self.precedence(expr))
233    }
234
235    /// Determines the effective precedence of a subexpression. Some expressions
236    /// have higher or lower precedence when adjacent to particular operators.
237    pub(crate) fn precedence(self, expr: &Expr) -> ExprPrecedence {
238        if self.next_operator_can_begin_expr {
239            // Decrease precedence of value-less jumps when followed by an
240            // operator that would otherwise get interpreted as beginning a
241            // value for the jump.
242            if let ExprKind::Break(..)
243            | ExprKind::Ret(..)
244            | ExprKind::Yeet(..)
245            | ExprKind::Yield(YieldKind::Prefix(..)) = expr.kind
246            {
247                return ExprPrecedence::Jump;
248            }
249        }
250
251        if !self.next_operator_can_continue_expr {
252            // Increase precedence of expressions that extend to the end of
253            // current statement or group.
254            if let ExprKind::Break(..)
255            | ExprKind::Closure(..)
256            | ExprKind::Ret(..)
257            | ExprKind::Yeet(..)
258            | ExprKind::Yield(YieldKind::Prefix(..))
259            | ExprKind::Range(None, ..) = expr.kind
260            {
261                return ExprPrecedence::Prefix;
262            }
263        }
264
265        expr.precedence()
266    }
267}