Repository navigation
Allow single-value enum types to have size 0 #15747
Description
Activity
I'm filing this just as a possibility for future optimization. It doesn't have to be a high priority, since any use cases for this could probably be fulfilled by an empty struct instead.
- addedI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.
on Sep 16, 2014 - added a commit that references this issue
on Dec 22, 2014 - removedI-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.
on Mar 16, 2015 Triage: no change.
- addedT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
on May 9, 2017 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 nominating for the lang team to decide -- we should confirm that we want this before compiler team implements.
- addedT-langRelevant to the language teamRelevant to the language teamand removed
on Jun 22, 2017 Discussed in @rust-lang/lang -- If the enum is not
#[repr(C)], it seems like something that is within our rights to do. I would assume thatFoo as usizestill gives0, even though the discriminant is not reified.2 remaining items
I think any univariant fieldless enum which is not
repr(Rust)should be a ZST, because just adding a single ZST field makes the enum a ZST.- added a commit that references this issue
on Apr 26, 2018 - added a commit that references this issue
on Apr 30, 2018
Since type
Foohas only one value, it could have size zero. (This is already the case for single-valued struct types likestruct Foo;.)Open question: If this change is made, should
Foo as uintstill evaluate to0in the example above?Note: It would still be possible to force a non-zero size using
[#repr(...)]orenum Foo { Foo = 1 }.