From b64aadb8a9702ea92bac2c086c9b2fe39cb96031 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20R=C3=B6nnkvist?= Date: Mon, 13 Jan 2025 10:34:49 +0100 Subject: [PATCH] Move deprecation warning to function definition Update tests to work with the new deprecation --- crates/backend/src/codegen.rs | 10 ++++++++++ crates/macro-support/src/parser.rs | 5 ----- crates/macro/ui-tests/unsupported-options.stderr | 16 ++++++++-------- tests/wasm/futures.js | 2 +- tests/wasm/futures.rs | 1 - 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index c8decb4a136..8ae6b738c00 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -806,6 +806,16 @@ 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 => + #[deprecated(note = "async constructors are not supported")] + fn constructor() {} + constructor(); + }) + .to_tokens(into); + } } ast::MethodKind::Operation(operation) => match operation.kind { ast::OperationKind::Getter(_) | ast::OperationKind::Setter(_) => { diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index a10d823b3b3..46c76a1ef21 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -1418,11 +1418,6 @@ 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.stderr b/crates/macro/ui-tests/unsupported-options.stderr index cecba3a4b18..0ec899efad7 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 are not supported + --> 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 | @@ -93,11 +101,3 @@ 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 diff --git a/tests/wasm/futures.js b/tests/wasm/futures.js index b9560866071..127beb2f528 100644 --- a/tests/wasm/futures.js +++ b/tests/wasm/futures.js @@ -16,7 +16,7 @@ exports.call_exports = async function() { await assert.rejects(wasm.async_throw_jserror(), /async message/); await assert.rejects(wasm.async_throw_custom_error(), /custom error/); assert.strictEqual("Hi, Jim!", await wasm.async_take_reference("Jim")); - const foo = await new wasm.AsyncStruct(); + const foo = await wasm.AsyncStruct.new(); assert.strictEqual(42, await foo.method()); await wasm.async_take_js_reference(42); const buffer = new Int32Array([1, 2, 3, 4]); diff --git a/tests/wasm/futures.rs b/tests/wasm/futures.rs index 73b51dd06d3..c50722e1cad 100644 --- a/tests/wasm/futures.rs +++ b/tests/wasm/futures.rs @@ -123,7 +123,6 @@ pub struct AsyncStruct; #[wasm_bindgen] impl AsyncStruct { - #[wasm_bindgen(constructor)] pub async fn new() -> AsyncStruct { AsyncStruct }