-
Notifications
You must be signed in to change notification settings - Fork 898
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
Attributes on expressions become duplicated under certain conditions #4452
Comments
Interesting. Seems that at least as of v679 of the rustc-ap-* crates that the span is off for expression statements with attributes, at least for cast expression kinds, as the attributes are only associated to the subexpr of the cast and not the expr nor the statement. This should be fixed upstream in rustc (if it hasn't already), though I imagine we could work around it by using the lo of the span from the first attribute in these cases (Expr statement, expr kind cast, subexpr has attributes) rustfmt/src/formatting/spanned.rs Lines 69 to 71 in c240f10
|
@calebcartwright I'm trying to fix this issue and update the first attribute of |
Thanks for looking into it @chansuke! The fix will need to be made within The problem in this case currently is that the attributes are associated to the cast subexpression and not the cast expression itself, but correct recognition of the presence of attributes is required for rustfmt for span derivation. In the match arm I linked above for expression statements, we'll need to check the expression Does that make sense? |
Another, perhaps more degenerate, case fn foo() {
#[allow(clippy::cast_possible_wrap)]
{
// some comment
}
} Here rustfmt's output is quite mangled 🙃 |
@calebcartwright Thank you for your comment!! I will try adding |
Thanks @t-nelson. Although that presents with similar symptoms, that's a separate and distinct issue in upstream rustc that has subsequently been fixed. That should resolve itself after we update rustfmt to the next version of the rustc mods used for parsing |
Thanks @chansuke! I've gone ahead and opened a PR upstream in rustc to correct the span calculation, and would rather just pull in the fix from upstream vs. trying to work around it within rustfmt. It'll probably be a few days before we can grab the upstream fixes, but once we do this issue will turn into adding test cases to prevent regressions if you'd be interested in working on that. |
@calebcartwright Thank you for your comment!! I will close my PR. |
@chansuke - everything is unblocked now as of #4478, so you should be able to grab the latest changes from the master branch in this repo and add the tests. Please include a cast expression as noted in the original issue description, as well as a range expression (I discovered the same span issue existed while fixing the bug in rustc). Something as simple as the below should cover it // foo
#[attr]
1..2 While we're at it 😄 Please also include a test for blocks using the snippet posted above as well as the one from #4475 |
@calebcartwright I appreciate your support. I will add some tests on latest master. |
Closing as this has been resolved |
Describe the bug
Attributes on expressions become doubled every time
rustfmt
is ran when preceeded by a comment and the expression contains a cast, i.e.as i64
.To Reproduce
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2499dbd812381b73c863ad79691053fd
Running
rustfmt
undertools
on the above playground link causes the attribute to double on every invocation.Playing around with it, removing either the comment and
as i64
cause this not to reproduce, so it seems like it's the combination of them both that is causing this bug.Expected behavior
The attribute isn't doubled every time.
Meta
rustfmt 1.4.21-nightly (01f2ead 2020-09-04)
and latest stablerustfmt
directly on the file(edited by calebcartwright to include inline reproduction snippet)
The text was updated successfully, but these errors were encountered: