diff --git a/CHANGELOG.md b/CHANGELOG.md index f144ab366c6..3709011f7ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,9 @@ * `console.*()` calls in tests are now always intercepted by default. To show them use `--nocapture`. When shown they are always printed in-place instead of after test results, analogous to `cargo test`. [#4356](https://github.com/rustwasm/wasm-bindgen/pull/4356) +* Deprecation warning when using an async function as a constructor + [#4402](https://github.com/rustwasm/wasm-bindgen/pull/4402) + ### Fixed - Fixed using [JavaScript keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords) as identifiers not being handled correctly. diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index 46c76a1ef21..a10d823b3b3 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -1418,6 +1418,11 @@ impl MacroParse<&ClassMarker> for &mut syn::ImplItemFn { FunctionPosition::Impl { self_ty: class }, )?; let method_kind = if opts.constructor().is_some() { + if function.r#async { + self.attrs.push(syn::parse_quote! { + #[deprecated(note = "constructors cannot be async")] + }); + } ast::MethodKind::Constructor } else { let is_static = method_self.is_none(); diff --git a/crates/macro/ui-tests/unsupported-options.rs b/crates/macro/ui-tests/unsupported-options.rs index 1589027f30c..2d5ba51b5fa 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 } } @@ -79,4 +79,8 @@ impl NonWasmType { pub fn static_method() {} } -fn main() {} +fn main() { + async { + RustStruct::new().await; + }; +} diff --git a/crates/macro/ui-tests/unsupported-options.stderr b/crates/macro/ui-tests/unsupported-options.stderr index 7a6dcd70537..cecba3a4b18 100644 --- a/crates/macro/ui-tests/unsupported-options.stderr +++ b/crates/macro/ui-tests/unsupported-options.stderr @@ -93,3 +93,11 @@ note: required by a bound in `CheckSupportsStaticProperty` | pub struct CheckSupportsStaticProperty(T); | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `CheckSupportsStaticProperty` = note: this error originates in the attribute macro `wasm_bindgen::prelude::__wasm_bindgen_class_marker` which comes from the expansion of the attribute macro `wasm_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: use of deprecated associated function `RustStruct::new`: constructors cannot be async + --> ui-tests/unsupported-options.rs:84:21 + | +84 | RustStruct::new().await; + | ^^^ + | + = note: `#[warn(deprecated)]` on by default