Skip to content

Commit 43edcf4

Browse files
committed
XXX: BoxMarker
XXX: mention how many comments there are on `end` calls saying "end outer head-block", "Close the outer-box"
1 parent e3e432d commit 43edcf4

File tree

9 files changed

+435
-365
lines changed

9 files changed

+435
-365
lines changed

compiler/rustc_ast_pretty/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(internal_features)]
33
#![doc(rust_logo)]
44
#![feature(box_patterns)]
5+
#![feature(negative_impls)]
56
#![feature(rustdoc_internals)]
67
// tidy-alphabetical-end
78

compiler/rustc_ast_pretty/src/pp.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,18 @@ struct BufEntry {
234234
size: isize,
235235
}
236236

237+
// Boxes opened with methods like `Printer::{c,i}box` must be closed with
238+
// `Printer::end`. This used to be done entirely implicitly, which is
239+
// error-prone. This marker type is now returned from the box opening methods
240+
// and consumed by `Printer::end`. This makes it much harder to fail to match
241+
// opens/closes. (It is still possible to mess up, e.g. by opening a box and
242+
// then closing it on one path while not closing it on another.)
243+
#[must_use]
244+
pub struct BoxMarker;
245+
246+
impl !Clone for BoxMarker {}
247+
impl !Copy for BoxMarker {}
248+
237249
impl Printer {
238250
pub fn new() -> Self {
239251
Printer {
@@ -270,17 +282,20 @@ impl Printer {
270282
}
271283
}
272284

273-
fn scan_begin(&mut self, token: BeginToken) {
285+
// This is is where `BoxMarker`s are produced.
286+
fn scan_begin(&mut self, token: BeginToken) -> BoxMarker {
274287
if self.scan_stack.is_empty() {
275288
self.left_total = 1;
276289
self.right_total = 1;
277290
self.buf.clear();
278291
}
279292
let right = self.buf.push(BufEntry { token: Token::Begin(token), size: -self.right_total });
280293
self.scan_stack.push_back(right);
294+
BoxMarker
281295
}
282296

283-
fn scan_end(&mut self) {
297+
// This is is where `BoxMarker`s are consumed.
298+
fn scan_end(&mut self, _b: BoxMarker) {
284299
if self.scan_stack.is_empty() {
285300
self.print_end();
286301
} else {

compiler/rustc_ast_pretty/src/pp/convenience.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
use std::borrow::Cow;
22

3-
use crate::pp::{BeginToken, BreakToken, Breaks, IndentStyle, Printer, SIZE_INFINITY, Token};
3+
use crate::pp::{
4+
BeginToken, BoxMarker, BreakToken, Breaks, IndentStyle, Printer, SIZE_INFINITY, Token,
5+
};
46

57
impl Printer {
68
/// "raw box"
7-
pub fn rbox(&mut self, indent: isize, breaks: Breaks) {
9+
pub fn rbox(&mut self, indent: isize, breaks: Breaks) -> BoxMarker {
810
self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks })
911
}
1012

1113
/// Inconsistent breaking box
12-
pub fn ibox(&mut self, indent: isize) {
14+
pub fn ibox(&mut self, indent: isize) -> BoxMarker {
1315
self.rbox(indent, Breaks::Inconsistent)
1416
}
1517

1618
/// Consistent breaking box
17-
pub fn cbox(&mut self, indent: isize) {
19+
pub fn cbox(&mut self, indent: isize) -> BoxMarker {
1820
self.rbox(indent, Breaks::Consistent)
1921
}
2022

21-
pub fn visual_align(&mut self) {
22-
self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent });
23+
pub fn visual_align(&mut self) -> BoxMarker {
24+
self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent })
2325
}
2426

2527
pub fn break_offset(&mut self, n: usize, off: isize) {
@@ -30,8 +32,8 @@ impl Printer {
3032
});
3133
}
3234

33-
pub fn end(&mut self) {
34-
self.scan_end()
35+
pub fn end(&mut self, b: BoxMarker) {
36+
self.scan_end(b)
3537
}
3638

3739
pub fn eof(mut self) -> String {

0 commit comments

Comments
 (0)