Skip to content

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

Closed as not planned
Closed as not planned

Description

@maia-s

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)

Activity

  1. changed the title [-]rustfmt does nothing[/-] [+]rustfmt suddenly stopped working, won't format files anymore[/+] on Jan 23, 2024
  2. changed the title [-]rustfmt suddenly stopped working, won't format files anymore[/-] [+]rustfmt suddenly stopped working, won't format anymore[/+] on Jan 23, 2024
  3. ytmimi commented on Jan 23, 2024

    @ytmimi
    Contributor

    @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.

  4. added
    E-needs-mcveCall for participation: this issue has a repro, but needs a Minimal Complete and Verifiable Example
    on Jan 23, 2024
  5. maia-s commented on Jan 23, 2024

    @maia-s
    Author

    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.

  6. maia-s commented on Jan 23, 2024

    @maia-s
    Author

    It seems to be related to having a trait with associated types with many complex bounds as a bound on another trait.

  7. ytmimi commented on Jan 23, 2024

    @ytmimi
    Contributor

    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?

  8. maia-s commented on Jan 23, 2024

    @maia-s
    Author

    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.

  9. ytmimi commented on Jan 23, 2024

    @ytmimi
    Contributor

    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.

  10. maia-s commented on Jan 23, 2024

    @maia-s
    Author

    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.

  11. maia-s commented on Jan 23, 2024

    @maia-s
    Author

    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

  12. ytmimi commented on Jan 24, 2024

    @ytmimi
    Contributor

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

  13. maia-s commented on Jan 24, 2024

    @maia-s
    Author

    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

  14. ytmimi commented on Jan 29, 2024

    @ytmimi
    Contributor

    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.

  15. ytmimi commented on Jan 29, 2024

    @ytmimi
    Contributor

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

  16. added
    S-duplicateStatus: duplicate of another issue or PR
    and removed
    E-needs-mcveCall for participation: this issue has a repro, but needs a Minimal Complete and Verifiable Example
    on Jan 29, 2024
  17. changed the title [-]rustfmt suddenly stopped working, won't format anymore[/-] [+]rustfmt won't format trait bounds that have comments[/+] on Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-commentsArea: commentsS-duplicateStatus: duplicate of another issue or PR

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions