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

Rustfmt adds extra bracket after cfg feature, breaking build #4630

Closed
avanhatt opened this issue Jan 7, 2021 · 2 comments
Closed

Rustfmt adds extra bracket after cfg feature, breaking build #4630

avanhatt opened this issue Jan 7, 2021 · 2 comments
Labels
bug Panic, non-idempotency, invalid code, etc. duplicate

Comments

@avanhatt
Copy link

avanhatt commented Jan 7, 2021

Running rustfmt on this snippet of code:

pub const fn vector_width() -> usize {
    #[cfg(feature="vec_width_2")]
    { 2 }
    // Default
    #[cfg(not(feature="vec_width_2"))]
    { 4 }
}

Rewrites it to the following, inserting two additional opening brackets {:

pub const fn vector_width() -> usize {
    #[cfg(feature = "vec_width_2")]
    {
        {        2
    }
    // Default
    #[cfg(not(feature = "vec_width_2"))]
    {
        {        4
    }
}

To Reproduce

For a minimal working example, I created a new lib project with the following lib.rs file (gist):

pub const fn vector_width() -> usize {
    #[cfg(feature="vec_width_2")]
    { 2 }
    // Default
    #[cfg(not(feature="vec_width_2"))]
    { 4 }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

This project builds before cargo fmt but fails to build after:

fmt-bracket-bug (master) $ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
fmt-bracket-bug (master) $ cargo fmt
fmt-bracket-bug (master) $ cargo build
   Compiling fmt-bracket-bug v0.1.0 (/Users/avh/research/fmt-bracket-bug)
error: this file contains an unclosed delimiter
  --> src/lib.rs:19:3
   |
1  | pub const fn vector_width() -> usize {
   |                                      - unclosed delimiter
2  |     #[cfg(feature = "vec_width_2")]
3  |     {
   |     - unclosed delimiter
4  |         {        2
   |         - this delimiter might not be properly closed...
5  |     }
   |     - ...as it matches this but it has different indentation
...
19 | }
   |   ^

error[E0308]: mismatched types
 --> src/lib.rs:1:32
  |
1 | pub const fn vector_width() -> usize {
  |              ------------      ^^^^^ expected `usize`, found `()`
  |              |
  |              implicitly returns `()` as its body has no tail or `return` expression

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
error: could not compile `fmt-bracket-bug`

To learn more, run the command again with --verbose.

The following is the diff after cargo fmt

fmt-bracket-bug (master) $ git diff
diff --git a/src/lib.rs b/src/lib.rs
index d4cc40e..a790f92 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,9 +1,13 @@
 pub const fn vector_width() -> usize {
-    #[cfg(feature="vec_width_2")]
-    { 2 }
+    #[cfg(feature = "vec_width_2")]
+    {
+        {        2
+    }
     // Default
-    #[cfg(not(feature="vec_width_2"))]
-    { 4 }
+    #[cfg(not(feature = "vec_width_2"))]
+    {
+        {        4
+    }
 }

Expected behavior

Rewrites for whitespace, without additional brackets:

pub const fn vector_width() -> usize {
    #[cfg(feature = "vec_width_2")]
    {
        2
    }
    // Default
    #[cfg(not(feature = "vec_width_2"))]
    {
        4
    }
}

Meta

  • rustfmt version: rustfmt 1.4.25-stable (0f29ff6 2020-11-11)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: cargo fmt
@avanhatt avanhatt added the bug Panic, non-idempotency, invalid code, etc. label Jan 7, 2021
@calebcartwright
Copy link
Member

Thanks for the report. This is a duplicate of a few prior reports (#4475, #4467, #4452, #4522), which has already been fixed and released so I am going to close.

The bug has existed for a while, and the fix for this is available starting from v1.4.26 so I don't think this will justify any kind of emergency backport/rust patch release on stable. In the interim you could consider using nightly (at least temporarily) if that's a possibility for you, so that you could get a newer version that has the fix (the latest nightly is v1.4.30), or alternatively perhaps building from source or grabbing the binaries from the GitHub release (more info here).

Otherwise you may just need to add #[rustfmt::skip] attributes and wait for the next stable release

@avanhatt
Copy link
Author

avanhatt commented Jan 8, 2021

Got it, thanks!

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. duplicate
Projects
None yet
Development

No branches or pull requests

2 participants