Skip to content

Commit

Permalink
Fix whitespacing issues in pretty-printing of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Dec 19, 2017
1 parent b39c4bc commit 7a95e71
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 42 deletions.
34 changes: 18 additions & 16 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,16 @@ impl<'a> State<'a> {
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)?;
}
Expand Down Expand Up @@ -764,7 +765,8 @@ impl<'a> State<'a> {
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(";")?;
}
Expand All @@ -788,6 +790,7 @@ impl<'a> State<'a> {
comma = true;
}
self.s.word(">")?;
self.nbsp()?;
}
Ok(())
}
Expand Down Expand Up @@ -2016,30 +2019,29 @@ impl<'a> State<'a> {
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<()> {
Expand Down
35 changes: 18 additions & 17 deletions src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1066,11 +1066,11 @@ impl<'a> State<'a> {
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("[")?;
Expand Down Expand Up @@ -1398,7 +1398,8 @@ impl<'a> State<'a> {
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(";")?;
}
Expand Down Expand Up @@ -1444,6 +1445,7 @@ impl<'a> State<'a> {
comma = true;
}
self.s.word(">")?;
self.nbsp()?;
}
Ok(())
}
Expand Down Expand Up @@ -2818,30 +2820,29 @@ impl<'a> State<'a> {
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,
Expand Down
6 changes: 3 additions & 3 deletions src/test/parse-fail/trait-object-bad-parens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
}
4 changes: 2 additions & 2 deletions src/test/parse-fail/trait-object-polytrait-priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion src/test/pretty/closure-reform-pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn call_it(f: Box<FnMut(String) -> String>) { }

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

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

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

Expand Down
6 changes: 3 additions & 3 deletions src/test/pretty/path-type-bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ trait Tr {
}
impl Tr for isize { }

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

fn main() {
let x: Box< Tr + Sync>;
let x: Box<Tr + Sync>;

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

0 comments on commit 7a95e71

Please sign in to comment.