rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Format crashes unexpectedly.

Open Frostie314159 opened this issue 1 year ago • 7 comments

I'm not sure what exactly caused this, since my crate uses generics extensively, but it seems to be related. rustc-ice-2024-02-01T09_12_26-75080.txt

Frostie314159 avatar Feb 01 '24 09:02 Frostie314159

@Frostie314159 I appreciate you opening an issue. However, without a code snippet that can be used to reproduce the issue there's not much anyone can do to help. When you have a moment can you please provide a reproducible example, let us know what version of rustfmt you're using, and what configuration options you were using at the time of the crash.

ytmimi avatar Feb 06 '24 18:02 ytmimi

Ok, in the meantime I've traced the issue down, it seemed to be related to enums having generic parameters with defaults and trait bounds, but without a where clause. Introducing the where clause fixed the issue. I'm currently on the go, but as soon as I get home, I will make an example.

Frostie314159 avatar Feb 06 '24 20:02 Frostie314159

That would be very helpful!

ytmimi avatar Feb 06 '24 22:02 ytmimi

After trying over and over to isolate the issue to a generic example, I was ultimately unable to do so. However I published a branch of my code with crash in it if that helps. Here is a link to the enum causing the crash: ieee80211-rs. I hope this helps to trace down the issue.

Frostie314159 avatar Feb 07 '24 11:02 Frostie314159

No worries. I appreciate that you spent some time trying to narrow it down. Having a link to a repo where I can reproduce the issue is the next best thing to a minimal code snippet.

It would still be helpful to know what version of rustfmt you're using and if you were using any configuration options when you ran into this issue? Also, do you actually see a panic! or are files that you'd expect to be formatted just not getting formatted?

ytmimi avatar Feb 07 '24 21:02 ytmimi

I ran rustfmt -V, with this output:

rustfmt 1.7.0-nightly (5518eaa 2024-01-29)

I'm on the current nightly channel and am using the default config. When running rustfmt I get a panic and the file attached to the original comment gets emitted. This is the original code, that caused the panic:

pub enum ManagementFrameBody<
    'a,
    RateIterator = SupportedRatesTLVReadRateIterator<'a>,
    ExtendedRateIterator = ExtendedSupportedRatesTLVReadRateIterator<'a>,
    TLVIterator: IntoIterator<Item = IEEE80211TLV<'a, RateIterator, ExtendedRateIterator>> = TLVReadIterator<'a>,
    ActionFramePayload = &'a [u8],
>
{
    Action(ActionFrameBody<ActionFramePayload>),
    ActionNoAck(ActionFrameBody<ActionFramePayload>),
    Beacon(BeaconFrameBody<'a, RateIterator, ExtendedRateIterator, TLVIterator>),
    ATIM,
    Unknown {
        sub_type: ManagementFrameSubtype,
        body: &'a [u8],
    },
}

Restructuring it to a where clause like this fixes the problem:

pub enum ManagementFrameBody<
    'a,
    RateIterator = SupportedRatesTLVReadRateIterator<'a>,
    ExtendedRateIterator = ExtendedSupportedRatesTLVReadRateIterator<'a>,
    TLVIterator = TLVReadIterator<'a>,
    ActionFramePayload = &'a [u8],
> where
    TLVIterator: IntoIterator<Item = IEEE80211TLV<'a, RateIterator, ExtendedRateIterator>>,
{
    Action(ActionFrameBody<ActionFramePayload>),
    ActionNoAck(ActionFrameBody<ActionFramePayload>),
    Beacon(BeaconFrameBody<'a, RateIterator, ExtendedRateIterator, TLVIterator>),
    ATIM,
    Unknown {
        sub_type: ManagementFrameSubtype,
        body: &'a [u8],
    },
}

Frostie314159 avatar Feb 08 '24 07:02 Frostie314159

All very useful details. Thank you for sharing 😁

ytmimi avatar Feb 08 '24 17:02 ytmimi