Skip to content
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

Disable field reordering for repr(int). #56887

Merged
merged 2 commits into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2059,9 +2059,10 @@ impl ReprOptions {
}

/// Returns `true` if this `#[repr()]` should inhibit struct field reordering
/// optimizations, such as with repr(C) or repr(packed(1)).
/// optimizations, such as with repr(C), repr(packed(1)), or repr(<int>).
pub fn inhibit_struct_field_reordering_opt(&self) -> bool {
!(self.flags & ReprFlags::IS_UNOPTIMISABLE).is_empty() || (self.pack == 1)
self.flags.intersects(ReprFlags::IS_UNOPTIMISABLE) || self.pack == 1 ||
self.int.is_some()
Copy link
Member

@eddyb eddyb Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @rkruppe @nagisa This seems somewhat fragile (I mean the pre-existing code).
The check for pack == 1 means things will still be reordered with #[repr(packed(N))], is that intentional?
Also, it seems to me that inhibit_enum_layout_opt is a subset of this function and maybe we should have just one function that checks all conditions?

Copy link
Member

@nagisa nagisa Dec 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repr RFC does not specify any behaviour /wrt repr(packed(N)) for enums and I’m not sure I recall us guaranteeing that repr(packed) specifies any representation and the reference seems to agree.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact as per strict reading of the reference, self.pack == 1 inhibiting reordering would be somewhat wrong, as it should cause "packing" for the original ordering, but since the "default" ordering is not specified, it seems to be fine either way...

}

/// Returns true if this `#[repr()]` should inhibit union abi optimisations
Expand Down
18 changes: 10 additions & 8 deletions src/test/run-pass/structs-enums/enum-non-c-like-repr-c-and-int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ use std::mem;
#[repr(C, u8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum MyEnum {
A(u32), // Single primitive value
B { x: u8, y: i16 }, // Composite, and the offset of `y` depends on tag being internal
C, // Empty
D(Option<u32>), // Contains an enum
E(Duration), // Contains a struct
A(u32), // Single primitive value
B { x: u8, y: i16, z: u8 }, // Composite, and the offsets of `y` and `z`
// depend on tag being internal
C, // Empty
D(Option<u32>), // Contains an enum
E(Duration), // Contains a struct
}

#[repr(C)]
Expand All @@ -44,14 +45,14 @@ union MyEnumPayload {

#[repr(u8)] #[derive(Copy, Clone)] enum MyEnumTag { A, B, C, D, E }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantA(u32);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB {x: u8, y: i16 }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB {x: u8, y: i16, z: u8 }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantD(Option<u32>);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(Duration);

fn main() {
let result: Vec<Result<MyEnum, ()>> = vec![
Ok(MyEnum::A(17)),
Ok(MyEnum::B { x: 206, y: 1145 }),
Ok(MyEnum::B { x: 206, y: 1145, z: 78 }),
Ok(MyEnum::C),
Err(()),
Ok(MyEnum::D(Some(407))),
Expand All @@ -63,7 +64,7 @@ fn main() {
// Binary serialized version of the above (little-endian)
let input: Vec<u8> = vec![
0, 17, 0, 0, 0,
1, 206, 121, 4,
1, 206, 121, 4, 78,
2,
8, /* invalid tag value */
3, 0, 151, 1, 0, 0,
Expand Down Expand Up @@ -112,6 +113,7 @@ fn parse_my_enum<'a>(dest: &'a mut MyEnum, buf: &mut &[u8]) -> Result<(), ()> {
MyEnumTag::B => {
dest.payload.B.x = read_u8(buf)?;
dest.payload.B.y = read_u16_le(buf)? as i16;
dest.payload.B.z = read_u8(buf)?;
}
MyEnumTag::C => {
/* do nothing */
Expand Down
18 changes: 10 additions & 8 deletions src/test/run-pass/structs-enums/enum-non-c-like-repr-c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ use std::mem;
#[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum MyEnum {
A(u32), // Single primitive value
B { x: u8, y: i16 }, // Composite, and the offset of `y` depends on tag being internal
C, // Empty
D(Option<u32>), // Contains an enum
E(Duration), // Contains a struct
A(u32), // Single primitive value
B { x: u8, y: i16, z: u8 }, // Composite, and the offset of `y` and `z`
// depend on tag being internal
C, // Empty
D(Option<u32>), // Contains an enum
E(Duration), // Contains a struct
}

#[repr(C)]
Expand All @@ -44,14 +45,14 @@ union MyEnumPayload {

#[repr(C)] #[derive(Copy, Clone)] enum MyEnumTag { A, B, C, D, E }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantA(u32);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB {x: u8, y: i16 }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB {x: u8, y: i16, z: u8 }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantD(Option<u32>);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(Duration);

fn main() {
let result: Vec<Result<MyEnum, ()>> = vec![
Ok(MyEnum::A(17)),
Ok(MyEnum::B { x: 206, y: 1145 }),
Ok(MyEnum::B { x: 206, y: 1145, z: 78 }),
Ok(MyEnum::C),
Err(()),
Ok(MyEnum::D(Some(407))),
Expand All @@ -63,7 +64,7 @@ fn main() {
// Binary serialized version of the above (little-endian)
let input: Vec<u8> = vec![
0, 17, 0, 0, 0,
1, 206, 121, 4,
1, 206, 121, 4, 78,
2,
8, /* invalid tag value */
3, 0, 151, 1, 0, 0,
Expand Down Expand Up @@ -112,6 +113,7 @@ fn parse_my_enum<'a>(dest: &'a mut MyEnum, buf: &mut &[u8]) -> Result<(), ()> {
MyEnumTag::B => {
dest.payload.B.x = read_u8(buf)?;
dest.payload.B.y = read_u16_le(buf)? as i16;
dest.payload.B.z = read_u8(buf)?;
}
MyEnumTag::C => {
/* do nothing */
Expand Down
18 changes: 10 additions & 8 deletions src/test/run-pass/structs-enums/enum-non-c-like-repr-int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ use std::mem;
#[repr(u8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum MyEnum {
A(u32), // Single primitive value
B { x: u8, y: i16 }, // Composite, and the offset of `y` depends on tag being internal
C, // Empty
D(Option<u32>), // Contains an enum
E(Duration), // Contains a struct
A(u32), // Single primitive value
B { x: u8, y: i16, z: u8 }, // Composite, and the offset of `y` and `z`
// depend on tag being internal
C, // Empty
D(Option<u32>), // Contains an enum
E(Duration), // Contains a struct
}

#[allow(non_snake_case)]
Expand All @@ -39,15 +40,15 @@ union MyEnumRepr {

#[repr(u8)] #[derive(Copy, Clone)] enum MyEnumTag { A, B, C, D, E }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantA(MyEnumTag, u32);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB { tag: MyEnumTag, x: u8, y: i16 }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB { tag: MyEnumTag, x: u8, y: i16, z: u8 }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantC(MyEnumTag);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantD(MyEnumTag, Option<u32>);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(MyEnumTag, Duration);

fn main() {
let result: Vec<Result<MyEnum, ()>> = vec![
Ok(MyEnum::A(17)),
Ok(MyEnum::B { x: 206, y: 1145 }),
Ok(MyEnum::B { x: 206, y: 1145, z: 78 }),
Ok(MyEnum::C),
Err(()),
Ok(MyEnum::D(Some(407))),
Expand All @@ -59,7 +60,7 @@ fn main() {
// Binary serialized version of the above (little-endian)
let input: Vec<u8> = vec![
0, 17, 0, 0, 0,
1, 206, 121, 4,
1, 206, 121, 4, 78,
2,
8, /* invalid tag value */
3, 0, 151, 1, 0, 0,
Expand Down Expand Up @@ -108,6 +109,7 @@ fn parse_my_enum<'a>(dest: &'a mut MyEnum, buf: &mut &[u8]) -> Result<(), ()> {
MyEnumTag::B => {
dest.B.x = read_u8(buf)?;
dest.B.y = read_u16_le(buf)? as i16;
dest.B.z = read_u8(buf)?;
}
MyEnumTag::C => {
/* do nothing */
Expand Down