Skip to content

Commit ba7b892

Browse files
authored
Rollup merge of rust-lang#56336 - nnethercote:clean-up-pp, r=nikomatsakis
Clean up and streamline the pretty-printer Some minor improvements.
2 parents 5afb113 + 64cd645 commit ba7b892

File tree

7 files changed

+254
-232
lines changed

7 files changed

+254
-232
lines changed

src/librustc/hir/print.rs

+32-29
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use hir;
2525
use hir::{PatKind, GenericBound, TraitBoundModifier, RangeEnd};
2626
use hir::{GenericParam, GenericParamKind, GenericArg};
2727

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

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

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

234-
pub fn head(&mut self, w: &str) -> io::Result<()> {
235+
pub fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) -> io::Result<()> {
236+
let w = w.into();
235237
// outer-box is consistent
236238
self.cbox(indent_unit)?;
237239
// head-box is inconsistent
@@ -303,7 +305,7 @@ impl<'a> State<'a> {
303305
pub fn synth_comment(&mut self, text: String) -> io::Result<()> {
304306
self.s.word("/*")?;
305307
self.s.space()?;
306-
self.s.word(&text[..])?;
308+
self.s.word(text)?;
307309
self.s.space()?;
308310
self.s.word("*/")
309311
}
@@ -468,7 +470,7 @@ impl<'a> State<'a> {
468470
self.end() // end the outer fn box
469471
}
470472
hir::ForeignItemKind::Static(ref t, m) => {
471-
self.head(&visibility_qualified(&item.vis, "static"))?;
473+
self.head(visibility_qualified(&item.vis, "static"))?;
472474
if m {
473475
self.word_space("mut")?;
474476
}
@@ -480,7 +482,7 @@ impl<'a> State<'a> {
480482
self.end() // end the outer cbox
481483
}
482484
hir::ForeignItemKind::Type => {
483-
self.head(&visibility_qualified(&item.vis, "type"))?;
485+
self.head(visibility_qualified(&item.vis, "type"))?;
484486
self.print_name(item.name)?;
485487
self.s.word(";")?;
486488
self.end()?; // end the head-ibox
@@ -495,7 +497,7 @@ impl<'a> State<'a> {
495497
default: Option<hir::BodyId>,
496498
vis: &hir::Visibility)
497499
-> io::Result<()> {
498-
self.s.word(&visibility_qualified(vis, ""))?;
500+
self.s.word(visibility_qualified(vis, ""))?;
499501
self.word_space("const")?;
500502
self.print_ident(ident)?;
501503
self.word_space(":")?;
@@ -534,7 +536,7 @@ impl<'a> State<'a> {
534536
self.ann.pre(self, AnnNode::Item(item))?;
535537
match item.node {
536538
hir::ItemKind::ExternCrate(orig_name) => {
537-
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
539+
self.head(visibility_qualified(&item.vis, "extern crate"))?;
538540
if let Some(orig_name) = orig_name {
539541
self.print_name(orig_name)?;
540542
self.s.space()?;
@@ -547,7 +549,7 @@ impl<'a> State<'a> {
547549
self.end()?; // end outer head-block
548550
}
549551
hir::ItemKind::Use(ref path, kind) => {
550-
self.head(&visibility_qualified(&item.vis, "use"))?;
552+
self.head(visibility_qualified(&item.vis, "use"))?;
551553
self.print_path(path, false)?;
552554

553555
match kind {
@@ -566,7 +568,7 @@ impl<'a> State<'a> {
566568
self.end()?; // end outer head-block
567569
}
568570
hir::ItemKind::Static(ref ty, m, expr) => {
569-
self.head(&visibility_qualified(&item.vis, "static"))?;
571+
self.head(visibility_qualified(&item.vis, "static"))?;
570572
if m == hir::MutMutable {
571573
self.word_space("mut")?;
572574
}
@@ -582,7 +584,7 @@ impl<'a> State<'a> {
582584
self.end()?; // end the outer cbox
583585
}
584586
hir::ItemKind::Const(ref ty, expr) => {
585-
self.head(&visibility_qualified(&item.vis, "const"))?;
587+
self.head(visibility_qualified(&item.vis, "const"))?;
586588
self.print_name(item.name)?;
587589
self.word_space(":")?;
588590
self.print_type(&ty)?;
@@ -609,7 +611,7 @@ impl<'a> State<'a> {
609611
self.ann.nested(self, Nested::Body(body))?;
610612
}
611613
hir::ItemKind::Mod(ref _mod) => {
612-
self.head(&visibility_qualified(&item.vis, "mod"))?;
614+
self.head(visibility_qualified(&item.vis, "mod"))?;
613615
self.print_name(item.name)?;
614616
self.nbsp()?;
615617
self.bopen()?;
@@ -618,18 +620,18 @@ impl<'a> State<'a> {
618620
}
619621
hir::ItemKind::ForeignMod(ref nmod) => {
620622
self.head("extern")?;
621-
self.word_nbsp(&nmod.abi.to_string())?;
623+
self.word_nbsp(nmod.abi.to_string())?;
622624
self.bopen()?;
623625
self.print_foreign_mod(nmod, &item.attrs)?;
624626
self.bclose(item.span)?;
625627
}
626628
hir::ItemKind::GlobalAsm(ref ga) => {
627-
self.head(&visibility_qualified(&item.vis, "global asm"))?;
628-
self.s.word(&ga.asm.as_str())?;
629+
self.head(visibility_qualified(&item.vis, "global asm"))?;
630+
self.s.word(ga.asm.as_str().get())?;
629631
self.end()?
630632
}
631633
hir::ItemKind::Ty(ref ty, ref generics) => {
632-
self.head(&visibility_qualified(&item.vis, "type"))?;
634+
self.head(visibility_qualified(&item.vis, "type"))?;
633635
self.print_name(item.name)?;
634636
self.print_generic_params(&generics.params)?;
635637
self.end()?; // end the inner ibox
@@ -642,7 +644,7 @@ impl<'a> State<'a> {
642644
self.end()?; // end the outer ibox
643645
}
644646
hir::ItemKind::Existential(ref exist) => {
645-
self.head(&visibility_qualified(&item.vis, "existential type"))?;
647+
self.head(visibility_qualified(&item.vis, "existential type"))?;
646648
self.print_name(item.name)?;
647649
self.print_generic_params(&exist.generics.params)?;
648650
self.end()?; // end the inner ibox
@@ -668,11 +670,11 @@ impl<'a> State<'a> {
668670
self.print_enum_def(enum_definition, params, item.name, item.span, &item.vis)?;
669671
}
670672
hir::ItemKind::Struct(ref struct_def, ref generics) => {
671-
self.head(&visibility_qualified(&item.vis, "struct"))?;
673+
self.head(visibility_qualified(&item.vis, "struct"))?;
672674
self.print_struct(struct_def, generics, item.name, item.span, true)?;
673675
}
674676
hir::ItemKind::Union(ref struct_def, ref generics) => {
675-
self.head(&visibility_qualified(&item.vis, "union"))?;
677+
self.head(visibility_qualified(&item.vis, "union"))?;
676678
self.print_struct(struct_def, generics, item.name, item.span, true)?;
677679
}
678680
hir::ItemKind::Impl(unsafety,
@@ -795,7 +797,7 @@ impl<'a> State<'a> {
795797
span: syntax_pos::Span,
796798
visibility: &hir::Visibility)
797799
-> io::Result<()> {
798-
self.head(&visibility_qualified(visibility, "enum"))?;
800+
self.head(visibility_qualified(visibility, "enum"))?;
799801
self.print_name(name)?;
800802
self.print_generic_params(&generics.params)?;
801803
self.print_where_clause(&generics.where_clause)?;
@@ -1587,14 +1589,14 @@ impl<'a> State<'a> {
15871589
}
15881590

15891591
pub fn print_usize(&mut self, i: usize) -> io::Result<()> {
1590-
self.s.word(&i.to_string())
1592+
self.s.word(i.to_string())
15911593
}
15921594

15931595
pub fn print_ident(&mut self, ident: ast::Ident) -> io::Result<()> {
15941596
if ident.is_raw_guess() {
1595-
self.s.word(&format!("r#{}", ident.name))?;
1597+
self.s.word(format!("r#{}", ident.name))?;
15961598
} else {
1597-
self.s.word(&ident.as_str())?;
1599+
self.s.word(ident.as_str().get())?;
15981600
}
15991601
self.ann.post(self, AnnNode::Name(&ident.name))
16001602
}
@@ -2010,7 +2012,7 @@ impl<'a> State<'a> {
20102012
self.commasep(Inconsistent, &decl.inputs, |s, ty| {
20112013
s.ibox(indent_unit)?;
20122014
if let Some(arg_name) = arg_names.get(i) {
2013-
s.s.word(&arg_name.as_str())?;
2015+
s.s.word(arg_name.as_str().get())?;
20142016
s.s.word(":")?;
20152017
s.s.space()?;
20162018
} else if let Some(body_id) = body_id {
@@ -2073,7 +2075,8 @@ impl<'a> State<'a> {
20732075
}
20742076
}
20752077

2076-
pub fn print_bounds(&mut self, prefix: &str, bounds: &[hir::GenericBound]) -> io::Result<()> {
2078+
pub fn print_bounds(&mut self, prefix: &'static str, bounds: &[hir::GenericBound])
2079+
-> io::Result<()> {
20772080
if !bounds.is_empty() {
20782081
self.s.word(prefix)?;
20792082
let mut first = true;
@@ -2322,7 +2325,7 @@ impl<'a> State<'a> {
23222325
Some(Abi::Rust) => Ok(()),
23232326
Some(abi) => {
23242327
self.word_nbsp("extern")?;
2325-
self.word_nbsp(&abi.to_string())
2328+
self.word_nbsp(abi.to_string())
23262329
}
23272330
None => Ok(()),
23282331
}
@@ -2332,7 +2335,7 @@ impl<'a> State<'a> {
23322335
match opt_abi {
23332336
Some(abi) => {
23342337
self.word_nbsp("extern")?;
2335-
self.word_nbsp(&abi.to_string())
2338+
self.word_nbsp(abi.to_string())
23362339
}
23372340
None => Ok(()),
23382341
}
@@ -2342,7 +2345,7 @@ impl<'a> State<'a> {
23422345
header: hir::FnHeader,
23432346
vis: &hir::Visibility)
23442347
-> io::Result<()> {
2345-
self.s.word(&visibility_qualified(vis, ""))?;
2348+
self.s.word(visibility_qualified(vis, ""))?;
23462349

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

23592362
if header.abi != Abi::Rust {
23602363
self.word_nbsp("extern")?;
2361-
self.word_nbsp(&header.abi.to_string())?;
2364+
self.word_nbsp(header.abi.to_string())?;
23622365
}
23632366

23642367
self.s.word("fn")

src/librustc_driver/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
530530
s.s.space()?;
531531
s.s.word("as")?;
532532
s.s.space()?;
533-
s.s.word(&self.tables.get().expr_ty(expr).to_string())?;
533+
s.s.word(self.tables.get().expr_ty(expr).to_string())?;
534534
s.pclose()
535535
}
536536
_ => Ok(()),

src/librustc_typeck/check_unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
185185
Some(orig_name) => format!("use {} as {};", orig_name, item.name),
186186
None => format!("use {};", item.name),
187187
};
188-
let replacement = visibility_qualified(&item.vis, &base_replacement);
188+
let replacement = visibility_qualified(&item.vis, base_replacement);
189189
tcx.struct_span_lint_node(lint, id, extern_crate.span, msg)
190190
.span_suggestion_short_with_applicability(
191191
extern_crate.span,

src/libsyntax/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2791,7 +2791,7 @@ impl<'a> Parser<'a> {
27912791
s.print_usize(float.trunc() as usize)?;
27922792
s.pclose()?;
27932793
s.s.word(".")?;
2794-
s.s.word(fstr.splitn(2, ".").last().unwrap())
2794+
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
27952795
});
27962796
err.span_suggestion_with_applicability(
27972797
lo.to(self.prev_span),

0 commit comments

Comments
 (0)