Skip to content

rustfmt failed: left behind trailing whitespace #6321

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

Closed
RalfJung opened this issue Sep 10, 2024 · 3 comments
Closed

rustfmt failed: left behind trailing whitespace #6321

RalfJung opened this issue Sep 10, 2024 · 3 comments
Labels
a-comments duplicate e-trailing whitespace error[internal]: left behind trailing whitespace

Comments

@RalfJung
Copy link
Member

RalfJung commented Sep 10, 2024

Rustfmt fails to format this file:

use std::mem;

// We have three fields to avoid the ScalarPair optimization.
#[allow(unused)]
enum E {
    None,
    Some(&'static (), &'static (), usize),
}

fn main() { unsafe {
    let mut p: mem::MaybeUninit<E> = mem::MaybeUninit::zeroed();
    // The copy when `E` is returned from `transmute` should destroy padding
    // (even when we use `write_unaligned`, which under the hood uses an untyped copy).
    p.as_mut_ptr().write_unaligned(mem::transmute((0usize, 0usize, 0usize)));
    // This is a `None`, so everything but the discriminant is padding.
    assert!(matches!(*p.as_ptr(), E::None));

    // Turns out the discriminant is (currently) stored
    // in the 2nd pointer, so the first half is padding.
    let c = &p as *const _ as *const u8;
    let _val = *c.add(0); // Get a padding byte.
    //~^ERROR: uninitialized
} }

with these settings:

version = "Two"

The error is:

error[internal]: left behind trailing whitespace
  --> /home/r/src/rust/miri/tests/fail/uninit/padding-enum.rs:23:23:1
   |
23 |         
   | ^^^^^^^^
   |

If I comment out the "version" setting, formatting works fine.

I saw a bunch of other issues with similar errors but it is not clear to me which one of them this could be a duplicate of.

@RalfJung
Copy link
Member Author

Strangely, even the "properly formatted" version of this file causes the same error:

use std::mem;

// We have three fields to avoid the ScalarPair optimization.
#[allow(unused)]
enum E {
    None,
    Some(&'static (), &'static (), usize),
}

fn main() {
    unsafe {
        let mut p: mem::MaybeUninit<E> = mem::MaybeUninit::zeroed();
        // The copy when `E` is returned from `transmute` should destroy padding
        // (even when we use `write_unaligned`, which under the hood uses an untyped copy).
        p.as_mut_ptr().write_unaligned(mem::transmute((0usize, 0usize, 0usize)));
        // This is a `None`, so everything but the discriminant is padding.
        assert!(matches!(*p.as_ptr(), E::None));

        // Turns out the discriminant is (currently) stored
        // in the 2nd pointer, so the first half is padding.
        let c = &p as *const _ as *const u8;
        let _val = *c.add(0); // Get a padding byte.        
        //~^ERROR: uninitialized
    }
}

It always adds an empty line with 8 spaces before the //~^ERROR: uninitialized comment.

@matthiaskrgr
Copy link
Member

ah 😆 I also just found this, looking at new rustfmt failures.

with

version = "Two"
merge_derives = false
imports_granularity = "Module"
use std::mem;

#[allow(unused)]
#[repr(C)]
union U {
    field: (u8, u16),
}

fn main() {
    unsafe {
        let p: U = mem::transmute(0u32); // The copy when `U` is returned from `transmute` should destroy padding.
        let c = &p as *const _ as *const [u8; 4];
        let _val = *c; // Read the entire thing, definitely contains the padding byte.
        
        //~^ERROR: uninitialized
    }
}

fails to fmt

error[internal]: left behind trailing whitespace
  --> /tmp/im/padding-union.rs:14:14:1
   |
14 |
   | ^^^^^^^^
   |

@ytmimi
Copy link
Contributor

ytmimi commented Sep 10, 2024

Duplicate of #6157 and #5391

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Sep 10, 2024
@ytmimi ytmimi added duplicate a-comments e-trailing whitespace error[internal]: left behind trailing whitespace labels Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments duplicate e-trailing whitespace error[internal]: left behind trailing whitespace
Projects
None yet
Development

No branches or pull requests

3 participants