Skip to content

Return an error if there are two enums with the same name#3669

Merged
daxpedda merged 2 commits intowasm-bindgen:mainfrom
thomasetter:enum-collision-fix
Oct 26, 2023
Merged

Return an error if there are two enums with the same name#3669
daxpedda merged 2 commits intowasm-bindgen:mainfrom
thomasetter:enum-collision-fix

Conversation

@thomasetter
Copy link
Copy Markdown
Contributor

This fixes the issue that two enums with the same name cause the "second" one to overwrite the "first" one.

Currently, looking at the output of target/wasm32-unknown-unknown/wbg-tmp-wasm-<some-hex-characters>.wasm/wasm-bindgen-test.js generated with

WASM_BINDGEN_SPLIT_LINKED_MODULES=1 cargo test --target wasm32-unknown-unknown

the part with the enums (was at line 4849 for me) looks like this:

**
*/
module.exports.Color = Object.freeze({ Green:0,"0":"Green",Yellow:1,"1":"Yellow",Red:2,"2":"Red", });
/**
*/
module.exports.JsRenamedEnum = Object.freeze({ A:10,"10":"A",B:20,"20":"B", });
/**
*/
module.exports.EnumArrayElement = Object.freeze({ Unit:0,"0":"Unit", });
/**
* annotated enum type
*/
module.exports.AnnotatedEnum = Object.freeze({
/**
* annotated enum variant 1
*/
Variant1:0,"0":"Variant1",
/**
* annotated enum variant 2
*/
Variant2:1,"1":"Variant2", });
/**
*/
module.exports.Color = Object.freeze({ Green:0,"0":"Green",Yellow:1,"1":"Yellow",Red:2,"2":"Red", });
/**
*/
module.exports.ColorWithCustomValues = Object.freeze({ Green:21,"21":"Green",Yellow:34,"34":"Yellow",Red:2,"2":"Red", });
/**
*/
module.exports.MyEnum = Object.freeze({ One:1,"1":"One",Two:2,"2":"Two", });

The second assignment module.exports.Color = overwrites the first one, causing the first one to have no effect.

The reason this was not captured by the tests is that the two enums were identical in shape (same unit keys/values).

Copy link
Copy Markdown
Member

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

LGTM!
Could you add a note in the changelog as well?

@thomasetter
Copy link
Copy Markdown
Contributor Author

Thanks for the quick review, added a note in the changelog under Changed.

Comment thread CHANGELOG.md Outdated
@daxpedda daxpedda merged commit 54f22ee into wasm-bindgen:main Oct 26, 2023
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