Skip to content

Commit 912c670

Browse files
committed
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.
1 parent c8d19a9 commit 912c670

File tree

4 files changed

+75
-74
lines changed

4 files changed

+75
-74
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
//! Note that HIR pretty printing is layered on top of this crate.
44
55
mod expr;
6+
mod fixup;
67
mod item;
78

89
use crate::pp::Breaks::{Consistent, Inconsistent};
910
use crate::pp::{self, Breaks};
10-
use crate::pprust::state::expr::FixupContext;
11+
use crate::pprust::state::fixup::FixupContext;
1112
use ast::TraitBoundModifiers;
1213
use rustc_ast::attr::AttrIdGenerator;
1314
use rustc_ast::ptr::P;

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+1-72
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::pp::Breaks::Inconsistent;
2+
use crate::pprust::state::fixup::FixupContext;
23
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
34
use ast::{ForLoopKind, MatchKind};
45
use itertools::{Itertools, Position};
@@ -14,78 +15,6 @@ use rustc_ast::{
1415
};
1516
use std::fmt::Write;
1617

17-
#[derive(Copy, Clone, Debug)]
18-
pub(crate) struct FixupContext {
19-
/// Print expression such that it can be parsed back as a statement
20-
/// consisting of the original expression.
21-
///
22-
/// The effect of this is for binary operators in statement position to set
23-
/// `leftmost_subexpression_in_stmt` when printing their left-hand operand.
24-
///
25-
/// ```ignore (illustrative)
26-
/// (match x {}) - 1; // match needs parens when LHS of binary operator
27-
///
28-
/// match x {}; // not when its own statement
29-
/// ```
30-
pub stmt: bool,
31-
32-
/// This is the difference between:
33-
///
34-
/// ```ignore (illustrative)
35-
/// (match x {}) - 1; // subexpression needs parens
36-
///
37-
/// let _ = match x {} - 1; // no parens
38-
/// ```
39-
///
40-
/// There are 3 distinguishable contexts in which `print_expr` might be
41-
/// called with the expression `$match` as its argument, where `$match`
42-
/// represents an expression of kind `ExprKind::Match`:
43-
///
44-
/// - stmt=false leftmost_subexpression_in_stmt=false
45-
///
46-
/// Example: `let _ = $match - 1;`
47-
///
48-
/// No parentheses required.
49-
///
50-
/// - stmt=false leftmost_subexpression_in_stmt=true
51-
///
52-
/// Example: `$match - 1;`
53-
///
54-
/// Must parenthesize `($match)`, otherwise parsing back the output as a
55-
/// statement would terminate the statement after the closing brace of
56-
/// the match, parsing `-1;` as a separate statement.
57-
///
58-
/// - stmt=true leftmost_subexpression_in_stmt=false
59-
///
60-
/// Example: `$match;`
61-
///
62-
/// No parentheses required.
63-
pub leftmost_subexpression_in_stmt: bool,
64-
65-
/// This is the difference between:
66-
///
67-
/// ```ignore (illustrative)
68-
/// if let _ = (Struct {}) {} // needs parens
69-
///
70-
/// match () {
71-
/// () if let _ = Struct {} => {} // no parens
72-
/// }
73-
/// ```
74-
pub parenthesize_exterior_struct_lit: bool,
75-
}
76-
77-
/// The default amount of fixing is minimal fixing. Fixups should be turned on
78-
/// in a targeted fashion where needed.
79-
impl Default for FixupContext {
80-
fn default() -> Self {
81-
FixupContext {
82-
stmt: false,
83-
leftmost_subexpression_in_stmt: false,
84-
parenthesize_exterior_struct_lit: false,
85-
}
86-
}
87-
}
88-
8918
impl<'a> State<'a> {
9019
fn print_else(&mut self, els: Option<&ast::Expr>) {
9120
if let Some(_else) = els {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#[derive(Copy, Clone, Debug)]
2+
pub(crate) struct FixupContext {
3+
/// Print expression such that it can be parsed back as a statement
4+
/// consisting of the original expression.
5+
///
6+
/// The effect of this is for binary operators in statement position to set
7+
/// `leftmost_subexpression_in_stmt` when printing their left-hand operand.
8+
///
9+
/// ```ignore (illustrative)
10+
/// (match x {}) - 1; // match needs parens when LHS of binary operator
11+
///
12+
/// match x {}; // not when its own statement
13+
/// ```
14+
pub stmt: bool,
15+
16+
/// This is the difference between:
17+
///
18+
/// ```ignore (illustrative)
19+
/// (match x {}) - 1; // subexpression needs parens
20+
///
21+
/// let _ = match x {} - 1; // no parens
22+
/// ```
23+
///
24+
/// There are 3 distinguishable contexts in which `print_expr` might be
25+
/// called with the expression `$match` as its argument, where `$match`
26+
/// represents an expression of kind `ExprKind::Match`:
27+
///
28+
/// - stmt=false leftmost_subexpression_in_stmt=false
29+
///
30+
/// Example: `let _ = $match - 1;`
31+
///
32+
/// No parentheses required.
33+
///
34+
/// - stmt=false leftmost_subexpression_in_stmt=true
35+
///
36+
/// Example: `$match - 1;`
37+
///
38+
/// Must parenthesize `($match)`, otherwise parsing back the output as a
39+
/// statement would terminate the statement after the closing brace of
40+
/// the match, parsing `-1;` as a separate statement.
41+
///
42+
/// - stmt=true leftmost_subexpression_in_stmt=false
43+
///
44+
/// Example: `$match;`
45+
///
46+
/// No parentheses required.
47+
pub leftmost_subexpression_in_stmt: bool,
48+
49+
/// This is the difference between:
50+
///
51+
/// ```ignore (illustrative)
52+
/// if let _ = (Struct {}) {} // needs parens
53+
///
54+
/// match () {
55+
/// () if let _ = Struct {} => {} // no parens
56+
/// }
57+
/// ```
58+
pub parenthesize_exterior_struct_lit: bool,
59+
}
60+
61+
/// The default amount of fixing is minimal fixing. Fixups should be turned on
62+
/// in a targeted fashion where needed.
63+
impl Default for FixupContext {
64+
fn default() -> Self {
65+
FixupContext {
66+
stmt: false,
67+
leftmost_subexpression_in_stmt: false,
68+
parenthesize_exterior_struct_lit: false,
69+
}
70+
}
71+
}

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::pp::Breaks::Inconsistent;
2-
use crate::pprust::state::expr::FixupContext;
2+
use crate::pprust::state::fixup::FixupContext;
33
use crate::pprust::state::{AnnNode, PrintState, State, INDENT_UNIT};
44

55
use ast::StaticItem;

0 commit comments

Comments
 (0)