-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move pretty-printer FixupContext to a module
Required for being able to make the fields private and force the use of accessor methods, which will be added in the next commit.
- Loading branch information
Showing
4 changed files
with
75 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#[derive(Copy, Clone, Debug)] | ||
pub(crate) struct FixupContext { | ||
/// Print expression such that it can be parsed back as a statement | ||
/// consisting of the original expression. | ||
/// | ||
/// The effect of this is for binary operators in statement position to set | ||
/// `leftmost_subexpression_in_stmt` when printing their left-hand operand. | ||
/// | ||
/// ```ignore (illustrative) | ||
/// (match x {}) - 1; // match needs parens when LHS of binary operator | ||
/// | ||
/// match x {}; // not when its own statement | ||
/// ``` | ||
pub stmt: bool, | ||
|
||
/// This is the difference between: | ||
/// | ||
/// ```ignore (illustrative) | ||
/// (match x {}) - 1; // subexpression needs parens | ||
/// | ||
/// let _ = match x {} - 1; // no parens | ||
/// ``` | ||
/// | ||
/// There are 3 distinguishable contexts in which `print_expr` might be | ||
/// called with the expression `$match` as its argument, where `$match` | ||
/// represents an expression of kind `ExprKind::Match`: | ||
/// | ||
/// - stmt=false leftmost_subexpression_in_stmt=false | ||
/// | ||
/// Example: `let _ = $match - 1;` | ||
/// | ||
/// No parentheses required. | ||
/// | ||
/// - stmt=false leftmost_subexpression_in_stmt=true | ||
/// | ||
/// Example: `$match - 1;` | ||
/// | ||
/// Must parenthesize `($match)`, otherwise parsing back the output as a | ||
/// statement would terminate the statement after the closing brace of | ||
/// the match, parsing `-1;` as a separate statement. | ||
/// | ||
/// - stmt=true leftmost_subexpression_in_stmt=false | ||
/// | ||
/// Example: `$match;` | ||
/// | ||
/// No parentheses required. | ||
pub leftmost_subexpression_in_stmt: bool, | ||
|
||
/// This is the difference between: | ||
/// | ||
/// ```ignore (illustrative) | ||
/// if let _ = (Struct {}) {} // needs parens | ||
/// | ||
/// match () { | ||
/// () if let _ = Struct {} => {} // no parens | ||
/// } | ||
/// ``` | ||
pub parenthesize_exterior_struct_lit: bool, | ||
} | ||
|
||
/// The default amount of fixing is minimal fixing. Fixups should be turned on | ||
/// in a targeted fashion where needed. | ||
impl Default for FixupContext { | ||
fn default() -> Self { | ||
FixupContext { | ||
stmt: false, | ||
leftmost_subexpression_in_stmt: false, | ||
parenthesize_exterior_struct_lit: false, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters