Skip to content

Allow single-value enum types to have size 0 #15747

Description

@mbrubeck
enum Foo { Foo }
std::mem::size_of::<Foo>() // evaluates to 1

Since type Foo has only one value, it could have size zero. (This is already the case for single-valued struct types like struct Foo;.)

Open question: If this change is made, should Foo as uint still evaluate to 0 in the example above?

Note: It would still be possible to force a non-zero size using [#repr(...)] or enum Foo { Foo = 1 }.

Activity

  1. mbrubeck commented on Jul 17, 2014

    @mbrubeck
    ContributorAuthor

    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.

  2. added
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    on Sep 16, 2014
  3. added a commit that references this issue on Dec 22, 2014
    a1d11bc
  4. removed
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    on Mar 16, 2015
  5. steveklabnik commented on Jun 6, 2016

    @steveklabnik
    Contributor

    Triage: no change.

  6. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on May 9, 2017
  7. SimonSapin commented on May 9, 2017

    @SimonSapin
    Contributor

    #37649 (comment)

    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
  8. Mark-Simulacrum commented on Jun 22, 2017

    @Mark-Simulacrum
    Member

    I'm nominating for the lang team to decide -- we should confirm that we want this before compiler team implements.

  9. nikomatsakis commented on Jul 6, 2017

    @nikomatsakis
    Contributor

    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 that Foo as usize still gives 0, even though the discriminant is not reified.

  10. 2 remaining items

  11. nox commented on Mar 30, 2018

    @nox
    Contributor

    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.

  12. added a commit that references this issue on Apr 26, 2018
  13. added a commit that references this issue on Apr 30, 2018
    2ebda12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions