From cab453250a3ceae5cf0cf7eac836c03b37e4ca8e Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:26:50 -0400 Subject: [PATCH] Move pp::Printer helpers to direct impl --- src/librustc/hir/map/mod.rs | 2 -- src/librustc/hir/print.rs | 13 +++++++ src/librustc_borrowck/dataflow.rs | 1 - src/librustc_driver/pretty.rs | 1 - src/libsyntax/lib.rs | 1 + src/libsyntax/parse/diagnostics.rs | 2 -- src/libsyntax/parse/parser.rs | 3 +- src/libsyntax/print/helpers.rs | 34 ++++++++++++++++++ src/libsyntax/print/pp.rs | 6 ++-- src/libsyntax/print/pprust.rs | 58 ++++++++---------------------- 10 files changed, 66 insertions(+), 55 deletions(-) create mode 100644 src/libsyntax/print/helpers.rs diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 63f60d0ab95..92898e9070b 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -1212,8 +1212,6 @@ pub fn print_node(&mut self, node: Node<'_>) { Node::Pat(a) => self.print_pat(&a), Node::Arm(a) => self.print_arm(&a), Node::Block(a) => { - use syntax::print::pprust::PrintState; - // containing cbox, will be closed by print-block at } self.cbox(print::indent_unit); // head-ibox, will be closed by print-block after { diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 246b3341a23..e014e7478db 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -73,6 +73,19 @@ pub struct State<'a> { ann: &'a (dyn PpAnn + 'a), } +impl std::ops::Deref for State<'_> { + type Target = pp::Printer; + fn deref(&self) -> &Self::Target { + &self.s + } +} + +impl std::ops::DerefMut for State<'_> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.s + } +} + impl<'a> PrintState<'a> for State<'a> { fn writer(&mut self) -> &mut pp::Printer { &mut self.s diff --git a/src/librustc_borrowck/dataflow.rs b/src/librustc_borrowck/dataflow.rs index f1f9f3f71e4..6eea64e0e7a 100644 --- a/src/librustc_borrowck/dataflow.rs +++ b/src/librustc_borrowck/dataflow.rs @@ -8,7 +8,6 @@ use rustc::ty::TyCtxt; use std::mem; use std::usize; -use syntax::print::pprust::PrintState; use log::debug; use rustc_data_structures::graph::implementation::OUTGOING; diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index cd38eb695eb..51b161c3768 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -19,7 +19,6 @@ use syntax::ast; use syntax::mut_visit::MutVisitor; use syntax::print::{pprust}; -use syntax::print::pprust::PrintState; use syntax_pos::FileName; use graphviz as dot; diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index a7c5ed158e0..ee7fb97ffd7 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -162,6 +162,7 @@ pub mod util { pub mod print { pub mod pp; pub mod pprust; + mod helpers; } pub mod ext { diff --git a/src/libsyntax/parse/diagnostics.rs b/src/libsyntax/parse/diagnostics.rs index edcdb18a037..ae24047ac82 100644 --- a/src/libsyntax/parse/diagnostics.rs +++ b/src/libsyntax/parse/diagnostics.rs @@ -611,8 +611,6 @@ fn tokens_to_string(tokens: &[TokenType]) -> String { match ty.node { TyKind::Rptr(ref lifetime, ref mut_ty) => { let sum_with_parens = pprust::to_string(|s| { - use crate::print::pprust::PrintState; - s.s.word("&"); s.print_opt_lifetime(lifetime); s.print_mutability(mut_ty.mutbl); diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a95b6891fb9..7dd7000b454 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2571,7 +2571,6 @@ fn parse_dot_or_call_expr_with_(&mut self, e0: P, lo: Span) -> PResult<'a, None => continue, }; let sugg = pprust::to_string(|s| { - use crate::print::pprust::PrintState; s.popen(); s.print_expr(&e); s.s.word( "."); @@ -4588,7 +4587,7 @@ pub fn parse_block(&mut self) -> PResult<'a, P> { stmt_span = stmt_span.with_hi(self.prev_span.hi()); } let sugg = pprust::to_string(|s| { - use crate::print::pprust::{PrintState, INDENT_UNIT}; + use crate::print::pprust::INDENT_UNIT; s.ibox(INDENT_UNIT); s.bopen(); s.print_stmt(&stmt); diff --git a/src/libsyntax/print/helpers.rs b/src/libsyntax/print/helpers.rs new file mode 100644 index 00000000000..3449e07f456 --- /dev/null +++ b/src/libsyntax/print/helpers.rs @@ -0,0 +1,34 @@ +use std::borrow::Cow; +use crate::print::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() + } +} diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index be5a0098578..660e77f77d0 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -597,7 +597,7 @@ fn print(&mut self, token: Token, l: isize) { // Convenience functions to talk to the printer. /// "raw box" - crate fn rbox(&mut self, indent: usize, b: Breaks) { + pub fn rbox(&mut self, indent: usize, b: Breaks) { self.scan_begin(BeginToken { offset: indent as isize, breaks: b @@ -605,7 +605,7 @@ fn print(&mut self, token: Token, l: isize) { } /// Inconsistent breaking box - crate fn ibox(&mut self, indent: usize) { + pub fn ibox(&mut self, indent: usize) { self.rbox(indent, Breaks::Inconsistent) } @@ -621,7 +621,7 @@ pub fn break_offset(&mut self, n: usize, off: isize) { }) } - crate fn end(&mut self) { + pub fn end(&mut self) { self.scan_end() } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 288417fcd89..98c629addc8 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -432,38 +432,23 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { format!("{}{}", to_string(|s| s.print_visibility(vis)), s) } -pub trait PrintState<'a> { +impl std::ops::Deref for State<'_> { + type Target = pp::Printer; + fn deref(&self) -> &Self::Target { + &self.s + } +} + +impl std::ops::DerefMut for State<'_> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.s + } +} + +pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefMut { fn writer(&mut self) -> &mut pp::Printer; fn comments(&mut self) -> &mut Option>; - fn word_space>>(&mut self, w: S) { - self.writer().word(w); - self.writer().space() - } - - fn popen(&mut self) { self.writer().word("(") } - - fn pclose(&mut self) { self.writer().word(")") } - - fn hardbreak_if_not_bol(&mut self) { - if !self.writer().is_beginning_of_line() { - self.writer().hardbreak() - } - } - - // "raw box" - fn rbox(&mut self, u: usize, b: pp::Breaks) { - self.writer().rbox(u, b) - } - - fn ibox(&mut self, u: usize) { - self.writer().ibox(u); - } - - fn end(&mut self) { - self.writer().end() - } - fn commasep(&mut self, b: Breaks, elts: &[T], mut op: F) where F: FnMut(&mut Self, &T), { @@ -728,12 +713,6 @@ fn print_tts_ext(&mut self, tts: tokenstream::TokenStream, convert_dollar_crate: } self.end(); } - - fn space_if_not_bol(&mut self) { - if !self.writer().is_beginning_of_line() { self.writer().space(); } - } - - fn nbsp(&mut self) { self.writer().word(" ") } } impl<'a> PrintState<'a> for State<'a> { @@ -747,15 +726,6 @@ fn comments(&mut self) -> &mut Option> { } impl<'a> State<'a> { - pub fn cbox(&mut self, u: usize) { - self.s.cbox(u); - } - - crate fn word_nbsp>>(&mut self, w: S) { - self.s.word(w); - self.nbsp() - } - crate fn head>>(&mut self, w: S) { let w = w.into(); // outer-box is consistent