diff --git a/CHANGELOG.md b/CHANGELOG.md index da6e2063ded..14b4c4b91cf 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) +* Throw an error on enum name collisions, previously only one enum would be emitted. + [#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, } }