-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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] attribute not work in macro_rules #3707
Comments
This is very strange. |
Hello, I'm also experiencing this issue. SummaryI defined a simple macro rule as below, which just adds a line macro_rules! with_wasm {
($name:ident {$field:ident: $t:ty}) => {
#[wasm_bindgen::prelude::wasm_bindgen]
pub struct $name {
pub $field: $t
}
}
} This macro simply converts with_wasm!(AStruct {
a_field: u8
}); to #[wasm_bindgen::prelude::wasm_bindgen]
pub struct AStruct {
pub a_field: u8,
} While the expanded code (the below) passes
Possible hint to solve this issueWhen I replace macro_rules! with_wasm {
($name:ident {$field:ident: $t:ty}) => {
#[wasm_bindgen::prelude::wasm_bindgen]
pub struct $name {
// Previously `pub $field: $t`
pub a_field: $t
}
}
} This suggests something weird happenning on macro expansion. especially field name evaluation. Possible hint to solve this issue 2When I remove macro_rules! with_wasm {
($name:ident {$field:ident: $t:ty}) => {
#[wasm_bindgen::prelude::wasm_bindgen]
pub struct $name {
// Previously `pub $field: $t`
$field: $t
}
}
} |
@ChiaMineJP thank you for your solutions, however they won't work for me :). I expect the field of the struct to be customizable, and exported to the js side, thus |
Hi @sshwy For example, the fact that replacing |
Update: I tried to compare token output of (I cloned this As I wrote in my comment above, replacing This suggests that there might be a bug in rust compiler and not in |
@sshwy The idea is to let some custom attribute add Please clone my repository to try my solution. |
Fixes rustwasm#3707 Previously, `(*js).borrow().#rust_name` was being given the span of `rust_name`, which seems like a fairly harmless thing to do at first glance. However, it turns out that the span of a token also affects its hygiene - turns out proc macros have hygiene too, not just declarative macros! This caused a problem because the declaration of `js` had a span of `Span::call_site()`, but it was being accessed with `rust_name`'s span, which might be a different context where `js` doesn't exist. Usually this is fine because `Span::call_site()` is in the same context as the struct definition: normal code. However, when you put `#[wasm_bindgen]` inside a `macro_rules!`, `Span::call_site()` is now in the context of that `macro_rules!`, while `rust_name`'s span is still in normal code which can't access variables from inside `macro_rules!`. To fix that I've just removed the span from `(*js).borrow().#rust_name`, making it `Span::call_site()`. I don't think this should have any effect on diagnostics anyway, since I don't see how that code could ever cause a compile error.
Fixes rustwasm#3707 Previously, `(*js).borrow().#rust_name` was being given the span of `rust_name`, which seems like a fairly harmless thing to do at first glance. However, it turns out that the span of a token also affects its hygiene - turns out proc macros have hygiene too, not just declarative macros! This caused a problem because the declaration of `js` had a span of `Span::call_site()`, but it was being accessed with `rust_name`'s span, which might be a different context where `js` doesn't exist. Usually this is fine because `Span::call_site()` is in the same context as the struct definition: normal code. However, when you put `#[wasm_bindgen]` inside a `macro_rules!`, `Span::call_site()` is now in the context of that `macro_rules!`, while `rust_name`'s span is still in normal code which can't access variables from inside `macro_rules!`. To fix that I've just removed the span from `(*js).borrow().#rust_name`, making it `Span::call_site()`. I don't think this should have any effect on diagnostics anyway, since I don't see how that code could ever cause a compile error.
Describe the Bug
When putting struct attributed
#[wasm_bindgen]
in a custom macro_rules, it throws errorcannot find value 'js' in this scope
.Steps to Reproduce
wasm_bindgen version:
0.2.88
.code snippet:
Expected Behavior
In my imagination, macro rules should be expanded before the proc macros, thus the above code should work as normal.
Correct me if I'm wrong.
The text was updated successfully, but these errors were encountered: