-
Notifications
You must be signed in to change notification settings - Fork 926
rustfmt won't format trait bounds that have comments #6039
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
Comments
@maia-s thanks for reaching out. Can you please put together a minimal reproducible example that someone can run rustfmt on to reproduce the issue. Without that there's not much anyone can do to help. Please also provide details about any configuration options that you're using. |
I understand, but if I try to make a minimal reproducible example rustfmt works on it again. Is there a way for me to diagnose this somehow? I haven't made any changes to rustfmt config. |
It seems to be related to having a trait with associated types with many complex bounds as a bound on another trait. |
If you can't reproduce the issue with a simple snippet can you put together a small test repo that illustrates the issue and send a link to that? |
I'll try. I'll let you know if I make any progress on that. I've isolated the issue to traits depending on one particular trait, but the trait in question has bounds from the entire crate, and currently I can only reproduce rustfmt not working in one file. That file uses the same construct as other files in the crate, but those other files work now. The problematic file also has another issue (not rustfmt related, I think) that causes rustc to be unable to infer the type of Self unless I leave out one particular associated type bound (again, the same construct works in other files). I have no idea what's going on there, but maybe whatever causes that also causes rustfmt to fail. |
Luckily rustfmt doesn't require its input to compile, it just needs to parse as valid rust code. fn some_function() {
if let Some(x) = some_variable_thats_no_defined {
// code
}
} So no need to recreate things exactly. You can use dummy names to get things down to a reproducible test case. |
Thank you! That information helped a lot. The problem is actually caused by having a comment in the list of trait bounds. This snippet reproduces the problem: trait Trait: //
Bound
{} Any part or contents of this trait won't be formatted. The comment doesn't have to be before the first bound, but it does have to be before a bound. |
Moving the comment to before the |
So removing the comments fixed your issue? If so then this might be a duplicate of #2055 |
Yes, looks like it. That explains why some other comments I wrote had disappeared, too Do you know if there's been any progress on that? I see there's a pull request that fixes it (and the duplication issue too), but it seems abandoned |
Unfortunately I don't think so. I took a look at #5059, and at first glance it doesn't seem like the easiest PR to review. It might be better to try a different approach to solve this issue. |
For now I'm going to close this as a duplicate. |
Out of nowhere, rustfmt has stopped working for one of my projects. It doesn't work in vscode with rust-analyzer, it doesn't work with
cargo fmt
on either stable or nightly in a terminal, and it doesn't work invoked on its own withrustfmt file
. If there's syntax errors in the file it will complain, but otherwise it does nothing.rustfmt --check
on a file that needs formatting returns success. I updated withrustup update
after it broke and it didn't change anything. Please help.rustfmt 1.7.0-stable (82e1608d 2023-12-21)
rustfmt 1.7.0-nightly (d5fd0997 2024-01-22)
The text was updated successfully, but these errors were encountered: