Skip to content
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

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

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
[#3669](https://github.com/rustwasm/wasm-bindgen/pull/3669)

### Fixed

* Fixed `wasm_bindgen` macro to handle raw identifiers in field names.
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}

Expand Down
11 changes: 9 additions & 2 deletions crates/cli-support/src/wit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli-support/src/wit/nonstandard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct WasmBindgenAux {

/// Auxiliary information to go into JS/TypeScript bindings describing the
/// exported enums from Rust.
pub enums: Vec<AuxEnum>,
pub enums: HashMap<String, AuxEnum>,

/// Auxiliary information to go into JS/TypeScript bindings describing the
/// exported structs from Rust and their fields they've got exported.
Expand Down
16 changes: 8 additions & 8 deletions tests/wasm/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
11 changes: 6 additions & 5 deletions tests/wasm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down