rustfmt won't format trait bounds that have comments #6039
Copy link
Copy link
Closed as not planned
Labels
A-commentsArea: commentsArea: commentsS-duplicateStatus: duplicate of another issue or PRStatus: duplicate of another issue or PR
Description
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 with rustfmt 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 with rustup 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)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-commentsArea: commentsArea: commentsS-duplicateStatus: duplicate of another issue or PRStatus: duplicate of another issue or PR
Activity
[-]rustfmt does nothing[/-][+]rustfmt suddenly stopped working, won't format files anymore[/+][-]rustfmt suddenly stopped working, won't format files anymore[/-][+]rustfmt suddenly stopped working, won't format anymore[/+]ytmimi commentedon Jan 23, 2024
@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.
maia-s commentedon Jan 23, 2024
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.
maia-s commentedon Jan 23, 2024
It seems to be related to having a trait with associated types with many complex bounds as a bound on another trait.
ytmimi commentedon Jan 23, 2024
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?
maia-s commentedon Jan 23, 2024
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.
ytmimi commentedon Jan 23, 2024
Luckily rustfmt doesn't require its input to compile, it just needs to parse as valid rust code.
So no need to recreate things exactly. You can use dummy names to get things down to a reproducible test case.
maia-s commentedon Jan 23, 2024
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:
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.
maia-s commentedon Jan 23, 2024
Moving the comment to before the
:(moving that to the next line) has the same effect. Moving the comment to beforeTraitcauses rustfmt to duplicate everything before the {} except for the comment and format the first halfytmimi commentedon Jan 24, 2024
So removing the comments fixed your issue? If so then this might be a duplicate of #2055
maia-s commentedon Jan 24, 2024
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
ytmimi commentedon Jan 29, 2024
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.
ytmimi commentedon Jan 29, 2024
For now I'm going to close this as a duplicate.
[-]rustfmt suddenly stopped working, won't format anymore[/-][+]rustfmt won't format trait bounds that have comments[/+]