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.
This commit is contained in:
parent
c8d19a92aa
commit
912c67043b
4 changed files with 75 additions and 74 deletions
|
@ -3,11 +3,12 @@
|
||||||
//! Note that HIR pretty printing is layered on top of this crate.
|
//! Note that HIR pretty printing is layered on top of this crate.
|
||||||
|
|
||||||
mod expr;
|
mod expr;
|
||||||
|
mod fixup;
|
||||||
mod item;
|
mod item;
|
||||||
|
|
||||||
use crate::pp::Breaks::{Consistent, Inconsistent};
|
use crate::pp::Breaks::{Consistent, Inconsistent};
|
||||||
use crate::pp::{self, Breaks};
|
use crate::pp::{self, Breaks};
|
||||||
use crate::pprust::state::expr::FixupContext;
|
use crate::pprust::state::fixup::FixupContext;
|
||||||
use ast::TraitBoundModifiers;
|
use ast::TraitBoundModifiers;
|
||||||
use rustc_ast::attr::AttrIdGenerator;
|
use rustc_ast::attr::AttrIdGenerator;
|
||||||
use rustc_ast::ptr::P;
|
use rustc_ast::ptr::P;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::pp::Breaks::Inconsistent;
|
use crate::pp::Breaks::Inconsistent;
|
||||||
|
use crate::pprust::state::fixup::FixupContext;
|
||||||
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
|
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
|
||||||
use ast::{ForLoopKind, MatchKind};
|
use ast::{ForLoopKind, MatchKind};
|
||||||
use itertools::{Itertools, Position};
|
use itertools::{Itertools, Position};
|
||||||
|
@ -14,78 +15,6 @@ use rustc_ast::{
|
||||||
};
|
};
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
#[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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> State<'a> {
|
impl<'a> State<'a> {
|
||||||
fn print_else(&mut self, els: Option<&ast::Expr>) {
|
fn print_else(&mut self, els: Option<&ast::Expr>) {
|
||||||
if let Some(_else) = els {
|
if let Some(_else) = els {
|
||||||
|
|
71
compiler/rustc_ast_pretty/src/pprust/state/fixup.rs
Normal file
71
compiler/rustc_ast_pretty/src/pprust/state/fixup.rs
Normal file
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::pp::Breaks::Inconsistent;
|
use crate::pp::Breaks::Inconsistent;
|
||||||
use crate::pprust::state::expr::FixupContext;
|
use crate::pprust::state::fixup::FixupContext;
|
||||||
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
|
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
|
||||||
|
|
||||||
use ast::StaticItem;
|
use ast::StaticItem;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue