Skip to content

Make @intFromEnum an error for empty enums #18795

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

Merged
merged 4 commits into from
Feb 6, 2024
Merged

Conversation

Rexicon226
Copy link
Contributor

@Rexicon226 Rexicon226 commented Feb 3, 2024

fixes #18767

  • makes running @intFromEnum on undefined return undefined
  • makes running @intFromEnum on empty enums (that are exhaustive) an error.

should we reconsider empty enums in general? they have no actual value representation, and so i'm confused why we have them.

@rohlem
Copy link
Contributor

rohlem commented Feb 3, 2024

should we reconsider empty enums in general? [...] i'm confused why we have them.

Empty enum-s are uninstantiable ("noreturn-like") types - see #15909 .

Short version:

Using such a type somewhere asserts to the compiler that it will never be instantiated.
(This is exactly the same mechanism as when noreturn as return type of a function marks the function may never return,
otherwise that type would be instantiated to be returned to the caller).
This can for example happen if someone reifies an enum via @Type with conditional fields, if none of the fields end up being present.
A union field having such a type can never be the active field, since there is no possible value to read from / write to it,
so it would be a logic bug (=> compile error) if the code still tried to access it in any way.


EDIT:
As for @intFromEnum(undefined), I don't know if this ever appeared in my code, but the more flexible design would be to return undefined again (propagate it).
If code unconditionally does the conversion, but then only uses the result if the input was defined, the behavior is still well-defined.
See also the excellent summary comment #211 (comment).

Copy link
Member

@Vexu Vexu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes running @intFromEnum on undefined an error, which is something that has bitten me too many times.

It should return undefined.

should we reconsider empty enums in general? they have no actual value representation, and so i'm confused why we have them.

See the issues I linked in my comment.

@mlugg
Copy link
Member

mlugg commented Feb 3, 2024

This PR makes one change which is objectively wrong (erroring on undefined) and another which depends on undecided language semantics (#15909 and friends). I don't think there would be any value in merging it.

@Rexicon226
Copy link
Contributor Author

Rexicon226 commented Feb 3, 2024

I hear the people, and undefined it shall return.

However in response to @mlugg I disagree. This is a patch for the issue. I think that I should add a link and a TODO to it. see tagName for the exact same temporary fix. We should not keep a crash for an undefined (no pun) amount of time because of a non resolved issue. I feel the solution is very clear, but I understand it's under debate.

(away for most of the day so no patch rn for return undefined)

@Rexicon226 Rexicon226 changed the title Make @intFromEnum an error for undefined and empty enums Make @intFromEnum an error for empty enums Feb 4, 2024
Copy link
Member

@Vexu Vexu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents the compiler from crashing and that's the best that can be done until the proper behavior is decided on.

@Vexu Vexu merged commit 24fb6d1 into ziglang:master Feb 6, 2024
@Rexicon226 Rexicon226 deleted the empty-enum branch February 7, 2024 00:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

compiler crashes (@intFromEnum?)
4 participants