Skip to content

Commit

Permalink
Move deprecation warning to function definition
Browse files Browse the repository at this point in the history
Update tests to work with the new deprecation
  • Loading branch information
danielronnkvist committed Jan 13, 2025
1 parent bb07a33 commit b64aadb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
10 changes: 10 additions & 0 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand Down
5 changes: 0 additions & 5 deletions crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions crates/macro/ui-tests/unsupported-options.stderr
Original file line number Diff line number Diff line change
@@ -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
|
Expand Down Expand Up @@ -93,11 +101,3 @@ note: required by a bound in `CheckSupportsStaticProperty`
| pub struct CheckSupportsStaticProperty<T: SupportsStaticProperty>(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
2 changes: 1 addition & 1 deletion tests/wasm/futures.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
1 change: 0 additions & 1 deletion tests/wasm/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ pub struct AsyncStruct;

#[wasm_bindgen]
impl AsyncStruct {
#[wasm_bindgen(constructor)]
pub async fn new() -> AsyncStruct {
AsyncStruct
}
Expand Down

0 comments on commit b64aadb

Please sign in to comment.