Open
Description
Also see this summary below.
Consider
#![allow(dead_code)]
use std::mem;
#[no_mangle]
pub fn sizeof_empty_struct_1() -> usize {
#[repr(C)]
struct EmptyS1 {
f: [i64; 0],
}
// Expected: 4
// Actual: 0
mem::size_of::<EmptyS1>()
}
#[no_mangle]
pub fn sizeof_empty_struct_2() -> usize {
#[repr(C, align(8))]
struct X {
i: i32,
}
#[repr(C)]
struct EmptyS2 {
x: [X; 0],
}
// Expected: 8
// Actual: 0
mem::size_of::<EmptyS2>()
}
#[no_mangle]
pub fn sizeof_enum() -> usize {
#[repr(C)]
enum E {
A = 1111111111111111111
}
// Expected: 4
// Actual: 8
mem::size_of::<E>()
}
#[no_mangle]
pub fn sizeof_empty_union_1() -> usize {
#[repr(C)]
union EmptyU1 {
f: [i8; 0],
}
// Expected: 1
// Actual: 0
mem::size_of::<EmptyU1>()
}
#[no_mangle]
pub fn sizeof_empty_union_2() -> usize {
#[repr(C)]
union EmptyU2 {
f: [i64; 0],
}
// Expected: 8
// Actual: 0
mem::size_of::<EmptyU2>()
}
and the corresponding MSVC output: https://godbolt.org/z/csv4qc
The behavior of MSVC is described here as far as it is known to me: https://github.com/mahkoh/repr-c/blob/a04e931b67eed500aea672587492bd7335ea549d/repc/impl/src/builder/msvc.rs#L215-L236
Metadata
Metadata
Assignees
Labels
Area: Foreign function interface (FFI)Area: the `#[repr(stuff)]` attributeCategory: This is a bug.Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessOperating system: WindowsToolchain: MSVC, Operating system: WindowsMedium priorityRelevant to the language team, which will review and decide on the PR/issue.