diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index bbcbc9cea14..36fe3026b72 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -678,6 +678,14 @@ impl TryToTokens for ast::Export { }; let nargs = self.function.arguments.len() as u32; let attrs = &self.function.rust_attrs; + let safe_attrs = attrs + .iter() + .filter(|a| { + let path = a.path(); + path.is_ident("cfg") || path.is_ident("cfg_attr") + }) + .cloned() + .collect::>(); let start_check = if self.start { quote! { const _ASSERT: fn() = || -> #projection::Abi { loop {} }; } @@ -688,7 +696,7 @@ impl TryToTokens for ast::Export { (quote! { #[automatically_derived] const _: () = { - #(#attrs)* + #(#safe_attrs)* #[cfg_attr( all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))), export_name = #export_name, @@ -745,7 +753,7 @@ impl TryToTokens for ast::Export { #describe_args #describe_ret }, - attrs: attrs.clone(), + attrs: safe_attrs, wasm_bindgen: &self.wasm_bindgen, } .to_tokens(into); diff --git a/tests/wasm/attributes.js b/tests/wasm/attributes.js new file mode 100644 index 00000000000..39a1dd01988 --- /dev/null +++ b/tests/wasm/attributes.js @@ -0,0 +1,9 @@ +const wasm = require("wasm-bindgen-test.js"); +const assert = require("assert"); + +exports.js_works = () => { + assert.strictEqual(wasm.valid_export(), true); + assert.strictEqual(wasm.invalid_export, undefined); + assert.strictEqual(wasm.valid_attr_export(), true); + assert.strictEqual(wasm.invalid_attr_export, undefined); +}; diff --git a/tests/wasm/attributes.rs b/tests/wasm/attributes.rs new file mode 100644 index 00000000000..bfc695c04d5 --- /dev/null +++ b/tests/wasm/attributes.rs @@ -0,0 +1,36 @@ +use wasm_bindgen::prelude::*; +use wasm_bindgen_test::*; + +#[wasm_bindgen(module = "tests/wasm/attributes.js")] +extern "C" { + fn js_works(); +} + +#[wasm_bindgen_test] +fn works() { + js_works(); +} + +#[wasm_bindgen] +#[cfg(target_arch = "wasm32")] +pub fn valid_export() -> bool { + true +} + +#[wasm_bindgen] +#[cfg(not(target_arch = "wasm32"))] +pub fn invalid_export() -> bool { + false +} + +#[wasm_bindgen] +#[cfg_attr(not(target_arch = "wasm32"), cfg(feature = "missing-feature"))] +pub fn valid_attr_export() -> bool { + true +} + +#[wasm_bindgen] +#[cfg_attr(target_arch = "wasm32", cfg(feature = "missing-feature"))] +pub fn invalid_attr_export() -> bool { + false +} diff --git a/tests/wasm/main.rs b/tests/wasm/main.rs index 72bb5c414f4..22ed2dfd48f 100644 --- a/tests/wasm/main.rs +++ b/tests/wasm/main.rs @@ -16,6 +16,7 @@ use wasm_bindgen::prelude::*; pub mod api; pub mod arg_names; +pub mod attributes; pub mod bigint; pub mod char; pub mod classes;