-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Expressing the Swift struct and enum layouts to Rust #6
Comments
|
The Regarding ZSTs, the primary problem I see is things like this which would certainly be a bug if it occurred in Swift. Edit: I think that in addition, Swift assumes that all allocations are done by stride, not size, and ZSTs are the only Rust types which are not allocated based upon what Swift believes is a valid stride. |
I just discovered that |
For wrapping and indexing into // Required for ManuallyDrop in union.
#![feature(untagged_unions)]
use std::mem;
#[repr(C)]
union WrapperInner<T> {
value: mem::ManuallyDrop<T>,
_size: u8,
}
struct Wrapper<T>(WrapperInner<T>);
struct Zst;
fn main() {
// `Wrapper<u32>` has the same memory layout as `u32`.
assert_eq!(mem::size_of::<u32>(), 4);
assert_eq!(mem::align_of::<u32>(), 4);
assert_eq!(mem::size_of::<Wrapper<u32>>(), 4);
assert_eq!(mem::align_of::<Wrapper<u32>>(), 4);
// `Wrapper<Zst>` has the same alignment as `Zst` but always a size of 1.
assert_eq!(mem::size_of::<Zst>(), 0);
assert_eq!(mem::align_of::<Zst>(), 1);
assert_eq!(mem::size_of::<Wrapper<Zst>>(), 1);
assert_eq!(mem::align_of::<Wrapper<Zst>>(), 1);
} |
The Swift struct and enum layouts, distinguish between the size (store size) and stride (array size) of types.
In particular, the stable Swift 5 layout algorithm (mostly) documented here, which would be largely reasonable to define as a Rust
#[repr(Swift_5)]
, implemented naively is unsound.The notable example is in this playground
A possible way to fix this is to first add a (
Sized
-like) implicit auto traitStrided
which would encode the necessary guarantee (that the size equals the stride) and which would not be implemented by#[repr(Swift_5)]
types.Another point of remark is that Swift requires all types to have nonzero stride, even if they have zero size.
This means that Swift assumes all ZSTs have unique addresses in memory, if they have an address, though I do not know if Swift acts upon this assumption.
The text was updated successfully, but these errors were encountered: