Unwrap argument types in macro (#3625)

This commit is contained in:
Oliver 2023-09-20 16:57:15 -04:00 committed by GitHub
parent 9ae2a606e1
commit 4bafdbebcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 2 deletions

View File

@ -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.

View File

@ -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,

12
tests/wasm/macro_rules.rs Normal file
View File

@ -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]);

View File

@ -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;