-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Should enum Foo { SingleVariant }
be zero-size?
#37649
Comments
frewsxcv and misdreavus on IRC point out that the discriminant is already eliminated in some cases: playbot: enum Foo { SingleVariant(i32) } ::std::mem::size_of::<Foo>()
4
playbot: enum Foo { SingleVariant(()) } ::std::mem::size_of::<Foo>()
0
playbot: enum Foo { SingleVariant{} } ::std::mem::size_of::<Foo>()
1 |
I'm quite sure it should be. This looks like a bug. The thing is that an enum with 1 const-like variant counts as a "C-like" enum, and has a "semi-well-defined" representation. Not sure what to do about that. |
I agree that this inconsistency feels like a bug. @arielb1 The section https://doc.rust-lang.org/reference.html#enumerations of the Rust reference seems to only state that it can be cast (using The change would possibly break assumptions of existing crates on the size of such enumerations (for example when they are |
Sure. The layout of |
What about the size of
and then the size of
and then
and what about this
I mean, whatever the behavior is, it would be inconsistent with something. |
@petrochenkov I think all of your example except the |
I agree with @SimonSapin here. There's also |
Duplicate of #15747. |
This prints
1
in rustc 1.14.0-nightly (cae6ab1 2016-11-05):I’m guessing this one byte is the enum’s discriminant. But since there’s a single variant, that discriminant is useless. Could such a type be zero-size? For what it’s worth, an empty
enum Bar {}
is already zero-size.This type could be written
struct Foo;
which is zero-size, but a single-variant enum could come up if it’s generated by a macro that can generate any number of variants depending on its arguments.The text was updated successfully, but these errors were encountered: