Skip to content

Commit 4bafdbe

Browse files
authored
Unwrap argument types in macro (#3625)
1 parent 9ae2a60 commit 4bafdbe

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
proc-macro.
130130
[#3601](https://github.com/rustwasm/wasm-bindgen/pull/3601)
131131

132+
* Fix bug with function arguments coming from `macro_rules!`.
133+
[#3625](https://github.com/rustwasm/wasm-bindgen/pull/3625)
134+
132135
### Removed
133136

134137
* Removed `ReadableStreamByobReader::read_with_u8_array()` because it doesn't work with Wasm.

crates/backend/src/codegen.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,16 @@ impl TryToTokens for ast::Export {
523523
argtys.push(&*arg.ty);
524524
let i = i + offset;
525525
let ident = Ident::new(&format!("arg{}", i), Span::call_site());
526-
let ty = &arg.ty;
527-
match &*arg.ty {
526+
fn unwrap_nested_types(ty: &syn::Type) -> &syn::Type {
527+
match &ty {
528+
syn::Type::Group(syn::TypeGroup { ref elem, .. }) => unwrap_nested_types(elem),
529+
syn::Type::Paren(syn::TypeParen { ref elem, .. }) => unwrap_nested_types(elem),
530+
_ => ty,
531+
}
532+
}
533+
let ty = unwrap_nested_types(&arg.ty);
534+
535+
match &ty {
528536
syn::Type::Reference(syn::TypeReference {
529537
mutability: Some(_),
530538
elem,

tests/wasm/macro_rules.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! This tests that the `wasm_bindgen` macro produces code that compiles for this use case.
2+
//! `cargo test --target wasm32-unknown-unknown` will not run if this test breaks.
3+
use wasm_bindgen::prelude::*;
4+
5+
macro_rules! my_export {
6+
($i: ident, $s: ty) => {
7+
#[wasm_bindgen]
8+
pub fn $i(_: $s) {}
9+
};
10+
}
11+
12+
my_export!(should_compile, &[i32]);

tests/wasm/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod js_keywords;
3535
pub mod js_objects;
3636
pub mod jscast;
3737
pub mod link_to;
38+
pub mod macro_rules;
3839
pub mod math;
3940
pub mod no_shims;
4041
pub mod node;

0 commit comments

Comments
 (0)