-
Notifications
You must be signed in to change notification settings - Fork 4
Transmute integer to fieldless enum #840
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
base: master
Are you sure you want to change the base?
Conversation
This test should fail the test suite as it thunks, but doesn't due to a hack rule that was introduced. This rule stops the thunk from occurring.
- Corrected rules to check discriminant is valid; - Aggregate returned uses the discriminant to look up the variant; - Added error condition to prevent thunk if discriminant is invalid; - Added fail and passing tests;
- `#cast` uses `truncate` similar to `SwitchInt`; - Added test cases with negative discriminant; - Added test cases with positive discriminant and negative value; - Signed discriminants test fails but for unrelated reason I believe.
jberthold
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pre-approving, but please change the description text
| Another specialisation is getting the discriminant of `enum`s without fields after converting some integer data to it | ||
| (see `#discriminant` and `rvalueDiscriminant`). | ||
| If none of the `enum` variants has any fields, the `Transmute` of a number to the `enum` data is necessarily just the discriminant itself., and can be returned as the integer value afgter adjusting to the byte length of the discriminant: | ||
| If none of the `enum` variants has any fields, the `Transmute` of a number to the `enum` data is necessarily just the discriminant itself., and can be returned as the integer value after adjusting to the byte length of the discriminant: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That description still refers to the thunk-removing rule, should be updated.
| │ (100 steps) | ||
| └─ 3 (stuck, leaf) | ||
| #traverseProjection ( toLocal ( 2 ) , thunk ( #decodeConstant ( constantKindAllo | ||
| span: 153 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should look into why this is failing. My guess is that the decoding code is not ready for signed discriminants.
This PR implements semantics to cast (transmute) an integer into an enum if that enum is known to have no fields in it's variants. Some things to consider that influence this PR:
u8 -> i8fine,u8 -> u16not fine);u128in the type data even if they are unsigned at the source level;The combination of these points mean that the approach to soundly casting an integer into an enum is to treat the incoming integer as unsigned (converting if signed), and check if that value is in the discriminants. If yes, follow to the corresponding variant position; if not return
#UBErrorInvalidDisciminantsInEnumCast.The added code has meant a work around with retrieving the discriminant of a thunked cast can be removed.
All added tests that should pass do except for one which is failing, but I am unsure what the reason is. I would like some feedback on that.