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

Implement the special repr(C)-non-clike-enum layout #46123

Merged
merged 4 commits into from Nov 28, 2017

Conversation

Projects
None yet
8 participants
@Gankro
Contributor

Gankro commented Nov 20, 2017

This is the second half of rust-lang/rfcs#2195

which specifies that

#[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
}

Has the same layout as

#[repr(C)]
struct MyEnumRepr {
    tag: MyEnumTag,
    payload: MyEnumPayload,
}

#[repr(C)]
#[allow(non_snake_case)]
union MyEnumPayload {
    A: MyEnumVariantA,
    B: MyEnumVariantB,
    D: MyEnumVariantD,
    E: MyEnumVariantE,
}

#[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 MyEnumVariantD(Option<u32>);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(Duration);
@rust-highfive

This comment has been minimized.

Show comment
Hide comment
@rust-highfive

rust-highfive Nov 20, 2017

Collaborator

r? @petrochenkov

(rust_highfive has picked a reviewer for you, use r? to override)

Collaborator

rust-highfive commented Nov 20, 2017

r? @petrochenkov

(rust_highfive has picked a reviewer for you, use r? to override)

@Gankro

This comment has been minimized.

Show comment
Hide comment
@Gankro

Gankro Nov 20, 2017

Contributor

r? @eddyb

Contributor

Gankro commented Nov 20, 2017

r? @eddyb

@Gankro

This comment has been minimized.

Show comment
Hide comment
@Gankro

Gankro Nov 20, 2017

Contributor

Changed impl to reflect discussion in IRC. Also fixed a test that was asserting the old behaviour.

Contributor

Gankro commented Nov 20, 2017

Changed impl to reflect discussion in IRC. Also fixed a test that was asserting the old behaviour.

@Gankro

This comment has been minimized.

Show comment
Hide comment
@Gankro

Gankro Nov 20, 2017

Contributor

Comments addressed

Contributor

Gankro commented Nov 20, 2017

Comments addressed

@eddyb

This comment has been minimized.

Show comment
Hide comment
@eddyb

eddyb Nov 20, 2017

Member

cc @rust-lang/compiler @rust-lang/lang LGTM, should this wait on the RFC / have a FCP?

Member

eddyb commented Nov 20, 2017

cc @rust-lang/compiler @rust-lang/lang LGTM, should this wait on the RFC / have a FCP?

@Gankro

This comment has been minimized.

Show comment
Hide comment
@Gankro

Gankro Nov 20, 2017

Contributor

Comments addressed.

Contributor

Gankro commented Nov 20, 2017

Comments addressed.

@eddyb

eddyb approved these changes Nov 20, 2017

@carols10cents

This comment has been minimized.

Show comment
Hide comment
@carols10cents

carols10cents Nov 27, 2017

Member

eddyb (Eduard-Mihai Burtescu) approved these changes 7 days ago

soooo do I hear an r+ @eddyb? or an fcp merge? :)

Member

carols10cents commented Nov 27, 2017

eddyb (Eduard-Mihai Burtescu) approved these changes 7 days ago

soooo do I hear an r+ @eddyb? or an fcp merge? :)

@eddyb

This comment has been minimized.

Show comment
Hide comment
@eddyb

eddyb Nov 27, 2017

Member

@carols10cents But nobody answered...

Member

eddyb commented Nov 27, 2017

@carols10cents But nobody answered...

@nikomatsakis

This comment has been minimized.

Show comment
Hide comment
@nikomatsakis

nikomatsakis Nov 28, 2017

Contributor

hould this wait on the RFC / have a FCP?

I do expect the RFC to be accepted, but I don't believe we have to block on it. I believe the current behavior is undefined, so we are within our rights to adjust it.

FCP might be reasonable, though I think not really necessary, I don't feel like major controversy is expected here.

Contributor

nikomatsakis commented Nov 28, 2017

hould this wait on the RFC / have a FCP?

I do expect the RFC to be accepted, but I don't believe we have to block on it. I believe the current behavior is undefined, so we are within our rights to adjust it.

FCP might be reasonable, though I think not really necessary, I don't feel like major controversy is expected here.

@eddyb

This comment has been minimized.

Show comment
Hide comment
@eddyb
Member

eddyb commented Nov 28, 2017

@bors r+

@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 28, 2017

Contributor

📌 Commit 904ccbc has been approved by eddyb

Contributor

bors commented Nov 28, 2017

📌 Commit 904ccbc has been approved by eddyb

@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 28, 2017

Contributor

⌛️ Testing commit 904ccbc with merge 2e0fd27...

Contributor

bors commented Nov 28, 2017

⌛️ Testing commit 904ccbc with merge 2e0fd27...

bors added a commit that referenced this pull request Nov 28, 2017

Auto merge of #46123 - Gankro:c-repr, r=eddyb
Implement the special repr(C)-non-clike-enum layout

This is the second half of rust-lang/rfcs#2195

which specifies that

```rust
#[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
}
```

Has the same layout as

```rust
#[repr(C)]
struct MyEnumRepr {
    tag: MyEnumTag,
    payload: MyEnumPayload,
}

#[repr(C)]
#[allow(non_snake_case)]
union MyEnumPayload {
    A: MyEnumVariantA,
    B: MyEnumVariantB,
    D: MyEnumVariantD,
    E: MyEnumVariantE,
}

#[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 MyEnumVariantD(Option<u32>);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(Duration);

```
@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 28, 2017

Contributor

💔 Test failed - status-travis

Contributor

bors commented Nov 28, 2017

💔 Test failed - status-travis

@Gankro

This comment has been minimized.

Show comment
Hide comment
@Gankro

Gankro Nov 28, 2017

Contributor

uhhh so apparently

#[repr(u64)]
enum E6 {
    A(u8, u16, u8),
    B(u8, u16, u8)
}

is 16 bytes on i586-gnu-i686-musl, but u64 is only aligned to 4 bytes?

wat

[00:50:11] thread 'main' panicked at 'assertion failed: `(left == right)`
[00:50:11]   left: `16`,
[00:50:11]  right: `12`', /checkout/src/test/run-pass/multiple-reprs.rs:78:4
    assert_eq!(size_of::<E6>(), align_size(12, align_of::<u64>()));
Contributor

Gankro commented Nov 28, 2017

uhhh so apparently

#[repr(u64)]
enum E6 {
    A(u8, u16, u8),
    B(u8, u16, u8)
}

is 16 bytes on i586-gnu-i686-musl, but u64 is only aligned to 4 bytes?

wat

[00:50:11] thread 'main' panicked at 'assertion failed: `(left == right)`
[00:50:11]   left: `16`,
[00:50:11]  right: `12`', /checkout/src/test/run-pass/multiple-reprs.rs:78:4
    assert_eq!(size_of::<E6>(), align_size(12, align_of::<u64>()));
@Gankro

This comment has been minimized.

Show comment
Hide comment
@Gankro

Gankro Nov 28, 2017

Contributor

nevermind, I'm dumb. 6 + 8 != 12. On 64-bit that error didn't matter.

Contributor

Gankro commented Nov 28, 2017

nevermind, I'm dumb. 6 + 8 != 12. On 64-bit that error didn't matter.

@eddyb

This comment has been minimized.

Show comment
Hide comment
@eddyb
Member

eddyb commented Nov 28, 2017

@bors r+

@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 28, 2017

Contributor

📌 Commit 0e63d27 has been approved by eddyb

Contributor

bors commented Nov 28, 2017

📌 Commit 0e63d27 has been approved by eddyb

@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 28, 2017

Contributor

⌛️ Testing commit 0e63d27 with merge 436ac89...

Contributor

bors commented Nov 28, 2017

⌛️ Testing commit 0e63d27 with merge 436ac89...

bors added a commit that referenced this pull request Nov 28, 2017

Auto merge of #46123 - Gankro:c-repr, r=eddyb
Implement the special repr(C)-non-clike-enum layout

This is the second half of rust-lang/rfcs#2195

which specifies that

```rust
#[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
}
```

Has the same layout as

```rust
#[repr(C)]
struct MyEnumRepr {
    tag: MyEnumTag,
    payload: MyEnumPayload,
}

#[repr(C)]
#[allow(non_snake_case)]
union MyEnumPayload {
    A: MyEnumVariantA,
    B: MyEnumVariantB,
    D: MyEnumVariantD,
    E: MyEnumVariantE,
}

#[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 MyEnumVariantD(Option<u32>);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(Duration);

```
@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Nov 28, 2017

Contributor

☀️ Test successful - status-appveyor, status-travis
Approved by: eddyb
Pushing 436ac89 to master...

Contributor

bors commented Nov 28, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: eddyb
Pushing 436ac89 to master...

@bors bors merged commit 0e63d27 into rust-lang:master Nov 28, 2017

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment