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

#[wasm_bindgen] fails if an enum variant has a #[cfg(xxx)] that evaluates to false #2058

Open
0xd4d opened this issue Mar 26, 2020 · 3 comments
Labels

Comments

@0xd4d
Copy link

0xd4d commented Mar 26, 2020

#[wasm_bindgen] fails if an enum variant has a #[cfg(xxx)] that evaluates to false

Same thing with structs.

wasm-bindgen = "0.2.59"

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub enum MyEnum {
	#[cfg(feature = "zero")]
	Zero,
}

#[wasm_bindgen]
pub struct MyStruct {
    #[cfg(feature = "field")]
    pub field: u32,
}

Errors:

  • error[E0605]: non-primitive cast: MyEnum as u32
  • error[E0599]: no variant or associated item named Zero found for enum MyEnum in the current scope
  • error[E0609]: no field field on type wasm_bindgen::__rt::Ref<'_, MyStruct>
error[E0605]: non-primitive cast: `MyEnum` as `u32`
 --> src\lib.rs:3:1
  |
3 | #[wasm_bindgen]
  | ^^^^^^^^^^^^^^^
  |
  = note: an `as` expression can only be used to convert between primitive types. Consider using the `From` trait

error[E0599]: no variant or associated item named `Zero` found for enum `MyEnum` in the current scope
 --> src\lib.rs:6:2
  |
4 | pub enum MyEnum {
  | --------------- variant or associated item `Zero` not found here
5 |     #[cfg(feature = "zero")]
6 |     Zero,
  |     ^^^^ variant or associated item not found in `MyEnum`

error[E0609]: no field `field` on type `wasm_bindgen::__rt::Ref<'_, MyStruct>`
  --> src\lib.rs:12:9
   |
12 |     pub field: u32,
   |         ^^^^^ unknown field

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0599, E0605, E0609.
For more information about an error, try `rustc --explain E0599`.
@0xd4d 0xd4d added the bug label Mar 26, 2020
@alexcrichton
Copy link
Contributor

Thanks for the report! This'll probably require some macro-fu to make sure that the #[cfg] on each variant/fields gets to the right locations, since right now we'll probably keep the attribute on the field but we won't apply the attribute to all code that handles the field. The best workaround for now would be to put #[cfg] on the enum itself and define the enum twice, but I know that's unfortunately not great :(

@jbr
Copy link

jbr commented Apr 13, 2022

Is this issue up to date? Redefining the enum multiple times isn't easily typed when there are a lot of enum variants that need to be feature-configured. Is there another workaround? How hard would this be to take on as a first wasm-bindgen issue?

@jbr
Copy link

jbr commented Apr 18, 2022

I believe this might be resolved by rust-lang/rust#82679

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

No branches or pull requests

2 participants