Skip to content

Commit

Permalink
Remove writer function from PrintState
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-Simulacrum committed Jul 10, 2019
1 parent 73c1752 commit 096cb41
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
6 changes: 1 addition & 5 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ impl std::ops::DerefMut for State<'_> {
}

impl<'a> PrintState<'a> for State<'a> {
fn writer(&mut self) -> &mut pp::Printer {
&mut self.s
}

fn comments(&mut self) -> &mut Option<Comments<'a>> {
&mut self.comments
}
Expand Down Expand Up @@ -1182,7 +1178,7 @@ impl<'a> State<'a> {

fn print_literal(&mut self, lit: &hir::Lit) {
self.maybe_print_comment(lit.span.lo());
self.writer().word(lit.node.to_lit_token().to_string())
self.word(lit.node.to_lit_token().to_string())
}

pub fn print_expr(&mut self, expr: &hir::Expr) {
Expand Down
69 changes: 32 additions & 37 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ impl std::ops::DerefMut for State<'_> {
}

pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefMut {
fn writer(&mut self) -> &mut pp::Printer;
fn comments(&mut self) -> &mut Option<Comments<'a>>;

fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F)
Expand Down Expand Up @@ -476,51 +475,51 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
match cmnt.style {
comments::Mixed => {
assert_eq!(cmnt.lines.len(), 1);
self.writer().zerobreak();
self.writer().word(cmnt.lines[0].clone());
self.writer().zerobreak()
self.zerobreak();
self.word(cmnt.lines[0].clone());
self.zerobreak()
}
comments::Isolated => {
self.hardbreak_if_not_bol();
for line in &cmnt.lines {
// Don't print empty lines because they will end up as trailing
// whitespace
if !line.is_empty() {
self.writer().word(line.clone());
self.word(line.clone());
}
self.writer().hardbreak();
self.hardbreak();
}
}
comments::Trailing => {
if !self.writer().is_beginning_of_line() {
self.writer().word(" ");
if !self.is_beginning_of_line() {
self.word(" ");
}
if cmnt.lines.len() == 1 {
self.writer().word(cmnt.lines[0].clone());
self.writer().hardbreak()
self.word(cmnt.lines[0].clone());
self.hardbreak()
} else {
self.ibox(0);
for line in &cmnt.lines {
if !line.is_empty() {
self.writer().word(line.clone());
self.word(line.clone());
}
self.writer().hardbreak();
self.hardbreak();
}
self.end();
}
}
comments::BlankLine => {
// We need to do at least one, possibly two hardbreaks.
let twice = match self.writer().last_token() {
let twice = match self.last_token() {
pp::Token::String(s) => ";" == s,
pp::Token::Begin(_) => true,
pp::Token::End => true,
_ => false
};
if twice {
self.writer().hardbreak();
self.hardbreak();
}
self.writer().hardbreak();
self.hardbreak();
}
}
if let Some(cm) = self.comments() {
Expand All @@ -534,7 +533,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM

fn print_literal(&mut self, lit: &ast::Lit) {
self.maybe_print_comment(lit.span.lo());
self.writer().word(lit.token.to_string())
self.word(lit.token.to_string())
}

fn print_string(&mut self, st: &str,
Expand All @@ -549,7 +548,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
string=st))
}
};
self.writer().word(st)
self.word(st)
}

fn print_inner_attributes(&mut self,
Expand Down Expand Up @@ -601,10 +600,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
fn print_attribute_path(&mut self, path: &ast::Path) {
for (i, segment) in path.segments.iter().enumerate() {
if i > 0 {
self.writer().word("::");
self.word("::");
}
if segment.ident.name != kw::PathRoot {
self.writer().word(ident_to_string(segment.ident, segment.ident.is_raw_guess()));
self.word(ident_to_string(segment.ident, segment.ident.is_raw_guess()));
}
}
}
Expand All @@ -620,21 +619,21 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
}
self.maybe_print_comment(attr.span.lo());
if attr.is_sugared_doc {
self.writer().word(attr.value_str().unwrap().as_str().to_string());
self.writer().hardbreak()
self.word(attr.value_str().unwrap().as_str().to_string());
self.hardbreak()
} else {
match attr.style {
ast::AttrStyle::Inner => self.writer().word("#!["),
ast::AttrStyle::Outer => self.writer().word("#["),
ast::AttrStyle::Inner => self.word("#!["),
ast::AttrStyle::Outer => self.word("#["),
}
if let Some(mi) = attr.meta() {
self.print_meta_item(&mi);
} else {
self.print_attribute_path(&attr.path);
self.writer().space();
self.space();
self.print_tts(attr.tokens.clone());
}
self.writer().word("]");
self.word("]");
}
}

Expand All @@ -655,7 +654,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
ast::MetaItemKind::Word => self.print_attribute_path(&item.path),
ast::MetaItemKind::NameValue(ref value) => {
self.print_attribute_path(&item.path);
self.writer().space();
self.space();
self.word_space("=");
self.print_literal(value);
}
Expand All @@ -681,20 +680,20 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
fn print_tt(&mut self, tt: tokenstream::TokenTree, convert_dollar_crate: bool) {
match tt {
TokenTree::Token(ref token) => {
self.writer().word(token_to_string_ext(&token, convert_dollar_crate));
self.word(token_to_string_ext(&token, convert_dollar_crate));
match token.kind {
token::DocComment(..) => {
self.writer().hardbreak()
self.hardbreak()
}
_ => {}
}
}
TokenTree::Delimited(_, delim, tts) => {
self.writer().word(token_kind_to_string(&token::OpenDelim(delim)));
self.writer().space();
self.word(token_kind_to_string(&token::OpenDelim(delim)));
self.space();
self.print_tts(tts);
self.writer().space();
self.writer().word(token_kind_to_string(&token::CloseDelim(delim)))
self.space();
self.word(token_kind_to_string(&token::CloseDelim(delim)))
},
}
}
Expand All @@ -707,7 +706,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
self.ibox(0);
for (i, tt) in tts.into_trees().enumerate() {
if i != 0 {
self.writer().space();
self.space();
}
self.print_tt(tt, convert_dollar_crate);
}
Expand All @@ -716,10 +715,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
}

impl<'a> PrintState<'a> for State<'a> {
fn writer(&mut self) -> &mut pp::Printer {
&mut self.s
}

fn comments(&mut self) -> &mut Option<Comments<'a>> {
&mut self.comments
}
Expand Down

0 comments on commit 096cb41

Please sign in to comment.