From 5a81c7d4b0bdbb82ef6018fe4200cf74479e12c6 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Thu, 27 Jul 2017 11:09:38 +0900 Subject: [PATCH] Use correct BytePos for where when rewriting trait --- src/items.rs | 12 +++++++++--- tests/target/issue-1824.rs | 5 +++++ tests/target/trait.rs | 6 +++--- tests/target/where-clause-rfc.rs | 2 +- 4 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 tests/target/issue-1824.rs diff --git a/src/items.rs b/src/items.rs index cd2cdf28fe64..463614712062 100644 --- a/src/items.rs +++ b/src/items.rs @@ -962,8 +962,14 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) .checked_sub(last_line_width(&result)) ); let pos_before_where = if type_param_bounds.is_empty() { - // We do not use this, so it does not matter - generics.span.hi + if generics.where_clause.predicates.is_empty() { + // We do not use this, so it does not matter + item.span.lo + } else { + let snippet = context.snippet(item.span); + let where_pos = snippet.find_uncommented("where"); + item.span.lo + where_pos.map_or(BytePos(0), |p| BytePos(p as u32)) + } } else { type_param_bounds[type_param_bounds.len() - 1].span().hi }; @@ -974,7 +980,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) Shape::legacy(where_budget, offset.block_only()), where_density, "{", - !has_body, + false, trait_bound_str.is_empty() && last_line_width(&generics_str) == 1, None, item.span, diff --git a/tests/target/issue-1824.rs b/tests/target/issue-1824.rs new file mode 100644 index 000000000000..1c4c2db46dcb --- /dev/null +++ b/tests/target/issue-1824.rs @@ -0,0 +1,5 @@ +pub trait Ingredient +where + Self: Send, +{ +} diff --git a/tests/target/trait.rs b/tests/target/trait.rs index e4758e98b5e9..cb1e0beb5551 100644 --- a/tests/target/trait.rs +++ b/tests/target/trait.rs @@ -54,13 +54,13 @@ trait ConstCheck: Foo trait Tttttttttttttttttttttttttttttttttttttttttttttttttttttttttt where - T: Foo + T: Foo, { } trait Ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt where - T: Foo + T: Foo, { } @@ -75,6 +75,6 @@ trait FooBar trait WhereList where T: Foo, - J: Bar + J: Bar, { } diff --git a/tests/target/where-clause-rfc.rs b/tests/target/where-clause-rfc.rs index bdf1f9d25742..ebfdc073eaa7 100644 --- a/tests/target/where-clause-rfc.rs +++ b/tests/target/where-clause-rfc.rs @@ -110,6 +110,6 @@ pub trait SomeTrait + Display + Write + Read - + FromStr + + FromStr, { }