Skip to content

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

Closed
maia-s opened this issue Jan 23, 2024 · 12 comments
Closed

rustfmt won't format trait bounds that have comments #6039

maia-s opened this issue Jan 23, 2024 · 12 comments

Comments

@maia-s
Copy link

maia-s commented Jan 23, 2024

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)

@maia-s maia-s changed the title rustfmt does nothing rustfmt suddenly stopped working, won't format files anymore Jan 23, 2024
@maia-s maia-s changed the title rustfmt suddenly stopped working, won't format files anymore rustfmt suddenly stopped working, won't format anymore Jan 23, 2024
@ytmimi
Copy link
Contributor

ytmimi commented 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.

@ytmimi ytmimi added the needs-mcve needs a Minimal Complete and Verifiable Example label Jan 23, 2024
@maia-s
Copy link
Author

maia-s commented 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
Copy link
Author

maia-s commented 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
Copy link
Contributor

ytmimi commented 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
Copy link
Author

maia-s commented 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
Copy link
Contributor

ytmimi commented Jan 23, 2024

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.

@maia-s
Copy link
Author

maia-s commented 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:

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.

@maia-s
Copy link
Author

maia-s commented Jan 23, 2024

Moving the comment to before the : (moving that to the next line) has the same effect. Moving the comment to before Trait causes rustfmt to duplicate everything before the {} except for the comment and format the first half

@ytmimi
Copy link
Contributor

ytmimi commented Jan 24, 2024

So removing the comments fixed your issue? If so then this might be a duplicate of #2055

@maia-s
Copy link
Author

maia-s commented 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
Copy link
Contributor

ytmimi commented 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
Copy link
Contributor

ytmimi commented Jan 29, 2024

For now I'm going to close this as a duplicate.

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2024
@ytmimi ytmimi added duplicate and removed needs-mcve needs a Minimal Complete and Verifiable Example labels Jan 29, 2024
@ytmimi ytmimi changed the title rustfmt suddenly stopped working, won't format anymore rustfmt won't format trait bounds that have comments Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants