Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the lists module for formatting the generic bounds #3680

Closed
wants to merge 11 commits into from
Closed
8 changes: 4 additions & 4 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ mod test {
* test3
*/"#,
false,
Shape::legacy(100, Indent::new(0, 0)),
Shape::legacy(100, Indent::empty()),
&wrap_normalize_config).unwrap();
assert_eq!("/// test1\n/// test2\n// test3", comment);

Expand All @@ -1798,7 +1798,7 @@ mod test {

// test2"#,
false,
Shape::legacy(100, Indent::new(0, 0)),
Shape::legacy(100, Indent::empty()),
&wrap_normalize_config).unwrap();
assert_eq!("// test1\n\n// test2", comment);

Expand All @@ -1807,7 +1807,7 @@ mod test {

//@ test2"#,
false,
Shape::legacy(100, Indent::new(0, 0)),
Shape::legacy(100, Indent::empty()),
&wrap_normalize_config).unwrap();
assert_eq!("//@ test1\n\n//@ test2", comment);

Expand All @@ -1819,7 +1819,7 @@ mod test {
another bare line!
*/"#,
false,
Shape::legacy(100, Indent::new(0, 0)),
Shape::legacy(100, Indent::empty()),
&wrap_config).unwrap();
assert_eq!("// test1\n/*\n a bare line!\n\n another bare line!\n*/", comment);
}
Expand Down
40 changes: 28 additions & 12 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -990,20 +990,32 @@ pub(crate) fn format_trait(
rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape)?;
result.push_str(&generics_str);

// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
if !generic_bounds.is_empty() {
let ident_hi = context
.snippet_provider
.span_after(item.span, &item.ident.as_str());
let bound_hi = generic_bounds.last().unwrap().span().hi();
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
if contains_comment(snippet) {
return None;
}

let comment = if context.config.version() == Version::Two {
let comment_span = mk_sp(generics.span.hi(), generic_bounds[0].span().lo());
let after_colon = context.snippet_provider.span_after(comment_span, ":");
recover_missing_comment_in_span(
mk_sp(after_colon, comment_span.hi()),
shape,
context,
// 1 = ":"
last_line_width(&result) + 1,
)
.unwrap_or_default()
} else {
let ident_hi = context
.snippet_provider
.span_after(item.span, &item.ident.as_str());
let bound_hi = generic_bounds.last().unwrap().span().hi();
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
if contains_comment(snippet) {
return None;
}
String::new()
};
result = rewrite_assign_rhs_with(
context,
result + ":",
format!("{}:{}", result, comment),
generic_bounds,
shape,
RhsTactics::ForceNextLineWithoutIndent,
Expand Down Expand Up @@ -1048,7 +1060,11 @@ pub(crate) fn format_trait(
if let Some(lo) = item_snippet.find('/') {
// 1 = `{`
let comment_hi = body_lo - BytePos(1);
let comment_lo = item.span.lo() + BytePos(lo as u32);
let comment_lo = if generic_bounds.is_empty() {
item.span.lo() + BytePos(lo as u32)
} else {
generic_bounds[generic_bounds.len() - 1].span().hi()
};
if comment_lo < comment_hi {
match recover_missing_comment_in_span(
mk_sp(comment_lo, comment_hi),
Expand Down
Loading