Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up and streamline the pretty-printer #56336

Merged
merged 4 commits into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 32 additions & 29 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use hir;
use hir::{PatKind, GenericBound, TraitBoundModifier, RangeEnd};
use hir::{GenericParam, GenericParamKind, GenericArg};

use std::borrow::Cow;
use std::cell::Cell;
use std::io::{self, Write, Read};
use std::iter::Peekable;
Expand Down Expand Up @@ -209,7 +210,7 @@ pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
String::from_utf8(wr).unwrap()
}

pub fn visibility_qualified(vis: &hir::Visibility, w: &str) -> String {
pub fn visibility_qualified<S: Into<Cow<'static, str>>>(vis: &hir::Visibility, w: S) -> String {
to_string(NO_ANN, |s| {
s.print_visibility(vis)?;
s.s.word(w)
Expand All @@ -226,12 +227,13 @@ impl<'a> State<'a> {
self.s.word(" ")
}

pub fn word_nbsp(&mut self, w: &str) -> io::Result<()> {
pub fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) -> io::Result<()> {
self.s.word(w)?;
self.nbsp()
}

pub fn head(&mut self, w: &str) -> io::Result<()> {
pub fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) -> io::Result<()> {
let w = w.into();
// outer-box is consistent
self.cbox(indent_unit)?;
// head-box is inconsistent
Expand Down Expand Up @@ -303,7 +305,7 @@ impl<'a> State<'a> {
pub fn synth_comment(&mut self, text: String) -> io::Result<()> {
self.s.word("/*")?;
self.s.space()?;
self.s.word(&text[..])?;
self.s.word(text)?;
self.s.space()?;
self.s.word("*/")
}
Expand Down Expand Up @@ -468,7 +470,7 @@ impl<'a> State<'a> {
self.end() // end the outer fn box
}
hir::ForeignItemKind::Static(ref t, m) => {
self.head(&visibility_qualified(&item.vis, "static"))?;
self.head(visibility_qualified(&item.vis, "static"))?;
if m {
self.word_space("mut")?;
}
Expand All @@ -480,7 +482,7 @@ impl<'a> State<'a> {
self.end() // end the outer cbox
}
hir::ForeignItemKind::Type => {
self.head(&visibility_qualified(&item.vis, "type"))?;
self.head(visibility_qualified(&item.vis, "type"))?;
self.print_name(item.name)?;
self.s.word(";")?;
self.end()?; // end the head-ibox
Expand All @@ -495,7 +497,7 @@ impl<'a> State<'a> {
default: Option<hir::BodyId>,
vis: &hir::Visibility)
-> io::Result<()> {
self.s.word(&visibility_qualified(vis, ""))?;
self.s.word(visibility_qualified(vis, ""))?;
self.word_space("const")?;
self.print_ident(ident)?;
self.word_space(":")?;
Expand Down Expand Up @@ -534,7 +536,7 @@ impl<'a> State<'a> {
self.ann.pre(self, AnnNode::Item(item))?;
match item.node {
hir::ItemKind::ExternCrate(orig_name) => {
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
self.head(visibility_qualified(&item.vis, "extern crate"))?;
if let Some(orig_name) = orig_name {
self.print_name(orig_name)?;
self.s.space()?;
Expand All @@ -547,7 +549,7 @@ impl<'a> State<'a> {
self.end()?; // end outer head-block
}
hir::ItemKind::Use(ref path, kind) => {
self.head(&visibility_qualified(&item.vis, "use"))?;
self.head(visibility_qualified(&item.vis, "use"))?;
self.print_path(path, false)?;

match kind {
Expand All @@ -566,7 +568,7 @@ impl<'a> State<'a> {
self.end()?; // end outer head-block
}
hir::ItemKind::Static(ref ty, m, expr) => {
self.head(&visibility_qualified(&item.vis, "static"))?;
self.head(visibility_qualified(&item.vis, "static"))?;
if m == hir::MutMutable {
self.word_space("mut")?;
}
Expand All @@ -582,7 +584,7 @@ impl<'a> State<'a> {
self.end()?; // end the outer cbox
}
hir::ItemKind::Const(ref ty, expr) => {
self.head(&visibility_qualified(&item.vis, "const"))?;
self.head(visibility_qualified(&item.vis, "const"))?;
self.print_name(item.name)?;
self.word_space(":")?;
self.print_type(&ty)?;
Expand All @@ -609,7 +611,7 @@ impl<'a> State<'a> {
self.ann.nested(self, Nested::Body(body))?;
}
hir::ItemKind::Mod(ref _mod) => {
self.head(&visibility_qualified(&item.vis, "mod"))?;
self.head(visibility_qualified(&item.vis, "mod"))?;
self.print_name(item.name)?;
self.nbsp()?;
self.bopen()?;
Expand All @@ -618,18 +620,18 @@ impl<'a> State<'a> {
}
hir::ItemKind::ForeignMod(ref nmod) => {
self.head("extern")?;
self.word_nbsp(&nmod.abi.to_string())?;
self.word_nbsp(nmod.abi.to_string())?;
self.bopen()?;
self.print_foreign_mod(nmod, &item.attrs)?;
self.bclose(item.span)?;
}
hir::ItemKind::GlobalAsm(ref ga) => {
self.head(&visibility_qualified(&item.vis, "global asm"))?;
self.s.word(&ga.asm.as_str())?;
self.head(visibility_qualified(&item.vis, "global asm"))?;
self.s.word(ga.asm.as_str().get())?;
self.end()?
}
hir::ItemKind::Ty(ref ty, ref generics) => {
self.head(&visibility_qualified(&item.vis, "type"))?;
self.head(visibility_qualified(&item.vis, "type"))?;
self.print_name(item.name)?;
self.print_generic_params(&generics.params)?;
self.end()?; // end the inner ibox
Expand All @@ -642,7 +644,7 @@ impl<'a> State<'a> {
self.end()?; // end the outer ibox
}
hir::ItemKind::Existential(ref exist) => {
self.head(&visibility_qualified(&item.vis, "existential type"))?;
self.head(visibility_qualified(&item.vis, "existential type"))?;
self.print_name(item.name)?;
self.print_generic_params(&exist.generics.params)?;
self.end()?; // end the inner ibox
Expand All @@ -668,11 +670,11 @@ impl<'a> State<'a> {
self.print_enum_def(enum_definition, params, item.name, item.span, &item.vis)?;
}
hir::ItemKind::Struct(ref struct_def, ref generics) => {
self.head(&visibility_qualified(&item.vis, "struct"))?;
self.head(visibility_qualified(&item.vis, "struct"))?;
self.print_struct(struct_def, generics, item.name, item.span, true)?;
}
hir::ItemKind::Union(ref struct_def, ref generics) => {
self.head(&visibility_qualified(&item.vis, "union"))?;
self.head(visibility_qualified(&item.vis, "union"))?;
self.print_struct(struct_def, generics, item.name, item.span, true)?;
}
hir::ItemKind::Impl(unsafety,
Expand Down Expand Up @@ -795,7 +797,7 @@ impl<'a> State<'a> {
span: syntax_pos::Span,
visibility: &hir::Visibility)
-> io::Result<()> {
self.head(&visibility_qualified(visibility, "enum"))?;
self.head(visibility_qualified(visibility, "enum"))?;
self.print_name(name)?;
self.print_generic_params(&generics.params)?;
self.print_where_clause(&generics.where_clause)?;
Expand Down Expand Up @@ -1587,14 +1589,14 @@ impl<'a> State<'a> {
}

pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
self.s.word(&i.to_string())
self.s.word(i.to_string())
}

pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> {
if ident.is_raw_guess() {
self.s.word(&format!("r#{}", ident.name))?;
self.s.word(format!("r#{}", ident.name))?;
} else {
self.s.word(&ident.as_str())?;
self.s.word(ident.as_str().get())?;
}
self.ann.post(self, AnnNode::Name(&ident.name))
}
Expand Down Expand Up @@ -2010,7 +2012,7 @@ impl<'a> State<'a> {
self.commasep(Inconsistent, &decl.inputs, |s, ty| {
s.ibox(indent_unit)?;
if let Some(arg_name) = arg_names.get(i) {
s.s.word(&arg_name.as_str())?;
s.s.word(arg_name.as_str().get())?;
s.s.word(":")?;
s.s.space()?;
} else if let Some(body_id) = body_id {
Expand Down Expand Up @@ -2073,7 +2075,8 @@ impl<'a> State<'a> {
}
}

pub fn print_bounds(&mut self, prefix: &str, bounds: &[hir::GenericBound]) -> io::Result<()> {
pub fn print_bounds(&mut self, prefix: &'static str, bounds: &[hir::GenericBound])
-> io::Result<()> {
if !bounds.is_empty() {
self.s.word(prefix)?;
let mut first = true;
Expand Down Expand Up @@ -2322,7 +2325,7 @@ impl<'a> State<'a> {
Some(Abi::Rust) => Ok(()),
Some(abi) => {
self.word_nbsp("extern")?;
self.word_nbsp(&abi.to_string())
self.word_nbsp(abi.to_string())
}
None => Ok(()),
}
Expand All @@ -2332,7 +2335,7 @@ impl<'a> State<'a> {
match opt_abi {
Some(abi) => {
self.word_nbsp("extern")?;
self.word_nbsp(&abi.to_string())
self.word_nbsp(abi.to_string())
}
None => Ok(()),
}
Expand All @@ -2342,7 +2345,7 @@ impl<'a> State<'a> {
header: hir::FnHeader,
vis: &hir::Visibility)
-> io::Result<()> {
self.s.word(&visibility_qualified(vis, ""))?;
self.s.word(visibility_qualified(vis, ""))?;

match header.constness {
hir::Constness::NotConst => {}
Expand All @@ -2358,7 +2361,7 @@ impl<'a> State<'a> {

if header.abi != Abi::Rust {
self.word_nbsp("extern")?;
self.word_nbsp(&header.abi.to_string())?;
self.word_nbsp(header.abi.to_string())?;
}

self.s.word("fn")
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
s.s.space()?;
s.s.word("as")?;
s.s.space()?;
s.s.word(&self.tables.get().expr_ty(expr).to_string())?;
s.s.word(self.tables.get().expr_ty(expr).to_string())?;
s.pclose()
}
_ => Ok(()),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
Some(orig_name) => format!("use {} as {};", orig_name, item.name),
None => format!("use {};", item.name),
};
let replacement = visibility_qualified(&item.vis, &base_replacement);
let replacement = visibility_qualified(&item.vis, base_replacement);
tcx.struct_span_lint_node(lint, id, extern_crate.span, msg)
.span_suggestion_short_with_applicability(
extern_crate.span,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2795,7 +2795,7 @@ impl<'a> Parser<'a> {
s.print_usize(float.trunc() as usize)?;
s.pclose()?;
s.s.word(".")?;
s.s.word(fstr.splitn(2, ".").last().unwrap())
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
});
err.span_suggestion_with_applicability(
lo.to(self.prev_span),
Expand Down
Loading