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

/* comment */ inside trait generics gets duplicated #5358

Closed
xhalo32 opened this issue May 31, 2022 · 1 comment · Fixed by #5446
Closed

/* comment */ inside trait generics gets duplicated #5358

xhalo32 opened this issue May 31, 2022 · 1 comment · Fixed by #5446
Labels
a-comments bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors help wanted

Comments

@xhalo32
Copy link

xhalo32 commented May 31, 2022

trait Test</* comment */ T> {}

Gets reformatted as

trait Test</* comment */ T> /* comment */ T> {}

Whereas no changes were expected.

Playground:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=88c36ba8a83e2c01030bbbf95327be6a

Setting version = Two has no effect.

λ rustfmt --version
rustfmt 1.4.38-stable (7737e0b 2022-04-04)
@calebcartwright calebcartwright added bug Panic, non-idempotency, invalid code, etc. a-comments good first issue Issues up for grabs, also good candidates for new rustfmt contributors help wanted labels May 31, 2022
@calebcartwright
Copy link
Member

Thank you for the report! This feels like a bug with duplicative/overlapping comment recovery that's probably using the wrong span. The bug doesn't occur if there's a where clause present, so I suspect the problem is somewhere around here:

rustfmt/src/items.rs

Lines 1084 to 1104 in 79515f1

} else {
let item_snippet = context.snippet(item.span);
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);
if comment_lo < comment_hi {
match recover_missing_comment_in_span(
mk_sp(comment_lo, comment_hi),
Shape::indented(offset, context.config),
context,
last_line_width(&result),
) {
Some(ref missing_comment) if !missing_comment.is_empty() => {
result.push_str(missing_comment);
}
_ => (),
}
}
}
}

For anyone interested in taking a look, we should probably reduce the area that's being scanned for comments there to something less than the entire span of the item (most likely simply from the end of the where clause's span to the start of the body span)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments bug Panic, non-idempotency, invalid code, etc. good first issue Issues up for grabs, also good candidates for new rustfmt contributors help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants