Skip to content

Commit 8fa9c8c

Browse files
committed
Handle comments between trait generics and bounds
1 parent 19b34fe commit 8fa9c8c

File tree

3 files changed

+160
-6
lines changed

3 files changed

+160
-6
lines changed

src/formatting/items.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,30 +1136,42 @@ pub(crate) fn format_trait(
11361136
);
11371137
result.push_str(&header);
11381138

1139+
// FIXME: rustfmt fails to format when there are comments before the ident.
1140+
if contains_comment(context.snippet(mk_sp(item.span.lo(), generics.span.lo()))) {
1141+
return None;
1142+
}
1143+
11391144
let body_lo = context.snippet_provider.span_after(item.span, "{");
11401145

11411146
let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
11421147
let generics_str =
11431148
rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape)?;
11441149
result.push_str(&generics_str);
11451150

1146-
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1151+
// FIXME(#2055): rustfmt fails to format when there are comments within trait bounds.
11471152
if !generic_bounds.is_empty() {
1148-
let ident_hi = context
1149-
.snippet_provider
1150-
.span_after(item.span, &item.ident.as_str());
1153+
let bound_lo = generic_bounds.first().unwrap().span().lo();
11511154
let bound_hi = generic_bounds.last().unwrap().span().hi();
1152-
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
1155+
let snippet = context.snippet(mk_sp(bound_lo, bound_hi));
11531156
if contains_comment(snippet) {
11541157
return None;
11551158
}
11561159

1157-
result = rewrite_assign_rhs_with(
1160+
// Rewrite rhs and combine lhs with pre-bound comment
1161+
let ident_hi = context
1162+
.snippet_provider
1163+
.span_after(item.span, &item.ident.as_str());
1164+
let ident_hi = context
1165+
.snippet_provider
1166+
.span_after(mk_sp(ident_hi, item.span.hi()), ":");
1167+
result = rewrite_assign_rhs_with_comments(
11581168
context,
11591169
result + ":",
11601170
generic_bounds,
11611171
shape,
11621172
RhsTactics::ForceNextLineWithoutIndent,
1173+
mk_sp(ident_hi, bound_lo),
1174+
true,
11631175
)?;
11641176
}
11651177

@@ -1199,6 +1211,8 @@ pub(crate) fn format_trait(
11991211
}
12001212
let pre_block_span = if !generics.where_clause.predicates.is_empty() {
12011213
mk_sp(generics.where_clause.span.hi(), item.span.hi())
1214+
} else if !generic_bounds.is_empty() {
1215+
mk_sp(generic_bounds.last().unwrap().span().hi(), item.span.hi())
12021216
} else {
12031217
item.span
12041218
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Based on issue #2055:
2+
pub trait A {}
3+
pub trait B {}
4+
pub trait C {}
5+
pub trait Foo1:
6+
A + C
7+
+ B
8+
{}
9+
pub trait Foo2:
10+
// A and C
11+
A + C
12+
+ B
13+
{}
14+
pub trait Foo3:
15+
/* A and C */
16+
A + C
17+
+ B
18+
{}
19+
pub trait Foo4:// A and C
20+
A + C
21+
+ B
22+
{}
23+
pub trait Foo5:/* A and C */
24+
A + C
25+
+ B
26+
{}
27+
pub trait Foo6:/* A and C */A + C + B{}
28+
29+
// Other cases
30+
trait Person{
31+
fn name(&self) -> String;
32+
}
33+
/*comment1*/trait Person{
34+
fn name(&self) -> String;
35+
}
36+
trait Student:/* comment1 */Person/* comment2 */{
37+
fn university(&self) -> String;
38+
}
39+
trait Programmer/* comment1 */{
40+
fn fav_language(&self) -> String;
41+
}
42+
trait CompSciStudent1:/* comment1 */Programmer + Student/* comment2 */{
43+
fn git_username(&self) -> String;
44+
}
45+
trait CompSciStudent2:/* comment1 Longggggggggggggggggggggggggggggggggggggggggggggggggg */Programmer + Student/* comment2 */{
46+
fn git_username(&self) -> String;
47+
}
48+
trait CompSciStudent3:// comment1
49+
Programmer + Student/* comment2 */{
50+
fn git_username(&self) -> String;
51+
}
52+
trait CompSciStudent4:// comment1 Longgggggggggggggggggggggggggggggggggggggggggggggggggg
53+
Programmer + Student/* comment2 */{
54+
fn git_username(&self) -> String;
55+
}
56+
57+
// Comment before Ident
58+
trait /* comment1 */ Person {
59+
fn fav_language(&self) -> String;
60+
}
61+
trait // comment1
62+
Person {
63+
fn fav_language(&self) -> String;
64+
}
65+
trait /* comment 1 */ Programmer /* comment2 */ {
66+
fn fav_language(&self) -> String;
67+
}
68+
trait /* comment1 */ CompSciStudent1: /* comment2 */ Programmer + Student /* comment3 */ {
69+
fn git_username(&self) -> String;
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Based on issue #2055:
2+
pub trait A {}
3+
pub trait B {}
4+
pub trait C {}
5+
pub trait Foo1: A + C + B {}
6+
pub trait Foo2:
7+
// A and C
8+
A + C + B
9+
{
10+
}
11+
pub trait Foo3:
12+
/* A and C */
13+
A + C + B
14+
{
15+
}
16+
pub trait Foo4: // A and C
17+
A + C + B
18+
{
19+
}
20+
pub trait Foo5: /* A and C */ A + C + B {}
21+
pub trait Foo6: /* A and C */ A + C + B {}
22+
23+
// Other cases
24+
trait Person {
25+
fn name(&self) -> String;
26+
}
27+
/*comment1*/
28+
trait Person {
29+
fn name(&self) -> String;
30+
}
31+
trait Student: /* comment1 */ Person /* comment2 */ {
32+
fn university(&self) -> String;
33+
}
34+
trait Programmer /* comment1 */ {
35+
fn fav_language(&self) -> String;
36+
}
37+
trait CompSciStudent1: /* comment1 */ Programmer + Student /* comment2 */ {
38+
fn git_username(&self) -> String;
39+
}
40+
trait CompSciStudent2:
41+
/* comment1 Longggggggggggggggggggggggggggggggggggggggggggggggggg */
42+
Programmer + Student /* comment2 */
43+
{
44+
fn git_username(&self) -> String;
45+
}
46+
trait CompSciStudent3: // comment1
47+
Programmer + Student /* comment2 */
48+
{
49+
fn git_username(&self) -> String;
50+
}
51+
trait CompSciStudent4: // comment1 Longgggggggggggggggggggggggggggggggggggggggggggggggggg
52+
Programmer + Student /* comment2 */
53+
{
54+
fn git_username(&self) -> String;
55+
}
56+
57+
// Comment before Ident
58+
trait /* comment1 */ Person {
59+
fn fav_language(&self) -> String;
60+
}
61+
trait // comment1
62+
Person {
63+
fn fav_language(&self) -> String;
64+
}
65+
trait /* comment 1 */ Programmer /* comment2 */ {
66+
fn fav_language(&self) -> String;
67+
}
68+
trait /* comment1 */ CompSciStudent1: /* comment2 */ Programmer + Student /* comment3 */ {
69+
fn git_username(&self) -> String;
70+
}

0 commit comments

Comments
 (0)