Skip to content

Commit

Permalink
Fix ambiguous associated type error when enum has variant called Error (
Browse files Browse the repository at this point in the history
#3678)

* add test enum with Error variant

* add codegen to resolve enum compilation error

* add test coverage for EnumWithErrorVariant
  • Loading branch information
mlwilkerson authored Nov 2, 2023
1 parent 0b5f0ee commit 962f580
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ impl ToTokens for ast::Enum {
type Error = #wasm_bindgen::JsValue;

fn try_from(value: #wasm_bindgen::JsValue)
-> #wasm_bindgen::__rt::std::result::Result<Self, Self::Error> {
-> #wasm_bindgen::__rt::std::result::Result<Self, <#enum_name as #wasm_bindgen::__rt::core::convert::TryFrom<JsValue>>::Error> {
let js = f64::try_from(&value)? as u32;

#wasm_bindgen::__rt::std::result::Result::Ok(
Expand Down
4 changes: 4 additions & 0 deletions tests/wasm/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ exports.js_expect_enum_none = a => {
exports.js_renamed_enum = b => {
assert.strictEqual(wasm.JsRenamedEnum.B, b);
};

exports.js_enum_with_error_variant = () => {
assert.strictEqual(wasm.EnumWithErrorVariant.Error, 2);
};
14 changes: 14 additions & 0 deletions tests/wasm/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" {
fn js_expect_enum(x: Color, y: Option<Color>);
fn js_expect_enum_none(x: Option<Color>);
fn js_renamed_enum(b: RenamedEnum);
fn js_enum_with_error_variant();
}

#[wasm_bindgen]
Expand Down Expand Up @@ -71,6 +72,14 @@ pub fn handle_optional_enums(x: Option<Color>) -> Option<Color> {
x
}

#[wasm_bindgen]
#[derive(Copy, Clone)]
pub enum EnumWithErrorVariant {
OK,
Warning,
Error,
}

#[wasm_bindgen_test]
fn test_optional_enums() {
use self::Color::*;
Expand All @@ -95,3 +104,8 @@ fn test_optional_enum_values() {
fn test_renamed_enum() {
js_renamed_enum(RenamedEnum::B);
}

#[wasm_bindgen_test]
fn test_enum_with_error_variant() {
js_enum_with_error_variant();
}

0 comments on commit 962f580

Please sign in to comment.