Skip to content

Commit 7a95e71

Browse files
committed
Fix whitespacing issues in pretty-printing of bounds
1 parent b39c4bc commit 7a95e71

File tree

6 files changed

+45
-42
lines changed

6 files changed

+45
-42
lines changed

src/librustc/hir/print.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,16 @@ impl<'a> State<'a> {
408408
hir::TyTraitObject(ref bounds, ref lifetime) => {
409409
let mut first = true;
410410
for bound in bounds {
411-
self.nbsp()?;
412411
if first {
413412
first = false;
414413
} else {
414+
self.nbsp()?;
415415
self.word_space("+")?;
416416
}
417417
self.print_poly_trait_ref(bound)?;
418418
}
419419
if !lifetime.is_elided() {
420+
self.nbsp()?;
420421
self.word_space("+")?;
421422
self.print_lifetime(lifetime)?;
422423
}
@@ -764,7 +765,8 @@ impl<'a> State<'a> {
764765
real_bounds.push(b.clone());
765766
}
766767
}
767-
self.print_bounds(" = ", &real_bounds[..])?;
768+
self.nbsp()?;
769+
self.print_bounds("=", &real_bounds[..])?;
768770
self.print_where_clause(&generics.where_clause)?;
769771
self.s.word(";")?;
770772
}
@@ -788,6 +790,7 @@ impl<'a> State<'a> {
788790
comma = true;
789791
}
790792
self.s.word(">")?;
793+
self.nbsp()?;
791794
}
792795
Ok(())
793796
}
@@ -2016,30 +2019,29 @@ impl<'a> State<'a> {
20162019
self.s.word(prefix)?;
20172020
let mut first = true;
20182021
for bound in bounds {
2019-
self.nbsp()?;
2022+
if !(first && prefix.is_empty()) {
2023+
self.nbsp()?;
2024+
}
20202025
if first {
20212026
first = false;
20222027
} else {
20232028
self.word_space("+")?;
20242029
}
20252030

2026-
match *bound {
2027-
TraitTyParamBound(ref tref, TraitBoundModifier::None) => {
2028-
self.print_poly_trait_ref(tref)
2029-
}
2030-
TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => {
2031-
self.s.word("?")?;
2032-
self.print_poly_trait_ref(tref)
2031+
match bound {
2032+
TraitTyParamBound(tref, modifier) => {
2033+
if modifier == &TraitBoundModifier::Maybe {
2034+
self.s.word("?")?;
2035+
}
2036+
self.print_poly_trait_ref(tref)?;
20332037
}
2034-
RegionTyParamBound(ref lt) => {
2035-
self.print_lifetime(lt)
2038+
RegionTyParamBound(lt) => {
2039+
self.print_lifetime(lt)?;
20362040
}
2037-
}?
2041+
}
20382042
}
2039-
Ok(())
2040-
} else {
2041-
Ok(())
20422043
}
2044+
Ok(())
20432045
}
20442046

20452047
pub fn print_lifetime(&mut self, lifetime: &hir::Lifetime) -> io::Result<()> {

src/libsyntax/print/pprust.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -1066,11 +1066,11 @@ impl<'a> State<'a> {
10661066
self.print_qpath(path, qself, false)?
10671067
}
10681068
ast::TyKind::TraitObject(ref bounds, syntax) => {
1069-
let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn " } else { "" };
1069+
let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn" } else { "" };
10701070
self.print_bounds(prefix, &bounds[..])?;
10711071
}
10721072
ast::TyKind::ImplTrait(ref bounds) => {
1073-
self.print_bounds("impl ", &bounds[..])?;
1073+
self.print_bounds("impl", &bounds[..])?;
10741074
}
10751075
ast::TyKind::Array(ref ty, ref v) => {
10761076
self.s.word("[")?;
@@ -1398,7 +1398,8 @@ impl<'a> State<'a> {
13981398
real_bounds.push(b.clone());
13991399
}
14001400
}
1401-
self.print_bounds(" = ", &real_bounds[..])?;
1401+
self.nbsp()?;
1402+
self.print_bounds("=", &real_bounds[..])?;
14021403
self.print_where_clause(&generics.where_clause)?;
14031404
self.s.word(";")?;
14041405
}
@@ -1444,6 +1445,7 @@ impl<'a> State<'a> {
14441445
comma = true;
14451446
}
14461447
self.s.word(">")?;
1448+
self.nbsp()?;
14471449
}
14481450
Ok(())
14491451
}
@@ -2818,30 +2820,29 @@ impl<'a> State<'a> {
28182820
self.s.word(prefix)?;
28192821
let mut first = true;
28202822
for bound in bounds {
2821-
self.nbsp()?;
2823+
if !(first && prefix.is_empty()) {
2824+
self.nbsp()?;
2825+
}
28222826
if first {
28232827
first = false;
28242828
} else {
28252829
self.word_space("+")?;
28262830
}
28272831

2828-
(match *bound {
2829-
TraitTyParamBound(ref tref, TraitBoundModifier::None) => {
2830-
self.print_poly_trait_ref(tref)
2831-
}
2832-
TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => {
2833-
self.s.word("?")?;
2834-
self.print_poly_trait_ref(tref)
2832+
match bound {
2833+
TraitTyParamBound(tref, modifier) => {
2834+
if modifier == &TraitBoundModifier::Maybe {
2835+
self.s.word("?")?;
2836+
}
2837+
self.print_poly_trait_ref(tref)?;
28352838
}
2836-
RegionTyParamBound(ref lt) => {
2837-
self.print_lifetime(lt)
2839+
RegionTyParamBound(lt) => {
2840+
self.print_lifetime(lt)?;
28382841
}
2839-
})?
2842+
}
28402843
}
2841-
Ok(())
2842-
} else {
2843-
Ok(())
28442844
}
2845+
Ok(())
28452846
}
28462847

28472848
pub fn print_lifetime(&mut self,

src/test/parse-fail/trait-object-bad-parens.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ fn main() {
1414
let _: Box<((Copy)) + Copy>;
1515
//~^ ERROR expected a path on the left-hand side of `+`, not `((Copy))`
1616
let _: Box<(Copy + Copy) + Copy>;
17-
//~^ ERROR expected a path on the left-hand side of `+`, not `( Copy + Copy)`
17+
//~^ ERROR expected a path on the left-hand side of `+`, not `(Copy + Copy)`
1818
let _: Box<(Copy +) + Copy>;
19-
//~^ ERROR expected a path on the left-hand side of `+`, not `( Copy)`
19+
//~^ ERROR expected a path on the left-hand side of `+`, not `(Copy)`
2020
let _: Box<(dyn Copy) + Copy>;
21-
//~^ ERROR expected a path on the left-hand side of `+`, not `(dyn Copy)`
21+
//~^ ERROR expected a path on the left-hand side of `+`, not `(dyn Copy)`
2222
}

src/test/parse-fail/trait-object-polytrait-priority.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait Trait<'a> {}
1212

1313
fn main() {
1414
let _: &for<'a> Trait<'a> + 'static;
15-
//~^ ERROR expected a path on the left-hand side of `+`, not `& for<'a>Trait<'a>`
15+
//~^ ERROR expected a path on the left-hand side of `+`, not `&for<'a> Trait<'a>`
1616
//~| HELP try adding parentheses
17-
//~| SUGGESTION &( for<'a>Trait<'a> + 'static)
17+
//~| SUGGESTION &(for<'a> Trait<'a> + 'static)
1818
}

src/test/pretty/closure-reform-pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn call_it(f: Box<FnMut(String) -> String>) { }
1717

1818
fn call_this<F>(f: F) where F: Fn(&str) + Send { }
1919

20-
fn call_that<F>(f: F) where F: for<'a>Fn(&'a isize, &'a isize) -> isize { }
20+
fn call_that<F>(f: F) where F: for<'a> Fn(&'a isize, &'a isize) -> isize { }
2121

2222
fn call_extern(f: fn() -> isize) { }
2323

src/test/pretty/path-type-bounds.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ trait Tr {
1616
}
1717
impl Tr for isize { }
1818

19-
fn foo<'a>(x: Box< Tr + Sync + 'a>) -> Box< Tr + Sync + 'a> { x }
19+
fn foo<'a>(x: Box<Tr + Sync + 'a>) -> Box<Tr + Sync + 'a> { x }
2020

2121
fn main() {
22-
let x: Box< Tr + Sync>;
22+
let x: Box<Tr + Sync>;
2323

24-
Box::new(1isize) as Box< Tr + Sync>;
24+
Box::new(1isize) as Box<Tr + Sync>;
2525
}

0 commit comments

Comments
 (0)