From 9b0ebfa4e9e4906497eed5c6848bcca3cca23464 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 26 Jun 2019 07:23:27 -0400 Subject: Move literal_to_string to fmt::Display --- src/libsyntax/parse/literal.rs | 2 +- src/libsyntax/parse/token.rs | 28 ++++++++++++++++++++++++++++ src/libsyntax/print/pprust.rs | 6 +----- 3 files changed, 30 insertions(+), 6 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/literal.rs b/src/libsyntax/parse/literal.rs index ef55bf6b929..683d1641565 100644 --- a/src/libsyntax/parse/literal.rs +++ b/src/libsyntax/parse/literal.rs @@ -344,7 +344,7 @@ impl<'a> Parser<'a> { // Pack possible quotes and prefixes from the original literal into // the error literal's symbol so they can be pretty-printed faithfully. let suffixless_lit = token::Lit::new(lit.kind, lit.symbol, None); - let symbol = Symbol::intern(&pprust::literal_to_string(suffixless_lit)); + let symbol = Symbol::intern(&suffixless_lit.to_string()); let lit = token::Lit::new(token::Err, symbol, lit.suffix); Lit::from_lit_token(lit, span).map_err(|_| unreachable!()) } diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index ebd0decacb5..fccb556b95a 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -80,6 +80,34 @@ pub struct Lit { pub suffix: Option, } +impl fmt::Display for Lit { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let Lit { kind, symbol, suffix } = *self; + match kind { + Byte => write!(f, "b'{}'", symbol)?, + Char => write!(f, "'{}'", symbol)?, + Str => write!(f, "\"{}\"", symbol)?, + StrRaw(n) => write!(f, "r{delim}\"{string}\"{delim}", + delim="#".repeat(n as usize), + string=symbol)?, + ByteStr => write!(f, "b\"{}\"", symbol)?, + ByteStrRaw(n) => write!(f, "br{delim}\"{string}\"{delim}", + delim="#".repeat(n as usize), + string=symbol)?, + Integer | + Float | + Bool | + Err => write!(f, "{}", symbol)?, + } + + if let Some(suffix) = suffix { + write!(f, "{}", suffix)?; + } + + Ok(()) + } +} + impl LitKind { /// An English article for the literal token kind. crate fn article(self) -> &'static str { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 7e099bc4d50..a69dd94cfb6 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -426,10 +426,6 @@ pub fn attribute_to_string(attr: &ast::Attribute) -> String { to_string(|s| s.print_attribute(attr)) } -pub fn lit_to_string(l: &ast::Lit) -> String { - to_string(|s| s.print_literal(l)) -} - pub fn variant_to_string(var: &ast::Variant) -> String { to_string(|s| s.print_variant(var)) } @@ -597,7 +593,7 @@ pub trait PrintState<'a> { fn print_literal(&mut self, lit: &ast::Lit) { self.maybe_print_comment(lit.span.lo()); - self.writer().word(literal_to_string(lit.token)) + self.writer().word(lit.token.to_string()) } fn print_string(&mut self, st: &str, -- cgit 1.4.1-3-g733a5 From 0f10d114e447d5cdaf1de499e2d88b815aef7239 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 12:16:41 -0400 Subject: Remove duplicate attr_to_string attribute_to_string exists. --- src/libsyntax/parse/token.rs | 2 +- src/libsyntax/print/pprust.rs | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index fccb556b95a..472e4b474d6 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -816,7 +816,7 @@ fn prepend_attrs(sess: &ParseSess, assert_eq!(attr.style, ast::AttrStyle::Outer, "inner attributes should prevent cached tokens from existing"); - let source = pprust::attr_to_string(attr); + let source = pprust::attribute_to_string(attr); let macro_filename = FileName::macro_expansion_source_code(&source); if attr.is_sugared_doc { let stream = parse_stream_from_source_str(macro_filename, source, sess, Some(span)); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index a69dd94cfb6..4a3abe505ca 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -350,10 +350,6 @@ pub fn stmt_to_string(stmt: &ast::Stmt) -> String { to_string(|s| s.print_stmt(stmt)) } -pub fn attr_to_string(attr: &ast::Attribute) -> String { - to_string(|s| s.print_attribute(attr)) -} - pub fn item_to_string(i: &ast::Item) -> String { to_string(|s| s.print_item(i)) } -- cgit 1.4.1-3-g733a5 From a440337e2bfc41e59a001a1460231ef920978ad5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 15:43:13 -0400 Subject: Remove unused arm_to_string --- src/libsyntax/print/pprust.rs | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 4a3abe505ca..512023ff030 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -322,10 +322,6 @@ pub fn pat_to_string(pat: &ast::Pat) -> String { to_string(|s| s.print_pat(pat)) } -pub fn arm_to_string(arm: &ast::Arm) -> String { - to_string(|s| s.print_arm(arm)) -} - pub fn expr_to_string(e: &ast::Expr) -> String { to_string(|s| s.print_expr(e)) } -- cgit 1.4.1-3-g733a5 From 11d521676fbc371a7dffa195ee94075dc0558862 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 15:42:28 -0400 Subject: Move lifetime_to_string to Display impl --- src/libsyntax/ast.rs | 8 +++++++- src/libsyntax/print/pprust.rs | 4 ---- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index c627596bbdf..8801e89a0cf 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -50,11 +50,17 @@ impl fmt::Debug for Lifetime { f, "lifetime({}: {})", self.id, - pprust::lifetime_to_string(self) + self ) } } +impl fmt::Display for Lifetime { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.ident.name.as_str()) + } +} + /// A "Path" is essentially Rust's notion of a name. /// /// It's represented as a sequence of identifiers, diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 512023ff030..16610e22048 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -326,10 +326,6 @@ pub fn expr_to_string(e: &ast::Expr) -> String { to_string(|s| s.print_expr(e)) } -pub fn lifetime_to_string(lt: &ast::Lifetime) -> String { - to_string(|s| s.print_lifetime(*lt)) -} - pub fn tt_to_string(tt: tokenstream::TokenTree) -> String { to_string(|s| s.print_tt(tt, false)) } -- cgit 1.4.1-3-g733a5 From 44839802853954ca0eea629bb9d94068966f2960 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 16:00:38 -0400 Subject: Privatize and remove unused functions --- src/libsyntax/print/pprust.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 16610e22048..1d28b52362c 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -346,11 +346,11 @@ pub fn item_to_string(i: &ast::Item) -> String { to_string(|s| s.print_item(i)) } -pub fn impl_item_to_string(i: &ast::ImplItem) -> String { +fn impl_item_to_string(i: &ast::ImplItem) -> String { to_string(|s| s.print_impl_item(i)) } -pub fn trait_item_to_string(i: &ast::TraitItem) -> String { +fn trait_item_to_string(i: &ast::TraitItem) -> String { to_string(|s| s.print_trait_item(i)) } @@ -358,14 +358,6 @@ pub fn generic_params_to_string(generic_params: &[ast::GenericParam]) -> String to_string(|s| s.print_generic_params(generic_params)) } -pub fn where_clause_to_string(i: &ast::WhereClause) -> String { - to_string(|s| s.print_where_clause(i)) -} - -pub fn fn_block_to_string(p: &ast::FnDecl) -> String { - to_string(|s| s.print_fn_block_args(p)) -} - pub fn path_to_string(p: &ast::Path) -> String { to_string(|s| s.print_path(p, false, 0)) } @@ -378,7 +370,8 @@ pub fn vis_to_string(v: &ast::Visibility) -> String { to_string(|s| s.print_visibility(v)) } -pub fn fun_to_string(decl: &ast::FnDecl, +#[cfg(test)] +fn fun_to_string(decl: &ast::FnDecl, header: ast::FnHeader, name: ast::Ident, generics: &ast::Generics) @@ -392,7 +385,7 @@ pub fn fun_to_string(decl: &ast::FnDecl, }) } -pub fn block_to_string(blk: &ast::Block) -> String { +fn block_to_string(blk: &ast::Block) -> String { to_string(|s| { // containing cbox, will be closed by print-block at } s.cbox(INDENT_UNIT); @@ -414,7 +407,8 @@ pub fn attribute_to_string(attr: &ast::Attribute) -> String { to_string(|s| s.print_attribute(attr)) } -pub fn variant_to_string(var: &ast::Variant) -> String { +#[cfg(test)] +fn variant_to_string(var: &ast::Variant) -> String { to_string(|s| s.print_variant(var)) } @@ -422,15 +416,11 @@ pub fn arg_to_string(arg: &ast::Arg) -> String { to_string(|s| s.print_arg(arg, false)) } -pub fn mac_to_string(arg: &ast::Mac) -> String { - to_string(|s| s.print_mac(arg)) -} - -pub fn foreign_item_to_string(arg: &ast::ForeignItem) -> String { +fn foreign_item_to_string(arg: &ast::ForeignItem) -> String { to_string(|s| s.print_foreign_item(arg)) } -pub fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { +fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { format!("{}{}", to_string(|s| s.print_visibility(vis)), s) } -- cgit 1.4.1-3-g733a5 From a573d143a2b7ef9d04a8fe6904667762e47157bc Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 16:14:47 -0400 Subject: Remove unused boxes vector --- src/librustc/hir/print.rs | 15 --------------- src/libsyntax/print/pprust.rs | 12 ------------ 2 files changed, 27 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 827bbea7bd2..137f20d6c32 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -74,7 +74,6 @@ pub struct State<'a> { cm: Option<&'a SourceMap>, comments: Option>, cur_cmnt: usize, - boxes: Vec, ann: &'a (dyn PpAnn + 'a), } @@ -83,10 +82,6 @@ impl<'a> PrintState<'a> for State<'a> { &mut self.s } - fn boxes(&mut self) -> &mut Vec { - &mut self.boxes - } - fn comments(&mut self) -> &mut Option> { &mut self.comments } @@ -141,7 +136,6 @@ impl<'a> State<'a> { cm: Some(cm), comments, cur_cmnt: 0, - boxes: Vec::new(), ann, } } @@ -157,7 +151,6 @@ pub fn to_string(ann: &dyn PpAnn, f: F) -> String cm: None, comments: None, cur_cmnt: 0, - boxes: Vec::new(), ann, }; f(&mut printer); @@ -175,7 +168,6 @@ pub fn visibility_qualified>>(vis: &hir::Visibility, w impl<'a> State<'a> { pub fn cbox(&mut self, u: usize) { - self.boxes.push(pp::Breaks::Consistent); self.s.cbox(u); } @@ -226,13 +218,6 @@ impl<'a> State<'a> { self.bclose_(span, indent_unit) } - pub fn in_cbox(&self) -> bool { - match self.boxes.last() { - Some(&last_box) => last_box == pp::Breaks::Consistent, - None => false, - } - } - pub fn space_if_not_bol(&mut self) { if !self.is_bol() { self.s.space(); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 1d28b52362c..4c86300b805 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -48,7 +48,6 @@ pub struct State<'a> { cm: Option<&'a SourceMap>, comments: Option>, cur_cmnt: usize, - boxes: Vec, ann: &'a (dyn PpAnn+'a), is_expanded: bool } @@ -113,7 +112,6 @@ impl<'a> State<'a> { cm: Some(cm), comments, cur_cmnt: 0, - boxes: Vec::new(), ann, is_expanded, } @@ -130,7 +128,6 @@ pub fn to_string(f: F) -> String where cm: None, comments: None, cur_cmnt: 0, - boxes: Vec::new(), ann: &NoAnn, is_expanded: false }; @@ -426,7 +423,6 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { pub trait PrintState<'a> { fn writer(&mut self) -> &mut pp::Printer<'a>; - fn boxes(&mut self) -> &mut Vec; fn comments(&mut self) -> &mut Option>; fn cur_cmnt(&mut self) -> &mut usize; @@ -466,17 +462,14 @@ pub trait PrintState<'a> { // "raw box" fn rbox(&mut self, u: usize, b: pp::Breaks) { - self.boxes().push(b); self.writer().rbox(u, b) } fn ibox(&mut self, u: usize) { - self.boxes().push(pp::Breaks::Inconsistent); self.writer().ibox(u); } fn end(&mut self) { - self.boxes().pop().unwrap(); self.writer().end() } @@ -763,10 +756,6 @@ impl<'a> PrintState<'a> for State<'a> { &mut self.s } - fn boxes(&mut self) -> &mut Vec { - &mut self.boxes - } - fn comments(&mut self) -> &mut Option> { &mut self.comments } @@ -778,7 +767,6 @@ impl<'a> PrintState<'a> for State<'a> { impl<'a> State<'a> { pub fn cbox(&mut self, u: usize) { - self.boxes.push(pp::Breaks::Consistent); self.s.cbox(u); } -- cgit 1.4.1-3-g733a5 From 59b161c7c84e48b9379fbed877a2fa5c13db0526 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 16:32:15 -0400 Subject: Stop Option-wrapping comments We always check against the length before indexing anyway. --- src/librustc/hir/print.rs | 8 ++++---- src/libsyntax/print/pprust.rs | 24 ++++++++++-------------- 2 files changed, 14 insertions(+), 18 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 137f20d6c32..9acf6d895e3 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -72,7 +72,7 @@ impl PpAnn for hir::Crate { pub struct State<'a> { pub s: pp::Printer<'a>, cm: Option<&'a SourceMap>, - comments: Option>, + comments: Vec, cur_cmnt: usize, ann: &'a (dyn PpAnn + 'a), } @@ -82,7 +82,7 @@ impl<'a> PrintState<'a> for State<'a> { &mut self.s } - fn comments(&mut self) -> &mut Option> { + fn comments(&mut self) -> &mut Vec { &mut self.comments } @@ -134,7 +134,7 @@ impl<'a> State<'a> { State { s: pp::mk_printer(out), cm: Some(cm), - comments, + comments: comments.unwrap_or_default(), cur_cmnt: 0, ann, } @@ -149,7 +149,7 @@ pub fn to_string(ann: &dyn PpAnn, f: F) -> String let mut printer = State { s: pp::mk_printer(&mut wr), cm: None, - comments: None, + comments: Vec::new(), cur_cmnt: 0, ann, }; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 4c86300b805..460c4434712 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -46,7 +46,7 @@ impl PpAnn for NoAnn {} pub struct State<'a> { pub s: pp::Printer<'a>, cm: Option<&'a SourceMap>, - comments: Option>, + comments: Vec, cur_cmnt: usize, ann: &'a (dyn PpAnn+'a), is_expanded: bool @@ -110,7 +110,7 @@ impl<'a> State<'a> { State { s: pp::mk_printer(out), cm: Some(cm), - comments, + comments: comments.unwrap_or_default(), cur_cmnt: 0, ann, is_expanded, @@ -126,7 +126,7 @@ pub fn to_string(f: F) -> String where let mut printer = State { s: pp::mk_printer(&mut wr), cm: None, - comments: None, + comments: Vec::new(), cur_cmnt: 0, ann: &NoAnn, is_expanded: false @@ -423,7 +423,7 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { pub trait PrintState<'a> { fn writer(&mut self) -> &mut pp::Printer<'a>; - fn comments(&mut self) -> &mut Option>; + fn comments(&mut self) -> &mut Vec; fn cur_cmnt(&mut self) -> &mut usize; fn word_space>>(&mut self, w: S) { @@ -550,15 +550,11 @@ pub trait PrintState<'a> { fn next_comment(&mut self) -> Option { let cur_cmnt = *self.cur_cmnt(); - match *self.comments() { - Some(ref cmnts) => { - if cur_cmnt < cmnts.len() { - Some(cmnts[cur_cmnt].clone()) - } else { - None - } - } - _ => None + let cmnts = &*self.comments(); + if cur_cmnt < cmnts.len() { + Some(cmnts[cur_cmnt].clone()) + } else { + None } } @@ -756,7 +752,7 @@ impl<'a> PrintState<'a> for State<'a> { &mut self.s } - fn comments(&mut self) -> &mut Option> { + fn comments(&mut self) -> &mut Vec { &mut self.comments } -- cgit 1.4.1-3-g733a5 From 9b5e39723dc8193a2c80289b062f87d618a59d11 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 16:35:23 -0400 Subject: Inline State::new There was only one callsite for each and this removes the unwrap_or_default's on the comments argument --- src/librustc/hir/print.rs | 10 +--------- src/libsyntax/print/pprust.rs | 10 +--------- 2 files changed, 2 insertions(+), 18 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 9acf6d895e3..c12983ba1d3 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -123,18 +123,10 @@ impl<'a> State<'a> { ann: &'a dyn PpAnn) -> State<'a> { let comments = comments::gather_comments(sess, filename, input); - State::new(cm, out, ann, Some(comments)) - } - - pub fn new(cm: &'a SourceMap, - out: &'a mut String, - ann: &'a dyn PpAnn, - comments: Option>) - -> State<'a> { State { s: pp::mk_printer(out), cm: Some(cm), - comments: comments.unwrap_or_default(), + comments: comments, cur_cmnt: 0, ann, } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 460c4434712..b7bbcc2c555 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -99,18 +99,10 @@ impl<'a> State<'a> { ann: &'a dyn PpAnn, is_expanded: bool) -> State<'a> { let comments = comments::gather_comments(sess, filename, input); - State::new(cm, out, ann, Some(comments), is_expanded) - } - - pub fn new(cm: &'a SourceMap, - out: &'a mut String, - ann: &'a dyn PpAnn, - comments: Option>, - is_expanded: bool) -> State<'a> { State { s: pp::mk_printer(out), cm: Some(cm), - comments: comments.unwrap_or_default(), + comments, cur_cmnt: 0, ann, is_expanded, -- cgit 1.4.1-3-g733a5 From 0eb2e566c1d9ee6526e670802debda9c0afabde5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 18:29:15 -0400 Subject: Combine comment-handling logic into struct This also permits sharing the underlying code between pprust and hir::print. --- src/librustc/hir/print.rs | 42 ++++--------------- src/libsyntax/print/pprust.rs | 94 ++++++++++++++++++++++++++----------------- 2 files changed, 65 insertions(+), 71 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index c12983ba1d3..3af72dbb43c 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -2,10 +2,9 @@ use rustc_target::spec::abi::Abi; use syntax::ast; use syntax::source_map::{SourceMap, Spanned}; use syntax::parse::ParseSess; -use syntax::parse::lexer::comments; use syntax::print::pp::{self, Breaks}; use syntax::print::pp::Breaks::{Consistent, Inconsistent}; -use syntax::print::pprust::PrintState; +use syntax::print::pprust::{Comments, PrintState}; use syntax::symbol::kw; use syntax::util::parser::{self, AssocOp, Fixity}; use syntax_pos::{self, BytePos, FileName}; @@ -71,9 +70,7 @@ impl PpAnn for hir::Crate { pub struct State<'a> { pub s: pp::Printer<'a>, - cm: Option<&'a SourceMap>, - comments: Vec, - cur_cmnt: usize, + comments: Option>, ann: &'a (dyn PpAnn + 'a), } @@ -82,13 +79,9 @@ impl<'a> PrintState<'a> for State<'a> { &mut self.s } - fn comments(&mut self) -> &mut Vec { + fn comments(&mut self) -> &mut Option> { &mut self.comments } - - fn cur_cmnt(&mut self) -> &mut usize { - &mut self.cur_cmnt - } } #[allow(non_upper_case_globals)] @@ -122,12 +115,9 @@ impl<'a> State<'a> { out: &'a mut String, ann: &'a dyn PpAnn) -> State<'a> { - let comments = comments::gather_comments(sess, filename, input); State { s: pp::mk_printer(out), - cm: Some(cm), - comments: comments, - cur_cmnt: 0, + comments: Some(Comments::new(cm, sess, filename, input)), ann, } } @@ -140,9 +130,7 @@ pub fn to_string(ann: &dyn PpAnn, f: F) -> String { let mut printer = State { s: pp::mk_printer(&mut wr), - cm: None, - comments: Vec::new(), - cur_cmnt: 0, + comments: None, ann, }; f(&mut printer); @@ -2151,23 +2139,9 @@ impl<'a> State<'a> { span: syntax_pos::Span, next_pos: Option) { - let cm = match self.cm { - Some(cm) => cm, - _ => return, - }; - if let Some(ref cmnt) = self.next_comment() { - if (*cmnt).style != comments::Trailing { - return; - } - let span_line = cm.lookup_char_pos(span.hi()); - let comment_line = cm.lookup_char_pos((*cmnt).pos); - let mut next = (*cmnt).pos + BytePos(1); - if let Some(p) = next_pos { - next = p; - } - if span.hi() < (*cmnt).pos && (*cmnt).pos < next && - span_line.line == comment_line.line { - self.print_comment(cmnt); + if let Some(cmnts) = self.comments() { + if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) { + self.print_comment(&cmnt); } } } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index b7bbcc2c555..638384acc4c 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -43,11 +43,53 @@ pub struct NoAnn; impl PpAnn for NoAnn {} +pub struct Comments<'a> { + cm: &'a SourceMap, + comments: Vec, + current: usize, +} + +impl<'a> Comments<'a> { + pub fn new( + cm: &'a SourceMap, + sess: &ParseSess, + filename: FileName, + input: String, + ) -> Comments<'a> { + let comments = comments::gather_comments(sess, filename, input); + Comments { + cm, + comments, + current: 0, + } + } + + pub fn next(&self) -> Option { + self.comments.get(self.current).cloned() + } + + pub fn trailing_comment( + &mut self, + span: syntax_pos::Span, + next_pos: Option, + ) -> Option { + if let Some(cmnt) = self.next() { + if cmnt.style != comments::Trailing { return None; } + let span_line = self.cm.lookup_char_pos(span.hi()); + let comment_line = self.cm.lookup_char_pos(cmnt.pos); + let next = next_pos.unwrap_or_else(|| cmnt.pos + BytePos(1)); + if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line { + return Some(cmnt); + } + } + + None + } +} + pub struct State<'a> { pub s: pp::Printer<'a>, - cm: Option<&'a SourceMap>, - comments: Vec, - cur_cmnt: usize, + comments: Option>, ann: &'a (dyn PpAnn+'a), is_expanded: bool } @@ -98,12 +140,9 @@ impl<'a> State<'a> { out: &'a mut String, ann: &'a dyn PpAnn, is_expanded: bool) -> State<'a> { - let comments = comments::gather_comments(sess, filename, input); State { s: pp::mk_printer(out), - cm: Some(cm), - comments, - cur_cmnt: 0, + comments: Some(Comments::new(cm, sess, filename, input)), ann, is_expanded, } @@ -117,9 +156,7 @@ pub fn to_string(f: F) -> String where { let mut printer = State { s: pp::mk_printer(&mut wr), - cm: None, - comments: Vec::new(), - cur_cmnt: 0, + comments: None, ann: &NoAnn, is_expanded: false }; @@ -415,8 +452,7 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { pub trait PrintState<'a> { fn writer(&mut self) -> &mut pp::Printer<'a>; - fn comments(&mut self) -> &mut Vec; - fn cur_cmnt(&mut self) -> &mut usize; + fn comments(&mut self) -> &mut Option>; fn word_space>>(&mut self, w: S) { self.writer().word(w); @@ -537,17 +573,13 @@ pub trait PrintState<'a> { self.writer().hardbreak(); } } - *self.cur_cmnt() = *self.cur_cmnt() + 1; + if let Some(cm) = self.comments() { + cm.current += 1; + } } fn next_comment(&mut self) -> Option { - let cur_cmnt = *self.cur_cmnt(); - let cmnts = &*self.comments(); - if cur_cmnt < cmnts.len() { - Some(cmnts[cur_cmnt].clone()) - } else { - None - } + self.comments().as_mut().and_then(|c| c.next()) } fn print_literal(&mut self, lit: &ast::Lit) { @@ -744,13 +776,9 @@ impl<'a> PrintState<'a> for State<'a> { &mut self.s } - fn comments(&mut self) -> &mut Vec { + fn comments(&mut self) -> &mut Option> { &mut self.comments } - - fn cur_cmnt(&mut self) -> &mut usize { - &mut self.cur_cmnt - } } impl<'a> State<'a> { @@ -2913,18 +2941,10 @@ impl<'a> State<'a> { crate fn maybe_print_trailing_comment(&mut self, span: syntax_pos::Span, next_pos: Option) - { - let cm = match self.cm { - Some(cm) => cm, - _ => return, - }; - if let Some(ref cmnt) = self.next_comment() { - if cmnt.style != comments::Trailing { return; } - let span_line = cm.lookup_char_pos(span.hi()); - let comment_line = cm.lookup_char_pos(cmnt.pos); - let next = next_pos.unwrap_or_else(|| cmnt.pos + BytePos(1)); - if span.hi() < cmnt.pos && cmnt.pos < next && span_line.line == comment_line.line { - self.print_comment(cmnt); + { + if let Some(cmnts) = self.comments() { + if let Some(cmnt) = cmnts.trailing_comment(span, next_pos) { + self.print_comment(&cmnt); } } } -- cgit 1.4.1-3-g733a5 From 7e3791469fce12a95d5d85b9a8744a61a0970fe1 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 17:12:11 -0400 Subject: Replace src: &mut dyn Read with String --- src/librustc/hir/print.rs | 5 ++--- src/librustc_driver/pretty.rs | 21 ++++++++++----------- src/libsyntax/parse/lexer/comments.rs | 5 +---- src/libsyntax/print/pprust.rs | 5 ++--- 4 files changed, 15 insertions(+), 21 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 3af72dbb43c..fc3430566a0 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -16,7 +16,6 @@ use crate::hir::ptr::P; use std::borrow::Cow; use std::cell::Cell; -use std::io::Read; use std::vec; pub enum AnnNode<'a> { @@ -93,7 +92,7 @@ pub fn print_crate<'a>(cm: &'a SourceMap, sess: &ParseSess, krate: &hir::Crate, filename: FileName, - input: &mut dyn Read, + input: String, out: &'a mut String, ann: &'a dyn PpAnn) { @@ -111,7 +110,7 @@ impl<'a> State<'a> { pub fn new_from_input(cm: &'a SourceMap, sess: &ParseSess, filename: FileName, - input: &mut dyn Read, + input: String, out: &'a mut String, ann: &'a dyn PpAnn) -> State<'a> { diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index fc55d5ac355..df8dc3871b7 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -687,16 +687,14 @@ pub fn visit_crate(sess: &Session, krate: &mut ast::Crate, ppm: PpMode) { } } -fn get_source(input: &Input, sess: &Session) -> (Vec, FileName) { +fn get_source(input: &Input, sess: &Session) -> (String, FileName) { let src_name = source_name(input); - let src = sess.source_map() + let src = String::clone(&sess.source_map() .get_source_file(&src_name) .unwrap() .src .as_ref() - .unwrap() - .as_bytes() - .to_vec(); + .unwrap()); (src, src_name) } @@ -719,7 +717,6 @@ pub fn print_after_parsing(sess: &Session, ofile: Option<&Path>) { let (src, src_name) = get_source(input, sess); - let mut rdr = &*src; let mut out = String::new(); if let PpmSource(s) = ppm { @@ -732,7 +729,7 @@ pub fn print_after_parsing(sess: &Session, &sess.parse_sess, krate, src_name, - &mut rdr, + src, out, annotation.pp_ann(), false) @@ -764,13 +761,13 @@ pub fn print_after_hir_lowering<'tcx>( let (src, src_name) = get_source(input, tcx.sess); - let mut rdr = &src[..]; let mut out = String::new(); match (ppm, opt_uii) { (PpmSource(s), _) => { // Silently ignores an identified node. let out = &mut out; + let src = src.clone(); s.call_with_pp_support(tcx.sess, Some(tcx), move |annotation| { debug!("pretty printing source code {:?}", s); let sess = annotation.sess(); @@ -778,7 +775,7 @@ pub fn print_after_hir_lowering<'tcx>( &sess.parse_sess, krate, src_name, - &mut rdr, + src, out, annotation.pp_ann(), true) @@ -787,6 +784,7 @@ pub fn print_after_hir_lowering<'tcx>( (PpmHir(s), None) => { let out = &mut out; + let src = src.clone(); s.call_with_pp_support_hir(tcx, move |annotation, krate| { debug!("pretty printing source code {:?}", s); let sess = annotation.sess(); @@ -794,7 +792,7 @@ pub fn print_after_hir_lowering<'tcx>( &sess.parse_sess, krate, src_name, - &mut rdr, + src, out, annotation.pp_ann()) }) @@ -810,6 +808,7 @@ pub fn print_after_hir_lowering<'tcx>( (PpmHir(s), Some(uii)) => { let out = &mut out; + let src = src.clone(); s.call_with_pp_support_hir(tcx, move |annotation, _| { debug!("pretty printing source code {:?}", s); let sess = annotation.sess(); @@ -817,7 +816,7 @@ pub fn print_after_hir_lowering<'tcx>( let mut pp_state = pprust_hir::State::new_from_input(sess.source_map(), &sess.parse_sess, src_name, - &mut rdr, + src, out, annotation.pp_ann()); for node_id in uii.all_matching_node_ids(hir_map) { diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index 988f1aa38d9..6ed2a7adad1 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -8,7 +8,6 @@ use crate::parse::lexer::{self, ParseSess, StringReader}; use syntax_pos::{BytePos, CharPos, Pos, FileName}; use log::debug; -use std::io::Read; use std::usize; #[derive(Clone, Copy, PartialEq, Debug)] @@ -340,10 +339,8 @@ fn consume_comment(rdr: &mut StringReader<'_>, // it appears this function is called only from pprust... that's // probably not a good thing. -pub fn gather_comments(sess: &ParseSess, path: FileName, srdr: &mut dyn Read) -> Vec +pub fn gather_comments(sess: &ParseSess, path: FileName, src: String) -> Vec { - let mut src = String::new(); - srdr.read_to_string(&mut src).unwrap(); let cm = SourceMap::new(sess.source_map().path_mapping().clone()); let source_file = cm.new_source_file(path, src); let mut rdr = lexer::StringReader::new(sess, source_file, None); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 638384acc4c..a6680ee02b5 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -21,7 +21,6 @@ use syntax_pos::{self, BytePos}; use syntax_pos::{DUMMY_SP, FileName, Span}; use std::borrow::Cow; -use std::io::Read; pub enum AnnNode<'a> { Ident(&'a ast::Ident), @@ -102,7 +101,7 @@ pub fn print_crate<'a>(cm: &'a SourceMap, sess: &ParseSess, krate: &ast::Crate, filename: FileName, - input: &mut dyn Read, + input: String, out: &mut String, ann: &'a dyn PpAnn, is_expanded: bool) { @@ -136,7 +135,7 @@ impl<'a> State<'a> { pub fn new_from_input(cm: &'a SourceMap, sess: &ParseSess, filename: FileName, - input: &mut dyn Read, + input: String, out: &'a mut String, ann: &'a dyn PpAnn, is_expanded: bool) -> State<'a> { -- cgit 1.4.1-3-g733a5 From e0db2e606cdb242db1466ae51129c9a6b81560fe Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 17:48:21 -0400 Subject: print_crate returns String instead of taking an out pointer --- src/librustc/hir/print.rs | 10 +++++----- src/librustc_driver/pretty.rs | 9 +++------ src/libsyntax/print/pprust.rs | 9 +++++---- 3 files changed, 13 insertions(+), 15 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index fc3430566a0..01cdd8a6f22 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -93,17 +93,17 @@ pub fn print_crate<'a>(cm: &'a SourceMap, krate: &hir::Crate, filename: FileName, input: String, - out: &'a mut String, - ann: &'a dyn PpAnn) - { - let mut s = State::new_from_input(cm, sess, filename, input, out, ann); + ann: &'a dyn PpAnn) -> String { + let mut out = String::new(); + let mut s = State::new_from_input(cm, sess, filename, input, &mut out, ann); // When printing the AST, we sometimes need to inject `#[no_std]` here. // Since you can't compile the HIR, it's not necessary. s.print_mod(&krate.module, &krate.attrs); s.print_remaining_comments(); - s.s.eof() + s.s.eof(); + out } impl<'a> State<'a> { diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index df8dc3871b7..3e6d843ffbe 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -725,12 +725,11 @@ pub fn print_after_parsing(sess: &Session, s.call_with_pp_support(sess, None, move |annotation| { debug!("pretty printing source code {:?}", s); let sess = annotation.sess(); - pprust::print_crate(sess.source_map(), + *out = pprust::print_crate(sess.source_map(), &sess.parse_sess, krate, src_name, src, - out, annotation.pp_ann(), false) }) @@ -771,12 +770,11 @@ pub fn print_after_hir_lowering<'tcx>( s.call_with_pp_support(tcx.sess, Some(tcx), move |annotation| { debug!("pretty printing source code {:?}", s); let sess = annotation.sess(); - pprust::print_crate(sess.source_map(), + *out = pprust::print_crate(sess.source_map(), &sess.parse_sess, krate, src_name, src, - out, annotation.pp_ann(), true) }) @@ -788,12 +786,11 @@ pub fn print_after_hir_lowering<'tcx>( s.call_with_pp_support_hir(tcx, move |annotation, krate| { debug!("pretty printing source code {:?}", s); let sess = annotation.sess(); - pprust_hir::print_crate(sess.source_map(), + *out = pprust_hir::print_crate(sess.source_map(), &sess.parse_sess, krate, src_name, src, - out, annotation.pp_ann()) }) } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index a6680ee02b5..555276234f7 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -102,10 +102,10 @@ pub fn print_crate<'a>(cm: &'a SourceMap, krate: &ast::Crate, filename: FileName, input: String, - out: &mut String, ann: &'a dyn PpAnn, - is_expanded: bool) { - let mut s = State::new_from_input(cm, sess, filename, input, out, ann, is_expanded); + is_expanded: bool) -> String { + let mut out = String::new(); + let mut s = State::new_from_input(cm, sess, filename, input, &mut out, ann, is_expanded); if is_expanded && std_inject::injected_crate_name().is_some() { // We need to print `#![no_std]` (and its feature gate) so that @@ -128,7 +128,8 @@ pub fn print_crate<'a>(cm: &'a SourceMap, s.print_mod(&krate.module, &krate.attrs); s.print_remaining_comments(); - s.s.eof() + s.s.eof(); + out } impl<'a> State<'a> { -- cgit 1.4.1-3-g733a5 From e0ffa7c3d2115b9a1d22b6a5de288aa696abf50f Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 18:32:04 -0400 Subject: Inline State::new_from_input in pprust This function took too many arguments and are simple on the inside; inlining them makes complexity go down. hir::print's copy is unfortunately used from librustc_driver so inlining it is not as straightforward. --- src/libsyntax/print/pprust.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 555276234f7..43714d3015b 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -105,7 +105,12 @@ pub fn print_crate<'a>(cm: &'a SourceMap, ann: &'a dyn PpAnn, is_expanded: bool) -> String { let mut out = String::new(); - let mut s = State::new_from_input(cm, sess, filename, input, &mut out, ann, is_expanded); + let mut s = State { + s: pp::mk_printer(&mut out), + comments: Some(Comments::new(cm, sess, filename, input)), + ann, + is_expanded, + }; if is_expanded && std_inject::injected_crate_name().is_some() { // We need to print `#![no_std]` (and its feature gate) so that @@ -132,23 +137,6 @@ pub fn print_crate<'a>(cm: &'a SourceMap, out } -impl<'a> State<'a> { - pub fn new_from_input(cm: &'a SourceMap, - sess: &ParseSess, - filename: FileName, - input: String, - out: &'a mut String, - ann: &'a dyn PpAnn, - is_expanded: bool) -> State<'a> { - State { - s: pp::mk_printer(out), - comments: Some(Comments::new(cm, sess, filename, input)), - ann, - is_expanded, - } - } -} - pub fn to_string(f: F) -> String where F: FnOnce(&mut State<'_>), { -- cgit 1.4.1-3-g733a5 From 00ca508608381f9594dc4a60d3c5b436660a2e19 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 19:10:18 -0400 Subject: Move pp::Printer out field to owned String This enforces that eof() must be called to get the String out, and generally is better from an API perspective. No users of pretty printing pre-allocate the buffer. --- src/librustc/hir/print.rs | 31 ++++++++++++------------------- src/librustc_driver/pretty.rs | 3 +-- src/libsyntax/print/pp.rs | 15 ++++++++------- src/libsyntax/print/pprust.rs | 32 +++++++++++++------------------- 4 files changed, 34 insertions(+), 47 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 01cdd8a6f22..8342331e360 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -68,13 +68,13 @@ impl PpAnn for hir::Crate { } pub struct State<'a> { - pub s: pp::Printer<'a>, + pub s: pp::Printer, comments: Option>, ann: &'a (dyn PpAnn + 'a), } impl<'a> PrintState<'a> for State<'a> { - fn writer(&mut self) -> &mut pp::Printer<'a> { + fn writer(&mut self) -> &mut pp::Printer { &mut self.s } @@ -94,16 +94,14 @@ pub fn print_crate<'a>(cm: &'a SourceMap, filename: FileName, input: String, ann: &'a dyn PpAnn) -> String { - let mut out = String::new(); - let mut s = State::new_from_input(cm, sess, filename, input, &mut out, ann); + let mut s = State::new_from_input(cm, sess, filename, input, ann); // When printing the AST, we sometimes need to inject `#[no_std]` here. // Since you can't compile the HIR, it's not necessary. s.print_mod(&krate.module, &krate.attrs); s.print_remaining_comments(); - s.s.eof(); - out + s.s.eof() } impl<'a> State<'a> { @@ -111,11 +109,10 @@ impl<'a> State<'a> { sess: &ParseSess, filename: FileName, input: String, - out: &'a mut String, ann: &'a dyn PpAnn) -> State<'a> { State { - s: pp::mk_printer(out), + s: pp::mk_printer(), comments: Some(Comments::new(cm, sess, filename, input)), ann, } @@ -125,17 +122,13 @@ impl<'a> State<'a> { pub fn to_string(ann: &dyn PpAnn, f: F) -> String where F: FnOnce(&mut State<'_>) { - let mut wr = String::new(); - { - let mut printer = State { - s: pp::mk_printer(&mut wr), - comments: None, - ann, - }; - f(&mut printer); - printer.s.eof(); - } - wr + let mut printer = State { + s: pp::mk_printer(), + comments: None, + ann, + }; + f(&mut printer); + printer.s.eof() } pub fn visibility_qualified>>(vis: &hir::Visibility, w: S) -> String { diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index 3e6d843ffbe..cd38eb695eb 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -814,7 +814,6 @@ pub fn print_after_hir_lowering<'tcx>( &sess.parse_sess, src_name, src, - out, annotation.pp_ann()); for node_id in uii.all_matching_node_ids(hir_map) { let hir_id = tcx.hir().node_to_hir_id(node_id); @@ -826,7 +825,7 @@ pub fn print_after_hir_lowering<'tcx>( pp_state.synth_comment(path); pp_state.s.hardbreak(); } - pp_state.s.eof(); + *out = pp_state.s.eof(); }) } diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index f64e95aee5b..ea90defcd50 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -235,13 +235,13 @@ crate struct PrintStackElem { const SIZE_INFINITY: isize = 0xffff; -pub fn mk_printer(out: &mut String) -> Printer<'_> { +pub fn mk_printer() -> Printer { let linewidth = 78; // Yes 55, it makes the ring buffers big enough to never fall behind. let n: usize = 55 * linewidth; debug!("mk_printer {}", linewidth); Printer { - out, + out: String::new(), buf_max_len: n, margin: linewidth as isize, space: linewidth as isize, @@ -258,8 +258,8 @@ pub fn mk_printer(out: &mut String) -> Printer<'_> { } } -pub struct Printer<'a> { - out: &'a mut String, +pub struct Printer { + out: String, buf_max_len: usize, /// Width of lines we're constrained to margin: isize, @@ -300,7 +300,7 @@ impl Default for BufEntry { } } -impl<'a> Printer<'a> { +impl Printer { pub fn last_token(&mut self) -> Token { self.buf[self.right].token.clone() } @@ -629,8 +629,9 @@ impl<'a> Printer<'a> { self.pretty_print_end() } - pub fn eof(&mut self) { - self.pretty_print_eof() + pub fn eof(mut self) -> String { + self.pretty_print_eof(); + self.out } pub fn word>>(&mut self, wrd: S) { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 43714d3015b..54672d9da2e 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -87,7 +87,7 @@ impl<'a> Comments<'a> { } pub struct State<'a> { - pub s: pp::Printer<'a>, + pub s: pp::Printer, comments: Option>, ann: &'a (dyn PpAnn+'a), is_expanded: bool @@ -104,9 +104,8 @@ pub fn print_crate<'a>(cm: &'a SourceMap, input: String, ann: &'a dyn PpAnn, is_expanded: bool) -> String { - let mut out = String::new(); let mut s = State { - s: pp::mk_printer(&mut out), + s: pp::mk_printer(), comments: Some(Comments::new(cm, sess, filename, input)), ann, is_expanded, @@ -133,25 +132,20 @@ pub fn print_crate<'a>(cm: &'a SourceMap, s.print_mod(&krate.module, &krate.attrs); s.print_remaining_comments(); - s.s.eof(); - out + s.s.eof() } pub fn to_string(f: F) -> String where F: FnOnce(&mut State<'_>), { - let mut wr = String::new(); - { - let mut printer = State { - s: pp::mk_printer(&mut wr), - comments: None, - ann: &NoAnn, - is_expanded: false - }; - f(&mut printer); - printer.s.eof(); - } - wr + let mut printer = State { + s: pp::mk_printer(), + comments: None, + ann: &NoAnn, + is_expanded: false + }; + f(&mut printer); + printer.s.eof() } fn binop_to_string(op: BinOpToken) -> &'static str { @@ -439,7 +433,7 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { } pub trait PrintState<'a> { - fn writer(&mut self) -> &mut pp::Printer<'a>; + fn writer(&mut self) -> &mut pp::Printer; fn comments(&mut self) -> &mut Option>; fn word_space>>(&mut self, w: S) { @@ -760,7 +754,7 @@ pub trait PrintState<'a> { } impl<'a> PrintState<'a> for State<'a> { - fn writer(&mut self) -> &mut pp::Printer<'a> { + fn writer(&mut self) -> &mut pp::Printer { &mut self.s } -- cgit 1.4.1-3-g733a5 From 4c58fc32ae07653b35ed3156da7bdb9a78ad1b05 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 22:27:36 -0400 Subject: Fully privatize (vs. crate visibility) functions --- src/libsyntax/print/pp.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index ea90defcd50..6af524406e2 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -222,13 +222,13 @@ fn buf_str(buf: &[BufEntry], left: usize, right: usize, lim: usize) -> String { } #[derive(Copy, Clone)] -crate enum PrintStackBreak { +enum PrintStackBreak { Fits, Broken(Breaks), } #[derive(Copy, Clone)] -crate struct PrintStackElem { +struct PrintStackElem { offset: isize, pbreak: PrintStackBreak } @@ -380,7 +380,7 @@ impl Printer { } } - crate fn check_stream(&mut self) { + fn check_stream(&mut self) { debug!("check_stream Vec<{}, {}> with left_total={}, right_total={}", self.left, self.right, self.left_total, self.right_total); if self.right_total - self.left_total > self.space { @@ -398,24 +398,24 @@ impl Printer { } } - crate fn scan_push(&mut self, x: usize) { + fn scan_push(&mut self, x: usize) { debug!("scan_push {}", x); self.scan_stack.push_front(x); } - crate fn scan_pop(&mut self) -> usize { + fn scan_pop(&mut self) -> usize { self.scan_stack.pop_front().unwrap() } - crate fn scan_top(&mut self) -> usize { + fn scan_top(&mut self) -> usize { *self.scan_stack.front().unwrap() } - crate fn scan_pop_bottom(&mut self) -> usize { + fn scan_pop_bottom(&mut self) -> usize { self.scan_stack.pop_back().unwrap() } - crate fn advance_right(&mut self) { + fn advance_right(&mut self) { self.right += 1; self.right %= self.buf_max_len; // Extend the buf if necessary. @@ -425,7 +425,7 @@ impl Printer { assert_ne!(self.right, self.left); } - crate fn advance_left(&mut self) { + fn advance_left(&mut self) { debug!("advance_left Vec<{},{}>, sizeof({})={}", self.left, self.right, self.left, self.buf[self.left].size); @@ -458,7 +458,7 @@ impl Printer { } } - crate fn check_stack(&mut self, k: isize) { + fn check_stack(&mut self, k: isize) { if !self.scan_stack.is_empty() { let x = self.scan_top(); match self.buf[x].token { @@ -486,19 +486,19 @@ impl Printer { } } - crate fn print_newline(&mut self, amount: isize) { + fn print_newline(&mut self, amount: isize) { debug!("NEWLINE {}", amount); self.out.push('\n'); self.pending_indentation = 0; self.indent(amount); } - crate fn indent(&mut self, amount: isize) { + fn indent(&mut self, amount: isize) { debug!("INDENT {}", amount); self.pending_indentation += amount; } - crate fn get_top(&mut self) -> PrintStackElem { + fn get_top(&mut self) -> PrintStackElem { match self.print_stack.last() { Some(el) => *el, None => PrintStackElem { @@ -508,7 +508,7 @@ impl Printer { } } - crate fn print_begin(&mut self, b: BeginToken, l: isize) { + fn print_begin(&mut self, b: BeginToken, l: isize) { if l > self.space { let col = self.margin - self.space + b.offset; debug!("print Begin -> push broken block at col {}", col); @@ -525,14 +525,14 @@ impl Printer { } } - crate fn print_end(&mut self) { + fn print_end(&mut self) { debug!("print End -> pop End"); let print_stack = &mut self.print_stack; assert!(!print_stack.is_empty()); print_stack.pop().unwrap(); } - crate fn print_break(&mut self, b: BreakToken, l: isize) { + fn print_break(&mut self, b: BreakToken, l: isize) { let top = self.get_top(); match top.pbreak { PrintStackBreak::Fits => { @@ -562,7 +562,7 @@ impl Printer { } } - crate fn print_string(&mut self, s: Cow<'static, str>, len: isize) { + fn print_string(&mut self, s: Cow<'static, str>, len: isize) { debug!("print String({})", s); // assert!(len <= space); self.space -= len; @@ -579,7 +579,7 @@ impl Printer { self.out.push_str(&s); } - crate fn print(&mut self, token: Token, l: isize) { + fn print(&mut self, token: Token, l: isize) { debug!("print {} {} (remaining line space={})", token, l, self.space); debug!("{}", buf_str(&self.buf, -- cgit 1.4.1-3-g733a5 From ccf279d9c34c929dd6fdc07f00574de8c267d0b4 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 22:31:45 -0400 Subject: Remove useless call to indent --- src/libsyntax/print/pp.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index 6af524406e2..e26978f1ffc 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -315,7 +315,6 @@ impl Printer { self.check_stack(0); self.advance_left(); } - self.indent(0); } fn pretty_print_begin(&mut self, b: BeginToken) { -- cgit 1.4.1-3-g733a5 From cd2d8326a5bdc309c6726c9345380fd73c5c1bb5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 6 Jul 2019 07:28:25 -0400 Subject: Move BufEntry assignment into scan_push --- src/libsyntax/print/pp.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index e26978f1ffc..27139c1fc90 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -328,9 +328,7 @@ impl Printer { } debug!("pp Begin({})/buffer Vec<{},{}>", b.offset, self.left, self.right); - self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total }; - let right = self.right; - self.scan_push(right); + self.scan_push(BufEntry { token: Token::Begin(b), size: -self.right_total }); } fn pretty_print_end(&mut self) { @@ -340,9 +338,7 @@ impl Printer { } else { debug!("pp End/buffer Vec<{},{}>", self.left, self.right); self.advance_right(); - self.buf[self.right] = BufEntry { token: Token::End, size: -1 }; - let right = self.right; - self.scan_push(right); + self.scan_push(BufEntry { token: Token::End, size: -1 }); } } @@ -358,9 +354,7 @@ impl Printer { debug!("pp Break({})/buffer Vec<{},{}>", b.offset, self.left, self.right); self.check_stack(0); - let right = self.right; - self.scan_push(right); - self.buf[self.right] = BufEntry { token: Token::Break(b), size: -self.right_total }; + self.scan_push(BufEntry { token: Token::Break(b), size: -self.right_total }); self.right_total += b.blank_space; } @@ -397,9 +391,10 @@ impl Printer { } } - fn scan_push(&mut self, x: usize) { - debug!("scan_push {}", x); - self.scan_stack.push_front(x); + fn scan_push(&mut self, entry: BufEntry) { + debug!("scan_push {}", self.right); + self.buf[self.right] = entry; + self.scan_stack.push_front(self.right); } fn scan_pop(&mut self) -> usize { -- cgit 1.4.1-3-g733a5 From 55a6a761b94e543cbdfca6dab7e3e2226b5f437f Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 7 Jul 2019 10:15:47 -0400 Subject: Simplify check_stack implementation --- src/libsyntax/print/pp.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index 27139c1fc90..bff291348d7 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -452,26 +452,26 @@ impl Printer { } } - fn check_stack(&mut self, k: isize) { + fn check_stack(&mut self, k: usize) { if !self.scan_stack.is_empty() { let x = self.scan_top(); match self.buf[x].token { Token::Begin(_) => { if k > 0 { - let popped = self.scan_pop(); - self.buf[popped].size = self.buf[x].size + self.right_total; + self.scan_pop(); + self.buf[x].size += self.right_total; self.check_stack(k - 1); } } Token::End => { // paper says + not =, but that makes no sense. - let popped = self.scan_pop(); - self.buf[popped].size = 1; + self.scan_pop(); + self.buf[x].size = 1; self.check_stack(k + 1); } _ => { - let popped = self.scan_pop(); - self.buf[popped].size = self.buf[x].size + self.right_total; + self.scan_pop(); + self.buf[x].size += self.right_total; if k > 0 { self.check_stack(k); } -- cgit 1.4.1-3-g733a5 From 57cf7a2e5728e5b4d77230ffb1e372385f4f24dd Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 7 Jul 2019 10:38:09 -0400 Subject: Simplify print_end Presumably the code was from an older age of Rust and can now be written much simpler. --- src/libsyntax/print/pp.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index bff291348d7..602a39a1962 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -521,9 +521,7 @@ impl Printer { fn print_end(&mut self) { debug!("print End -> pop End"); - let print_stack = &mut self.print_stack; - assert!(!print_stack.is_empty()); - print_stack.pop().unwrap(); + self.print_stack.pop().unwrap(); } fn print_break(&mut self, b: BreakToken, l: isize) { -- cgit 1.4.1-3-g733a5 From 4783d9eaa5488ad8ff2c49a17c14ab6f604e4e71 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 7 Jul 2019 10:51:18 -0400 Subject: Remove is_begin/is_end functions from PrintState These are somewhat ambiguous (beginning/end of what?) so it's better to inline their one use into the code. --- src/libsyntax/print/pprust.rs | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 54672d9da2e..2d110aab879 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -445,20 +445,6 @@ pub trait PrintState<'a> { fn pclose(&mut self) { self.writer().word(")") } - fn is_begin(&mut self) -> bool { - match self.writer().last_token() { - pp::Token::Begin(_) => true, - _ => false, - } - } - - fn is_end(&mut self) -> bool { - match self.writer().last_token() { - pp::Token::End => true, - _ => false, - } - } - // is this the beginning of a line? fn is_bol(&mut self) -> bool { self.writer().last_token().is_eof() || self.writer().last_token().is_hardbreak_tok() @@ -545,11 +531,13 @@ pub trait PrintState<'a> { } comments::BlankLine => { // We need to do at least one, possibly two hardbreaks. - let is_semi = match self.writer().last_token() { + let twice = match self.writer().last_token() { pp::Token::String(s, _) => ";" == s, + pp::Token::Begin(_) => true, + pp::Token::End => true, _ => false }; - if is_semi || self.is_begin() || self.is_end() { + if twice { self.writer().hardbreak(); } self.writer().hardbreak(); -- cgit 1.4.1-3-g733a5 From 5879146392fc929bbcaa84fc7e3bcdeed284062e Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Mon, 8 Jul 2019 14:25:01 -0400 Subject: Rename pretty_print_* to scan_* to follow naming in the paper This is also easier to understand because the scan and print "tasks" are separate, but previously were both called "print" or "pretty print." --- src/libsyntax/print/pp.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index 602a39a1962..10b94aff0ef 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -131,7 +131,7 @@ //! it. //! //! In this implementation (following the paper, again) the SCAN process is the -//! methods called `Printer::pretty_print_*`, and the 'PRINT' process is the +//! methods called `Printer::scan_*`, and the 'PRINT' process is the //! method called `Printer::print`. use std::collections::VecDeque; @@ -310,14 +310,14 @@ impl Printer { self.buf[self.right].token = t; } - fn pretty_print_eof(&mut self) { + fn scan_eof(&mut self) { if !self.scan_stack.is_empty() { self.check_stack(0); self.advance_left(); } } - fn pretty_print_begin(&mut self, b: BeginToken) { + fn scan_begin(&mut self, b: BeginToken) { if self.scan_stack.is_empty() { self.left_total = 1; self.right_total = 1; @@ -331,7 +331,7 @@ impl Printer { self.scan_push(BufEntry { token: Token::Begin(b), size: -self.right_total }); } - fn pretty_print_end(&mut self) { + fn scan_end(&mut self) { if self.scan_stack.is_empty() { debug!("pp End/print Vec<{},{}>", self.left, self.right); self.print_end(); @@ -342,7 +342,7 @@ impl Printer { } } - fn pretty_print_break(&mut self, b: BreakToken) { + fn scan_break(&mut self, b: BreakToken) { if self.scan_stack.is_empty() { self.left_total = 1; self.right_total = 1; @@ -358,7 +358,7 @@ impl Printer { self.right_total += b.blank_space; } - fn pretty_print_string(&mut self, s: Cow<'static, str>, len: isize) { + fn scan_string(&mut self, s: Cow<'static, str>, len: isize) { if self.scan_stack.is_empty() { debug!("pp String('{}')/print Vec<{},{}>", s, self.left, self.right); @@ -594,7 +594,7 @@ impl Printer { /// "raw box" crate fn rbox(&mut self, indent: usize, b: Breaks) { - self.pretty_print_begin(BeginToken { + self.scan_begin(BeginToken { offset: indent as isize, breaks: b }) @@ -611,25 +611,25 @@ impl Printer { } pub fn break_offset(&mut self, n: usize, off: isize) { - self.pretty_print_break(BreakToken { + self.scan_break(BreakToken { offset: off, blank_space: n as isize }) } crate fn end(&mut self) { - self.pretty_print_end() + self.scan_end() } pub fn eof(mut self) -> String { - self.pretty_print_eof(); + self.scan_eof(); self.out } pub fn word>>(&mut self, wrd: S) { let s = wrd.into(); let len = s.len() as isize; - self.pretty_print_string(s, len) + self.scan_string(s, len) } fn spaces(&mut self, n: usize) { -- cgit 1.4.1-3-g733a5 From 04b80a5f5d0ac06a49b7937362235f9a23513b0c Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 09:58:34 -0400 Subject: Drop length from Token::String It was always set to the string's length --- src/libsyntax/print/pp.rs | 25 ++++++++++++++----------- src/libsyntax/print/pprust.rs | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index 10b94aff0ef..5e23288f554 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -163,7 +163,7 @@ pub enum Token { // In practice a string token contains either a `&'static str` or a // `String`. `Cow` is overkill for this because we never modify the data, // but it's more convenient than rolling our own more specialized type. - String(Cow<'static, str>, isize), + String(Cow<'static, str>), Break(BreakToken), Begin(BeginToken), End, @@ -194,7 +194,7 @@ impl Token { impl fmt::Display for Token { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - Token::String(ref s, len) => write!(f, "STR({},{})", s, len), + Token::String(ref s) => write!(f, "STR({},{})", s, s.len()), Token::Break(_) => f.write_str("BREAK"), Token::Begin(_) => f.write_str("BEGIN"), Token::End => f.write_str("END"), @@ -358,16 +358,17 @@ impl Printer { self.right_total += b.blank_space; } - fn scan_string(&mut self, s: Cow<'static, str>, len: isize) { + fn scan_string(&mut self, s: Cow<'static, str>) { if self.scan_stack.is_empty() { debug!("pp String('{}')/print Vec<{},{}>", s, self.left, self.right); - self.print_string(s, len); + self.print_string(s); } else { debug!("pp String('{}')/buffer Vec<{},{}>", s, self.left, self.right); self.advance_right(); - self.buf[self.right] = BufEntry { token: Token::String(s, len), size: len }; + let len = s.len() as isize; + self.buf[self.right] = BufEntry { token: Token::String(s), size: len }; self.right_total += len; self.check_stream(); } @@ -430,7 +431,8 @@ impl Printer { let len = match left { Token::Break(b) => b.blank_space, - Token::String(_, len) => { + Token::String(ref s) => { + let len = s.len() as isize; assert_eq!(len, left_size); len } @@ -554,7 +556,8 @@ impl Printer { } } - fn print_string(&mut self, s: Cow<'static, str>, len: isize) { + fn print_string(&mut self, s: Cow<'static, str>) { + let len = s.len() as isize; debug!("print String({})", s); // assert!(len <= space); self.space -= len; @@ -582,9 +585,10 @@ impl Printer { Token::Begin(b) => self.print_begin(b, l), Token::End => self.print_end(), Token::Break(b) => self.print_break(b, l), - Token::String(s, len) => { + Token::String(s) => { + let len = s.len() as isize; assert_eq!(len, l); - self.print_string(s, len); + self.print_string(s); } Token::Eof => panic!(), // Eof should never get here. } @@ -628,8 +632,7 @@ impl Printer { pub fn word>>(&mut self, wrd: S) { let s = wrd.into(); - let len = s.len() as isize; - self.scan_string(s, len) + self.scan_string(s) } fn spaces(&mut self, n: usize) { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 2d110aab879..e9cc89f95bb 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -532,7 +532,7 @@ pub trait PrintState<'a> { comments::BlankLine => { // We need to do at least one, possibly two hardbreaks. let twice = match self.writer().last_token() { - pp::Token::String(s, _) => ";" == s, + pp::Token::String(s) => ";" == s, pp::Token::Begin(_) => true, pp::Token::End => true, _ => false -- cgit 1.4.1-3-g733a5 From 39aa9bf7303c7fdf8d8f3fc62010f8493267cfd5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 07:38:31 -0400 Subject: Remove needless indirection in bclose --- src/librustc/hir/print.rs | 8 ++------ src/libsyntax/print/pprust.rs | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 8342331e360..4b87503ff88 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -169,10 +169,6 @@ impl<'a> State<'a> { self.end(); // close the head-box } - pub fn bclose_(&mut self, span: syntax_pos::Span, indented: usize) { - self.bclose_maybe_open(span, indented, true) - } - pub fn bclose_maybe_open(&mut self, span: syntax_pos::Span, indented: usize, @@ -187,7 +183,7 @@ impl<'a> State<'a> { } pub fn bclose(&mut self, span: syntax_pos::Span) { - self.bclose_(span, indent_unit) + self.bclose_maybe_open(span, indent_unit, true) } pub fn space_if_not_bol(&mut self) { @@ -1276,7 +1272,7 @@ impl<'a> State<'a> { for arm in arms { self.print_arm(arm); } - self.bclose_(expr.span, indent_unit); + self.bclose(expr.span); } hir::ExprKind::Closure(capture_clause, ref decl, body, _fn_decl_span, _gen) => { self.print_capture_clause(capture_clause); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index e9cc89f95bb..2e206811d7c 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -778,10 +778,6 @@ impl<'a> State<'a> { self.end(); // close the head-box } - crate fn bclose_(&mut self, span: syntax_pos::Span, - indented: usize) { - self.bclose_maybe_open(span, indented, true) - } crate fn bclose_maybe_open(&mut self, span: syntax_pos::Span, indented: usize, close_box: bool) { self.maybe_print_comment(span.hi()); @@ -792,7 +788,7 @@ impl<'a> State<'a> { } } crate fn bclose(&mut self, span: syntax_pos::Span) { - self.bclose_(span, INDENT_UNIT) + self.bclose_maybe_open(span, INDENT_UNIT, true) } crate fn break_offset_if_not_bol(&mut self, n: usize, @@ -2027,7 +2023,7 @@ impl<'a> State<'a> { for arm in arms { self.print_arm(arm); } - self.bclose_(expr.span, INDENT_UNIT); + self.bclose(expr.span); } ast::ExprKind::Closure( capture_clause, asyncness, movability, ref decl, ref body, _) => { -- cgit 1.4.1-3-g733a5 From e91dbc5916f508849532d1c7b3069b6c0c3a609b Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 07:42:05 -0400 Subject: Rename is_bol -> is_beginning_of_line Also moves it to pp::Printer from PrintState. --- src/librustc/hir/print.rs | 4 ++-- src/libsyntax/print/pp.rs | 6 +++++- src/libsyntax/print/pprust.rs | 13 ++++--------- 3 files changed, 11 insertions(+), 12 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 4b87503ff88..246b3341a23 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -187,13 +187,13 @@ impl<'a> State<'a> { } pub fn space_if_not_bol(&mut self) { - if !self.is_bol() { + 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.is_bol() { + if !self.s.is_beginning_of_line() { self.s.break_offset(n, off) } else { if off != 0 && self.s.last_token().is_hardbreak_tok() { diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index 5e23288f554..be5a0098578 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -301,7 +301,7 @@ impl Default for BufEntry { } impl Printer { - pub fn last_token(&mut self) -> Token { + pub fn last_token(&self) -> Token { self.buf[self.right].token.clone() } @@ -651,6 +651,10 @@ impl Printer { self.spaces(SIZE_INFINITY as usize) } + pub fn is_beginning_of_line(&self) -> bool { + self.last_token().is_eof() || self.last_token().is_hardbreak_tok() + } + pub fn hardbreak_tok_offset(off: isize) -> Token { Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY}) } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 2e206811d7c..288417fcd89 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -445,13 +445,8 @@ pub trait PrintState<'a> { fn pclose(&mut self) { self.writer().word(")") } - // is this the beginning of a line? - fn is_bol(&mut self) -> bool { - self.writer().last_token().is_eof() || self.writer().last_token().is_hardbreak_tok() - } - fn hardbreak_if_not_bol(&mut self) { - if !self.is_bol() { + if !self.writer().is_beginning_of_line() { self.writer().hardbreak() } } @@ -512,7 +507,7 @@ pub trait PrintState<'a> { } } comments::Trailing => { - if !self.is_bol() { + if !self.writer().is_beginning_of_line() { self.writer().word(" "); } if cmnt.lines.len() == 1 { @@ -735,7 +730,7 @@ pub trait PrintState<'a> { } fn space_if_not_bol(&mut self) { - if !self.is_bol() { self.writer().space(); } + if !self.writer().is_beginning_of_line() { self.writer().space(); } } fn nbsp(&mut self) { self.writer().word(" ") } @@ -793,7 +788,7 @@ impl<'a> State<'a> { crate fn break_offset_if_not_bol(&mut self, n: usize, off: isize) { - if !self.is_bol() { + if !self.s.is_beginning_of_line() { self.s.break_offset(n, off) } else { if off != 0 && self.s.last_token().is_hardbreak_tok() { -- cgit 1.4.1-3-g733a5 From cab453250a3ceae5cf0cf7eac836c03b37e4ca8e Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:26:50 -0400 Subject: 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 | 54 +++++++++----------------------------- 10 files changed, 64 insertions(+), 53 deletions(-) create mode 100644 src/libsyntax/print/helpers.rs (limited to 'src/libsyntax') 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 @@ impl<'a> print::State<'a> { 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::cfg::CFGIndex; 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 rustc_mir::util::{write_mir_pretty, write_mir_graphviz}; 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 visit; 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 @@ impl<'a> Parser<'a> { 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 @@ impl<'a> Parser<'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 @@ impl<'a> Parser<'a> { 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 @@ impl Printer { // 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 @@ impl Printer { } /// 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 @@ impl Printer { }) } - 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,37 +432,22 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { format!("{}{}", to_string(|s| s.print_visibility(vis)), s) } -pub trait PrintState<'a> { - 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) +impl std::ops::Deref for State<'_> { + type Target = pp::Printer; + fn deref(&self) -> &Self::Target { + &self.s } +} - fn ibox(&mut self, u: usize) { - self.writer().ibox(u); +impl std::ops::DerefMut for State<'_> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.s } +} - fn end(&mut self) { - self.writer().end() - } +pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefMut { + fn writer(&mut self) -> &mut pp::Printer; + fn comments(&mut self) -> &mut Option>; fn commasep(&mut self, b: Breaks, elts: &[T], mut op: F) where F: FnMut(&mut Self, &T), @@ -728,12 +713,6 @@ pub trait PrintState<'a> { } 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 @@ impl<'a> PrintState<'a> for State<'a> { } 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 -- cgit 1.4.1-3-g733a5 From 63fdf1a5274d19865ba27742b3b5c4e0c94b1838 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:30:08 -0400 Subject: Remove needless indent arguments We're always indenting by INDENT_UNIT anyway --- src/librustc/hir/print.rs | 23 +++++++---------------- src/libsyntax/parse/parser.rs | 2 +- src/libsyntax/print/pprust.rs | 19 ++++++++----------- 3 files changed, 16 insertions(+), 28 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index dd5d8ebde1c..f10efbacf2a 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -183,11 +183,10 @@ impl<'a> State<'a> { pub fn bclose_maybe_open(&mut self, span: syntax_pos::Span, - indented: usize, close_box: bool) { self.maybe_print_comment(span.hi()); - self.break_offset_if_not_bol(1, -(indented as isize)); + self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize)); self.s.word("}"); if close_box { self.end(); // close the outer-box @@ -195,7 +194,7 @@ impl<'a> State<'a> { } pub fn bclose(&mut self, span: syntax_pos::Span) { - self.bclose_maybe_open(span, INDENT_UNIT, true) + self.bclose_maybe_open(span, true) } pub fn space_if_not_bol(&mut self) { @@ -963,26 +962,18 @@ impl<'a> State<'a> { } pub fn print_block_unclosed(&mut self, blk: &hir::Block) { - self.print_block_unclosed_indent(blk, INDENT_UNIT) - } - - pub fn print_block_unclosed_indent(&mut self, - blk: &hir::Block, - indented: usize) - { - self.print_block_maybe_unclosed(blk, indented, &[], false) + self.print_block_maybe_unclosed(blk, &[], false) } pub fn print_block_with_attrs(&mut self, blk: &hir::Block, attrs: &[ast::Attribute]) { - self.print_block_maybe_unclosed(blk, INDENT_UNIT, attrs, true) + self.print_block_maybe_unclosed(blk, attrs, true) } pub fn print_block_maybe_unclosed(&mut self, blk: &hir::Block, - indented: usize, attrs: &[ast::Attribute], close_box: bool) { @@ -1006,7 +997,7 @@ impl<'a> State<'a> { self.print_expr(&expr); self.maybe_print_trailing_comment(expr.span, Some(blk.span.hi())); } - self.bclose_maybe_open(blk.span, indented, close_box); + self.bclose_maybe_open(blk.span, close_box); self.ann.post(self, AnnNode::Block(blk)) } @@ -1263,7 +1254,7 @@ impl<'a> State<'a> { self.print_ident(temp); // Print `}`: - self.bclose_maybe_open(expr.span, INDENT_UNIT, true); + self.bclose_maybe_open(expr.span, true); } hir::ExprKind::Loop(ref blk, opt_label, _) => { if let Some(label) = opt_label { @@ -1819,7 +1810,7 @@ impl<'a> State<'a> { self.word_space(":"); } // the block will close the pattern's ibox - self.print_block_unclosed_indent(&blk, INDENT_UNIT); + self.print_block_unclosed(&blk); // If it is a user-provided unsafe block, print a comma after it if let hir::UnsafeBlock(hir::UserProvided) = blk.rules { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7dd7000b454..83dbff6b2d5 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -4591,7 +4591,7 @@ impl<'a> Parser<'a> { s.ibox(INDENT_UNIT); s.bopen(); s.print_stmt(&stmt); - s.bclose_maybe_open(stmt.span, INDENT_UNIT, false) + s.bclose_maybe_open(stmt.span, false) }); e.span_suggestion( stmt_span, diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 98c629addc8..eddd8700de6 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -743,17 +743,16 @@ impl<'a> State<'a> { self.end(); // close the head-box } - crate fn bclose_maybe_open(&mut self, span: syntax_pos::Span, - indented: usize, close_box: bool) { + crate fn bclose_maybe_open(&mut self, span: syntax_pos::Span, close_box: bool) { self.maybe_print_comment(span.hi()); - self.break_offset_if_not_bol(1, -(indented as isize)); + self.break_offset_if_not_bol(1, -(INDENT_UNIT as isize)); self.s.word("}"); if close_box { self.end(); // close the outer-box } } crate fn bclose(&mut self, span: syntax_pos::Span) { - self.bclose_maybe_open(span, INDENT_UNIT, true) + self.bclose_maybe_open(span, true) } crate fn break_offset_if_not_bol(&mut self, n: usize, @@ -1559,20 +1558,18 @@ impl<'a> State<'a> { self.print_block_with_attrs(blk, &[]) } - crate fn print_block_unclosed_indent(&mut self, blk: &ast::Block, - indented: usize) { - self.print_block_maybe_unclosed(blk, indented, &[], false) + crate fn print_block_unclosed_indent(&mut self, blk: &ast::Block) { + self.print_block_maybe_unclosed(blk, &[], false) } crate fn print_block_with_attrs(&mut self, blk: &ast::Block, attrs: &[ast::Attribute]) { - self.print_block_maybe_unclosed(blk, INDENT_UNIT, attrs, true) + self.print_block_maybe_unclosed(blk, attrs, true) } crate fn print_block_maybe_unclosed(&mut self, blk: &ast::Block, - indented: usize, attrs: &[ast::Attribute], close_box: bool) { match blk.rules { @@ -1597,7 +1594,7 @@ impl<'a> State<'a> { } } - self.bclose_maybe_open(blk.span, indented, close_box); + self.bclose_maybe_open(blk.span, close_box); self.ann.post(self, AnnNode::Block(blk)) } @@ -2519,7 +2516,7 @@ impl<'a> State<'a> { } // the block will close the pattern's ibox - self.print_block_unclosed_indent(blk, INDENT_UNIT); + self.print_block_unclosed_indent(blk); // If it is a user-provided unsafe block, print a comma after it if let BlockCheckMode::Unsafe(ast::UserProvided) = blk.rules { -- cgit 1.4.1-3-g733a5 From 73c1752b8e9a6edb38cfadec25e430320300730b Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:49:37 -0400 Subject: Use constant instead of magic number --- src/librustc/hir/print.rs | 2 +- src/libsyntax/print/pprust.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index f10efbacf2a..3fb4339b1e6 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -1267,7 +1267,7 @@ impl<'a> State<'a> { } hir::ExprKind::Match(ref expr, ref arms, _) => { self.cbox(INDENT_UNIT); - self.ibox(4); + self.ibox(INDENT_UNIT); self.word_nbsp("match"); self.print_expr_as_cond(&expr); self.s.space(); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index eddd8700de6..61b3484c92a 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1976,7 +1976,7 @@ impl<'a> State<'a> { } ast::ExprKind::Match(ref expr, ref arms) => { self.cbox(INDENT_UNIT); - self.ibox(4); + self.ibox(INDENT_UNIT); self.word_nbsp("match"); self.print_expr_as_cond(expr); self.s.space(); -- cgit 1.4.1-3-g733a5 From 096cb4137dcc1901ae11fe5e8c5bb602bef7199c Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:51:56 -0400 Subject: Remove writer function from PrintState --- src/librustc/hir/print.rs | 6 +--- src/libsyntax/print/pprust.rs | 69 ++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 42 deletions(-) (limited to 'src/libsyntax') diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 3fb4339b1e6..5a14b30e699 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -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> { &mut self.comments } @@ -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) { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 61b3484c92a..8da8d3dc540 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -446,7 +446,6 @@ impl std::ops::DerefMut for State<'_> { } pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefMut { - fn writer(&mut self) -> &mut pp::Printer; fn comments(&mut self) -> &mut Option>; fn commasep(&mut self, b: Breaks, elts: &[T], mut op: F) @@ -476,9 +475,9 @@ pub trait PrintState<'a>: std::ops::Deref + 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(); @@ -486,41 +485,41 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM // 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() { @@ -534,7 +533,7 @@ pub trait PrintState<'a>: std::ops::Deref + 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, @@ -549,7 +548,7 @@ pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefM string=st)) } }; - self.writer().word(st) + self.word(st) } fn print_inner_attributes(&mut self, @@ -601,10 +600,10 @@ pub trait PrintState<'a>: std::ops::Deref + 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())); } } } @@ -620,21 +619,21 @@ pub trait PrintState<'a>: std::ops::Deref + 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("]"); } } @@ -655,7 +654,7 @@ pub trait PrintState<'a>: std::ops::Deref + 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); } @@ -681,20 +680,20 @@ pub trait PrintState<'a>: std::ops::Deref + 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))) }, } } @@ -707,7 +706,7 @@ pub trait PrintState<'a>: std::ops::Deref + 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); } @@ -716,10 +715,6 @@ pub trait PrintState<'a>: std::ops::Deref + 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> { &mut self.comments } -- cgit 1.4.1-3-g733a5 From 56a9237b595b4523f2be3123d52662739e89d4c2 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 11:10:03 -0400 Subject: File is now short enough for tidy --- src/libsyntax/print/pprust.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 8da8d3dc540..8050026a00d 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1,5 +1,3 @@ -// ignore-tidy-filelength - use crate::ast::{self, BlockCheckMode, PatKind, RangeEnd, RangeSyntax}; use crate::ast::{SelfKind, GenericBound, TraitBoundModifier}; use crate::ast::{Attribute, MacDelimiter, GenericArg}; -- cgit 1.4.1-3-g733a5