diff --git a/src/items.rs b/src/items.rs index 2de3a025e4d..d8d490f72c0 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 => { @@ -938,7 +940,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); diff --git a/src/utils.rs b/src/utils.rs index 73103e88a64..2f65d273246 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/source/trait.rs b/tests/source/trait.rs index 823b1e44a1e..e8de36db35e 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 de2af2d1807..bc16f12c9d7 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 056ded71daa..6f122e7e0b6 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 a743d7f3067..91172b39fe5 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 a570cb4d540..133e8babedb 100644 --- a/tests/target/trait.rs +++ b/tests/target/trait.rs @@ -100,6 +100,16 @@ trait Foo { > as UntypedTimeSeries>::IterRev; } +// #2331 +trait MyTrait< + AAAAAAAAAAAAAAAAAAAA, + BBBBBBBBBBBBBBBBBBBB, + CCCCCCCCCCCCCCCCCCCC, + DDDDDDDDDDDDDDDDDDDD, +> { + fn foo() {} +} + // Trait aliases trait FooBar = Foo + Bar; trait FooBar = Foo + Bar;