Skip to content

Conversation

@joshlf
Copy link
Member

@joshlf joshlf commented Jan 21, 2024

The implementation of this method checks for the presence of fields. While this is /similar/ to the concept of a C-like enum, it's not the same. Fieldless enums are exactly as described: none of their variants has fields. C-like enums, on the other hand, are more recently referred to as "unit-only" because their variants are not permitted to contain parentheses or curly braces. E.g., the following is a fieldless enum, but is not a C-like/unit-only enum:

  #[repr(u8)]
  enum Foo {
      A,   // This is fine
      B(), // This is not fine
      C{}  // This is not fine
  }

This distinction is important specifically when it comes to discriminants. The syntax Foo::A as u8 computes the discriminant value of the A variant. However, Foo::B is a type constructor, and thus is a function. As a result, Foo::B as u8 computes the address of the function Foo::B.

@joshlf joshlf requested a review from jswrenn January 21, 2024 23:46
@joshlf joshlf enabled auto-merge January 21, 2024 23:46
@joshlf joshlf force-pushed the derive-enum-c-like-field-less branch from d620424 to 8c787b3 Compare January 21, 2024 23:51
@joshlf joshlf requested a review from jswrenn January 21, 2024 23:51
jswrenn
jswrenn previously approved these changes Jan 21, 2024
The implementation of this method checks for the presence of fields.
While this is /similar/ to the concept of a C-like enum, it's not the
same. Fieldless enums are exactly as described: none of their variants
has fields. C-like enums, on the other hand, are more recently referred
to as "unit-only" because their variants are not permitted to contain
parentheses or curly braces. E.g., the following is a fieldless enum,
but is not a C-like/unit-only enum:

  #[repr(u8)]
  enum Foo {
      A,   // This is fine
      B(), // This is not fine
      C{}  // This is not fine
  }

This distinction is important specifically when it comes to
discriminants. The syntax `Foo::A as u8` computes the discriminant value
of the `A` variant. However, `Foo::B` is a type constructor, and thus is
a function. As a result, `Foo::B as u8` computes the address of the
function `Foo::B`.
@joshlf joshlf force-pushed the derive-enum-c-like-field-less branch from 8c787b3 to 11fb55b Compare January 22, 2024 00:07
@joshlf joshlf requested a review from jswrenn January 22, 2024 00:07
@joshlf joshlf added this pull request to the merge queue Jan 22, 2024
Merged via the queue into main with commit dd107ad Jan 22, 2024
@joshlf joshlf deleted the derive-enum-c-like-field-less branch January 22, 2024 00:20
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.

2 participants