Skip to content

Compiler unexpectedly panicked when formatting code #5876

Open
@gianzellweger

Description

@gianzellweger

I recently looked through all the features of Rustfmt and picked a few options where I preferred some option over the default. When trying this new mix, however, I got an error. I have attached both the log and my configuration.
rustc-ice-2023-08-02T21:08:46.715244Z-75321.txt
rustfmt.toml

Activity

ytmimi

ytmimi commented on Aug 2, 2023

@ytmimi
Contributor

@gianzellweger when you have a moment can you please provide a minimal code snippet inline that we can run rustfmt on with your configuration to reproduce the issue. something like:

fn main() {
    // code that reproduces the issue
}

Without an example code snippet there's very little we can do to help.

added
needs-mcveneeds a Minimal Complete and Verifiable Example
on Aug 2, 2023
gianzellweger

gianzellweger commented on Aug 2, 2023

@gianzellweger
Author

@ytmimi Is there a way to see which code it was trying to format? The entire project has about 2000 LOC and I do not think that satisfies the "minimal code snippet" requirement. I also tried it with another project and there it worked like intended.

ytmimi

ytmimi commented on Aug 2, 2023

@ytmimi
Contributor

@gianzellweger first, I want to say thanks for bringing this to our attention. If you can try to narrow it down that would be great. Just looking at the logs you've provided I think this might be a duplicate of #4968

gianzellweger

gianzellweger commented on Aug 2, 2023

@gianzellweger
Author

@ytmimi I have now narrowed it down to a single file (which is still 1000 lines, so not a helpful progression) and I have tried deactivating every single configuration on its own. The panic only goes away if I remove error_on_unformatted, everything else still produces the panic. Is there anything else I should try?

ytmimi

ytmimi commented on Aug 2, 2023

@ytmimi
Contributor

Normally I'd recommend running rustfmt with RUSTFMT_LOG=debug. The extra info might help you narrow it down, but I think there might be an issue with logging with the most recent released version that won't be resolved until we sync this repo back up with the upstream rust-lang/rust repo. I was also going to say that you could have run rustfmt with -v to figure out what file, but you already narrowed it down.

Any chance you can share a link to the file?

gianzellweger

gianzellweger commented on Aug 2, 2023

@gianzellweger
Author

@ytmimi How about I instead narrow it down to the specific piece of code? There's only about 10-20 functions which I can comment out separately until the error goes away.

gianzellweger

gianzellweger commented on Aug 2, 2023

@gianzellweger
Author

@ytmimi The cause either appears multiple times in this one file or the cause is something else entirely. I will link the code below. If you need anything else, feel free to comment. Thank you and good luck.
main.rs

ytmimi

ytmimi commented on Aug 2, 2023

@ytmimi
Contributor

Alright, I had a look at this and I think I've got it. In your case the error is happening in the component_tab function on the line that starts with format!("{:name_width$} {:temperature_width$.2}°C {:critical_width$}",. The issue is that there's a trailing space at the end of the line and you're using a °C unicode character. removing the whitespace will prevent the panic.

gianzellweger

gianzellweger commented on Aug 2, 2023

@gianzellweger
Author

@ytmimi That fixed it, thank you so much. Can we agree that this isn't optimal behavior from rustfmt and could be improved? Not trying to put any pressure on anybody.

ytmimi

ytmimi commented on Aug 2, 2023

@ytmimi
Contributor

@gianzellweger this particular issue will almost certainly be resolved once we add support for let-chains. The underlying issue here has more to do with not properly handling the size of the unicode char correctly. Similar issues have been reported before.

Here's a bit of a deep dive explanation into what's happening in this case:

fn fail() {
    if let Some(x) = x && y {
        format!("{:name_width$}  {:temperature_width$.2}°C  {:critical_width$}") 
        //                                                                      ^
        //                                                                      |
        //                                                                      |
        //                                                               trailing whitespace
    }
}

and here's a screenshot that highlights the whitespace at the end:

Screen Shot 2023-08-02 at 7 09 08 PM

The issue is that rustfmt doesn't currently support formatting let-chains so when rustfmt reaches any let-chain in the code the best it can do is copy the entire Span of the AST node into the output buffer and move on with formatting.

At the end of the formatting process rustfmt does a pass over that output buffer to see if there are instances of trailing whitespace or lines that exceed the max_width, and in this case the panic is happening during this reporting process. The panic in annotate-snippets is a direct result of not handling the unicode char length properly.

gianzellweger

gianzellweger commented on Aug 3, 2023

@gianzellweger
Author

@ytmimi That makes a lot of sense, as my code uses let-chains. Is there any issue that tracks the progress of this or has this implementation not started at all? Thank you once more.

added
bugPanic, non-idempotency, invalid code, etc.
and removed
needs-mcveneeds a Minimal Complete and Verifiable Example
on Aug 3, 2023

9 remaining items

Loading
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-whitespacebugPanic, non-idempotency, invalid code, etc.e-trailing whitespaceerror[internal]: left behind trailing whitespace

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @danielhuang@ytmimi@gianzellweger

        Issue actions

          Compiler unexpectedly panicked when formatting code · Issue #5876 · rust-lang/rustfmt