diff --git a/CHANGELOG.md b/CHANGELOG.md index 08dcd104a0d..79a517578cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ # `wasm-bindgen` Change Log -------------------------------------------------------------------------------- +## Unreleased + +### Changed + +* Deprecation warning when using an async function as a constructor + [#4402](https://github.com/rustwasm/wasm-bindgen/pull/4402) + + ## [0.2.100](https://github.com/rustwasm/wasm-bindgen/compare/0.2.99...0.2.100) Released 2025-01-12 diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index 01450ccb9df..73056b70390 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -811,6 +811,18 @@ impl TryToTokens for ast::Export { add_check(quote! { let _: #wasm_bindgen::__rt::marker::CheckSupportsConstructor<#class>; }); + + if self.function.r#async { + (quote_spanned! { + self.function.name_span => + const _: () = { + #[deprecated(note = "async constructors produce invalid TS code and support will be removed in the future")] + const fn constructor() {} + constructor(); + }; + }) + .to_tokens(into); + } } ast::MethodKind::Operation(operation) => match operation.kind { ast::OperationKind::Getter(_) | ast::OperationKind::Setter(_) => { diff --git a/crates/macro/ui-tests/unsupported-options.rs b/crates/macro/ui-tests/unsupported-options.rs index 1589027f30c..333288d63ac 100644 --- a/crates/macro/ui-tests/unsupported-options.rs +++ b/crates/macro/ui-tests/unsupported-options.rs @@ -12,7 +12,7 @@ impl RustStruct { pub fn static_method() {} #[wasm_bindgen(constructor)] - pub fn new() -> Self { + pub async fn new() -> Self { Self { data: 0 } } diff --git a/crates/macro/ui-tests/unsupported-options.stderr b/crates/macro/ui-tests/unsupported-options.stderr index 7a6dcd70537..c9ecd30ff01 100644 --- a/crates/macro/ui-tests/unsupported-options.stderr +++ b/crates/macro/ui-tests/unsupported-options.stderr @@ -1,3 +1,11 @@ +warning: use of deprecated function `RustStruct::new::{closure#0}::_::constructor`: async constructors produce invalid TS code and support will be removed in the future + --> ui-tests/unsupported-options.rs:15:18 + | +15 | pub async fn new() -> Self { + | ^^^ + | + = note: `#[warn(deprecated)]` on by default + error[E0277]: JavaScript constructors are not supported for `RustEnum` --> ui-tests/unsupported-options.rs:56:12 | diff --git a/tests/wasm/futures.rs b/tests/wasm/futures.rs index 73b51dd06d3..ddf364526d7 100644 --- a/tests/wasm/futures.rs +++ b/tests/wasm/futures.rs @@ -124,6 +124,7 @@ pub struct AsyncStruct; #[wasm_bindgen] impl AsyncStruct { #[wasm_bindgen(constructor)] + #[allow(deprecated)] pub async fn new() -> AsyncStruct { AsyncStruct }