Auto merge of #62099 - Mark-Simulacrum:syntax-print-clean-2, r=eddyb

Remove io::Result from syntax::print

Since we're now writing directly to the vector, there's no need to
thread results through the whole printing infrastructure
This commit is contained in:
bors 2019-07-05 06:55:48 +00:00
commit f119bf2761
10 changed files with 2070 additions and 2274 deletions

View File

@ -25,7 +25,6 @@ use crate::hir::print::Nested;
use crate::util::nodemap::FxHashMap;
use crate::util::common::time;
use std::io;
use std::result::Result::Err;
use crate::ty::query::Providers;
@ -1187,7 +1186,7 @@ pub fn map_crate<'hir>(sess: &crate::session::Session,
/// Identical to the `PpAnn` implementation for `hir::Crate`,
/// except it avoids creating a dependency on the whole crate.
impl<'hir> print::PpAnn for Map<'hir> {
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) -> io::Result<()> {
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
match nested {
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
@ -1199,7 +1198,7 @@ impl<'hir> print::PpAnn for Map<'hir> {
}
impl<'a> print::State<'a> {
pub fn print_node(&mut self, node: Node<'_>) -> io::Result<()> {
pub fn print_node(&mut self, node: Node<'_>) {
match node {
Node::Item(a) => self.print_item(&a),
Node::ForeignItem(a) => self.print_foreign_item(&a),
@ -1219,9 +1218,9 @@ impl<'a> print::State<'a> {
use syntax::print::pprust::PrintState;
// 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.ibox(0);
self.print_block(&a)
}
Node::Lifetime(a) => self.print_lifetime(&a),

File diff suppressed because it is too large Load Diff

View File

@ -6,7 +6,6 @@
use rustc::cfg;
use rustc::cfg::CFGIndex;
use rustc::ty::TyCtxt;
use std::io;
use std::mem;
use std::usize;
use syntax::print::pprust::PrintState;
@ -98,23 +97,23 @@ impl<'tcx, O: DataFlowOperator> DataFlowContext<'tcx, O> {
}
impl<'tcx, O: DataFlowOperator> pprust::PpAnn for DataFlowContext<'tcx, O> {
fn nested(&self, state: &mut pprust::State<'_>, nested: pprust::Nested) -> io::Result<()> {
fn nested(&self, state: &mut pprust::State<'_>, nested: pprust::Nested) {
pprust::PpAnn::nested(self.tcx.hir(), state, nested)
}
fn pre(&self,
ps: &mut pprust::State<'_>,
node: pprust::AnnNode<'_>) -> io::Result<()> {
node: pprust::AnnNode<'_>) {
let id = match node {
pprust::AnnNode::Name(_) => return Ok(()),
pprust::AnnNode::Name(_) => return,
pprust::AnnNode::Expr(expr) => expr.hir_id.local_id,
pprust::AnnNode::Block(blk) => blk.hir_id.local_id,
pprust::AnnNode::Item(_) |
pprust::AnnNode::SubItem(_) => return Ok(()),
pprust::AnnNode::SubItem(_) => return,
pprust::AnnNode::Pat(pat) => pat.hir_id.local_id
};
if !self.has_bitset_for_local_id(id) {
return Ok(());
return;
}
assert!(self.bits_per_id > 0);
@ -147,10 +146,9 @@ impl<'tcx, O: DataFlowOperator> pprust::PpAnn for DataFlowContext<'tcx, O> {
ps.synth_comment(
format!("id {}: {}{}{}{}", id.as_usize(), entry_str,
gens_str, action_kills_str, scope_kills_str))?;
ps.s.space()?;
gens_str, action_kills_str, scope_kills_str));
ps.s.space();
}
Ok(())
}
}
@ -531,8 +529,8 @@ 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.ibox(0)?;
s.cbox(pprust::indent_unit);
s.ibox(0);
s.print_expr(&body.value)
}));
}

View File

@ -297,12 +297,9 @@ impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested)
-> io::Result<()> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
if let Some(tcx) = self.tcx {
pprust_hir::PpAnn::nested(tcx.hir(), state, nested)
} else {
Ok(())
}
}
}
@ -323,37 +320,37 @@ impl<'hir> PrinterSupport for IdentifiedAnnotation<'hir> {
}
impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) -> io::Result<()> {
fn pre(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
match node {
pprust::AnnNode::Expr(_) => s.popen(),
_ => Ok(()),
_ => {}
}
}
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) -> io::Result<()> {
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
match node {
pprust::AnnNode::Ident(_) |
pprust::AnnNode::Name(_) => Ok(()),
pprust::AnnNode::Name(_) => {},
pprust::AnnNode::Item(item) => {
s.s.space()?;
s.s.space();
s.synth_comment(item.id.to_string())
}
pprust::AnnNode::SubItem(id) => {
s.s.space()?;
s.s.space();
s.synth_comment(id.to_string())
}
pprust::AnnNode::Block(blk) => {
s.s.space()?;
s.s.space();
s.synth_comment(format!("block {}", blk.id))
}
pprust::AnnNode::Expr(expr) => {
s.s.space()?;
s.synth_comment(expr.id.to_string())?;
s.s.space();
s.synth_comment(expr.id.to_string());
s.pclose()
}
pprust::AnnNode::Pat(pat) => {
s.s.space()?;
s.synth_comment(format!("pat {}", pat.id))
s.s.space();
s.synth_comment(format!("pat {}", pat.id));
}
}
}
@ -374,45 +371,42 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
}
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested)
-> io::Result<()> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
if let Some(ref tcx) = self.tcx {
pprust_hir::PpAnn::nested(tcx.hir(), state, nested)
} else {
Ok(())
}
}
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) -> io::Result<()> {
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
match node {
pprust_hir::AnnNode::Expr(_) => s.popen(),
_ => Ok(()),
_ => {}
}
}
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) -> io::Result<()> {
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
match node {
pprust_hir::AnnNode::Name(_) => Ok(()),
pprust_hir::AnnNode::Name(_) => {},
pprust_hir::AnnNode::Item(item) => {
s.s.space()?;
s.s.space();
s.synth_comment(format!("hir_id: {} hir local_id: {}",
item.hir_id, item.hir_id.local_id.as_u32()))
}
pprust_hir::AnnNode::SubItem(id) => {
s.s.space()?;
s.s.space();
s.synth_comment(id.to_string())
}
pprust_hir::AnnNode::Block(blk) => {
s.s.space()?;
s.s.space();
s.synth_comment(format!("block hir_id: {} hir local_id: {}",
blk.hir_id, blk.hir_id.local_id.as_u32()))
}
pprust_hir::AnnNode::Expr(expr) => {
s.s.space()?;
s.s.space();
s.synth_comment(format!("expr hir_id: {} hir local_id: {}",
expr.hir_id, expr.hir_id.local_id.as_u32()))?;
expr.hir_id, expr.hir_id.local_id.as_u32()));
s.pclose()
}
pprust_hir::AnnNode::Pat(pat) => {
s.s.space()?;
s.s.space();
s.synth_comment(format!("pat hir_id: {} hir local_id: {}",
pat.hir_id, pat.hir_id.local_id.as_u32()))
}
@ -435,19 +429,19 @@ impl<'a> PrinterSupport for HygieneAnnotation<'a> {
}
impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) -> io::Result<()> {
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
match node {
pprust::AnnNode::Ident(&ast::Ident { name, span }) => {
s.s.space()?;
s.s.space();
// FIXME #16420: this doesn't display the connections
// between syntax contexts
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
}
pprust::AnnNode::Name(&name) => {
s.s.space()?;
s.s.space();
s.synth_comment(name.as_u32().to_string())
}
_ => Ok(()),
_ => {}
}
}
}
@ -476,32 +470,30 @@ impl<'b, 'tcx> HirPrinterSupport<'tcx> for TypedAnnotation<'b, 'tcx> {
}
impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested)
-> io::Result<()> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
let old_tables = self.tables.get();
if let pprust_hir::Nested::Body(id) = nested {
self.tables.set(self.tcx.body_tables(id));
}
pprust_hir::PpAnn::nested(self.tcx.hir(), state, nested)?;
pprust_hir::PpAnn::nested(self.tcx.hir(), state, nested);
self.tables.set(old_tables);
Ok(())
}
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) -> io::Result<()> {
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
match node {
pprust_hir::AnnNode::Expr(_) => s.popen(),
_ => Ok(()),
_ => {}
}
}
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) -> io::Result<()> {
fn post(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
match node {
pprust_hir::AnnNode::Expr(expr) => {
s.s.space()?;
s.s.word("as")?;
s.s.space()?;
s.s.word(self.tables.get().expr_ty(expr).to_string())?;
s.pclose()
s.s.space();
s.s.word("as");
s.s.space();
s.s.word(self.tables.get().expr_ty(expr).to_string());
s.pclose();
}
_ => Ok(()),
_ => {},
}
}
}
@ -728,11 +720,11 @@ pub fn print_after_parsing(sess: &Session,
let (src, src_name) = get_source(input, sess);
let mut rdr = &*src;
let mut out = Vec::new();
let mut out = String::new();
if let PpmSource(s) = ppm {
// Silently ignores an identified node.
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support(sess, None, move |annotation| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
@ -741,15 +733,15 @@ pub fn print_after_parsing(sess: &Session,
krate,
src_name,
&mut rdr,
box out,
out,
annotation.pp_ann(),
false)
}).unwrap()
})
} else {
unreachable!();
};
write_output(out, ofile);
write_output(out.into_bytes(), ofile);
}
pub fn print_after_hir_lowering<'tcx>(
@ -773,12 +765,12 @@ pub fn print_after_hir_lowering<'tcx>(
let (src, src_name) = get_source(input, tcx.sess);
let mut rdr = &src[..];
let mut out = Vec::new();
let mut out = String::new();
match (ppm, opt_uii) {
(PpmSource(s), _) => {
// Silently ignores an identified node.
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support(tcx.sess, Some(tcx), move |annotation| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
@ -787,14 +779,14 @@ pub fn print_after_hir_lowering<'tcx>(
krate,
src_name,
&mut rdr,
box out,
out,
annotation.pp_ann(),
true)
})
}
(PpmHir(s), None) => {
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support_hir(tcx, move |annotation, krate| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
@ -803,21 +795,21 @@ pub fn print_after_hir_lowering<'tcx>(
krate,
src_name,
&mut rdr,
box out,
out,
annotation.pp_ann())
})
}
(PpmHirTree(s), None) => {
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support_hir(tcx, move |_annotation, krate| {
debug!("pretty printing source code {:?}", s);
write!(out, "{:#?}", krate)
})
*out = format!("{:#?}", krate);
});
}
(PpmHir(s), Some(uii)) => {
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support_hir(tcx, move |annotation, _| {
debug!("pretty printing source code {:?}", s);
let sess = annotation.sess();
@ -826,40 +818,38 @@ pub fn print_after_hir_lowering<'tcx>(
&sess.parse_sess,
src_name,
&mut rdr,
box out,
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);
let node = hir_map.get(hir_id);
pp_state.print_node(node)?;
pp_state.s.space()?;
pp_state.print_node(node);
pp_state.s.space();
let path = annotation.node_path(hir_id)
.expect("-Z unpretty missing node paths");
pp_state.synth_comment(path)?;
pp_state.s.hardbreak()?;
pp_state.synth_comment(path);
pp_state.s.hardbreak();
}
pp_state.s.eof()
pp_state.s.eof();
})
}
(PpmHirTree(s), Some(uii)) => {
let out: &mut dyn Write = &mut out;
let out = &mut out;
s.call_with_pp_support_hir(tcx, move |_annotation, _krate| {
debug!("pretty printing source code {:?}", s);
for node_id in uii.all_matching_node_ids(tcx.hir()) {
let hir_id = tcx.hir().node_to_hir_id(node_id);
let node = tcx.hir().get(hir_id);
write!(out, "{:#?}", node)?;
out.push_str(&format!("{:#?}", node));
}
Ok(())
})
}
_ => unreachable!(),
}
.unwrap();
write_output(out, ofile);
write_output(out.into_bytes(), ofile);
}
// In an ideal world, this would be a public function called by the driver after

View File

@ -1261,7 +1261,6 @@ pub fn noop_visit_vis<T: MutVisitor>(Spanned { node, span }: &mut Visibility, vi
#[cfg(test)]
mod tests {
use std::io;
use crate::ast::{self, Ident};
use crate::util::parser_testing::{string_to_crate, matches_codepattern};
use crate::print::pprust;
@ -1271,7 +1270,7 @@ mod tests {
// this version doesn't care about getting comments or docstrings in.
fn fake_print_crate(s: &mut pprust::State<'_>,
krate: &ast::Crate) -> io::Result<()> {
krate: &ast::Crate) {
s.print_mod(&krate.module, &krate.attrs)
}

View File

@ -613,12 +613,12 @@ impl<'a> Parser<'a> {
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)?;
s.popen()?;
s.print_type(&mut_ty.ty)?;
s.print_type_bounds(" +", &bounds)?;
s.s.word("&");
s.print_opt_lifetime(lifetime);
s.print_mutability(mut_ty.mutbl);
s.popen();
s.print_type(&mut_ty.ty);
s.print_type_bounds(" +", &bounds);
s.pclose()
});
err.span_suggestion(

View File

@ -2572,12 +2572,12 @@ impl<'a> Parser<'a> {
};
let sugg = pprust::to_string(|s| {
use crate::print::pprust::PrintState;
s.popen()?;
s.print_expr(&e)?;
s.s.word( ".")?;
s.print_usize(float.trunc() as usize)?;
s.pclose()?;
s.s.word(".")?;
s.popen();
s.print_expr(&e);
s.s.word( ".");
s.print_usize(float.trunc() as usize);
s.pclose();
s.s.word(".");
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
});
err.span_suggestion(
@ -4583,9 +4583,9 @@ impl<'a> Parser<'a> {
}
let sugg = pprust::to_string(|s| {
use crate::print::pprust::{PrintState, INDENT_UNIT};
s.ibox(INDENT_UNIT)?;
s.bopen()?;
s.print_stmt(&stmt)?;
s.ibox(INDENT_UNIT);
s.bopen();
s.print_stmt(&stmt);
s.bclose_maybe_open(stmt.span, INDENT_UNIT, false)
});
e.span_suggestion(

View File

@ -136,7 +136,6 @@
use std::collections::VecDeque;
use std::fmt;
use std::io;
use std::borrow::Cow;
use log::debug;
@ -172,7 +171,7 @@ pub enum Token {
}
impl Token {
pub fn is_eof(&self) -> bool {
crate fn is_eof(&self) -> bool {
match *self {
Token::Eof => true,
_ => false,
@ -223,20 +222,21 @@ fn buf_str(buf: &[BufEntry], left: usize, right: usize, lim: usize) -> String {
}
#[derive(Copy, Clone)]
pub enum PrintStackBreak {
crate enum PrintStackBreak {
Fits,
Broken(Breaks),
}
#[derive(Copy, Clone)]
pub struct PrintStackElem {
crate struct PrintStackElem {
offset: isize,
pbreak: PrintStackBreak
}
const SIZE_INFINITY: isize = 0xffff;
pub fn mk_printer<'a>(out: Box<dyn io::Write+'a>, linewidth: usize) -> Printer<'a> {
pub fn mk_printer(out: &mut String) -> 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);
@ -259,7 +259,7 @@ pub fn mk_printer<'a>(out: Box<dyn io::Write+'a>, linewidth: usize) -> Printer<'
}
pub struct Printer<'a> {
out: Box<dyn io::Write+'a>,
out: &'a mut String,
buf_max_len: usize,
/// Width of lines we're constrained to
margin: isize,
@ -300,8 +300,6 @@ impl Default for BufEntry {
}
}
const SPACES: [u8; 128] = [b' '; 128];
impl<'a> Printer<'a> {
pub fn last_token(&mut self) -> Token {
self.buf[self.right].token.clone()
@ -312,16 +310,15 @@ impl<'a> Printer<'a> {
self.buf[self.right].token = t;
}
fn pretty_print_eof(&mut self) -> io::Result<()> {
fn pretty_print_eof(&mut self) {
if !self.scan_stack.is_empty() {
self.check_stack(0);
self.advance_left()?;
self.advance_left();
}
self.indent(0);
Ok(())
}
fn pretty_print_begin(&mut self, b: BeginToken) -> io::Result<()> {
fn pretty_print_begin(&mut self, b: BeginToken) {
if self.scan_stack.is_empty() {
self.left_total = 1;
self.right_total = 1;
@ -335,24 +332,22 @@ impl<'a> Printer<'a> {
self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total };
let right = self.right;
self.scan_push(right);
Ok(())
}
fn pretty_print_end(&mut self) -> io::Result<()> {
fn pretty_print_end(&mut self) {
if self.scan_stack.is_empty() {
debug!("pp End/print Vec<{},{}>", self.left, self.right);
self.print_end()
self.print_end();
} 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);
Ok(())
}
}
fn pretty_print_break(&mut self, b: BreakToken) -> io::Result<()> {
fn pretty_print_break(&mut self, b: BreakToken) {
if self.scan_stack.is_empty() {
self.left_total = 1;
self.right_total = 1;
@ -368,25 +363,24 @@ impl<'a> Printer<'a> {
self.scan_push(right);
self.buf[self.right] = BufEntry { token: Token::Break(b), size: -self.right_total };
self.right_total += b.blank_space;
Ok(())
}
fn pretty_print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
fn pretty_print_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);
self.print_string(s, len)
self.print_string(s, len);
} 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 };
self.right_total += len;
self.check_stream()
self.check_stream();
}
}
pub fn check_stream(&mut self) -> io::Result<()> {
crate 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 {
@ -397,32 +391,31 @@ impl<'a> Printer<'a> {
let scanned = self.scan_pop_bottom();
self.buf[scanned].size = SIZE_INFINITY;
}
self.advance_left()?;
self.advance_left();
if self.left != self.right {
self.check_stream()?;
self.check_stream();
}
}
Ok(())
}
pub fn scan_push(&mut self, x: usize) {
crate fn scan_push(&mut self, x: usize) {
debug!("scan_push {}", x);
self.scan_stack.push_front(x);
}
pub fn scan_pop(&mut self) -> usize {
crate fn scan_pop(&mut self) -> usize {
self.scan_stack.pop_front().unwrap()
}
pub fn scan_top(&mut self) -> usize {
crate fn scan_top(&mut self) -> usize {
*self.scan_stack.front().unwrap()
}
pub fn scan_pop_bottom(&mut self) -> usize {
crate fn scan_pop_bottom(&mut self) -> usize {
self.scan_stack.pop_back().unwrap()
}
pub fn advance_right(&mut self) {
crate fn advance_right(&mut self) {
self.right += 1;
self.right %= self.buf_max_len;
// Extend the buf if necessary.
@ -432,7 +425,7 @@ impl<'a> Printer<'a> {
assert_ne!(self.right, self.left);
}
pub fn advance_left(&mut self) -> io::Result<()> {
crate fn advance_left(&mut self) {
debug!("advance_left Vec<{},{}>, sizeof({})={}", self.left, self.right,
self.left, self.buf[self.left].size);
@ -450,7 +443,7 @@ impl<'a> Printer<'a> {
_ => 0
};
self.print(left, left_size)?;
self.print(left, left_size);
self.left_total += len;
@ -463,11 +456,9 @@ impl<'a> Printer<'a> {
left_size = self.buf[self.left].size;
}
Ok(())
}
pub fn check_stack(&mut self, k: isize) {
crate fn check_stack(&mut self, k: isize) {
if !self.scan_stack.is_empty() {
let x = self.scan_top();
match self.buf[x].token {
@ -495,20 +486,19 @@ impl<'a> Printer<'a> {
}
}
pub fn print_newline(&mut self, amount: isize) -> io::Result<()> {
crate fn print_newline(&mut self, amount: isize) {
debug!("NEWLINE {}", amount);
let ret = writeln!(self.out);
self.out.push('\n');
self.pending_indentation = 0;
self.indent(amount);
ret
}
pub fn indent(&mut self, amount: isize) {
crate fn indent(&mut self, amount: isize) {
debug!("INDENT {}", amount);
self.pending_indentation += amount;
}
pub fn get_top(&mut self) -> PrintStackElem {
crate fn get_top(&mut self) -> PrintStackElem {
match self.print_stack.last() {
Some(el) => *el,
None => PrintStackElem {
@ -518,7 +508,7 @@ impl<'a> Printer<'a> {
}
}
pub fn print_begin(&mut self, b: BeginToken, l: isize) -> io::Result<()> {
crate 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);
@ -533,52 +523,46 @@ impl<'a> Printer<'a> {
pbreak: PrintStackBreak::Fits
});
}
Ok(())
}
pub fn print_end(&mut self) -> io::Result<()> {
crate 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();
Ok(())
}
pub fn print_break(&mut self, b: BreakToken, l: isize) -> io::Result<()> {
crate fn print_break(&mut self, b: BreakToken, l: isize) {
let top = self.get_top();
match top.pbreak {
PrintStackBreak::Fits => {
debug!("print Break({}) in fitting block", b.blank_space);
self.space -= b.blank_space;
self.indent(b.blank_space);
Ok(())
}
PrintStackBreak::Broken(Breaks::Consistent) => {
debug!("print Break({}+{}) in consistent block",
top.offset, b.offset);
let ret = self.print_newline(top.offset + b.offset);
self.print_newline(top.offset + b.offset);
self.space = self.margin - (top.offset + b.offset);
ret
}
PrintStackBreak::Broken(Breaks::Inconsistent) => {
if l > self.space {
debug!("print Break({}+{}) w/ newline in inconsistent",
top.offset, b.offset);
let ret = self.print_newline(top.offset + b.offset);
self.print_newline(top.offset + b.offset);
self.space = self.margin - (top.offset + b.offset);
ret
} else {
debug!("print Break({}) w/o newline in inconsistent",
b.blank_space);
self.indent(b.blank_space);
self.space -= b.blank_space;
Ok(())
}
}
}
}
pub fn print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
crate fn print_string(&mut self, s: Cow<'static, str>, len: isize) {
debug!("print String({})", s);
// assert!(len <= space);
self.space -= len;
@ -587,23 +571,15 @@ impl<'a> Printer<'a> {
//
// write!(self.out, "{: >n$}", "", n = self.pending_indentation as usize)?;
//
// But that is significantly slower than using `SPACES`. This code is
// sufficiently hot, and indents can get sufficiently large, that the
// difference is significant on some workloads.
let spaces_len = SPACES.len() as isize;
while self.pending_indentation >= spaces_len {
self.out.write_all(&SPACES)?;
self.pending_indentation -= spaces_len;
}
if self.pending_indentation > 0 {
self.out.write_all(&SPACES[0..self.pending_indentation as usize])?;
self.pending_indentation = 0;
}
write!(self.out, "{}", s)
// But that is significantly slower. This code is sufficiently hot, and indents can get
// sufficiently large, that the difference is significant on some workloads.
self.out.reserve(self.pending_indentation as usize);
self.out.extend(std::iter::repeat(' ').take(self.pending_indentation as usize));
self.pending_indentation = 0;
self.out.push_str(&s);
}
pub fn print(&mut self, token: Token, l: isize) -> io::Result<()> {
crate fn print(&mut self, token: Token, l: isize) {
debug!("print {} {} (remaining line space={})", token, l,
self.space);
debug!("{}", buf_str(&self.buf,
@ -616,7 +592,7 @@ impl<'a> Printer<'a> {
Token::Break(b) => self.print_break(b, l),
Token::String(s, len) => {
assert_eq!(len, l);
self.print_string(s, len)
self.print_string(s, len);
}
Token::Eof => panic!(), // Eof should never get here.
}
@ -625,7 +601,7 @@ impl<'a> Printer<'a> {
// Convenience functions to talk to the printer.
/// "raw box"
pub fn rbox(&mut self, indent: usize, b: Breaks) -> io::Result<()> {
crate fn rbox(&mut self, indent: usize, b: Breaks) {
self.pretty_print_begin(BeginToken {
offset: indent as isize,
breaks: b
@ -633,57 +609,53 @@ impl<'a> Printer<'a> {
}
/// Inconsistent breaking box
pub fn ibox(&mut self, indent: usize) -> io::Result<()> {
crate fn ibox(&mut self, indent: usize) {
self.rbox(indent, Breaks::Inconsistent)
}
/// Consistent breaking box
pub fn cbox(&mut self, indent: usize) -> io::Result<()> {
pub fn cbox(&mut self, indent: usize) {
self.rbox(indent, Breaks::Consistent)
}
pub fn break_offset(&mut self, n: usize, off: isize) -> io::Result<()> {
pub fn break_offset(&mut self, n: usize, off: isize) {
self.pretty_print_break(BreakToken {
offset: off,
blank_space: n as isize
})
}
pub fn end(&mut self) -> io::Result<()> {
crate fn end(&mut self) {
self.pretty_print_end()
}
pub fn eof(&mut self) -> io::Result<()> {
pub fn eof(&mut self) {
self.pretty_print_eof()
}
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) -> io::Result<()> {
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) {
let s = wrd.into();
let len = s.len() as isize;
self.pretty_print_string(s, len)
}
fn spaces(&mut self, n: usize) -> io::Result<()> {
fn spaces(&mut self, n: usize) {
self.break_offset(n, 0)
}
pub fn zerobreak(&mut self) -> io::Result<()> {
crate fn zerobreak(&mut self) {
self.spaces(0)
}
pub fn space(&mut self) -> io::Result<()> {
pub fn space(&mut self) {
self.spaces(1)
}
pub fn hardbreak(&mut self) -> io::Result<()> {
pub fn hardbreak(&mut self) {
self.spaces(SIZE_INFINITY as usize)
}
pub fn hardbreak_tok_offset(off: isize) -> Token {
Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
}
pub fn hardbreak_tok() -> Token {
Self::hardbreak_tok_offset(0)
}
}

File diff suppressed because it is too large Load Diff

View File

@ -26,7 +26,6 @@ use crate::mut_visit::{*, ExpectOne};
use crate::feature_gate::Features;
use crate::util::map_in_place::MapInPlace;
use crate::parse::{token, ParseSess};
use crate::print::pprust;
use crate::ast::{self, Ident};
use crate::ptr::P;
use crate::symbol::{self, Symbol, kw, sym};