Skip to content

499 enum support in mir-semantics #521

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 13 commits into from
Apr 10, 2025

Conversation

jberthold
Copy link
Member

@jberthold jberthold commented Apr 4, 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
  • Aggregates for structs and enums now have a correct Ty field (still TyUnknown for tuples, though)
  • RValue::Discriminant is implemented:

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

Comment on lines +675 to +680
syntax Int ::= #lookupDiscriminant ( TypeInfo , VariantIdx ) [function, total]
| #lookupDiscrAux ( Discriminants , VariantIdx ) [function]
// --------------------------------------------------------------------
rule #lookupDiscriminant(typeInfoEnumType(_, _, DISCRIMINANTS), IDX) => #lookupDiscrAux(DISCRIMINANTS, IDX)
requires isInt(#lookupDiscrAux(DISCRIMINANTS, IDX)) [preserves-definedness]
rule #lookupDiscriminant(_OTHER, _) => 0 [owise, preserves-definedness] // default 0. May be undefined behaviour, though.
Copy link
Member Author

Choose a reason for hiding this comment

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

Instead of this default returning 0 , we could signal undefined behaviour - if it was clear what exactly constitutes undefined behaviour here, the discussion on rust-lang/rust#91095 remains unresolved IIUC.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is consistent with miri which is probably good to start with. But I think signalling UB would be good for a future version

@jberthold jberthold force-pushed the 499-enum-support-tmp branch from 4f57b7b to d8f38c3 Compare April 4, 2025 09:24
@jberthold jberthold marked this pull request as ready for review April 4, 2025 09:28
@jberthold jberthold requested review from dkcumming and gtrepta April 4, 2025 09:28
Copy link
Collaborator

@dkcumming dkcumming left a comment

Choose a reason for hiding this comment

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

Jost and I chatted through this on a call and I agree with the reasoning.

Comment on lines +675 to +680
syntax Int ::= #lookupDiscriminant ( TypeInfo , VariantIdx ) [function, total]
| #lookupDiscrAux ( Discriminants , VariantIdx ) [function]
// --------------------------------------------------------------------
rule #lookupDiscriminant(typeInfoEnumType(_, _, DISCRIMINANTS), IDX) => #lookupDiscrAux(DISCRIMINANTS, IDX)
requires isInt(#lookupDiscrAux(DISCRIMINANTS, IDX)) [preserves-definedness]
rule #lookupDiscriminant(_OTHER, _) => 0 [owise, preserves-definedness] // default 0. May be undefined behaviour, though.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is consistent with miri which is probably good to start with. But I think signalling UB would be good for a future version

@automergerpr-permission-manager automergerpr-permission-manager bot merged commit 8029cf1 into master Apr 10, 2025
6 checks passed
@automergerpr-permission-manager automergerpr-permission-manager bot deleted the 499-enum-support-tmp branch April 10, 2025 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for enums
3 participants