rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.01k stars 886 forks source link

Internal error "left behind trailing whitespace" for assignment with RHS comment #4515

Open scottmcm opened 3 years ago

scottmcm commented 3 years ago

Describe the bug

error[internal]: left behind trailing whitespace
 --> /playground/src/main.rs:4:4:17
  |
4 |     let new_len = 
  |                  ^
  |

To Reproduce

Repros on the playground, https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8d26c934db762f2165ab9b0cc4d2ef61

    pub unsafe fn as_chunks_mut_unchecked<const N: usize>(&mut self) -> &mut [[T; N]] {
        debug_assert_ne!(N, 0);
        debug_assert_eq!(self.len() % N, 0);
        let new_len = 
            // SAFETY: Our precondition is exactly what's needed to call this
            unsafe { crate::intrinsics::exact_div(self.len(), N) };
        // SAFETY: We cast a slice of `new_len * N` elements into
        // a slice of `new_len` many `N` elements chunks.
        unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), new_len) }
    }
calebcartwright commented 3 years ago

Thanks for the report @scottmcm. As is often the case with these formatting errors, rustfmt isn't properly handling the comments between the assignment operator and the rhs, so it is bailing and reverting to the original formatting to prevent stripping those comments. Your original snippet has a trailing space after the assignment operator, so when that original snippet is restored that trailing space is also retained.

You can resolve the formatting error by removing the trailing space, though note that rustfmt currently won't be doing any formatting of the rhs.

For anyone interested in working on this, it's true that rustfmt on the master branch (v2.0 RC) doesn't result in the formatting error like rustfmt v1.x but rustfmt v2 still isn't properly handling this either as the indentation and spacing are also off

scottmcm commented 3 years ago

Thanks for the reply, @calebcartwright. The internal error messaging was enough to clue me in and unblock myself. (And make me realize that I forget to configure my editor on this machine to auto-delete those...)

whizsid commented 3 years ago

I will work on this issue.

John-Nagle commented 3 years ago

Also hit this issue:

$ cargo fmt
error[internal]: left behind trailing whitespace
   --> /home/john/projects/sl/SL-test-viewer/sltestviewer/src/messages/msgdecodetest.rs:146:146:33
    |
146 |                 let rightanswer = 
    |                                  ^
    |

warning: rustfmt has failed to format. See previous 1 errors.

Same situation as in the bug report above:

                let rightanswer =
                "TestMessage { test_block1: TestBlock1 { test_block1_item: TestBlock1Item { test1: 258, test2: 00010203-0405-0607-0809-0a0b0c0d0e0f, test3: [1, 2, 3] } } }";

with a space after the "=".