Add support for C-style enums with implicit discriminants#4152
Merged
daxpedda merged 6 commits intowasm-bindgen:mainfrom Oct 10, 2024
Merged
Add support for C-style enums with implicit discriminants#4152daxpedda merged 6 commits intowasm-bindgen:mainfrom
daxpedda merged 6 commits intowasm-bindgen:mainfrom
Conversation
daxpedda
requested changes
Oct 9, 2024
Member
daxpedda
left a comment
There was a problem hiding this comment.
Small nit, otherwise looks great.
Thank you!
daxpedda
approved these changes
Oct 9, 2024
Member
daxpedda
left a comment
There was a problem hiding this comment.
Thanks!
The changelog entry needs to be moved to a new "Unreleased" section.
Contributor
Author
Done :) |
daxpedda
approved these changes
Oct 9, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #2273
Changes:
1. Implicit discriminants
I won't go into the algorithm for determining implicit discriminants, just read the docs. I just want to talk about a few details.
Firstly, while the behavior of implicit discriminants is consistent with rustc, everything would still work correctly even if it wasn't. Since we generate both the Rust and JS/TS enum with all discriminants explicitly specified, there will never a case where Rust and JS/TS disagree on what number a certain variant is. The worst that can happen is that we assign implicit discriminants numbers that users don't expect. (But again, we do it the same way rustc does, so it should be what users expect.)
Secondly, TS enums use the same algorithm for assigning implicit discriminants. The only difference is that TS allows duplicate discriminants while Rust doesn't. So JS/TS devs copying over TS enums with duplicate discriminants will result in a compiler error with and without
#[wasm_bindgen].2. Holes
Since I keep track of all discriminant values for better error messages, I implemented a simpler algorithm for finding holes. It's guaranteed to find the hole with the smallest numeric value and doesn't have issues with arithmetic overflow.
Note that the old algorithm didn't always find the hole with smallest numeric value, so Wasm bindgen might use different holes now. E.g.
enum E { A = 1, B = 3 }previously used the hole 2 but now uses 0. I don't think this is an issue, but I wanted to mention it.