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

compilation failure with ambiguous associated item when enum has variant called Error #3677

Closed
mlwilkerson opened this issue Nov 1, 2023 · 2 comments · Fixed by #3678
Closed
Labels

Comments

@mlwilkerson
Copy link
Contributor

mlwilkerson commented Nov 1, 2023

Describe the Bug

Recently, I started seeing an error message during compilation on a simple enum with the #[wasm_bingen] attribute macro. The significant detail about the macro is that it includes a variant named Error.

Here's the compilation error message:

error: ambiguous associated item
   --> tests/wasm/enums.rs:74:1
    |
74  | #[wasm_bindgen]
    | ^^^^^^^^^^^^^^^
    |
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #57644 <https://github.com/rust-lang/rust/issues/57644>
note: `Error` could refer to the variant defined here
   --> tests/wasm/enums.rs:79:5
    |
79  |     Error,
    |     ^^^^^
note: `Error` could also refer to the associated type defined here
   --> /Users/someuser/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/convert/mod.rs:641:5
    |
641 |     type Error;
    |     ^^^^^^^^^^
    = note: `#[deny(ambiguous_associated_items)]` on by default
    = note: this error originates in the attribute macro `wasm_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info)

Steps to Reproduce

  1. create an enum like this:
#[wasm_bindgen]
#[derive(Copy, Clone)]
pub enum EnumWithErrorVariant {
    OK,
    Warning,
    Error,
}
  1. compile it using rust 1.72 or 1.73, with wasm-bindgen 0.2.88

Expected Behavior

Compilation with no errors

Actual Behavior

Compilation error

Prototype

I found that the compilation error was eliminated when changing this codegen:

        impl wasm_bindgen::__rt::core::convert::TryFrom<wasm_bindgen::JsValue>
        for EnumWithErrorVariant {
            type Error = wasm_bindgen::JsValue;
            fn try_from(
                value: wasm_bindgen::JsValue,
            ) -> wasm_bindgen::__rt::std::result::Result<Self, Self::Error> {
        //...snip...
      }

To this:

impl wasm_bindgen::__rt::core::convert::TryFrom<wasm_bindgen::JsValue> for EnumWithErrorVariant {
    type Error = wasm_bindgen::JsValue;
    fn try_from(
        value: wasm_bindgen::JsValue,
    ) -> wasm_bindgen::__rt::std::result::Result<
        Self,
        <EnumWithErrorVariant as TryFrom<JsValue>>::Error,
    > {
//...snip...
}

Notice the Error type in the return Result is <EnumWithErrorVariant as TryFrom<JsValue>>::Error instead of Self::Error

This based on the guidance indicated by the rust issue linked in the error message:
rust-lang/rust#57644

@mlwilkerson
Copy link
Contributor Author

The linked PR includes a reproduction of the bug within this repo's wasm test suite, along with a proposed fix.

@bastibense
Copy link

Ahh, I stumbled across this while porting some code to Rrust. One of the enum values was named Error so the compiler got confused. 😓

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants