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.
This commit is contained in:
bors 2021-12-06 06:58:41 +00:00
commit ba9fc4fbfe
3 changed files with 30 additions and 114 deletions

View file

@ -35,4 +35,14 @@ pub fn word_nbsp<S: Into<Cow<'static, str>>>(&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<Cow<'static, str>>) {
self.word("/*");
self.space();
self.word(text);
self.space();
self.word("*/")
}
}

View file

@ -349,6 +349,25 @@ fn next_comment(&mut self) -> Option<Comment> {
self.comments().as_mut().and_then(|c| c.next())
}
fn maybe_print_trailing_comment(&mut self, span: rustc_span::Span, next_pos: Option<BytePos>) {
if let Some(cmnts) = self.comments() {
if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
self.print_comment(&cmnt);
}
}
}
fn print_remaining_comments(&mut self) {
// If there aren't any remaining comments, then we need to manually
// make sure there is a line break at the end.
if self.next_comment().is_none() {
self.hardbreak();
}
while let Some(ref cmnt) = self.next_comment() {
self.print_comment(cmnt)
}
}
fn print_literal(&mut self, lit: &ast::Lit) {
self.maybe_print_comment(lit.span.lo());
self.word(lit.token.to_string())
@ -893,16 +912,6 @@ pub fn new() -> State<'a> {
State { s: pp::mk_printer(), comments: None, ann: &NoAnn }
}
// Synthesizes a comment that was not textually present in the original source
// file.
pub fn synth_comment(&mut self, text: String) {
self.s.word("/*");
self.s.space();
self.s.word(text);
self.s.space();
self.s.word("*/")
}
crate fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
where
F: FnMut(&mut State<'_>, &T),
@ -2920,29 +2929,6 @@ pub fn print_mutability(&mut self, mutbl: ast::Mutability, print_const: bool) {
self.end();
}
crate fn maybe_print_trailing_comment(
&mut self,
span: rustc_span::Span,
next_pos: Option<BytePos>,
) {
if let Some(cmnts) = self.comments() {
if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
self.print_comment(&cmnt);
}
}
}
crate fn print_remaining_comments(&mut self) {
// If there aren't any remaining comments, then we need to manually
// make sure there is a line break at the end.
if self.next_comment().is_none() {
self.s.hardbreak();
}
while let Some(ref cmnt) = self.next_comment() {
self.print_comment(cmnt);
}
}
crate fn print_fn_header_info(&mut self, header: ast::FnHeader) {
self.print_constness(header.constness);
self.print_asyncness(header.asyncness);

View file

@ -10,7 +10,7 @@
use rustc_hir::{GenericBound, PatKind, RangeEnd, TraitBoundModifier};
use rustc_span::source_map::{SourceMap, Spanned};
use rustc_span::symbol::{kw, Ident, IdentPrinter, Symbol};
use rustc_span::{self, BytePos, FileName};
use rustc_span::{self, FileName};
use rustc_target::spec::abi::Abi;
use std::borrow::Cow;
@ -241,36 +241,6 @@ pub fn enum_def_to_string(
}
impl<'a> State<'a> {
pub fn cbox(&mut self, u: usize) {
self.s.cbox(u);
}
pub fn nbsp(&mut self) {
self.s.word(" ")
}
pub fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) {
self.s.word(w);
self.nbsp()
}
pub fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) {
let w = w.into();
// outer-box is consistent
self.cbox(INDENT_UNIT);
// head-box is inconsistent
self.ibox(w.len() + 1);
// keyword that starts the head
if !w.is_empty() {
self.word_nbsp(w);
}
}
pub fn bopen(&mut self) {
self.s.word("{");
self.end(); // close the head-box
}
pub fn bclose_maybe_open(&mut self, span: rustc_span::Span, close_box: bool) {
self.maybe_print_comment(span.hi());
self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize));
@ -284,33 +254,6 @@ pub fn bclose(&mut self, span: rustc_span::Span) {
self.bclose_maybe_open(span, true)
}
pub fn space_if_not_bol(&mut self) {
if !self.s.is_beginning_of_line() {
self.s.space();
}
}
pub fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
if !self.s.is_beginning_of_line() {
self.s.break_offset(n, off)
} else if off != 0 && self.s.last_token().is_hardbreak_tok() {
// We do something pretty sketchy here: tuck the nonzero
// offset-adjustment we were going to deposit along with the
// break into the previous hardbreak.
self.s.replace_last_token(pp::Printer::hardbreak_tok_offset(off));
}
}
// Synthesizes a comment that was not textually present in the original source
// file.
pub fn synth_comment(&mut self, text: String) {
self.s.word("/*");
self.s.space();
self.s.word(text);
self.s.space();
self.s.word("*/")
}
pub fn commasep_cmnt<T, F, G>(&mut self, b: Breaks, elts: &[T], mut op: F, mut get_span: G)
where
F: FnMut(&mut State<'_>, &T),
@ -2408,29 +2351,6 @@ pub fn print_ty_fn(
self.end();
}
pub fn maybe_print_trailing_comment(
&mut self,
span: rustc_span::Span,
next_pos: Option<BytePos>,
) {
if let Some(cmnts) = self.comments() {
if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) {
self.print_comment(&cmnt);
}
}
}
pub fn print_remaining_comments(&mut self) {
// If there aren't any remaining comments, then we need to manually
// make sure there is a line break at the end.
if self.next_comment().is_none() {
self.s.hardbreak();
}
while let Some(ref cmnt) = self.next_comment() {
self.print_comment(cmnt)
}
}
pub fn print_fn_header_info(&mut self, header: hir::FnHeader, vis: &hir::Visibility<'_>) {
self.s.word(visibility_qualified(vis, ""));