diff --git a/CHANGELOG.md b/CHANGELOG.md index 02f0bcffcb3..954601768dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,6 +126,9 @@ proc-macro. [#3601](https://github.com/rustwasm/wasm-bindgen/pull/3601) +* Fix bug with function arguments coming from `macro_rules!`. + [#3625](https://github.com/rustwasm/wasm-bindgen/pull/3625) + ### Removed * Removed `ReadableStreamByobReader::read_with_u8_array()` because it doesn't work with Wasm. diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index e45d4e50ba1..a65418fdf86 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -523,8 +523,16 @@ impl TryToTokens for ast::Export { argtys.push(&*arg.ty); let i = i + offset; let ident = Ident::new(&format!("arg{}", i), Span::call_site()); - let ty = &arg.ty; - match &*arg.ty { + fn unwrap_nested_types(ty: &syn::Type) -> &syn::Type { + match &ty { + syn::Type::Group(syn::TypeGroup { ref elem, .. }) => unwrap_nested_types(elem), + syn::Type::Paren(syn::TypeParen { ref elem, .. }) => unwrap_nested_types(elem), + _ => ty, + } + } + let ty = unwrap_nested_types(&arg.ty); + + match &ty { syn::Type::Reference(syn::TypeReference { mutability: Some(_), elem, diff --git a/tests/wasm/macro_rules.rs b/tests/wasm/macro_rules.rs new file mode 100644 index 00000000000..42c4b2a1d1d --- /dev/null +++ b/tests/wasm/macro_rules.rs @@ -0,0 +1,12 @@ +//! This tests that the `wasm_bindgen` macro produces code that compiles for this use case. +//! `cargo test --target wasm32-unknown-unknown` will not run if this test breaks. +use wasm_bindgen::prelude::*; + +macro_rules! my_export { + ($i: ident, $s: ty) => { + #[wasm_bindgen] + pub fn $i(_: $s) {} + }; +} + +my_export!(should_compile, &[i32]); diff --git a/tests/wasm/main.rs b/tests/wasm/main.rs index eebde19677e..30ea94b422d 100644 --- a/tests/wasm/main.rs +++ b/tests/wasm/main.rs @@ -35,6 +35,7 @@ pub mod js_keywords; pub mod js_objects; pub mod jscast; pub mod link_to; +pub mod macro_rules; pub mod math; pub mod no_shims; pub mod node;