- Notifications
You must be signed in to change notification settings - Fork 14k
Open
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-reprArea: the `#[repr(stuff)]` attributeArea: the `#[repr(stuff)]` attributeC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-windowsOperating system: WindowsOperating system: WindowsO-windows-msvcToolchain: MSVC, Operating system: WindowsToolchain: MSVC, Operating system: WindowsP-mediumMedium priorityMedium priorityT-langRelevant to the language teamRelevant to the language team
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
stepancheg
Metadata
Metadata
Assignees
Labels
A-FFIArea: Foreign function interface (FFI)Area: Foreign function interface (FFI)A-reprArea: the `#[repr(stuff)]` attributeArea: the `#[repr(stuff)]` attributeC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessO-windowsOperating system: WindowsOperating system: WindowsO-windows-msvcToolchain: MSVC, Operating system: WindowsToolchain: MSVC, Operating system: WindowsP-mediumMedium priorityMedium priorityT-langRelevant to the language teamRelevant to the language team