From f6e5ac640be7ca71f389b6842cfeb83c24bd15c7 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Mon, 1 Jul 2019 20:06:30 +0300 Subject: [PATCH 01/35] rustc_mir: treat DropAndReplace as Drop + Assign in qualify_consts. --- src/librustc_mir/transform/qualify_consts.rs | 23 +++++++++++++--- src/test/ui/consts/const-eval/const_let.rs | 15 +++++++++-- .../ui/consts/const-eval/const_let.stderr | 27 +++++++++++++------ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs index f082b5e5a046..72180d60bd6d 100644 --- a/src/librustc_mir/transform/qualify_consts.rs +++ b/src/librustc_mir/transform/qualify_consts.rs @@ -142,6 +142,7 @@ impl<'a, 'tcx> ConstCx<'a, 'tcx> { #[derive(Copy, Clone, Debug)] enum ValueSource<'a, 'tcx> { Rvalue(&'a Rvalue<'tcx>), + DropAndReplace(&'a Operand<'tcx>), Call { callee: &'a Operand<'tcx>, args: &'a [Operand<'tcx>], @@ -298,6 +299,7 @@ trait Qualif { fn in_value(cx: &ConstCx<'_, 'tcx>, source: ValueSource<'_, 'tcx>) -> bool { match source { ValueSource::Rvalue(rvalue) => Self::in_rvalue(cx, rvalue), + ValueSource::DropAndReplace(source) => Self::in_operand(cx, source), ValueSource::Call { callee, args, return_ty } => { Self::in_call(cx, callee, args, return_ty) } @@ -889,6 +891,7 @@ impl<'a, 'tcx> Checker<'a, 'tcx> { let target = match body[bb].terminator().kind { TerminatorKind::Goto { target } | TerminatorKind::Drop { target, .. } | + TerminatorKind::DropAndReplace { target, .. } | TerminatorKind::Assert { target, .. } | TerminatorKind::Call { destination: Some((_, target)), .. } => { Some(target) @@ -900,7 +903,6 @@ impl<'a, 'tcx> Checker<'a, 'tcx> { } TerminatorKind::SwitchInt {..} | - TerminatorKind::DropAndReplace { .. } | TerminatorKind::Resume | TerminatorKind::Abort | TerminatorKind::GeneratorDrop | @@ -1393,8 +1395,15 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> { for arg in args { self.visit_operand(arg, location); } - } else if let TerminatorKind::Drop { location: ref place, .. } = *kind { - self.super_terminator_kind(kind, location); + } else if let TerminatorKind::Drop { + location: ref place, .. + } | TerminatorKind::DropAndReplace { + location: ref place, .. + } = *kind { + match *kind { + TerminatorKind::DropAndReplace { .. } => {} + _ => self.super_terminator_kind(kind, location), + } // Deny *any* live drops anywhere other than functions. if self.mode.requires_const_checking() { @@ -1423,6 +1432,14 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> { } } } + + match *kind { + TerminatorKind::DropAndReplace { ref value, .. } => { + self.assign(place, ValueSource::DropAndReplace(value), location); + self.visit_operand(value, location); + } + _ => {} + } } else { // Qualify any operands inside other terminators. self.super_terminator_kind(kind, location); diff --git a/src/test/ui/consts/const-eval/const_let.rs b/src/test/ui/consts/const-eval/const_let.rs index 63321b912007..18692dbced67 100644 --- a/src/test/ui/consts/const-eval/const_let.rs +++ b/src/test/ui/consts/const-eval/const_let.rs @@ -9,10 +9,21 @@ impl Drop for FakeNeedsDrop { // ok const X: FakeNeedsDrop = { let x = FakeNeedsDrop; x }; +// ok (used to incorrectly error, see #62273) +const X2: FakeNeedsDrop = { let x; x = FakeNeedsDrop; x }; + // error const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x }; -//~^ ERROR constant contains unimplemented expression type +//~^ ERROR destructors cannot be evaluated at compile-time + +// error +const Y2: FakeNeedsDrop = { let mut x; x = FakeNeedsDrop; x = FakeNeedsDrop; x }; +//~^ ERROR destructors cannot be evaluated at compile-time // error const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); }; -//~^ ERROR constant contains unimplemented expression type +//~^ ERROR destructors cannot be evaluated at compile-time + +// error +const Z2: () = { let mut x; x = None; x = Some(FakeNeedsDrop); }; +//~^ ERROR destructors cannot be evaluated at compile-time diff --git a/src/test/ui/consts/const-eval/const_let.stderr b/src/test/ui/consts/const-eval/const_let.stderr index 00de97e6fb3a..0a6a222ae296 100644 --- a/src/test/ui/consts/const-eval/const_let.stderr +++ b/src/test/ui/consts/const-eval/const_let.stderr @@ -1,15 +1,26 @@ -error[E0019]: constant contains unimplemented expression type - --> $DIR/const_let.rs:13:55 +error[E0493]: destructors cannot be evaluated at compile-time + --> $DIR/const_let.rs:16:32 | LL | const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x }; - | ^ + | ^^^^^ constants cannot evaluate destructors -error[E0019]: constant contains unimplemented expression type - --> $DIR/const_let.rs:17:35 +error[E0493]: destructors cannot be evaluated at compile-time + --> $DIR/const_let.rs:20:33 + | +LL | const Y2: FakeNeedsDrop = { let mut x; x = FakeNeedsDrop; x = FakeNeedsDrop; x }; + | ^^^^^ constants cannot evaluate destructors + +error[E0493]: destructors cannot be evaluated at compile-time + --> $DIR/const_let.rs:24:21 | LL | const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); }; - | ^ + | ^^^^^ constants cannot evaluate destructors -error: aborting due to 2 previous errors +error[E0493]: destructors cannot be evaluated at compile-time + --> $DIR/const_let.rs:28:22 + | +LL | const Z2: () = { let mut x; x = None; x = Some(FakeNeedsDrop); }; + | ^^^^^ constants cannot evaluate destructors + +error: aborting due to 4 previous errors -For more information about this error, try `rustc --explain E0019`. From 89188815ed1ec2d6fa498e8dc1eeecdc26e355e4 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sun, 7 Jul 2019 15:03:07 +0100 Subject: [PATCH 02/35] Give index temporaries a drop scope --- src/librustc_mir/build/expr/as_place.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/librustc_mir/build/expr/as_place.rs b/src/librustc_mir/build/expr/as_place.rs index 0640c01d255c..82accb47437c 100644 --- a/src/librustc_mir/build/expr/as_place.rs +++ b/src/librustc_mir/build/expr/as_place.rs @@ -73,13 +73,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { let (usize_ty, bool_ty) = (this.hir.usize_ty(), this.hir.bool_ty()); let slice = unpack!(block = this.as_place(block, lhs)); - // region_scope=None so place indexes live forever. They are scalars so they - // do not need storage annotations, and they are often copied between - // places. // Making this a *fresh* temporary also means we do not have to worry about // the index changing later: Nothing will ever change this temporary. // The "retagging" transformation (for Stacked Borrows) relies on this. - let idx = unpack!(block = this.as_temp(block, None, index, Mutability::Mut)); + let idx = unpack!(block = this.as_temp( + block, + expr.temp_lifetime, + index, + Mutability::Not, + )); // bounds check: let (len, lt) = ( From 163c059354ed24d8b5e1c50177fd4dd2872d63e7 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sun, 7 Jul 2019 15:04:43 +0100 Subject: [PATCH 03/35] Only omit StorageLive/Dead for variable that are never initialized With `feature(never_type)`, it's not guaranteed that any variable with type `!` isn't ever assigned to. --- src/librustc_mir/build/expr/as_temp.rs | 65 ++++++++++++++--------- src/test/mir-opt/loop_test.rs | 1 + src/test/ui/borrowck/assign-never-type.rs | 14 +++++ 3 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 src/test/ui/borrowck/assign-never-type.rs diff --git a/src/librustc_mir/build/expr/as_temp.rs b/src/librustc_mir/build/expr/as_temp.rs index 1fe6be8bbc82..dbcc330eca38 100644 --- a/src/librustc_mir/build/expr/as_temp.rs +++ b/src/librustc_mir/build/expr/as_temp.rs @@ -3,6 +3,7 @@ use crate::build::{BlockAnd, BlockAndExtension, Builder}; use crate::build::scope::DropKind; use crate::hair::*; +use rustc::hir; use rustc::middle::region; use rustc::mir::*; @@ -66,32 +67,46 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { }; let temp_place = &Place::from(temp); - if !expr_ty.is_never() { - this.cfg.push( - block, - Statement { - source_info, - kind: StatementKind::StorageLive(temp), - }, - ); - - // In constants, `temp_lifetime` is `None` for temporaries that live for the - // `'static` lifetime. Thus we do not drop these temporaries and simply leak them. - // This is equivalent to what `let x = &foo();` does in functions. The temporary - // is lifted to their surrounding scope. In a function that means the temporary lives - // until just before the function returns. In constants that means it outlives the - // constant's initialization value computation. Anything outliving a constant - // must have the `'static` lifetime and live forever. - // Anything with a shorter lifetime (e.g the `&foo()` in `bar(&foo())` or anything - // within a block will keep the regular drops just like runtime code. - if let Some(temp_lifetime) = temp_lifetime { - this.schedule_drop( - expr_span, - temp_lifetime, - temp, - expr_ty, - DropKind::Storage, + match expr.kind { + // Don't bother with StorageLive and Dead for these temporaries, + // they are never assigned. + ExprKind::Break { .. } | + ExprKind::Continue { .. } | + ExprKind::Return { .. } => (), + ExprKind::Block { + body: hir::Block { expr: None, targeted_by_break: false, .. } + } if expr_ty.is_never() => (), + _ => { + this.cfg.push( + block, + Statement { + source_info, + kind: StatementKind::StorageLive(temp), + }, ); + + // In constants, `temp_lifetime` is `None` for temporaries that + // live for the `'static` lifetime. Thus we do not drop these + // temporaries and simply leak them. + // This is equivalent to what `let x = &foo();` does in + // functions. The temporary is lifted to their surrounding + // scope. In a function that means the temporary lives until + // just before the function returns. In constants that means it + // outlives the constant's initialization value computation. + // Anything outliving a constant must have the `'static` + // lifetime and live forever. + // Anything with a shorter lifetime (e.g the `&foo()` in + // `bar(&foo())` or anything within a block will keep the + // regular drops just like runtime code. + if let Some(temp_lifetime) = temp_lifetime { + this.schedule_drop( + expr_span, + temp_lifetime, + temp, + expr_ty, + DropKind::Storage, + ); + } } } diff --git a/src/test/mir-opt/loop_test.rs b/src/test/mir-opt/loop_test.rs index 177080c04f97..418febbdc01e 100644 --- a/src/test/mir-opt/loop_test.rs +++ b/src/test/mir-opt/loop_test.rs @@ -26,6 +26,7 @@ fn main() { // _1 = (); // StorageDead(_2); // StorageDead(_1); +// StorageLive(_4); // goto -> bb5; // } // ... diff --git a/src/test/ui/borrowck/assign-never-type.rs b/src/test/ui/borrowck/assign-never-type.rs new file mode 100644 index 000000000000..4f30ea146702 --- /dev/null +++ b/src/test/ui/borrowck/assign-never-type.rs @@ -0,0 +1,14 @@ +// Regression test for issue 62165 + +// check-pass + +#![feature(never_type)] + +pub fn main() { + loop { + match None { + None => return, + Some(val) => val, + }; + }; +} From bc322af444006973addb3e65bcd74f033080402b Mon Sep 17 00:00:00 2001 From: Freyskeyd Date: Mon, 8 Jul 2019 16:03:29 +0200 Subject: [PATCH 04/35] doc(ptr): add example for {read,write}_unaligned Signed-off-by: Freyskeyd --- src/libcore/ptr/mod.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/libcore/ptr/mod.rs b/src/libcore/ptr/mod.rs index 2a6c2b1331e5..df66a2978de4 100644 --- a/src/libcore/ptr/mod.rs +++ b/src/libcore/ptr/mod.rs @@ -669,6 +669,22 @@ pub unsafe fn read(src: *const T) -> T { /// /// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however. // FIXME: Update docs based on outcome of RFC #2582 and friends. +/// +/// # Examples +/// +/// Read an usize value from a byte buffer: +/// +/// ``` +/// use std::mem; +/// +/// fn read_usize(x: &[u8]) -> usize { +/// assert!(x.len() >= mem::size_of::()); +/// +/// let ptr = x.as_ptr() as *const usize; +/// +/// unsafe { ptr.read_unaligned() } +/// } +/// ``` #[inline] #[stable(feature = "ptr_unaligned", since = "1.17.0")] pub unsafe fn read_unaligned(src: *const T) -> T { @@ -839,6 +855,22 @@ pub unsafe fn write(dst: *mut T, src: T) { /// /// Accessing unaligned fields directly with e.g. `packed.unaligned` is safe however. // FIXME: Update docs based on outcome of RFC #2582 and friends. +/// +/// # Examples +/// +/// Write an usize value to a byte buffer: +/// +/// ``` +/// use std::mem; +/// +/// fn write_usize(x: &mut [u8], val: usize) { +/// assert!(x.len() >= mem::size_of::()); +/// +/// let ptr = x.as_mut_ptr() as *mut usize; +/// +/// unsafe { ptr.write_unaligned(val) } +/// } +/// ``` #[inline] #[stable(feature = "ptr_unaligned", since = "1.17.0")] pub unsafe fn write_unaligned(dst: *mut T, src: T) { From 76a8bc2473e60be35f96ecc268c6bb1d992ede78 Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Sun, 7 Jul 2019 16:54:48 -0700 Subject: [PATCH 05/35] Use fold in Iterator::last Replace last impl with fold --- src/libcore/iter/traits/iterator.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs index 30923c741450..d934b20a33fa 100644 --- a/src/libcore/iter/traits/iterator.rs +++ b/src/libcore/iter/traits/iterator.rs @@ -263,9 +263,7 @@ pub trait Iterator { #[inline] #[stable(feature = "rust1", since = "1.0.0")] fn last(self) -> Option where Self: Sized { - let mut last = None; - for x in self { last = Some(x); } - last + self.fold(None, |_, x| Some(x)) } /// Returns the `n`th element of the iterator. From 9b0ebfa4e9e4906497eed5c6848bcca3cca23464 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Wed, 26 Jun 2019 07:23:27 -0400 Subject: [PATCH 06/35] Move literal_to_string to fmt::Display --- src/librustc/hir/print.rs | 4 ++-- src/libsyntax/parse/literal.rs | 2 +- src/libsyntax/parse/token.rs | 28 ++++++++++++++++++++++++++++ src/libsyntax/print/pprust.rs | 6 +----- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 6817107635a6..827bbea7bd2a 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -5,7 +5,7 @@ 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::{self, PrintState}; +use syntax::print::pprust::PrintState; use syntax::symbol::kw; use syntax::util::parser::{self, AssocOp, Fixity}; use syntax_pos::{self, BytePos, FileName}; @@ -1226,7 +1226,7 @@ impl<'a> State<'a> { fn print_literal(&mut self, lit: &hir::Lit) { self.maybe_print_comment(lit.span.lo()); - self.writer().word(pprust::literal_to_string(lit.node.to_lit_token())) + self.writer().word(lit.node.to_lit_token().to_string()) } pub fn print_expr(&mut self, expr: &hir::Expr) { diff --git a/src/libsyntax/parse/literal.rs b/src/libsyntax/parse/literal.rs index ef55bf6b9293..683d16415654 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 ebd0decacb59..fccb556b95ad 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 7e099bc4d509..a69dd94cfb69 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, From 0f10d114e447d5cdaf1de499e2d88b815aef7239 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 12:16:41 -0400 Subject: [PATCH 07/35] 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(-) diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index fccb556b95ad..472e4b474d62 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 a69dd94cfb69..4a3abe505caa 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)) } From a440337e2bfc41e59a001a1460231ef920978ad5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 15:43:13 -0400 Subject: [PATCH 08/35] Remove unused arm_to_string --- src/libsyntax/print/pprust.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 4a3abe505caa..512023ff0303 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)) } From 11d521676fbc371a7dffa195ee94075dc0558862 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 15:42:28 -0400 Subject: [PATCH 09/35] 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(-) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index c627596bbdf2..8801e89a0cfc 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 512023ff0303..16610e220487 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)) } From ae5ed9b72914ea28749072cbaff86bc34a43884c Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 12:25:34 -0400 Subject: [PATCH 10/35] Don't re-collect tokenstream twice to pretty print --- src/librustc_metadata/encoder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index c7f57be64261..567b6b1a891b 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -1316,7 +1316,7 @@ impl EncodeContext<'tcx> { let def_id = self.tcx.hir().local_def_id(macro_def.hir_id); Entry { kind: EntryKind::MacroDef(self.lazy(&MacroDef { - body: pprust::tts_to_string(¯o_def.body.trees().collect::>()), + body: pprust::tokens_to_string(macro_def.body.clone()), legacy: macro_def.legacy, })), visibility: self.lazy(&ty::Visibility::Public), From 44839802853954ca0eea629bb9d94068966f2960 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 16:00:38 -0400 Subject: [PATCH 11/35] Privatize and remove unused functions --- src/libsyntax/print/pprust.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 16610e220487..1d28b52362c1 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) } From a573d143a2b7ef9d04a8fe6904667762e47157bc Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 16:14:47 -0400 Subject: [PATCH 12/35] Remove unused boxes vector --- src/librustc/hir/print.rs | 15 --------------- src/libsyntax/print/pprust.rs | 12 ------------ 2 files changed, 27 deletions(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 827bbea7bd2a..137f20d6c32b 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 1d28b52362c1..4c86300b8056 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); } From 59b161c7c84e48b9379fbed877a2fa5c13db0526 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 16:32:15 -0400 Subject: [PATCH 13/35] 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(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 137f20d6c32b..9acf6d895e3e 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 4c86300b8056..460c44347128 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 } From 9b5e39723dc8193a2c80289b062f87d618a59d11 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 16:35:23 -0400 Subject: [PATCH 14/35] 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(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 9acf6d895e3e..c12983ba1d35 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 460c44347128..b7bbcc2c5559 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, From 0eb2e566c1d9ee6526e670802debda9c0afabde5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 18:29:15 -0400 Subject: [PATCH 15/35] 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(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index c12983ba1d35..3af72dbb43c3 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 b7bbcc2c5559..638384acc4c3 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); } } } From 7e3791469fce12a95d5d85b9a8744a61a0970fe1 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 17:12:11 -0400 Subject: [PATCH 16/35] 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(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 3af72dbb43c3..fc3430566a0d 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 fc55d5ac3559..df8dc3871b7e 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 988f1aa38d92..6ed2a7adad1c 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 638384acc4c3..a6680ee02b52 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> { From e0db2e606cdb242db1466ae51129c9a6b81560fe Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 17:48:21 -0400 Subject: [PATCH 17/35] 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(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index fc3430566a0d..01cdd8a6f22b 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 df8dc3871b7e..3e6d843ffbe3 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 a6680ee02b52..555276234f7a 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> { From e0ffa7c3d2115b9a1d22b6a5de288aa696abf50f Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 18:32:04 -0400 Subject: [PATCH 18/35] 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(-) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 555276234f7a..43714d3015be 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<'_>), { From 00ca508608381f9594dc4a60d3c5b436660a2e19 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 19:10:18 -0400 Subject: [PATCH 19/35] 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(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 01cdd8a6f22b..8342331e3609 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 3e6d843ffbe3..cd38eb695eb5 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 f64e95aee5bc..ea90defcd508 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 43714d3015be..54672d9da2e2 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 } From 4c58fc32ae07653b35ed3156da7bdb9a78ad1b05 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 22:27:36 -0400 Subject: [PATCH 20/35] Fully privatize (vs. crate visibility) functions --- src/libsyntax/print/pp.rs | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index ea90defcd508..6af524406e23 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, From ccf279d9c34c929dd6fdc07f00574de8c267d0b4 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 22:31:45 -0400 Subject: [PATCH 21/35] Remove useless call to indent --- src/libsyntax/print/pp.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index 6af524406e23..e26978f1ffc9 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) { From cd2d8326a5bdc309c6726c9345380fd73c5c1bb5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 6 Jul 2019 07:28:25 -0400 Subject: [PATCH 22/35] Move BufEntry assignment into scan_push --- src/libsyntax/print/pp.rs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index e26978f1ffc9..27139c1fc903 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 { From 55a6a761b94e543cbdfca6dab7e3e2226b5f437f Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 7 Jul 2019 10:15:47 -0400 Subject: [PATCH 23/35] Simplify check_stack implementation --- src/libsyntax/print/pp.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index 27139c1fc903..bff291348d70 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); } From 57cf7a2e5728e5b4d77230ffb1e372385f4f24dd Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 7 Jul 2019 10:38:09 -0400 Subject: [PATCH 24/35] 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(-) diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index bff291348d70..602a39a19627 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) { From 4783d9eaa5488ad8ff2c49a17c14ab6f604e4e71 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sun, 7 Jul 2019 10:51:18 -0400 Subject: [PATCH 25/35] 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(-) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 54672d9da2e2..2d110aab8798 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(); From 5879146392fc929bbcaa84fc7e3bcdeed284062e Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Mon, 8 Jul 2019 14:25:01 -0400 Subject: [PATCH 26/35] 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(-) diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index 602a39a19627..10b94aff0efa 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) { From 04b80a5f5d0ac06a49b7937362235f9a23513b0c Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Fri, 5 Jul 2019 09:58:34 -0400 Subject: [PATCH 27/35] 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(-) diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index 10b94aff0efa..5e23288f5541 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 2d110aab8798..e9cc89f95bbb 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 From 39aa9bf7303c7fdf8d8f3fc62010f8493267cfd5 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 07:38:31 -0400 Subject: [PATCH 28/35] Remove needless indirection in bclose --- src/librustc/hir/print.rs | 8 ++------ src/libsyntax/print/pprust.rs | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 8342331e3609..4b87503ff88c 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 e9cc89f95bbb..2e206811d7ca 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, _) => { From e91dbc5916f508849532d1c7b3069b6c0c3a609b Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 07:42:05 -0400 Subject: [PATCH 29/35] 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(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 4b87503ff88c..246b3341a23f 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 5e23288f5541..be5a00985789 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 2e206811d7ca..288417fcd89c 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() { From cab453250a3ceae5cf0cf7eac836c03b37e4ca8e Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:26:50 -0400 Subject: [PATCH 30/35] Move pp::Printer helpers to direct impl --- src/librustc/hir/map/mod.rs | 2 -- src/librustc/hir/print.rs | 13 +++++++ src/librustc_borrowck/dataflow.rs | 1 - src/librustc_driver/pretty.rs | 1 - src/libsyntax/lib.rs | 1 + src/libsyntax/parse/diagnostics.rs | 2 -- src/libsyntax/parse/parser.rs | 3 +- src/libsyntax/print/helpers.rs | 34 ++++++++++++++++++ src/libsyntax/print/pp.rs | 6 ++-- src/libsyntax/print/pprust.rs | 58 ++++++++---------------------- 10 files changed, 66 insertions(+), 55 deletions(-) create mode 100644 src/libsyntax/print/helpers.rs diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 63f60d0ab952..92898e9070be 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 246b3341a23f..e014e7478db8 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 f1f9f3f71e4a..6eea64e0e7af 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 cd38eb695eb5..51b161c37680 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 a7c5ed158e02..ee7fb97ffd71 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 edcdb18a037d..ae24047ac824 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 a95b6891fb9d..7dd7000b4546 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 000000000000..3449e07f4562 --- /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 be5a00985789..660e77f77d0f 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 288417fcd89c..98c629addc8e 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -432,38 +432,23 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { format!("{}{}", to_string(|s| s.print_visibility(vis)), s) } -pub trait PrintState<'a> { +impl std::ops::Deref for State<'_> { + type Target = pp::Printer; + fn deref(&self) -> &Self::Target { + &self.s + } +} + +impl std::ops::DerefMut for State<'_> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.s + } +} + +pub trait PrintState<'a>: std::ops::Deref + std::ops::DerefMut { fn writer(&mut self) -> &mut pp::Printer; fn comments(&mut self) -> &mut Option>; - fn word_space>>(&mut self, w: S) { - self.writer().word(w); - self.writer().space() - } - - fn popen(&mut self) { self.writer().word("(") } - - fn pclose(&mut self) { self.writer().word(")") } - - fn hardbreak_if_not_bol(&mut self) { - if !self.writer().is_beginning_of_line() { - self.writer().hardbreak() - } - } - - // "raw box" - fn rbox(&mut self, u: usize, b: pp::Breaks) { - self.writer().rbox(u, b) - } - - fn ibox(&mut self, u: usize) { - self.writer().ibox(u); - } - - fn end(&mut self) { - self.writer().end() - } - fn commasep(&mut self, b: Breaks, elts: &[T], mut op: F) where F: FnMut(&mut Self, &T), { @@ -728,12 +713,6 @@ 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 From daf1b29f298ec7e5c345764554e6d03ac65134de Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:32:25 -0400 Subject: [PATCH 31/35] Properly case indent_unit constant --- src/librustc/hir/map/mod.rs | 2 +- src/librustc/hir/print.rs | 49 +++++++++++++++---------------- src/librustc_borrowck/dataflow.rs | 2 +- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 92898e9070be..9fb85410fc77 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -1213,7 +1213,7 @@ impl<'a> print::State<'a> { Node::Arm(a) => self.print_arm(&a), Node::Block(a) => { // containing cbox, will be closed by print-block at } - self.cbox(print::indent_unit); + self.cbox(print::INDENT_UNIT); // head-ibox, will be closed by print-block after { self.ibox(0); self.print_block(&a) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index e014e7478db8..dd5d8ebde1c8 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -96,8 +96,7 @@ impl<'a> PrintState<'a> for State<'a> { } } -#[allow(non_upper_case_globals)] -pub const indent_unit: usize = 4; +pub const INDENT_UNIT: usize = 4; /// Requires you to pass an input filename and reader so that /// it can scan the input text for comments to copy forward. @@ -168,7 +167,7 @@ impl<'a> State<'a> { pub fn head>>(&mut self, w: S) { let w = w.into(); // outer-box is consistent - self.cbox(indent_unit); + self.cbox(INDENT_UNIT); // head-box is inconsistent self.ibox(w.len() + 1); // keyword that starts the head @@ -196,7 +195,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, INDENT_UNIT, true) } pub fn space_if_not_bol(&mut self) { @@ -732,7 +731,7 @@ impl<'a> State<'a> { self.space_if_not_bol(); self.maybe_print_comment(v.span.lo()); self.print_outer_attributes(&v.node.attrs); - self.ibox(indent_unit); + self.ibox(INDENT_UNIT); self.print_variant(v); self.s.word(","); self.end(); @@ -919,10 +918,10 @@ impl<'a> State<'a> { decl: impl Fn(&mut Self) ) { self.space_if_not_bol(); - self.ibox(indent_unit); + self.ibox(INDENT_UNIT); self.word_nbsp("let"); - self.ibox(indent_unit); + self.ibox(INDENT_UNIT); decl(self); self.end(); @@ -964,7 +963,7 @@ impl<'a> State<'a> { } pub fn print_block_unclosed(&mut self, blk: &hir::Block) { - self.print_block_unclosed_indent(blk, indent_unit) + self.print_block_unclosed_indent(blk, INDENT_UNIT) } pub fn print_block_unclosed_indent(&mut self, @@ -978,7 +977,7 @@ impl<'a> State<'a> { blk: &hir::Block, attrs: &[ast::Attribute]) { - self.print_block_maybe_unclosed(blk, indent_unit, attrs, true) + self.print_block_maybe_unclosed(blk, INDENT_UNIT, attrs, true) } pub fn print_block_maybe_unclosed(&mut self, @@ -1055,7 +1054,7 @@ impl<'a> State<'a> { } fn print_expr_vec(&mut self, exprs: &[hir::Expr]) { - self.ibox(indent_unit); + self.ibox(INDENT_UNIT); self.s.word("["); self.commasep_exprs(Inconsistent, exprs); self.s.word("]"); @@ -1063,7 +1062,7 @@ impl<'a> State<'a> { } fn print_expr_repeat(&mut self, element: &hir::Expr, count: &hir::AnonConst) { - self.ibox(indent_unit); + self.ibox(INDENT_UNIT); self.s.word("["); self.print_expr(element); self.word_space(";"); @@ -1082,7 +1081,7 @@ impl<'a> State<'a> { self.commasep_cmnt(Consistent, &fields[..], |s, field| { - s.ibox(indent_unit); + s.ibox(INDENT_UNIT); if !field.is_shorthand { s.print_ident(field.ident); s.word_space(":"); @@ -1093,7 +1092,7 @@ impl<'a> State<'a> { |f| f.span); match *wth { Some(ref expr) => { - self.ibox(indent_unit); + self.ibox(INDENT_UNIT); if !fields.is_empty() { self.s.word(","); self.s.space(); @@ -1198,7 +1197,7 @@ impl<'a> State<'a> { pub fn print_expr(&mut self, expr: &hir::Expr) { self.maybe_print_comment(expr.span.lo()); self.print_outer_attributes(&expr.attrs); - self.ibox(indent_unit); + self.ibox(INDENT_UNIT); self.ann.pre(self, AnnNode::Expr(expr)); match expr.node { hir::ExprKind::Box(ref expr) => { @@ -1250,7 +1249,7 @@ impl<'a> State<'a> { } hir::ExprKind::DropTemps(ref init) => { // Print `{`: - self.cbox(indent_unit); + self.cbox(INDENT_UNIT); self.ibox(0); self.bopen(); @@ -1264,7 +1263,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, INDENT_UNIT, true); } hir::ExprKind::Loop(ref blk, opt_label, _) => { if let Some(label) = opt_label { @@ -1276,7 +1275,7 @@ impl<'a> State<'a> { self.print_block(&blk); } hir::ExprKind::Match(ref expr, ref arms, _) => { - self.cbox(indent_unit); + self.cbox(INDENT_UNIT); self.ibox(4); self.word_nbsp("match"); self.print_expr_as_cond(&expr); @@ -1308,7 +1307,7 @@ impl<'a> State<'a> { self.word_space(":"); } // containing cbox, will be closed by print-block at } - self.cbox(indent_unit); + self.cbox(INDENT_UNIT); // head-box, will be closed by print-block after { self.ibox(0); self.print_block(&blk); @@ -1678,7 +1677,7 @@ impl<'a> State<'a> { self.commasep_cmnt(Consistent, &fields[..], |s, f| { - s.cbox(indent_unit); + s.cbox(INDENT_UNIT); if !f.node.is_shorthand { s.print_ident(f.node.ident); s.word_nbsp(":"); @@ -1787,7 +1786,7 @@ impl<'a> State<'a> { if arm.attrs.is_empty() { self.s.space(); } - self.cbox(indent_unit); + self.cbox(INDENT_UNIT); self.ann.pre(self, AnnNode::Arm(arm)); self.ibox(0); self.print_outer_attributes(&arm.attrs); @@ -1820,7 +1819,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_indent(&blk, INDENT_UNIT); // If it is a user-provided unsafe block, print a comma after it if let hir::UnsafeBlock(hir::UserProvided) = blk.rules { @@ -1859,7 +1858,7 @@ impl<'a> State<'a> { // Make sure we aren't supplied *both* `arg_names` and `body_id`. assert!(arg_names.is_empty() || body_id.is_none()); self.commasep(Inconsistent, &decl.inputs, |s, ty| { - s.ibox(indent_unit); + s.ibox(INDENT_UNIT); if let Some(arg_name) = arg_names.get(i) { s.s.word(arg_name.as_str().to_string()); s.s.word(":"); @@ -1886,7 +1885,7 @@ impl<'a> State<'a> { self.s.word("|"); let mut i = 0; self.commasep(Inconsistent, &decl.inputs, |s, ty| { - s.ibox(indent_unit); + s.ibox(INDENT_UNIT); s.ann.nested(s, Nested::BodyArgPat(body_id, i)); i += 1; @@ -2085,7 +2084,7 @@ impl<'a> State<'a> { } self.space_if_not_bol(); - self.ibox(indent_unit); + self.ibox(INDENT_UNIT); self.word_space("->"); match decl.output { hir::DefaultReturn(..) => unreachable!(), @@ -2107,7 +2106,7 @@ impl<'a> State<'a> { generic_params: &[hir::GenericParam], arg_names: &[ast::Ident]) { - self.ibox(indent_unit); + self.ibox(INDENT_UNIT); if !generic_params.is_empty() { self.s.word("for"); self.print_generic_params(generic_params); diff --git a/src/librustc_borrowck/dataflow.rs b/src/librustc_borrowck/dataflow.rs index 6eea64e0e7af..94849728a931 100644 --- a/src/librustc_borrowck/dataflow.rs +++ b/src/librustc_borrowck/dataflow.rs @@ -529,7 +529,7 @@ impl<'tcx, O: DataFlowOperator + Clone + 'static> DataFlowContext<'tcx, O> { debug!("Dataflow result for {}:", self.analysis_name); debug!("{}", pprust::to_string(self, |s| { - s.cbox(pprust::indent_unit); + s.cbox(pprust::INDENT_UNIT); s.ibox(0); s.print_expr(&body.value) })); From 63fdf1a5274d19865ba27742b3b5c4e0c94b1838 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:30:08 -0400 Subject: [PATCH 32/35] 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(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index dd5d8ebde1c8..f10efbacf2a9 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 7dd7000b4546..83dbff6b2d57 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 98c629addc8e..eddd8700de6b 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 { From 73c1752b8e9a6edb38cfadec25e430320300730b Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:49:37 -0400 Subject: [PATCH 33/35] 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(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index f10efbacf2a9..3fb4339b1e68 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 eddd8700de6b..61b3484c92a7 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(); From 096cb4137dcc1901ae11fe5e8c5bb602bef7199c Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 09:51:56 -0400 Subject: [PATCH 34/35] Remove writer function from PrintState --- src/librustc/hir/print.rs | 6 +-- src/libsyntax/print/pprust.rs | 69 ++++++++++++++++------------------- 2 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index 3fb4339b1e68..5a14b30e699b 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 61b3484c92a7..8da8d3dc540b 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 } From 56a9237b595b4523f2be3123d52662739e89d4c2 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 9 Jul 2019 11:10:03 -0400 Subject: [PATCH 35/35] File is now short enough for tidy --- src/libsyntax/print/pprust.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 8da8d3dc540b..8050026a00d9 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};