From a6d609b45ef1dbea7d552b0a88e19f76fdb67ef4 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Tue, 2 Jan 2018 13:50:43 +0900 Subject: [PATCH 1/3] Add and update tests for #2320 and #2331 --- tests/source/trait.rs | 5 +++++ tests/target/big-impl-rfc.rs | 9 ++++++--- tests/target/big-impl.rs | 9 ++++++--- tests/target/impls.rs | 9 ++++++--- tests/target/trait.rs | 11 +++++++++++ 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/tests/source/trait.rs b/tests/source/trait.rs index 823b1e44a1e0..e8de36db35e6 100644 --- a/tests/source/trait.rs +++ b/tests/source/trait.rs @@ -72,6 +72,11 @@ trait Foo { type IteRev = > as UntypedTimeSeries>::IterRev; } +// #2331 +trait MyTrait { + fn foo() {} +} + // Trait aliases trait FooBar = Foo diff --git a/tests/target/big-impl-rfc.rs b/tests/target/big-impl-rfc.rs index de2af2d18070..bc16f12c9d7a 100644 --- a/tests/target/big-impl-rfc.rs +++ b/tests/target/big-impl-rfc.rs @@ -29,11 +29,13 @@ where // #1369 impl Foo - for Bar { + for Bar +{ fn foo() {} } impl Foo - for Bar { + for Bar +{ fn foo() {} } impl< @@ -41,7 +43,8 @@ impl< ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName, > Foo - for Bar { + for Bar +{ fn foo() {} } impl Foo diff --git a/tests/target/big-impl.rs b/tests/target/big-impl.rs index 056ded71daae..6f122e7e0b68 100644 --- a/tests/target/big-impl.rs +++ b/tests/target/big-impl.rs @@ -27,11 +27,13 @@ where // #1369 impl Foo - for Bar { + for Bar +{ fn foo() {} } impl Foo - for Bar { + for Bar +{ fn foo() {} } impl< @@ -39,7 +41,8 @@ impl< ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName, > Foo - for Bar { + for Bar +{ fn foo() {} } impl Foo diff --git a/tests/target/impls.rs b/tests/target/impls.rs index a743d7f3067d..91172b39fe59 100644 --- a/tests/target/impls.rs +++ b/tests/target/impls.rs @@ -141,11 +141,13 @@ mod m { } impl - Handle, HandleType> { + Handle, HandleType> +{ } impl PartialEq - for Handle, HandleType> { + for Handle, HandleType> +{ } mod x { @@ -160,7 +162,8 @@ mod x { } impl - Issue1249 { + Issue1249 +{ // Creates a new flow constructor. fn foo() {} } diff --git a/tests/target/trait.rs b/tests/target/trait.rs index a570cb4d5408..e4d78f722296 100644 --- a/tests/target/trait.rs +++ b/tests/target/trait.rs @@ -100,6 +100,17 @@ trait Foo { > as UntypedTimeSeries>::IterRev; } +// #2331 +trait MyTrait< + AAAAAAAAAAAAAAAAAAAA, +BBBBBBBBBBBBBBBBBBBB, +CCCCCCCCCCCCCCCCCCCC, +DDDDDDDDDDDDDDDDDDDD, +> { + fn foo() {} +} + + // Trait aliases trait FooBar = Foo + Bar; trait FooBar = Foo + Bar; From c355f3854c0e6d751d493dca3782533918e8c84d Mon Sep 17 00:00:00 2001 From: topecongiro Date: Tue, 2 Jan 2018 13:51:25 +0900 Subject: [PATCH 2/3] Use correct budget when rewriting generics of trait --- src/items.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/items.rs b/src/items.rs index 2de3a025e4de..93492e3f8e0f 100644 --- a/src/items.rs +++ b/src/items.rs @@ -938,7 +938,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) let body_lo = context.codemap.span_after(item.span, "{"); - let shape = Shape::indented(offset, context.config); + let shape = Shape::indented(offset, context.config).offset_left(result.len())?; let generics_str = rewrite_generics(context, generics, shape, mk_sp(item.span.lo(), body_lo))?; result.push_str(&generics_str); From 19d6a3c7868c5c887c1c617fbef86bba8718625c Mon Sep 17 00:00:00 2001 From: topecongiro Date: Tue, 2 Jan 2018 13:52:19 +0900 Subject: [PATCH 3/3] Put the opening brace of impl on the next line We put the opening brace on the next line if the following conditions hold: 1. the result before '{' ends with comments or contains newline 2. the last line of the result before '{' is not extendable (i.e. consists of '>' and whitespaces). --- src/items.rs | 8 +++++--- src/utils.rs | 2 +- tests/target/trait.rs | 7 +++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/items.rs b/src/items.rs index 93492e3f8e0f..d8d490f72c0f 100644 --- a/src/items.rs +++ b/src/items.rs @@ -33,8 +33,8 @@ use types::join_bounds; use utils::{colon_spaces, contains_skip, first_line_width, format_abi, format_constness, format_defaultness, format_mutability, format_unsafety, format_visibility, is_attributes_extendable, last_line_contains_single_line_comment, - last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, starts_with_newline, - stmt_expr, trimmed_last_line_width}; + last_line_extendable, last_line_used_width, last_line_width, mk_sp, + semicolon_for_expr, starts_with_newline, stmt_expr, trimmed_last_line_width}; use vertical::rewrite_with_alignment; use visitor::FmtVisitor; @@ -639,8 +639,10 @@ pub fn format_impl( } result.push_str(&where_clause_str); + let need_newline = !last_line_extendable(&result) + && (last_line_contains_single_line_comment(&result) || result.contains('\n')); match context.config.brace_style() { - _ if last_line_contains_single_line_comment(&result) => result.push_str(&sep), + _ if need_newline => result.push_str(&sep), BraceStyle::AlwaysNextLine => result.push_str(&sep), BraceStyle::PreferSameLine => result.push(' '), BraceStyle::SameLineWhere => { diff --git a/src/utils.rs b/src/utils.rs index 73103e88a64f..2f65d2732465 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -178,7 +178,7 @@ pub fn last_line_extendable(s: &str) -> bool { } for c in s.chars().rev() { match c { - ')' | ']' | '}' | '?' => continue, + ')' | ']' | '}' | '?' | '>' => continue, '\n' => break, _ if c.is_whitespace() => continue, _ => return false, diff --git a/tests/target/trait.rs b/tests/target/trait.rs index e4d78f722296..133e8babedb9 100644 --- a/tests/target/trait.rs +++ b/tests/target/trait.rs @@ -103,14 +103,13 @@ trait Foo { // #2331 trait MyTrait< AAAAAAAAAAAAAAAAAAAA, -BBBBBBBBBBBBBBBBBBBB, -CCCCCCCCCCCCCCCCCCCC, -DDDDDDDDDDDDDDDDDDDD, + BBBBBBBBBBBBBBBBBBBB, + CCCCCCCCCCCCCCCCCCCC, + DDDDDDDDDDDDDDDDDDDD, > { fn foo() {} } - // Trait aliases trait FooBar = Foo + Bar; trait FooBar = Foo + Bar;