Skip to content

Commit

Permalink
Unwrap argument types in macro (#3625)
Browse files Browse the repository at this point in the history
  • Loading branch information
snOm3ad committed Sep 20, 2023
1 parent 9ae2a60 commit 4bafdbe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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.
Expand Down
12 changes: 10 additions & 2 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions tests/wasm/macro_rules.rs
Original file line number Diff line number Diff line change
@@ -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]);
1 change: 1 addition & 0 deletions tests/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4bafdbe

Please sign in to comment.