Skip to content

Commit e343aa2

Browse files
authored
Merge pull request #2332 from topecongiro/impl-and-trait
Fix up formatting bugs in impl and trait
2 parents 0f24bc0 + 19d6a3c commit e343aa2

File tree

7 files changed

+40
-14
lines changed

7 files changed

+40
-14
lines changed

src/items.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use types::join_bounds;
3333
use utils::{colon_spaces, contains_skip, first_line_width, format_abi, format_constness,
3434
format_defaultness, format_mutability, format_unsafety, format_visibility,
3535
is_attributes_extendable, last_line_contains_single_line_comment,
36-
last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, starts_with_newline,
37-
stmt_expr, trimmed_last_line_width};
36+
last_line_extendable, last_line_used_width, last_line_width, mk_sp,
37+
semicolon_for_expr, starts_with_newline, stmt_expr, trimmed_last_line_width};
3838
use vertical::rewrite_with_alignment;
3939
use visitor::FmtVisitor;
4040

@@ -639,8 +639,10 @@ pub fn format_impl(
639639
}
640640
result.push_str(&where_clause_str);
641641

642+
let need_newline = !last_line_extendable(&result)
643+
&& (last_line_contains_single_line_comment(&result) || result.contains('\n'));
642644
match context.config.brace_style() {
643-
_ if last_line_contains_single_line_comment(&result) => result.push_str(&sep),
645+
_ if need_newline => result.push_str(&sep),
644646
BraceStyle::AlwaysNextLine => result.push_str(&sep),
645647
BraceStyle::PreferSameLine => result.push(' '),
646648
BraceStyle::SameLineWhere => {
@@ -938,7 +940,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
938940

939941
let body_lo = context.codemap.span_after(item.span, "{");
940942

941-
let shape = Shape::indented(offset, context.config);
943+
let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
942944
let generics_str =
943945
rewrite_generics(context, generics, shape, mk_sp(item.span.lo(), body_lo))?;
944946
result.push_str(&generics_str);

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub fn last_line_extendable(s: &str) -> bool {
178178
}
179179
for c in s.chars().rev() {
180180
match c {
181-
')' | ']' | '}' | '?' => continue,
181+
')' | ']' | '}' | '?' | '>' => continue,
182182
'\n' => break,
183183
_ if c.is_whitespace() => continue,
184184
_ => return false,

tests/source/trait.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ trait Foo {
7272
type IteRev = <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
7373
}
7474

75+
// #2331
76+
trait MyTrait<AAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBBBBBBBBBB, CCCCCCCCCCCCCCCCCCCC, DDDDDDDDDDDDDDDDDDDD> {
77+
fn foo() {}
78+
}
79+
7580
// Trait aliases
7681
trait FooBar =
7782
Foo

tests/target/big-impl-rfc.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,22 @@ where
2929

3030
// #1369
3131
impl<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName> Foo
32-
for Bar {
32+
for Bar
33+
{
3334
fn foo() {}
3435
}
3536
impl Foo<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName>
36-
for Bar {
37+
for Bar
38+
{
3739
fn foo() {}
3840
}
3941
impl<
4042
ExcessivelyLongGenericName,
4143
ExcessivelyLongGenericName,
4244
AnotherExcessivelyLongGenericName,
4345
> Foo<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName>
44-
for Bar {
46+
for Bar
47+
{
4548
fn foo() {}
4649
}
4750
impl<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName> Foo

tests/target/big-impl.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,22 @@ where
2727

2828
// #1369
2929
impl<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName> Foo
30-
for Bar {
30+
for Bar
31+
{
3132
fn foo() {}
3233
}
3334
impl Foo<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName>
34-
for Bar {
35+
for Bar
36+
{
3537
fn foo() {}
3638
}
3739
impl<
3840
ExcessivelyLongGenericName,
3941
ExcessivelyLongGenericName,
4042
AnotherExcessivelyLongGenericName,
4143
> Foo<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName>
42-
for Bar {
44+
for Bar
45+
{
4346
fn foo() {}
4447
}
4548
impl<ExcessivelyLongGenericName, ExcessivelyLongGenericName, AnotherExcessivelyLongGenericName> Foo

tests/target/impls.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,13 @@ mod m {
141141
}
142142

143143
impl<BorrowType, K, V, NodeType, HandleType>
144-
Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType> {
144+
Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType>
145+
{
145146
}
146147

147148
impl<BorrowType, K, V, NodeType, HandleType> PartialEq
148-
for Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType> {
149+
for Handle<NodeRef<BorrowType, K, V, NodeType>, HandleType>
150+
{
149151
}
150152

151153
mod x {
@@ -160,7 +162,8 @@ mod x {
160162
}
161163

162164
impl<ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNodeFoo>
163-
Issue1249<ConcreteThreadSafeLayoutNode> {
165+
Issue1249<ConcreteThreadSafeLayoutNode>
166+
{
164167
// Creates a new flow constructor.
165168
fn foo() {}
166169
}

tests/target/trait.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ trait Foo {
100100
<MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
101101
}
102102

103+
// #2331
104+
trait MyTrait<
105+
AAAAAAAAAAAAAAAAAAAA,
106+
BBBBBBBBBBBBBBBBBBBB,
107+
CCCCCCCCCCCCCCCCCCCC,
108+
DDDDDDDDDDDDDDDDDDDD,
109+
> {
110+
fn foo() {}
111+
}
112+
103113
// Trait aliases
104114
trait FooBar = Foo + Bar;
105115
trait FooBar<A, B, C> = Foo + Bar;

0 commit comments

Comments
 (0)