Uh oh!
There was an error while loading. Please reload this page.
Document #[repr] on non-C-like enums - #25651
Conversation
rust-highfive
commented
May 20, 2015
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @steveklabnik (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see CONTRIBUTING.md for more information. |
bluss
commented
May 20, 2015
This documents current behavior. Is repr(X) on non-C-like enums is a bug? In that case we need to fix that by rejecting code instead. |
josephglanville
commented
May 22, 2015
I hope it's not a bug, it would be a breaking change if so. I am using the behaviour and I would assume others are to. The main reason was that the collections::enum_set::CLike is marked unstable and this seemed to do what I need anyways. |
alexcrichton
commented
May 26, 2015
bluss
commented
May 26, 2015
Great to have that clear! @SimonSapin and I discovered that this is quite crucial for arrayvec and smallvec -- see issue servo/rust-smallvec#5 |
SimonSapin
commented
May 26, 2015
It seems very fragile to relay on on |
bluss
commented
May 26, 2015
The idea of this PR was to make it non-fragile: The needed guarantee is written into the reference and a test to prove it added to the testsuite. |
SimonSapin
commented
May 26, 2015
I made a PR into your PR to add another test for the behavior that arrayvec relies on: bluss#1 |
bluss
commented
May 26, 2015
I never knew you could submit PRs to PRs, that's neat. arrayvec uses |
SimonSapin
commented
May 27, 2015
I think it tests a slightly different case, but feel free to take it or not. |
Document #[repr] on non-C-like enums
rustc accepts the following today:
#[repr(u8)]
enum Flag<T> {
Dropped,
Alive(T),
}
and it has a good use (it appears to me): this inhibits the non-nullable
pointer optimization that the regular Option<T> and similar enums allow.
Document this in the reference, and add tests to make sure it compiles.
This means that we guarantee with `repr` that the discriminant will be
present and with that size, but not sure if we want to guarantee
anything more (no guarantee on placement in struct).bluss
commented
May 27, 2015
Oh, my tests are actually completely wrong |
bluss
commented
May 27, 2015
Updated PR. Included @SimonSapin's testcase (thanks!), and fixed mine, which were embarrassingly completely incorrect the first time. |
There was a problem hiding this comment.
Is... this not undefined behavior? We explicitly tell LLVM that all &T references are not null, and this is making a null one?
There was a problem hiding this comment.
Right. It should never load the value in the zero variable or in its location inside the enum though, can it be safe that way?
There was a problem hiding this comment.
Instead of inspecting the payload, could this just assert that the size of the enum is twice the size of a word?
There was a problem hiding this comment.
Well that's basically what the tests above already do. They are less specific maybe.
So we should remove this?
There was a problem hiding this comment.
Yeah I think it's fine to remove.
rustc accepts the following today:
#[repr(u8)]
enum Flag<T> {
Dropped,
Alive(T),
}
and it has a good use (it appears to me): this inhibits the non-nullable
pointer optimization that the regular Option<T> and similar enums allow.
Document this in the reference, and add tests to make sure it compiles.
This means that we guarantee with `repr` that the discriminant will be
present and with that size, but not sure if we want to guarantee
anything more (no guarantee on placement in struct).bluss
commented
May 28, 2015
Thanks, PR updated. Only tests sizes now. |
alexcrichton
commented
May 28, 2015
bluss
commented
May 28, 2015
Thanks! |
Document #[repr] on non-C-like enums
rustc accepts the following today:
#[repr(u8)]
enum Flag<T> {
Dropped,
Alive(T),
}
and it has a good use (it appears to me): this inhibits the non-nullable
pointer optimization that the regular Option<T> and similar enums allow.
Document this in the reference, and add tests to make sure it compiles.
This means that we guarantee with `repr` that the discriminant will be
present and with that size, but not sure if we want to guarantee
anything more (no guarantee on placement in struct).Document #[repr] on non-C-like enums
rustc accepts the following today:
#[repr(u8)]
enum Flag<T> {
Dropped,
Alive(T),
}
and it has a good use (it appears to me): this inhibits the non-nullable
pointer optimization that the regular Option<T> and similar enums allow.
Document this in the reference, and add tests to make sure it compiles.
This means that we guarantee with `repr` that the discriminant will be
present and with that size, but not sure if we want to guarantee
anything more (no guarantee on placement in struct).bors
commented
May 29, 2015
⌛ Testing commit d0e6396 with merge c449a9f... |
bors
commented
May 29, 2015
💔 Test failed - auto-mac-32-opt |
There was a problem hiding this comment.
thread '<main>' panicked at 'assertion failed:(left == right)(left:4, right:8)', /Users/rustbuild/src/rust-buildbot/slave/auto-mac-32-opt/build/src/test/run-pass/enum-discrim-manual-sizing-2.rs:89
on 32-bit. Looks like there is a bug in #[repr]?
There was a problem hiding this comment.
Hm yes, that sounds like a bug in #[repr]
Document #[repr] on non-C-like enums
rustc accepts the following today:
#[repr(u8)]
enum Flag<T> {
Dropped,
Alive(T),
}
and it has a good use (it appears to me): this inhibits the non-nullable
pointer optimization that the regular Option<T> and similar enums allow.
Document this in the reference, and add tests to make sure it compiles.
This means that we guarantee with `repr` that the discriminant will be
present and with that size, but not sure if we want to guarantee
anything more (no guarantee on placement in struct).bluss
commented
May 29, 2015
We need to scrub this PR until the bug is fixed. I'll see what I can do though |
steveklabnik
commented
May 29, 2015
@bors: r- |
bluss
commented
Jun 8, 2015
Closing for now, reported a bug. |
Document #[repr] on non-C-like enums
rustc accepts the following today:
and it has a good use (it appears to me): this inhibits the non-nullable
pointer optimization that the regular Option and similar enums allow.
Document this in the reference, and add tests to make sure it compiles.
This means that we guarantee with
reprthat the discriminant will bepresent and with that size, but not sure if we want to guarantee
anything more (no guarantee on placement in struct).