From 0529b94578aba5147649516dcf0186eb193f2e40 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 16 Apr 2026 18:16:27 +1000 Subject: [PATCH 1/2] Move `Token` impl block. For no apparent reason it's in a different file to `Token` itself. This commit moves it. --- compiler/rustc_ast_pretty/src/pp.rs | 6 ++++++ compiler/rustc_ast_pretty/src/pp/convenience.rs | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 4108671a3629..9d0888a15d8f 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -188,6 +188,12 @@ pub(crate) enum Token { End, } +impl Token { + pub(crate) fn is_hardbreak_tok(&self) -> bool { + *self == Printer::hardbreak_tok_offset(0) + } +} + #[derive(Copy, Clone)] enum PrintFrame { Fits, diff --git a/compiler/rustc_ast_pretty/src/pp/convenience.rs b/compiler/rustc_ast_pretty/src/pp/convenience.rs index 9b902b38122c..c9589535940a 100644 --- a/compiler/rustc_ast_pretty/src/pp/convenience.rs +++ b/compiler/rustc_ast_pretty/src/pp/convenience.rs @@ -89,9 +89,3 @@ pub fn trailing_comma_or_space(&mut self) { }); } } - -impl Token { - pub(crate) fn is_hardbreak_tok(&self) -> bool { - *self == Printer::hardbreak_tok_offset(0) - } -} From 9e940e40519573b150c85c76c593d7cd777411e1 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 16 Apr 2026 18:16:11 +1000 Subject: [PATCH 2/2] Merge `Printer` impl blocks. `rustc_ast_pretty::pp` defines `Printer` and has a 346 line `impl Printer` block for it. `rustc_ast_pretty::pp::convenience` has another `impl Printer` block with 85 lines. `rustc_ast_pretty::helpers` has another `impl Printer` block with 45 lines. This commit merges the two small `impl Printer` blocks into the bigger one, because there is no good reason for them to be separate. Doing this eliminates the `rustc_ast_pretty::pp::convenience` and `rustc_ast_pretty::helpers` modules; no great loss given that they were small and had extremely generic names. --- compiler/rustc_ast_pretty/src/helpers.rs | 49 ------- compiler/rustc_ast_pretty/src/lib.rs | 1 - compiler/rustc_ast_pretty/src/pp.rs | 129 +++++++++++++++++- .../rustc_ast_pretty/src/pp/convenience.rs | 91 ------------ 4 files changed, 128 insertions(+), 142 deletions(-) delete mode 100644 compiler/rustc_ast_pretty/src/helpers.rs delete mode 100644 compiler/rustc_ast_pretty/src/pp/convenience.rs diff --git a/compiler/rustc_ast_pretty/src/helpers.rs b/compiler/rustc_ast_pretty/src/helpers.rs deleted file mode 100644 index 34641ea2f5ae..000000000000 --- a/compiler/rustc_ast_pretty/src/helpers.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::borrow::Cow; - -use crate::pp::Printer; - -impl Printer { - pub fn word_space>>(&mut self, w: W) { - self.word(w); - self.space(); - } - - pub fn popen(&mut self) { - self.word("("); - } - - pub fn pclose(&mut self) { - self.word(")"); - } - - pub fn hardbreak_if_not_bol(&mut self) { - if !self.is_beginning_of_line() { - self.hardbreak() - } - } - - pub fn space_if_not_bol(&mut self) { - if !self.is_beginning_of_line() { - self.space(); - } - } - - pub fn nbsp(&mut self) { - self.word(" ") - } - - pub fn word_nbsp>>(&mut self, w: S) { - self.word(w); - self.nbsp() - } - - /// Synthesizes a comment that was not textually present in the original - /// source file. - pub fn synth_comment(&mut self, text: impl Into>) { - self.word("/*"); - self.space(); - self.word(text); - self.space(); - self.word("*/") - } -} diff --git a/compiler/rustc_ast_pretty/src/lib.rs b/compiler/rustc_ast_pretty/src/lib.rs index a7d9f89fb3df..bfc1d387b700 100644 --- a/compiler/rustc_ast_pretty/src/lib.rs +++ b/compiler/rustc_ast_pretty/src/lib.rs @@ -3,6 +3,5 @@ #![feature(negative_impls)] // tidy-alphabetical-end -mod helpers; pub mod pp; pub mod pprust; diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 9d0888a15d8f..c7a38d981b89 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -132,7 +132,6 @@ //! methods called `Printer::scan_*`, and the 'PRINT' process is the //! method called `Printer::print`. -mod convenience; mod ring; use std::borrow::Cow; @@ -485,4 +484,132 @@ fn print_string(&mut self, string: &str) { self.out.push_str(string); self.space -= string.len() as isize; } + + /// Synthesizes a comment that was not textually present in the original + /// source file. + pub fn synth_comment(&mut self, text: impl Into>) { + self.word("/*"); + self.space(); + self.word(text); + self.space(); + self.word("*/") + } + + /// "raw box" + 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) -> BoxMarker { + self.rbox(indent, Breaks::Inconsistent) + } + + /// Consistent breaking box + pub fn cbox(&mut self, indent: isize) -> BoxMarker { + self.rbox(indent, 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) { + self.scan_break(BreakToken { + offset: off, + blank_space: n as isize, + ..BreakToken::default() + }); + } + + pub fn end(&mut self, b: BoxMarker) { + self.scan_end(b) + } + + pub fn eof(mut self) -> String { + self.scan_eof(); + self.out + } + + pub fn word>>(&mut self, wrd: S) { + let string = wrd.into(); + self.scan_string(string) + } + + pub fn word_space>>(&mut self, w: W) { + self.word(w); + self.space(); + } + + pub fn nbsp(&mut self) { + self.word(" ") + } + + pub fn word_nbsp>>(&mut self, w: S) { + self.word(w); + self.nbsp() + } + + fn spaces(&mut self, n: usize) { + self.break_offset(n, 0) + } + + pub fn zerobreak(&mut self) { + self.spaces(0) + } + + pub fn space(&mut self) { + self.spaces(1) + } + + pub fn popen(&mut self) { + self.word("("); + } + + pub fn pclose(&mut self) { + self.word(")"); + } + + pub fn hardbreak(&mut self) { + self.spaces(SIZE_INFINITY as usize) + } + + pub fn is_beginning_of_line(&self) -> bool { + match self.last_token() { + Some(last_token) => last_token.is_hardbreak_tok(), + None => true, + } + } + + pub fn hardbreak_if_not_bol(&mut self) { + if !self.is_beginning_of_line() { + self.hardbreak() + } + } + + pub fn space_if_not_bol(&mut self) { + if !self.is_beginning_of_line() { + self.space(); + } + } + + pub(crate) fn hardbreak_tok_offset(off: isize) -> Token { + Token::Break(BreakToken { + offset: off, + blank_space: SIZE_INFINITY, + ..BreakToken::default() + }) + } + + pub fn trailing_comma(&mut self) { + self.scan_break(BreakToken { pre_break: Some(','), ..BreakToken::default() }); + } + + pub fn trailing_comma_or_space(&mut self) { + self.scan_break(BreakToken { + blank_space: 1, + pre_break: Some(','), + ..BreakToken::default() + }); + } } diff --git a/compiler/rustc_ast_pretty/src/pp/convenience.rs b/compiler/rustc_ast_pretty/src/pp/convenience.rs deleted file mode 100644 index c9589535940a..000000000000 --- a/compiler/rustc_ast_pretty/src/pp/convenience.rs +++ /dev/null @@ -1,91 +0,0 @@ -use std::borrow::Cow; - -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) -> BoxMarker { - self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks }) - } - - /// Inconsistent breaking box - pub fn ibox(&mut self, indent: isize) -> BoxMarker { - self.rbox(indent, Breaks::Inconsistent) - } - - /// Consistent breaking box - pub fn cbox(&mut self, indent: isize) -> BoxMarker { - self.rbox(indent, 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) { - self.scan_break(BreakToken { - offset: off, - blank_space: n as isize, - ..BreakToken::default() - }); - } - - pub fn end(&mut self, b: BoxMarker) { - self.scan_end(b) - } - - pub fn eof(mut self) -> String { - self.scan_eof(); - self.out - } - - pub fn word>>(&mut self, wrd: S) { - let string = wrd.into(); - self.scan_string(string) - } - - fn spaces(&mut self, n: usize) { - self.break_offset(n, 0) - } - - pub fn zerobreak(&mut self) { - self.spaces(0) - } - - pub fn space(&mut self) { - self.spaces(1) - } - - pub fn hardbreak(&mut self) { - self.spaces(SIZE_INFINITY as usize) - } - - pub fn is_beginning_of_line(&self) -> bool { - match self.last_token() { - Some(last_token) => last_token.is_hardbreak_tok(), - None => true, - } - } - - pub(crate) fn hardbreak_tok_offset(off: isize) -> Token { - Token::Break(BreakToken { - offset: off, - blank_space: SIZE_INFINITY, - ..BreakToken::default() - }) - } - - pub fn trailing_comma(&mut self) { - self.scan_break(BreakToken { pre_break: Some(','), ..BreakToken::default() }); - } - - pub fn trailing_comma_or_space(&mut self) { - self.scan_break(BreakToken { - blank_space: 1, - pre_break: Some(','), - ..BreakToken::default() - }); - } -}