Skip to content

Commit f91c83a

Browse files
committed
XXX: closures
1 parent 5b8ca52 commit f91c83a

File tree

2 files changed

+55
-38
lines changed

2 files changed

+55
-38
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,27 @@ impl<'a> State<'a> {
10851085
State { s: pp::Printer::new(), comments: None, ann: &NoAnn }
10861086
}
10871087

1088+
pub fn with_ibox(&mut self, indent: isize, f: impl FnOnce(&mut Self)) {
1089+
let ib = self.ibox(indent);
1090+
f(self);
1091+
self.end(ib);
1092+
}
1093+
1094+
// njn: name?
1095+
pub fn with_cbox_ibox_print_block_with_attrs(
1096+
&mut self,
1097+
cindent: isize, // njn: always 0?
1098+
iindent: isize,
1099+
block: &ast::Block,
1100+
attrs: &[ast::Attribute],
1101+
f: impl FnOnce(&mut Self),
1102+
) {
1103+
let cb = self.cbox(cindent);
1104+
let ib = self.ibox(iindent);
1105+
f(self);
1106+
self.print_block_with_attrs(block, attrs, cb, ib);
1107+
}
1108+
10881109
fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
10891110
where
10901111
F: FnMut(&mut State<'_>, &T),

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

+34-38
Original file line numberDiff line numberDiff line change
@@ -110,39 +110,37 @@ impl<'a> State<'a> {
110110
}
111111

112112
fn print_expr_vec(&mut self, exprs: &[P<ast::Expr>]) {
113-
let ib = self.ibox(INDENT_UNIT);
114-
self.word("[");
115-
self.commasep_exprs(Inconsistent, exprs);
116-
self.word("]");
117-
self.end(ib);
113+
self.with_ibox(INDENT_UNIT, |this: &mut Self| {
114+
this.word("[");
115+
this.commasep_exprs(Inconsistent, exprs);
116+
this.word("]");
117+
});
118118
}
119119

120120
pub(super) fn print_expr_anon_const(
121121
&mut self,
122122
expr: &ast::AnonConst,
123123
attrs: &[ast::Attribute],
124124
) {
125-
let ib = self.ibox(INDENT_UNIT);
126-
self.word("const");
127-
self.nbsp();
128-
if let ast::ExprKind::Block(block, None) = &expr.value.kind {
129-
let cb = self.cbox(0);
130-
let ib = self.ibox(0);
131-
self.print_block_with_attrs(block, attrs, cb, ib);
132-
} else {
133-
self.print_expr(&expr.value, FixupContext::default());
134-
}
135-
self.end(ib);
125+
self.with_ibox(INDENT_UNIT, |this| {
126+
this.word("const");
127+
this.nbsp();
128+
if let ast::ExprKind::Block(block, None) = &expr.value.kind {
129+
this.with_cbox_ibox_print_block_with_attrs(0, 0, block, attrs, |_| {});
130+
} else {
131+
this.print_expr(&expr.value, FixupContext::default());
132+
}
133+
});
136134
}
137135

138136
fn print_expr_repeat(&mut self, element: &ast::Expr, count: &ast::AnonConst) {
139-
let ib = self.ibox(INDENT_UNIT);
140-
self.word("[");
141-
self.print_expr(element, FixupContext::default());
142-
self.word_space(";");
143-
self.print_expr(&count.value, FixupContext::default());
144-
self.word("]");
145-
self.end(ib);
137+
self.with_ibox(INDENT_UNIT, |this| {
138+
this.word("[");
139+
this.print_expr(element, FixupContext::default());
140+
this.word_space(";");
141+
this.print_expr(&count.value, FixupContext::default());
142+
this.word("]");
143+
});
146144
}
147145

148146
fn print_expr_struct(
@@ -438,14 +436,12 @@ impl<'a> State<'a> {
438436
ast::ExprKind::Type(expr, ty) => {
439437
self.word("builtin # type_ascribe");
440438
self.popen();
441-
let ib = self.ibox(0);
442-
self.print_expr(expr, FixupContext::default());
443-
444-
self.word(",");
445-
self.space_if_not_bol();
446-
self.print_type(ty);
447-
448-
self.end(ib);
439+
self.with_ibox(0, |this| {
440+
this.print_expr(expr, FixupContext::default());
441+
this.word(",");
442+
this.space_if_not_bol();
443+
this.print_type(ty);
444+
});
449445
self.pclose();
450446
}
451447
ast::ExprKind::Let(pat, scrutinee, _, _) => {
@@ -457,12 +453,11 @@ impl<'a> State<'a> {
457453
self.print_ident(label.ident);
458454
self.word_space(":");
459455
}
460-
let cb = self.cbox(0);
461-
let ib = self.ibox(0);
462-
self.word_nbsp("while");
463-
self.print_expr_as_cond(test);
464-
self.space();
465-
self.print_block_with_attrs(blk, attrs, cb, ib);
456+
self.with_cbox_ibox_print_block_with_attrs(0, 0, blk, attrs, |this| {
457+
this.word_nbsp("while");
458+
this.print_expr_as_cond(test);
459+
this.space();
460+
}
466461
}
467462
ast::ExprKind::ForLoop { pat, iter, body, label, kind } => {
468463
if let Some(label) = label {
@@ -833,6 +828,7 @@ impl<'a> State<'a> {
833828
if arm.attrs.is_empty() {
834829
self.space();
835830
}
831+
// njn: hmm, tricky case for the closure approach
836832
let cb = self.cbox(INDENT_UNIT);
837833
let ib = self.ibox(0);
838834
self.maybe_print_comment(arm.pat.span.lo());
@@ -874,7 +870,7 @@ impl<'a> State<'a> {
874870
// other path :(
875871
self.word(",");
876872
}
877-
self.end(cb); // Close enclosing cbox.
873+
self.end(cb); // Close enclosing cbox. // njn: remove low-value comments of this sort
878874
}
879875

880876
fn print_closure_binder(&mut self, binder: &ast::ClosureBinder) {

0 commit comments

Comments
 (0)