Skip to content

Introduce BoxMarker to improve pretty-printing correctness #140316

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_ast_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(internal_features)]
#![doc(rust_logo)]
#![feature(box_patterns)]
#![feature(negative_impls)]
#![feature(rustdoc_internals)]
// tidy-alphabetical-end

Expand Down
40 changes: 38 additions & 2 deletions compiler/rustc_ast_pretty/src/pp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,38 @@ struct BufEntry {
size: isize,
}

// Boxes opened with methods like `Printer::{cbox,ibox}` must be closed with
// `Printer::end`. Failure to do so can result in bad indenting, or in extreme
// cases, cause no output to be produced at all.
//
// Box opening and closing used to be entirely implicit, which was hard to
// understand and easy to get wrong. This marker type is now returned from the
// box opening methods and forgotten by `Printer::end`. Any marker that isn't
// forgotten will trigger a panic in `drop`. (Closing a box more than once
// isn't possible because `BoxMarker` doesn't implement `Copy` or `Clone`.)
//
// FIXME(nnethercote): the panic in `drop` is currently disabled because a few
// places fail to close their boxes. It can be enabled once they are fixed.
//
// Note: it would be better to make open/close mismatching impossible and avoid
// the need for this marker type altogether by having functions like
// `with_ibox` that open a box, call a closure, and then close the box. That
// would work for simple cases, but box lifetimes sometimes interact with
// complex control flow and across function boundaries in ways that are
// difficult to handle with such a technique.
#[must_use]
pub struct BoxMarker;

impl !Clone for BoxMarker {}
impl !Copy for BoxMarker {}

impl Drop for BoxMarker {
fn drop(&mut self) {
// FIXME(nnethercote): enable once the bad cases are fixed
//panic!("BoxMarker not ended with `Printer::end()`");
}
}

impl Printer {
pub fn new() -> Self {
Printer {
Expand Down Expand Up @@ -270,23 +302,27 @@ impl Printer {
}
}

fn scan_begin(&mut self, token: BeginToken) {
// This is is where `BoxMarker`s are produced.
fn scan_begin(&mut self, token: BeginToken) -> BoxMarker {
if self.scan_stack.is_empty() {
self.left_total = 1;
self.right_total = 1;
self.buf.clear();
}
let right = self.buf.push(BufEntry { token: Token::Begin(token), size: -self.right_total });
self.scan_stack.push_back(right);
BoxMarker
}

fn scan_end(&mut self) {
// This is is where `BoxMarker`s are consumed.
fn scan_end(&mut self, b: BoxMarker) {
if self.scan_stack.is_empty() {
self.print_end();
} else {
let right = self.buf.push(BufEntry { token: Token::End, size: -1 });
self.scan_stack.push_back(right);
}
std::mem::forget(b)
}

fn scan_break(&mut self, token: BreakToken) {
Expand Down
18 changes: 10 additions & 8 deletions compiler/rustc_ast_pretty/src/pp/convenience.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
use std::borrow::Cow;

use crate::pp::{BeginToken, BreakToken, Breaks, IndentStyle, Printer, SIZE_INFINITY, Token};
use crate::pp::{
BeginToken, BoxMarker, BreakToken, Breaks, IndentStyle, Printer, SIZE_INFINITY, Token,
};

impl Printer {
/// "raw box"
pub fn rbox(&mut self, indent: isize, breaks: Breaks) {
pub fn rbox(&mut self, indent: isize, breaks: Breaks) -> BoxMarker {
self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks })
}

/// Inconsistent breaking box
pub fn ibox(&mut self, indent: isize) {
pub fn ibox(&mut self, indent: isize) -> BoxMarker {
self.rbox(indent, Breaks::Inconsistent)
}

/// Consistent breaking box
pub fn cbox(&mut self, indent: isize) {
pub fn cbox(&mut self, indent: isize) -> BoxMarker {
self.rbox(indent, Breaks::Consistent)
}

pub fn visual_align(&mut self) {
self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent });
pub fn visual_align(&mut self) -> BoxMarker {
self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent })
}

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

pub fn end(&mut self) {
self.scan_end()
pub fn end(&mut self, b: BoxMarker) {
self.scan_end(b)
}

pub fn eof(mut self) -> String {
Expand Down
Loading
Loading