-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Support repr alignment on unions. #43274
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![feature(attr_literals)] | ||
#![feature(repr_align)] | ||
#![feature(untagged_unions)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The feature should be unnecessary here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried removing it, but it seems it's still needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked more into why this is the case - it's needed because of the struct inside the union in one of the tests. The struct is not Copy by default and unions require their members to be copy by default. Non-copy types in untagged unions aren't stabilised yet. If I made the struct derive from Copy then I could probably remove the feature, I think I'll leave it for now though. |
||
|
||
use std::mem::{size_of, size_of_val, align_of, align_of_val}; | ||
|
||
#[repr(align(16))] | ||
pub union U16 { | ||
a: u8, | ||
b: u32 | ||
} | ||
|
||
fn main() { | ||
assert_eq!(align_of::<U16>(), 16); | ||
assert_eq!(size_of::<U16>(), 16); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above, size of should be max(union stride, a string and b stride) shouldn't it? |
||
let u = U16 { a: 10 }; | ||
unsafe { | ||
assert_eq!(align_of_val(&u.a), 1); | ||
assert_eq!(size_of_val(&u.a), 1); | ||
assert_eq!(u.a, 10); | ||
} | ||
|
||
let u = U16 { b: 11 }; | ||
unsafe { | ||
assert_eq!(align_of_val(&u.b), 4); | ||
assert_eq!(size_of_val(&u.b), 4); | ||
assert_eq!(u.b, 11); | ||
} | ||
|
||
hybrid::check_hybrid(); | ||
} | ||
|
||
mod hybrid { | ||
use std::mem::{size_of, align_of}; | ||
|
||
#[repr(align(16))] | ||
struct S1 { | ||
a: u16, | ||
b: u8, | ||
} | ||
|
||
#[repr(align(32))] | ||
union U { | ||
s: S1, | ||
c: u16, | ||
} | ||
|
||
#[repr(align(64))] | ||
struct S2 { | ||
d: u8, | ||
u: U, | ||
} | ||
|
||
pub fn check_hybrid() { | ||
assert_eq!(align_of::<S1>(), 16); | ||
assert_eq!(size_of::<S1>(), 16); | ||
assert_eq!(align_of::<U>(), 32); | ||
assert_eq!(size_of::<U>(), 32); | ||
assert_eq!(align_of::<S2>(), 64); | ||
assert_eq!(size_of::<S2>(), 64); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why should it be 32? It's a union, not a struct, the size should be the same as the largest element shouldn't it?