From 0259a5c5f405b861d01c136edc4ccea5bae83c88 Mon Sep 17 00:00:00 2001 From: Thomas Etter Date: Tue, 24 Oct 2023 14:54:02 +0200 Subject: [PATCH 1/2] Fix the issue that one could define two enums with the second one overwriting the first one. --- CHANGELOG.md | 3 +++ crates/cli-support/src/js/mod.rs | 2 +- crates/cli-support/src/wit/mod.rs | 11 +++++++++-- crates/cli-support/src/wit/nonstandard.rs | 2 +- tests/wasm/node.js | 16 ++++++++-------- tests/wasm/node.rs | 11 ++++++----- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da6e2063ded..3a4f46b88a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -112,6 +112,9 @@ enhancing type safety and compatibility. [#3647](https://github.com/rustwasm/wasm-bindgen/pull/3647) +* Stop tolerating name collisions on enums, this would previously just emit one of them. + [#3669](https://github.com/rustwasm/wasm-bindgen/pull/3669) + ### Fixed * Fixed `wasm_bindgen` macro to handle raw identifiers in field names. diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index ae3df700d2c..57ff8f14f13 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -2455,7 +2455,7 @@ impl<'a> Context<'a> { pairs.sort_by_key(|(k, _)| *k); check_duplicated_getter_and_setter_names(&pairs)?; - for e in self.aux.enums.iter() { + for (_, e) in self.aux.enums.iter() { self.generate_enum(e)?; } diff --git a/crates/cli-support/src/wit/mod.rs b/crates/cli-support/src/wit/mod.rs index 9e962433f00..ca0211478b8 100644 --- a/crates/cli-support/src/wit/mod.rs +++ b/crates/cli-support/src/wit/mod.rs @@ -852,8 +852,15 @@ impl<'a> Context<'a> { .collect(), generate_typescript: enum_.generate_typescript, }; - self.aux.enums.push(aux); - Ok(()) + let mut result = Ok(()); + self.aux + .enums + .entry(aux.name.clone()) + .and_modify(|existing| { + result = Err(anyhow!("duplicate enums:\n{:?}\n{:?}", existing, aux)); + }) + .or_insert(aux); + result } fn struct_(&mut self, struct_: decode::Struct<'_>) -> Result<(), Error> { diff --git a/crates/cli-support/src/wit/nonstandard.rs b/crates/cli-support/src/wit/nonstandard.rs index fe8142d027e..a9b7f90e599 100644 --- a/crates/cli-support/src/wit/nonstandard.rs +++ b/crates/cli-support/src/wit/nonstandard.rs @@ -41,7 +41,7 @@ pub struct WasmBindgenAux { /// Auxiliary information to go into JS/TypeScript bindings describing the /// exported enums from Rust. - pub enums: Vec, + pub enums: HashMap, /// Auxiliary information to go into JS/TypeScript bindings describing the /// exported structs from Rust and their fields they've got exported. diff --git a/tests/wasm/node.js b/tests/wasm/node.js index 41baa8b240d..b386567315f 100644 --- a/tests/wasm/node.js +++ b/tests/wasm/node.js @@ -24,14 +24,14 @@ exports.test_works = function() { assert.strictEqual(r2.add(2), 13); r2.free(); - assert.strictEqual(wasm.Color.Green, 0); - assert.strictEqual(wasm.Color.Yellow, 1); - assert.strictEqual(wasm.Color.Red, 2); - assert.strictEqual(wasm.Color[0], 'Green'); - assert.strictEqual(wasm.Color[1], 'Yellow'); - assert.strictEqual(wasm.Color[2], 'Red'); - assert.strictEqual(Object.keys(wasm.Color).length, 6); - assert.strictEqual(wasm.cycle(wasm.Color.Green), wasm.Color.Yellow); + assert.strictEqual(wasm.NodeColor.Green, 0); + assert.strictEqual(wasm.NodeColor.Yellow, 1); + assert.strictEqual(wasm.NodeColor.Red, 2); + assert.strictEqual(wasm.NodeColor[0], 'Green'); + assert.strictEqual(wasm.NodeColor[1], 'Yellow'); + assert.strictEqual(wasm.NodeColor[2], 'Red'); + assert.strictEqual(Object.keys(wasm.NodeColor).length, 6); + assert.strictEqual(wasm.cycle(wasm.NodeColor.Green), wasm.NodeColor.Yellow); wasm.node_math(1.0, 2.0); }; diff --git a/tests/wasm/node.rs b/tests/wasm/node.rs index 742dbcfb5e2..8f57e1731d1 100644 --- a/tests/wasm/node.rs +++ b/tests/wasm/node.rs @@ -34,18 +34,19 @@ impl Foo { } } +// Use a different name to avoid a collision with the `Color` enum in enums.rs when --no-modules is used. #[wasm_bindgen] -pub enum Color { +pub enum NodeColor { Green, Yellow, Red, } #[wasm_bindgen] -pub fn cycle(color: Color) -> Color { +pub fn cycle(color: NodeColor) -> NodeColor { match color { - Color::Green => Color::Yellow, - Color::Yellow => Color::Red, - Color::Red => Color::Green, + NodeColor::Green => NodeColor::Yellow, + NodeColor::Yellow => NodeColor::Red, + NodeColor::Red => NodeColor::Green, } } From 3107e0e8aac9972c53de4b0754ec509a808a8dd6 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Thu, 26 Oct 2023 10:57:48 +0200 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a4f46b88a6..14b4c4b91cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -112,7 +112,7 @@ enhancing type safety and compatibility. [#3647](https://github.com/rustwasm/wasm-bindgen/pull/3647) -* Stop tolerating name collisions on enums, this would previously just emit one of them. +* Throw an error on enum name collisions, previously only one enum would be emitted. [#3669](https://github.com/rustwasm/wasm-bindgen/pull/3669) ### Fixed