Skip to content

Commit cc0daad

Browse files
committed
Changes per comments and with added test cases
1 parent bceaf18 commit cc0daad

File tree

7 files changed

+181
-28
lines changed

7 files changed

+181
-28
lines changed

src/formatting/expr.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,12 +1938,13 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
19381938
rewrite_assign_rhs_with(context, lhs, ex, shape, RhsTactics::Default)
19391939
}
19401940

1941-
pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
1941+
pub(crate) fn rewrite_rhs_expr<R: Rewrite>(
19421942
context: &RewriteContext<'_>,
19431943
lhs: &str,
19441944
ex: &R,
19451945
shape: Shape,
19461946
rhs_tactics: RhsTactics,
1947+
lhs_separator: &str,
19471948
) -> Option<String> {
19481949
let last_line_width = last_line_width(&lhs).saturating_sub(if lhs.contains('\n') {
19491950
shape.indent.width()
@@ -1956,7 +1957,7 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
19561957
offset: shape.offset + last_line_width + 1,
19571958
..shape
19581959
});
1959-
let has_rhs_comment = if let Some(offset) = lhs.find_last_uncommented("=") {
1960+
let has_rhs_comment = if let Some(offset) = lhs.find_last_uncommented(lhs_separator) {
19601961
lhs.trim_end().len() > offset + 1
19611962
} else {
19621963
false
@@ -1980,7 +1981,7 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19801981
rhs_tactics: RhsTactics,
19811982
) -> Option<String> {
19821983
let lhs = lhs.into();
1983-
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?;
1984+
let rhs = rewrite_rhs_expr(context, &lhs, ex, shape, rhs_tactics, "=")?;
19841985
Some(lhs + &rhs)
19851986
}
19861987

@@ -2000,7 +2001,7 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
20002001
} else {
20012002
shape
20022003
};
2003-
let rhs = rewrite_assign_rhs_expr(context, &lhs, ex, shape, rhs_tactics)?;
2004+
let rhs = rewrite_rhs_expr(context, &lhs, ex, shape, rhs_tactics, "=")?;
20042005

20052006
if contains_comment {
20062007
let rhs = rhs.trim_start();
@@ -2010,6 +2011,35 @@ pub(crate) fn rewrite_assign_rhs_with_comments<S: Into<String>, R: Rewrite>(
20102011
}
20112012
}
20122013

2014+
pub(crate) fn rewrite_trait_rhs_with_comments<S: Into<String>, R: Rewrite>(
2015+
context: &RewriteContext<'_>,
2016+
lhs: S,
2017+
ex: &R,
2018+
shape: Shape,
2019+
rhs_tactics: RhsTactics,
2020+
between_span: Span,
2021+
allow_extend: bool,
2022+
) -> Option<String> {
2023+
let lhs = lhs.into();
2024+
let contains_comment = contains_comment(context.snippet(between_span));
2025+
2026+
let rhs = rewrite_rhs_expr(context, &lhs, ex, shape, rhs_tactics, ":")?;
2027+
2028+
if contains_comment {
2029+
let rhs = rhs.trim_start();
2030+
combine_strs_with_missing_comments(
2031+
context,
2032+
&lhs,
2033+
&rhs,
2034+
between_span,
2035+
shape.block_left(context.config.tab_spaces())?,
2036+
allow_extend,
2037+
)
2038+
} else {
2039+
Some(lhs + &rhs)
2040+
}
2041+
}
2042+
20132043
fn choose_rhs<R: Rewrite>(
20142044
context: &RewriteContext<'_>,
20152045
expr: &R,

src/formatting/items.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::formatting::{
1919
},
2020
expr::{
2121
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
22-
rewrite_assign_rhs_with_comments, RhsTactics,
22+
rewrite_assign_rhs_with_comments, rewrite_trait_rhs_with_comments, RhsTactics,
2323
},
2424
lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator},
2525
macros::{rewrite_macro, MacroPosition},
@@ -1122,6 +1122,12 @@ pub(crate) fn format_trait(
11221122
if let ast::ItemKind::Trait(trait_kind) = &item.kind {
11231123
let ast::TraitKind(is_auto, unsafety, ref generics, ref generic_bounds, ref trait_items) =
11241124
**trait_kind;
1125+
1126+
// FIXME: rustfmt fails to format when there are comments before the ident.
1127+
if contains_comment(context.snippet(mk_sp(item.span.lo(), item.ident.span.lo()))) {
1128+
return None;
1129+
}
1130+
11251131
let mut result = String::with_capacity(128);
11261132
let header = format!(
11271133
"{}{}{}trait ",
@@ -1131,35 +1137,23 @@ pub(crate) fn format_trait(
11311137
);
11321138
result.push_str(&header);
11331139

1134-
// FIXME: rustfmt fails to format when there are comments before the ident.
1135-
if contains_comment(context.snippet(mk_sp(item.span.lo(), generics.span.lo()))) {
1136-
return None;
1137-
}
1138-
11391140
let body_lo = context.snippet_provider.span_after(item.span, "{");
11401141

11411142
let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
11421143
let generics_str =
11431144
rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape)?;
11441145
result.push_str(&generics_str);
11451146

1146-
// FIXME(#2055): rustfmt fails to format when there are comments within trait bounds.
11471147
if !generic_bounds.is_empty() {
1148-
let bound_lo = generic_bounds.first().unwrap().span().lo();
1149-
let bound_hi = generic_bounds.last().unwrap().span().hi();
1150-
let snippet = context.snippet(mk_sp(bound_lo, bound_hi));
1151-
if contains_comment(snippet) {
1152-
return None;
1153-
}
1154-
11551148
// Rewrite rhs and combine lhs with pre-bound comment
1149+
let bound_lo = generic_bounds.first().unwrap().span().lo();
11561150
let ident_hi = context
11571151
.snippet_provider
11581152
.span_after(item.span, &item.ident.as_str());
11591153
let ident_hi = context
11601154
.snippet_provider
11611155
.span_after(mk_sp(ident_hi, item.span.hi()), ":");
1162-
result = rewrite_assign_rhs_with_comments(
1156+
result = rewrite_trait_rhs_with_comments(
11631157
context,
11641158
result + ":",
11651159
generic_bounds,
@@ -1205,7 +1199,7 @@ pub(crate) fn format_trait(
12051199
result.push_str(&where_clause_str);
12061200
}
12071201

1208-
/* Note: `where_clause` always exists; Span is empty when no where clause in the code */
1202+
/* Note: `where_clause` always exists; Span is empty when no `where` clause in the code */
12091203
let pre_block_span = mk_sp(generics.where_clause.span.hi(), item.span.hi());
12101204
let pre_block_snippet = context.snippet(pre_block_span);
12111205
if let Some(lo) = pre_block_snippet.find('/') {

src/formatting/macros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,9 @@ impl MacroBranch {
13561356
result += " =>";
13571357
}
13581358

1359+
// Note (from issue #4759): comments between the end of `whole_body` and
1360+
// the end of `span` are currently ignored.
1361+
13591362
if !context.config.format_macro_bodies() {
13601363
result += " ";
13611364
result += context.snippet(self.whole_body);

tests/source/lhs-to-rhs-between-comments/trait.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ A + C
2525
+ B
2626
{}
2727
pub trait Foo6:/* A and C */A + C + B{}
28+
pub trait Foo7:
29+
A+C
30+
// and B
31+
+B{}
32+
pub trait Foo8:
33+
// A and C
34+
A+C
35+
// and B
36+
+B{}
2837

2938
// Other cases
3039
trait Person{
@@ -68,3 +77,35 @@ trait /* comment 1 */ Programmer /* comment2 */ {
6877
trait /* comment1 */ CompSciStudent1: /* comment2 */ Programmer + Student /* comment3 */ {
6978
fn git_username(&self) -> String;
7079
}
80+
81+
// Traits with where and comments
82+
trait Bar where Self: Sized, Option<Self>: Foo
83+
{}
84+
/*comment0*/trait Bar/*comment1*/where Self: Sized/*comment2*/,/*comment3*/Option<Self>: Foo/*comment4*/
85+
{}
86+
trait Bar//comment1 Longgggggggggggggggggggggggggggggggggggggggggggggggggg
87+
where Self: Sized/*comment2*/,/*comment3*/Option<Self>: Foo/*comment4*/
88+
{}
89+
trait Bar/*comment1*/where Self: Sized/*comment2*/,/*comment3*/Option<Self>: Foo//comment4 Longgggggggggggggggggggggggggggggggggggggggggggggggggg
90+
{}
91+
trait Bar/*comment1 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/where Self: Sized/*comment2 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/,/*comment3 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/Option<Self>: Foo/*comment4 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/
92+
{}
93+
trait ConstCheck<T>:/*comment1*/Foo where T: Baz/*comment2*/{
94+
const J: i32;
95+
}
96+
97+
// Some other trait cases with comments
98+
/*comment0*/auto trait Example/*comment1*/{}
99+
pub unsafe auto trait PubUnsafeExample/*comment1*/{}
100+
pub unsafe auto trait PubUnsafeExample// comment1
101+
{}
102+
trait Foo/*comment1*/{ type Bar: Baz; type Inner: Foo = Box< Foo >; }
103+
pub trait Iterator/*comment1*/{
104+
type Item;
105+
fn next(&mut self) -> Option<Self::Item>;
106+
}
107+
pub trait Iterator//comment1
108+
{
109+
type Item;
110+
fn next(&mut self) -> Option<Self::Item>;
111+
}

tests/target/lhs-to-rhs-between-comments/trait.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ pub trait Foo4: // A and C
1919
}
2020
pub trait Foo5: /* A and C */ A + C + B {}
2121
pub trait Foo6: /* A and C */ A + C + B {}
22+
pub trait Foo7:
23+
A
24+
+ C
25+
// and B
26+
+ B
27+
{
28+
}
29+
pub trait Foo8:
30+
// A and C
31+
A
32+
+ C
33+
// and B
34+
+ B
35+
{
36+
}
2237

2338
// Other cases
2439
trait Person {
@@ -68,3 +83,69 @@ trait /* comment 1 */ Programmer /* comment2 */ {
6883
trait /* comment1 */ CompSciStudent1: /* comment2 */ Programmer + Student /* comment3 */ {
6984
fn git_username(&self) -> String;
7085
}
86+
87+
// Traits with where and comments
88+
trait Bar
89+
where
90+
Self: Sized,
91+
Option<Self>: Foo,
92+
{
93+
}
94+
/*comment0*/
95+
trait Bar
96+
/*comment1*/
97+
where
98+
Self: Sized, /*comment2*/
99+
/*comment3*/ Option<Self>: Foo, /*comment4*/
100+
{
101+
}
102+
trait Bar
103+
//comment1 Longgggggggggggggggggggggggggggggggggggggggggggggggggg
104+
where
105+
Self: Sized, /*comment2*/
106+
/*comment3*/ Option<Self>: Foo, /*comment4*/
107+
{
108+
}
109+
trait Bar
110+
/*comment1*/
111+
where
112+
Self: Sized, /*comment2*/
113+
/*comment3*/ Option<Self>: Foo,
114+
//comment4 Longgggggggggggggggggggggggggggggggggggggggggggggggggg
115+
{
116+
}
117+
trait Bar
118+
/*comment1 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/
119+
where
120+
Self: Sized, /*comment2 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/
121+
/*comment3 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/ Option<Self>: Foo,
122+
/*comment4 Longgggggggggggggggggggggggggggggggggggggggggggggggggg*/
123+
{
124+
}
125+
trait ConstCheck<T>: /*comment1*/ Foo
126+
where
127+
T: Baz, /*comment2*/
128+
{
129+
const J: i32;
130+
}
131+
132+
// Some other trait cases with comments
133+
/*comment0*/
134+
auto trait Example /*comment1*/ {}
135+
pub unsafe auto trait PubUnsafeExample /*comment1*/ {}
136+
pub unsafe auto trait PubUnsafeExample // comment1
137+
{
138+
}
139+
trait Foo /*comment1*/ {
140+
type Bar: Baz;
141+
type Inner: Foo = Box<Foo>;
142+
}
143+
pub trait Iterator /*comment1*/ {
144+
type Item;
145+
fn next(&mut self) -> Option<Self::Item>;
146+
}
147+
pub trait Iterator //comment1
148+
{
149+
type Item;
150+
fn next(&mut self) -> Option<Self::Item>;
151+
}

tests/target/trait.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,13 @@ trait Y // comment
8686

8787
// #2055
8888
pub trait Foo:
89-
// A and C
90-
A + C
91-
// and B
89+
// A and C
90+
A
91+
+ C
92+
// and B
9293
+ B
93-
{}
94+
{
95+
}
9496

9597
// #2158
9698
trait Foo {

tests/target/trait_2015_edition.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ trait Y // comment
8989

9090
// #2055
9191
pub trait Foo:
92-
// A and C
93-
A + C
94-
// and B
92+
// A and C
93+
A
94+
+ C
95+
// and B
9596
+ B
96-
{}
97+
{
98+
}
9799

98100
// #2158
99101
trait Foo {

0 commit comments

Comments
 (0)