Skip to content

Commit 9744fbd

Browse files
committed
Handle comments between trait generics and bounds
1 parent 19b34fe commit 9744fbd

File tree

3 files changed

+118
-6
lines changed

3 files changed

+118
-6
lines changed

src/formatting/items.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,23 +1143,30 @@ pub(crate) fn format_trait(
11431143
rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape)?;
11441144
result.push_str(&generics_str);
11451145

1146-
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1146+
// FIXME(#2055): rustfmt fails to format when there are comments within trait bounds.
11471147
if !generic_bounds.is_empty() {
1148-
let ident_hi = context
1149-
.snippet_provider
1150-
.span_after(item.span, &item.ident.as_str());
1148+
let bound_lo = generic_bounds.first().unwrap().span().lo();
11511149
let bound_hi = generic_bounds.last().unwrap().span().hi();
1152-
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
1150+
let snippet = context.snippet(mk_sp(bound_lo, bound_hi));
11531151
if contains_comment(snippet) {
11541152
return None;
11551153
}
11561154

1157-
result = rewrite_assign_rhs_with(
1155+
// Rewrite rhs and combine lhs with pre-bound comment
1156+
let ident_hi = context
1157+
.snippet_provider
1158+
.span_after(item.span, &item.ident.as_str());
1159+
let ident_hi = context
1160+
.snippet_provider
1161+
.span_after(mk_sp(ident_hi, item.span.hi()), ":");
1162+
result = rewrite_assign_rhs_with_comments(
11581163
context,
11591164
result + ":",
11601165
generic_bounds,
11611166
shape,
11621167
RhsTactics::ForceNextLineWithoutIndent,
1168+
mk_sp(ident_hi, bound_lo),
1169+
true,
11631170
)?;
11641171
}
11651172

@@ -1199,6 +1206,8 @@ pub(crate) fn format_trait(
11991206
}
12001207
let pre_block_span = if !generics.where_clause.predicates.is_empty() {
12011208
mk_sp(generics.where_clause.span.hi(), item.span.hi())
1209+
} else if !generic_bounds.is_empty() {
1210+
mk_sp(generic_bounds.last().unwrap().span().hi(), item.span.hi())
12021211
} else {
12031212
item.span
12041213
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
trait Student:/* comment1 */Person/* comment2 */{
34+
fn university(&self) -> String;
35+
}
36+
trait Programmer/* comment1 */{
37+
fn fav_language(&self) -> String;
38+
}
39+
trait/* comment1 */CompSciStudent1:/* comment2 */Programmer + Student/* comment3 */{
40+
fn git_username(&self) -> String;
41+
}
42+
trait/* comment1 */CompSciStudent2:/* comment2 Longggggggggggggggggggggggggggggggggggggggggggggggggg */Programmer + Student/* comment3 */{
43+
fn git_username(&self) -> String;
44+
}
45+
trait/* comment1 */CompSciStudent3:// comment2
46+
Programmer + Student/* comment3 */{
47+
fn git_username(&self) -> String;
48+
}
49+
trait/* comment1 */CompSciStudent4:// comment2 Longgggggggggggggggggggggggggggggggggggggggggggggggggg
50+
Programmer + Student/* comment3 */{
51+
fn git_username(&self) -> String;
52+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
trait Student: /* comment1 */ Person /* comment2 */ {
28+
fn university(&self) -> String;
29+
}
30+
trait Programmer /* comment1 */ {
31+
fn fav_language(&self) -> String;
32+
}
33+
trait CompSciStudent1: /* comment2 */ Programmer + Student /* comment3 */ {
34+
fn git_username(&self) -> String;
35+
}
36+
trait CompSciStudent2:
37+
/* comment2 Longggggggggggggggggggggggggggggggggggggggggggggggggg */
38+
Programmer + Student /* comment3 */
39+
{
40+
fn git_username(&self) -> String;
41+
}
42+
trait CompSciStudent3: // comment2
43+
Programmer + Student /* comment3 */
44+
{
45+
fn git_username(&self) -> String;
46+
}
47+
trait CompSciStudent4: // comment2 Longgggggggggggggggggggggggggggggggggggggggggggggggggg
48+
Programmer + Student /* comment3 */
49+
{
50+
fn git_username(&self) -> String;
51+
}

0 commit comments

Comments
 (0)