rust-lang/rust

do zero-sized allocations have to obey alignment requests?

pnkfelix opened this issue · 1 comments

Consider the following code (playpen):

fn main() {
    use std::mem;
    let array_u8 = Box::new([0_u8; 0]);
    let array_u64 = Box::new([0_u64; 0]);
    let addr_u8 = &*array_u8 as *const _ as usize;
    let addr_u64 = &*array_u64 as *const _ as usize;
    
    println!("alignof  [u8; 0] {:?}", mem::align_of::<[u8; 0]>());
    println!("alignof [u64; 0] {:?}", mem::align_of::<[u64; 0]>());
    println!("array_u8 addr: {:?} array_u64 addr: {:?}", addr_u8, addr_u64);
    
    assert!(addr_u8 % mem::align_of::<[u8; 0]>() == 0);
    assert!(addr_u64 % mem::align_of::<[u64; 0]>() == 0);
}

In Rust stable, this assert-failed. In Rust beta and nightly, it passes. (From what I can, this change in behavior is due to #41064 )

So maybe there's no bug at all. But I wanted to at least have a place to track discussion of whether this particular change is something we want to require of all user allocators, rather than forcing it to be intermixed with other discussion on #32838

(Thanks to Ralf Jung for pointing out this question about allocator behavior on IRC.)

Ugh I need to think more before going through the work of filing tickets. Zero-sized allocations are illegal according to the API of trait Alloc. Thank goodness!