Skip to content

Commit cab4532

Browse files
Move pp::Printer helpers to direct impl
1 parent e91dbc5 commit cab4532

File tree

10 files changed

+64
-53
lines changed

10 files changed

+64
-53
lines changed

src/librustc/hir/map/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1212,8 +1212,6 @@ impl<'a> print::State<'a> {
12121212
Node::Pat(a) => self.print_pat(&a),
12131213
Node::Arm(a) => self.print_arm(&a),
12141214
Node::Block(a) => {
1215-
use syntax::print::pprust::PrintState;
1216-
12171215
// containing cbox, will be closed by print-block at }
12181216
self.cbox(print::indent_unit);
12191217
// head-ibox, will be closed by print-block after {

src/librustc/hir/print.rs

+13
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ pub struct State<'a> {
7373
ann: &'a (dyn PpAnn + 'a),
7474
}
7575

76+
impl std::ops::Deref for State<'_> {
77+
type Target = pp::Printer;
78+
fn deref(&self) -> &Self::Target {
79+
&self.s
80+
}
81+
}
82+
83+
impl std::ops::DerefMut for State<'_> {
84+
fn deref_mut(&mut self) -> &mut Self::Target {
85+
&mut self.s
86+
}
87+
}
88+
7689
impl<'a> PrintState<'a> for State<'a> {
7790
fn writer(&mut self) -> &mut pp::Printer {
7891
&mut self.s

src/librustc_borrowck/dataflow.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc::cfg::CFGIndex;
88
use rustc::ty::TyCtxt;
99
use std::mem;
1010
use std::usize;
11-
use syntax::print::pprust::PrintState;
1211
use log::debug;
1312

1413
use rustc_data_structures::graph::implementation::OUTGOING;

src/librustc_driver/pretty.rs

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_mir::util::{write_mir_pretty, write_mir_graphviz};
1919
use syntax::ast;
2020
use syntax::mut_visit::MutVisitor;
2121
use syntax::print::{pprust};
22-
use syntax::print::pprust::PrintState;
2322
use syntax_pos::FileName;
2423

2524
use graphviz as dot;

src/libsyntax/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ pub mod visit;
162162
pub mod print {
163163
pub mod pp;
164164
pub mod pprust;
165+
mod helpers;
165166
}
166167

167168
pub mod ext {

src/libsyntax/parse/diagnostics.rs

-2
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,6 @@ impl<'a> Parser<'a> {
611611
match ty.node {
612612
TyKind::Rptr(ref lifetime, ref mut_ty) => {
613613
let sum_with_parens = pprust::to_string(|s| {
614-
use crate::print::pprust::PrintState;
615-
616614
s.s.word("&");
617615
s.print_opt_lifetime(lifetime);
618616
s.print_mutability(mut_ty.mutbl);

src/libsyntax/parse/parser.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2571,7 +2571,6 @@ impl<'a> Parser<'a> {
25712571
None => continue,
25722572
};
25732573
let sugg = pprust::to_string(|s| {
2574-
use crate::print::pprust::PrintState;
25752574
s.popen();
25762575
s.print_expr(&e);
25772576
s.s.word( ".");
@@ -4588,7 +4587,7 @@ impl<'a> Parser<'a> {
45884587
stmt_span = stmt_span.with_hi(self.prev_span.hi());
45894588
}
45904589
let sugg = pprust::to_string(|s| {
4591-
use crate::print::pprust::{PrintState, INDENT_UNIT};
4590+
use crate::print::pprust::INDENT_UNIT;
45924591
s.ibox(INDENT_UNIT);
45934592
s.bopen();
45944593
s.print_stmt(&stmt);

src/libsyntax/print/helpers.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::borrow::Cow;
2+
use crate::print::pp::Printer;
3+
4+
impl Printer {
5+
pub fn word_space<W: Into<Cow<'static, str>>>(&mut self, w: W) {
6+
self.word(w);
7+
self.space();
8+
}
9+
10+
pub fn popen(&mut self) {
11+
self.word("(");
12+
}
13+
14+
pub fn pclose(&mut self) {
15+
self.word(")");
16+
}
17+
18+
pub fn hardbreak_if_not_bol(&mut self) {
19+
if !self.is_beginning_of_line() {
20+
self.hardbreak()
21+
}
22+
}
23+
24+
pub fn space_if_not_bol(&mut self) {
25+
if !self.is_beginning_of_line() { self.space(); }
26+
}
27+
28+
pub fn nbsp(&mut self) { self.word(" ") }
29+
30+
pub fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) {
31+
self.word(w);
32+
self.nbsp()
33+
}
34+
}

src/libsyntax/print/pp.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -597,15 +597,15 @@ impl Printer {
597597
// Convenience functions to talk to the printer.
598598

599599
/// "raw box"
600-
crate fn rbox(&mut self, indent: usize, b: Breaks) {
600+
pub fn rbox(&mut self, indent: usize, b: Breaks) {
601601
self.scan_begin(BeginToken {
602602
offset: indent as isize,
603603
breaks: b
604604
})
605605
}
606606

607607
/// Inconsistent breaking box
608-
crate fn ibox(&mut self, indent: usize) {
608+
pub fn ibox(&mut self, indent: usize) {
609609
self.rbox(indent, Breaks::Inconsistent)
610610
}
611611

@@ -621,7 +621,7 @@ impl Printer {
621621
})
622622
}
623623

624-
crate fn end(&mut self) {
624+
pub fn end(&mut self) {
625625
self.scan_end()
626626
}
627627

src/libsyntax/print/pprust.rs

+12-42
Original file line numberDiff line numberDiff line change
@@ -432,37 +432,22 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
432432
format!("{}{}", to_string(|s| s.print_visibility(vis)), s)
433433
}
434434

435-
pub trait PrintState<'a> {
436-
fn writer(&mut self) -> &mut pp::Printer;
437-
fn comments(&mut self) -> &mut Option<Comments<'a>>;
438-
439-
fn word_space<S: Into<Cow<'static, str>>>(&mut self, w: S) {
440-
self.writer().word(w);
441-
self.writer().space()
442-
}
443-
444-
fn popen(&mut self) { self.writer().word("(") }
445-
446-
fn pclose(&mut self) { self.writer().word(")") }
447-
448-
fn hardbreak_if_not_bol(&mut self) {
449-
if !self.writer().is_beginning_of_line() {
450-
self.writer().hardbreak()
451-
}
452-
}
453-
454-
// "raw box"
455-
fn rbox(&mut self, u: usize, b: pp::Breaks) {
456-
self.writer().rbox(u, b)
435+
impl std::ops::Deref for State<'_> {
436+
type Target = pp::Printer;
437+
fn deref(&self) -> &Self::Target {
438+
&self.s
457439
}
440+
}
458441

459-
fn ibox(&mut self, u: usize) {
460-
self.writer().ibox(u);
442+
impl std::ops::DerefMut for State<'_> {
443+
fn deref_mut(&mut self) -> &mut Self::Target {
444+
&mut self.s
461445
}
446+
}
462447

463-
fn end(&mut self) {
464-
self.writer().end()
465-
}
448+
pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefMut {
449+
fn writer(&mut self) -> &mut pp::Printer;
450+
fn comments(&mut self) -> &mut Option<Comments<'a>>;
466451

467452
fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F)
468453
where F: FnMut(&mut Self, &T),
@@ -728,12 +713,6 @@ pub trait PrintState<'a> {
728713
}
729714
self.end();
730715
}
731-
732-
fn space_if_not_bol(&mut self) {
733-
if !self.writer().is_beginning_of_line() { self.writer().space(); }
734-
}
735-
736-
fn nbsp(&mut self) { self.writer().word(" ") }
737716
}
738717

739718
impl<'a> PrintState<'a> for State<'a> {
@@ -747,15 +726,6 @@ impl<'a> PrintState<'a> for State<'a> {
747726
}
748727

749728
impl<'a> State<'a> {
750-
pub fn cbox(&mut self, u: usize) {
751-
self.s.cbox(u);
752-
}
753-
754-
crate fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) {
755-
self.s.word(w);
756-
self.nbsp()
757-
}
758-
759729
crate fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) {
760730
let w = w.into();
761731
// outer-box is consistent

0 commit comments

Comments
 (0)