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

Support for enums #499

Open
jberthold opened this issue Mar 21, 2025 · 0 comments · Fixed by #521
Open

Support for enums #499

jberthold opened this issue Mar 21, 2025 · 0 comments · Fixed by #521
Labels
P-Token M2 P-token required for supporting P-token code

Comments

@jberthold
Copy link
Member

jberthold commented Mar 21, 2025

Essentially, the Aggregate constructor of the Value sort will require an additional indicator of which variant of a represented enum is being used. The AggregateKind is available at creation and contains a VariantIdx (wrapped Int) which numbers the variants from 0 .. N (presumably in source code order).

The Rvalue::Discriminant operation retrieves an indicator of which variant is being matched (and 0 for all values that are not enums).
However, as it turned out, this indicator is not the VariantIdx included in the AggregateKind that is provided upon creation of the Value. Specifically, the discriminant of enum variants can be set to arbitrary unique usizes using

enum Thing {                     #[repr(u8)]
  A = 65,                        enum ThingWithFields {
  B, // = 66,                      X(x:isize) = 88,
  D = 68,                          Y(y: isize, z:u8) = 89,
}                                }

and the discriminant for A will be 65, whereas the AggregateKind for an A contains a VariantIdx of 0.

The SwitchInt terminator will use these Discriminant values, whereas the Downcast projection uses the VariantIdx.

Reliable information about what exactly the Discriminant should return is difficult to find, and part of its semantics is even not fully determined among experts, see this discussion.

@jberthold jberthold added the P-token required for supporting P-token code label Mar 21, 2025
automergerpr-permission-manager bot pushed a commit that referenced this issue Apr 10, 2025
* Using the new type metadata to get the enum variant discriminator
mapping and ADT Def ID
* Enums are created as `Aggregate` values, which now carry the variant
index
* `Aggregate`s for `struct`s and `enum`s now have a correct `Ty` field
(still `TyUnknown` for tuples, though)
* `RValue::Discriminant`  is implemented:
- for an `enum`, it looks up the discriminant and returns it as a `u128`
(discriminant type information unavailable)
- for other `Aggregate`s, it will always return `0` (see related
discussion in `rustc`: rust-lang/rust#91095)

This is a first step. The data returned by `Discriminant` is actually
not consistent (bit width and signedness do not match the indicated
`Ty`), which will cause issues if the obtained discriminant is used in
any way other than as a `SwitchInt` argument.

Fixes #499

---------

Co-authored-by: devops <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P-Token M2 P-token required for supporting P-token code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant