Skip to content
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

Braced expressions guarded by #[cfg] attrs are reformatted into invalid syntax #4382

Closed
jmillikin opened this issue Aug 13, 2020 · 0 comments · Fixed by #4387
Closed

Braced expressions guarded by #[cfg] attrs are reformatted into invalid syntax #4382

jmillikin opened this issue Aug 13, 2020 · 0 comments · Fixed by #4387
Labels
bug Panic, non-idempotency, invalid code, etc.

Comments

@jmillikin
Copy link

Describe the bug

Given this input file:

pub const NAME_MAX: usize = {
    #[cfg(target_os = "linux")]   { 1024 }
    #[cfg(target_os = "freebsd")] {  255 }
};

rustfmt with default settings converts it into

pub const NAME_MAX: usize = {
    #[cfg(target_os = "linux")]
    {[cfg(target_os = "linux")]   {        1024
    }
    #[cfg(target_os = "freebsd")]
    {[cfg(target_os = "freebsd")] {        255
    }
};

, which does not parse.

Expected behavior

Formatting code should not generate un-parseable code.

Meta

I can reproduce this on nightly (rustfmt 2.0.0-rc.2-nightly (0921e1fb 2020-07-29)) and on https://play.rust-lang.org/

@jmillikin jmillikin added the bug Panic, non-idempotency, invalid code, etc. label Aug 13, 2020
ayazhafiz added a commit to ayazhafiz/rustfmt that referenced this issue Aug 15, 2020
This commit partially reverts rust-lang#3934, opting to create a span that covers
the entire body of a closure when formatting a closure body with a
block-formatting strategy, rather than having the block-formatting code
determine if the visitor pointer should be rewound. The problem with
rewinding the visitor pointer is it may be incorrect for other (i.e.
non-artificial) AST nodes, as in the case of rust-lang#4382.

Closes rust-lang#4382
calebcartwright pushed a commit that referenced this issue Aug 16, 2020
This commit partially reverts #3934, opting to create a span that covers
the entire body of a closure when formatting a closure body with a
block-formatting strategy, rather than having the block-formatting code
determine if the visitor pointer should be rewound. The problem with
rewinding the visitor pointer is it may be incorrect for other (i.e.
non-artificial) AST nodes, as in the case of #4382.

Closes #4382
calebcartwright pushed a commit to calebcartwright/rustfmt that referenced this issue Nov 14, 2020
This commit partially reverts rust-lang#3934, opting to create a span that covers
the entire body of a closure when formatting a closure body with a
block-formatting strategy, rather than having the block-formatting code
determine if the visitor pointer should be rewound. The problem with
rewinding the visitor pointer is it may be incorrect for other (i.e.
non-artificial) AST nodes, as in the case of rust-lang#4382.

Closes rust-lang#4382
calebcartwright pushed a commit that referenced this issue Nov 14, 2020
This commit partially reverts #3934, opting to create a span that covers
the entire body of a closure when formatting a closure body with a
block-formatting strategy, rather than having the block-formatting code
determine if the visitor pointer should be rewound. The problem with
rewinding the visitor pointer is it may be incorrect for other (i.e.
non-artificial) AST nodes, as in the case of #4382.

Closes #4382
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant