From 7a95e716c731d1e358eeb8bf9741852f1278b4f3 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Tue, 19 Dec 2017 23:40:17 +0300 Subject: [PATCH] Fix whitespacing issues in pretty-printing of bounds --- src/librustc/hir/print.rs | 34 +++++++++--------- src/libsyntax/print/pprust.rs | 35 ++++++++++--------- .../parse-fail/trait-object-bad-parens.rs | 6 ++-- .../trait-object-polytrait-priority.rs | 4 +-- src/test/pretty/closure-reform-pretty.rs | 2 +- src/test/pretty/path-type-bounds.rs | 6 ++-- 6 files changed, 45 insertions(+), 42 deletions(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index c7bb121e901..b2c2c96f624 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -408,15 +408,16 @@ pub fn print_type(&mut self, ty: &hir::Ty) -> io::Result<()> { hir::TyTraitObject(ref bounds, ref lifetime) => { let mut first = true; for bound in bounds { - self.nbsp()?; if first { first = false; } else { + self.nbsp()?; self.word_space("+")?; } self.print_poly_trait_ref(bound)?; } if !lifetime.is_elided() { + self.nbsp()?; self.word_space("+")?; self.print_lifetime(lifetime)?; } @@ -764,7 +765,8 @@ pub fn print_item(&mut self, item: &hir::Item) -> io::Result<()> { real_bounds.push(b.clone()); } } - self.print_bounds(" = ", &real_bounds[..])?; + self.nbsp()?; + self.print_bounds("=", &real_bounds[..])?; self.print_where_clause(&generics.where_clause)?; self.s.word(";")?; } @@ -788,6 +790,7 @@ fn print_formal_lifetime_list(&mut self, lifetimes: &[hir::LifetimeDef]) -> io:: comma = true; } self.s.word(">")?; + self.nbsp()?; } Ok(()) } @@ -2016,30 +2019,29 @@ pub fn print_bounds(&mut self, prefix: &str, bounds: &[hir::TyParamBound]) -> io self.s.word(prefix)?; let mut first = true; for bound in bounds { - self.nbsp()?; + if !(first && prefix.is_empty()) { + self.nbsp()?; + } if first { first = false; } else { self.word_space("+")?; } - match *bound { - TraitTyParamBound(ref tref, TraitBoundModifier::None) => { - self.print_poly_trait_ref(tref) - } - TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => { - self.s.word("?")?; - self.print_poly_trait_ref(tref) + match bound { + TraitTyParamBound(tref, modifier) => { + if modifier == &TraitBoundModifier::Maybe { + self.s.word("?")?; + } + self.print_poly_trait_ref(tref)?; } - RegionTyParamBound(ref lt) => { - self.print_lifetime(lt) + RegionTyParamBound(lt) => { + self.print_lifetime(lt)?; } - }? + } } - Ok(()) - } else { - Ok(()) } + Ok(()) } pub fn print_lifetime(&mut self, lifetime: &hir::Lifetime) -> io::Result<()> { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index e9386e5187f..da4d5f5f676 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1066,11 +1066,11 @@ pub fn print_type(&mut self, ty: &ast::Ty) -> io::Result<()> { self.print_qpath(path, qself, false)? } ast::TyKind::TraitObject(ref bounds, syntax) => { - let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn " } else { "" }; + let prefix = if syntax == ast::TraitObjectSyntax::Dyn { "dyn" } else { "" }; self.print_bounds(prefix, &bounds[..])?; } ast::TyKind::ImplTrait(ref bounds) => { - self.print_bounds("impl ", &bounds[..])?; + self.print_bounds("impl", &bounds[..])?; } ast::TyKind::Array(ref ty, ref v) => { self.s.word("[")?; @@ -1398,7 +1398,8 @@ pub fn print_item(&mut self, item: &ast::Item) -> io::Result<()> { real_bounds.push(b.clone()); } } - self.print_bounds(" = ", &real_bounds[..])?; + self.nbsp()?; + self.print_bounds("=", &real_bounds[..])?; self.print_where_clause(&generics.where_clause)?; self.s.word(";")?; } @@ -1444,6 +1445,7 @@ fn print_formal_lifetime_list(&mut self, lifetimes: &[ast::LifetimeDef]) -> io:: comma = true; } self.s.word(">")?; + self.nbsp()?; } Ok(()) } @@ -2818,30 +2820,29 @@ pub fn print_bounds(&mut self, self.s.word(prefix)?; let mut first = true; for bound in bounds { - self.nbsp()?; + if !(first && prefix.is_empty()) { + self.nbsp()?; + } if first { first = false; } else { self.word_space("+")?; } - (match *bound { - TraitTyParamBound(ref tref, TraitBoundModifier::None) => { - self.print_poly_trait_ref(tref) - } - TraitTyParamBound(ref tref, TraitBoundModifier::Maybe) => { - self.s.word("?")?; - self.print_poly_trait_ref(tref) + match bound { + TraitTyParamBound(tref, modifier) => { + if modifier == &TraitBoundModifier::Maybe { + self.s.word("?")?; + } + self.print_poly_trait_ref(tref)?; } - RegionTyParamBound(ref lt) => { - self.print_lifetime(lt) + RegionTyParamBound(lt) => { + self.print_lifetime(lt)?; } - })? + } } - Ok(()) - } else { - Ok(()) } + Ok(()) } pub fn print_lifetime(&mut self, diff --git a/src/test/parse-fail/trait-object-bad-parens.rs b/src/test/parse-fail/trait-object-bad-parens.rs index 3e8c140eb19..25e9c3071d6 100644 --- a/src/test/parse-fail/trait-object-bad-parens.rs +++ b/src/test/parse-fail/trait-object-bad-parens.rs @@ -14,9 +14,9 @@ fn main() { let _: Box<((Copy)) + Copy>; //~^ ERROR expected a path on the left-hand side of `+`, not `((Copy))` let _: Box<(Copy + Copy) + Copy>; - //~^ ERROR expected a path on the left-hand side of `+`, not `( Copy + Copy)` + //~^ ERROR expected a path on the left-hand side of `+`, not `(Copy + Copy)` let _: Box<(Copy +) + Copy>; - //~^ ERROR expected a path on the left-hand side of `+`, not `( Copy)` + //~^ ERROR expected a path on the left-hand side of `+`, not `(Copy)` let _: Box<(dyn Copy) + Copy>; - //~^ ERROR expected a path on the left-hand side of `+`, not `(dyn Copy)` + //~^ ERROR expected a path on the left-hand side of `+`, not `(dyn Copy)` } diff --git a/src/test/parse-fail/trait-object-polytrait-priority.rs b/src/test/parse-fail/trait-object-polytrait-priority.rs index 9df4be4595a..b5fc06ddaac 100644 --- a/src/test/parse-fail/trait-object-polytrait-priority.rs +++ b/src/test/parse-fail/trait-object-polytrait-priority.rs @@ -12,7 +12,7 @@ trait Trait<'a> {} fn main() { let _: &for<'a> Trait<'a> + 'static; - //~^ ERROR expected a path on the left-hand side of `+`, not `& for<'a>Trait<'a>` + //~^ ERROR expected a path on the left-hand side of `+`, not `&for<'a> Trait<'a>` //~| HELP try adding parentheses - //~| SUGGESTION &( for<'a>Trait<'a> + 'static) + //~| SUGGESTION &(for<'a> Trait<'a> + 'static) } diff --git a/src/test/pretty/closure-reform-pretty.rs b/src/test/pretty/closure-reform-pretty.rs index 63568dbcd5b..a1fdebf6fa1 100644 --- a/src/test/pretty/closure-reform-pretty.rs +++ b/src/test/pretty/closure-reform-pretty.rs @@ -17,7 +17,7 @@ fn call_it(f: Box String>) { } fn call_this(f: F) where F: Fn(&str) + Send { } -fn call_that(f: F) where F: for<'a>Fn(&'a isize, &'a isize) -> isize { } +fn call_that(f: F) where F: for<'a> Fn(&'a isize, &'a isize) -> isize { } fn call_extern(f: fn() -> isize) { } diff --git a/src/test/pretty/path-type-bounds.rs b/src/test/pretty/path-type-bounds.rs index 651d2b67941..ae6a0fc3142 100644 --- a/src/test/pretty/path-type-bounds.rs +++ b/src/test/pretty/path-type-bounds.rs @@ -16,10 +16,10 @@ fn dummy(&self) { } } impl Tr for isize { } -fn foo<'a>(x: Box< Tr + Sync + 'a>) -> Box< Tr + Sync + 'a> { x } +fn foo<'a>(x: Box) -> Box { x } fn main() { - let x: Box< Tr + Sync>; + let x: Box; - Box::new(1isize) as Box< Tr + Sync>; + Box::new(1isize) as Box; } -- GitLab