Skip to content
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

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

Closed
mbrubeck opened this issue Jul 17, 2014 · 7 comments
Closed

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

mbrubeck opened this issue Jul 17, 2014 · 7 comments
Labels
A-codegen Area: Code generation C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@mbrubeck
Copy link
Contributor

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 }.

@mbrubeck
Copy link
Contributor Author

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.

@thestinger thestinger added the I-slow Issue: Problems and improvements with respect to performance of generated code. label Sep 16, 2014
Aatch added a commit to Aatch/rust that referenced this issue Dec 22, 2014
This makes an enum like `enum Foo { Foo }` be zero-sized. If a
discriminant is set to non-zero, it still treats it as a c-like enum.

Fixes rust-lang#15747
@alexcrichton alexcrichton removed the I-slow Issue: Problems and improvements with respect to performance of generated code. label Mar 16, 2015
@steveklabnik
Copy link
Member

Triage: no change.

@frewsxcv frewsxcv added A-optimization T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 9, 2017
@SimonSapin
Copy link
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

@Mark-Simulacrum
Copy link
Member

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

@Mark-Simulacrum Mark-Simulacrum added I-nominated T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed A-optimization labels Jun 22, 2017
@nikomatsakis
Copy link
Contributor

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

@RReverser
Copy link
Contributor

enum Foo { Foo = 1 }

I'd say this doesn't have to force non-zero-size either, only repr should. Any discriminant can be inlined into usages just like 0 one.

@nox
Copy link
Contributor

nox commented Mar 30, 2018

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.

bors added a commit that referenced this issue Apr 26, 2018
Treat repr(Rust) univariant fieldless enums as ZSTs

This makes all those enums be represented the same way:

```rust
enum A1 { B1 }
enum A2 { B2 = 0 }
enum A3 { B3, C3(!) }
```

Related to #15747.

Cc @rust-lang/wg-codegen @rust-lang/lang
@bors bors closed this as completed in 8f36804 Apr 26, 2018
U007D pushed a commit to humanenginuity/rust that referenced this issue Apr 30, 2018
…#15747)

This makes all those enums be represented the same way:

```rust
enum A1 { B1 }
enum A2 { B2 = 0 }
enum A3 { B3, C3(!) }
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.