Skip to content

Commit ba9fc4f

Browse files
committed
Auto merge of #91565 - dtolnay:printhelpers, r=jackh726
Delete duplicated helpers from HIR printer These functions (`cbox`, `nbsp`, `word_nbsp`, `head`, `bopen`, `space_if_not_bol`, `break_offset_if_not_bol`, `synth_comment`, `maybe_print_trailing_comment`, `print_remaining_comments`) are duplicated with identical behavior across the AST printer and HIR printer, but are not specific to AST or HIR data structures.
2 parents 87dce6e + 596e33a commit ba9fc4f

File tree

3 files changed

+30
-114
lines changed

3 files changed

+30
-114
lines changed

compiler/rustc_ast_pretty/src/helpers.rs

+10
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,14 @@ impl Printer {
3535
self.word(w);
3636
self.nbsp()
3737
}
38+
39+
// Synthesizes a comment that was not textually present in the original
40+
// source file.
41+
pub fn synth_comment(&mut self, text: impl Into<Cow<'static, str>>) {
42+
self.word("/*");
43+
self.space();
44+
self.word(text);
45+
self.space();
46+
self.word("*/")
47+
}
3848
}

compiler/rustc_ast_pretty/src/pprust/state.rs

+19-33
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,25 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
349349
self.comments().as_mut().and_then(|c| c.next())
350350
}
351351

352+
fn maybe_print_trailing_comment(&mut self, span: rustc_span::Span, next_pos: Option<BytePos>) {
353+
if let Some(cmnts) = self.comments() {
354+
if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
355+
self.print_comment(&cmnt);
356+
}
357+
}
358+
}
359+
360+
fn print_remaining_comments(&mut self) {
361+
// If there aren't any remaining comments, then we need to manually
362+
// make sure there is a line break at the end.
363+
if self.next_comment().is_none() {
364+
self.hardbreak();
365+
}
366+
while let Some(ref cmnt) = self.next_comment() {
367+
self.print_comment(cmnt)
368+
}
369+
}
370+
352371
fn print_literal(&mut self, lit: &ast::Lit) {
353372
self.maybe_print_comment(lit.span.lo());
354373
self.word(lit.token.to_string())
@@ -893,16 +912,6 @@ impl<'a> State<'a> {
893912
State { s: pp::mk_printer(), comments: None, ann: &NoAnn }
894913
}
895914

896-
// Synthesizes a comment that was not textually present in the original source
897-
// file.
898-
pub fn synth_comment(&mut self, text: String) {
899-
self.s.word("/*");
900-
self.s.space();
901-
self.s.word(text);
902-
self.s.space();
903-
self.s.word("*/")
904-
}
905-
906915
crate fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
907916
where
908917
F: FnMut(&mut State<'_>, &T),
@@ -2920,29 +2929,6 @@ impl<'a> State<'a> {
29202929
self.end();
29212930
}
29222931

2923-
crate fn maybe_print_trailing_comment(
2924-
&mut self,
2925-
span: rustc_span::Span,
2926-
next_pos: Option<BytePos>,
2927-
) {
2928-
if let Some(cmnts) = self.comments() {
2929-
if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
2930-
self.print_comment(&cmnt);
2931-
}
2932-
}
2933-
}
2934-
2935-
crate fn print_remaining_comments(&mut self) {
2936-
// If there aren't any remaining comments, then we need to manually
2937-
// make sure there is a line break at the end.
2938-
if self.next_comment().is_none() {
2939-
self.s.hardbreak();
2940-
}
2941-
while let Some(ref cmnt) = self.next_comment() {
2942-
self.print_comment(cmnt);
2943-
}
2944-
}
2945-
29462932
crate fn print_fn_header_info(&mut self, header: ast::FnHeader) {
29472933
self.print_constness(header.constness);
29482934
self.print_asyncness(header.asyncness);

compiler/rustc_hir_pretty/src/lib.rs

+1-81
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::{GenericArg, GenericParam, GenericParamKind, Node};
1010
use rustc_hir::{GenericBound, PatKind, RangeEnd, TraitBoundModifier};
1111
use rustc_span::source_map::{SourceMap, Spanned};
1212
use rustc_span::symbol::{kw, Ident, IdentPrinter, Symbol};
13-
use rustc_span::{self, BytePos, FileName};
13+
use rustc_span::{self, FileName};
1414
use rustc_target::spec::abi::Abi;
1515

1616
use std::borrow::Cow;
@@ -241,36 +241,6 @@ pub fn enum_def_to_string(
241241
}
242242

243243
impl<'a> State<'a> {
244-
pub fn cbox(&mut self, u: usize) {
245-
self.s.cbox(u);
246-
}
247-
248-
pub fn nbsp(&mut self) {
249-
self.s.word(" ")
250-
}
251-
252-
pub fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) {
253-
self.s.word(w);
254-
self.nbsp()
255-
}
256-
257-
pub fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) {
258-
let w = w.into();
259-
// outer-box is consistent
260-
self.cbox(INDENT_UNIT);
261-
// head-box is inconsistent
262-
self.ibox(w.len() + 1);
263-
// keyword that starts the head
264-
if !w.is_empty() {
265-
self.word_nbsp(w);
266-
}
267-
}
268-
269-
pub fn bopen(&mut self) {
270-
self.s.word("{");
271-
self.end(); // close the head-box
272-
}
273-
274244
pub fn bclose_maybe_open(&mut self, span: rustc_span::Span, close_box: bool) {
275245
self.maybe_print_comment(span.hi());
276246
self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize));
@@ -284,33 +254,6 @@ impl<'a> State<'a> {
284254
self.bclose_maybe_open(span, true)
285255
}
286256

287-
pub fn space_if_not_bol(&mut self) {
288-
if !self.s.is_beginning_of_line() {
289-
self.s.space();
290-
}
291-
}
292-
293-
pub fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
294-
if !self.s.is_beginning_of_line() {
295-
self.s.break_offset(n, off)
296-
} else if off != 0 && self.s.last_token().is_hardbreak_tok() {
297-
// We do something pretty sketchy here: tuck the nonzero
298-
// offset-adjustment we were going to deposit along with the
299-
// break into the previous hardbreak.
300-
self.s.replace_last_token(pp::Printer::hardbreak_tok_offset(off));
301-
}
302-
}
303-
304-
// Synthesizes a comment that was not textually present in the original source
305-
// file.
306-
pub fn synth_comment(&mut self, text: String) {
307-
self.s.word("/*");
308-
self.s.space();
309-
self.s.word(text);
310-
self.s.space();
311-
self.s.word("*/")
312-
}
313-
314257
pub fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
315258
where
316259
F: FnMut(&mut State<'_>, &T),
@@ -2408,29 +2351,6 @@ impl<'a> State<'a> {
24082351
self.end();
24092352
}
24102353

2411-
pub fn maybe_print_trailing_comment(
2412-
&mut self,
2413-
span: rustc_span::Span,
2414-
next_pos: Option<BytePos>,
2415-
) {
2416-
if let Some(cmnts) = self.comments() {
2417-
if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
2418-
self.print_comment(&cmnt);
2419-
}
2420-
}
2421-
}
2422-
2423-
pub fn print_remaining_comments(&mut self) {
2424-
// If there aren't any remaining comments, then we need to manually
2425-
// make sure there is a line break at the end.
2426-
if self.next_comment().is_none() {
2427-
self.s.hardbreak();
2428-
}
2429-
while let Some(ref cmnt) = self.next_comment() {
2430-
self.print_comment(cmnt)
2431-
}
2432-
}
2433-
24342354
pub fn print_fn_header_info(&mut self, header: hir::FnHeader, vis: &hir::Visibility<'_>) {
24352355
self.s.word(visibility_qualified(vis, ""));
24362356

0 commit comments

Comments
 (0)