Skip to content

Commit

Permalink
Auto merge of rust-lang#91565 - dtolnay:printhelpers, r=jackh726
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Dec 6, 2021
2 parents 87dce6e + 596e33a commit ba9fc4f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 114 deletions.
10 changes: 10 additions & 0 deletions compiler/rustc_ast_pretty/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ impl Printer {
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("*/")
}
}
52 changes: 19 additions & 33 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,25 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
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())
Expand Down Expand Up @@ -893,16 +912,6 @@ impl<'a> 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),
Expand Down Expand Up @@ -2920,29 +2929,6 @@ impl<'a> State<'a> {
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);
Expand Down
82 changes: 1 addition & 81 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hir::{GenericArg, GenericParam, GenericParamKind, Node};
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;
Expand Down Expand Up @@ -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));
Expand All @@ -284,33 +254,6 @@ impl<'a> State<'a> {
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),
Expand Down Expand Up @@ -2408,29 +2351,6 @@ impl<'a> State<'a> {
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, ""));

Expand Down

0 comments on commit ba9fc4f

Please sign in to comment.