-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Enum tag sizes are too large #598
Comments
Also, fields with a type like |
|
I think this should be: |
|
There's no way to initialize an
Foo and Bar are exactly the same. Foo can be |
Why is |
Hmm, I see what you're saying. I'm not sure zig is prepared to handle or should handle the algebraic concept of |
Actually 0 does already exists as Anyway, since |
A type with 0 values and a type with 1 value are kinda the same. They both contain 0 bits of information.
a struct with 0 members would be a 0-bit type, if it were allowed (not sure if it is or not). an enum with 0 or 1 member would be a 0-bit type. a u0 would be a 0-bit type (whose value is always 0). Types are first class objects at comptime, so something like a pointer to void might happen as a result of passing void as a type parameter. For example, a set can be defined as a map from T to void. However, there's currently no way to add or delete struct members or enum members with comptime code. So it makes more sense for both of those 0-member situations to be compile errors. I can't think of any reason to explicitly make a struct or enum that's completely useless. |
Just looking at "number of bits of information", a singleton and an uninhabited type differ when they compose. You can use them in generic code. eg. if consumers of a generic feature must provide a type for what kind of errors they produce, a consumer can provide an empty type to signal that it never produces errors. Unless they really make the implementation difficult, I think they should be allowed. |
https://github.com/zig-lang/zig/blob/53b18c8542d6d40c3aa41e9d97290a4ca46eaa15/src/analyze.cpp#L1392
currently picks how big the tag for an enum should be by using
bits_needed_for_unsigned
on the number of fields.bits_needed_for_unsigned(x)
returns the smallest number of bits needed to hold the valuex
(unlessx
is 0, in which case it somewhat confusingly returns 0). That means if there are eg. two variants in an enum,enum { A, B }
, the tag is au2
, the smallest int that can hold 2.For an enum with 256 variants, this means it will take 2 bytes instead of 1.
Trying to change this got me errors involving
@enumTagName
when running the tests.The text was updated successfully, but these errors were encountered: