Skip to content

Commit 6167f66

Browse files
Add a deprecation notice about async constructors (#4402)
1 parent 2405ec2 commit 6167f66

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# `wasm-bindgen` Change Log
22
--------------------------------------------------------------------------------
33

4+
## Unreleased
5+
6+
### Changed
7+
8+
* Deprecation warning when using an async function as a constructor
9+
[#4402](https://github.com/rustwasm/wasm-bindgen/pull/4402)
10+
11+
412
## [0.2.100](https://github.com/rustwasm/wasm-bindgen/compare/0.2.99...0.2.100)
513

614
Released 2025-01-12

crates/backend/src/codegen.rs

+12
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,18 @@ impl TryToTokens for ast::Export {
811811
add_check(quote! {
812812
let _: #wasm_bindgen::__rt::marker::CheckSupportsConstructor<#class>;
813813
});
814+
815+
if self.function.r#async {
816+
(quote_spanned! {
817+
self.function.name_span =>
818+
const _: () = {
819+
#[deprecated(note = "async constructors produce invalid TS code and support will be removed in the future")]
820+
const fn constructor() {}
821+
constructor();
822+
};
823+
})
824+
.to_tokens(into);
825+
}
814826
}
815827
ast::MethodKind::Operation(operation) => match operation.kind {
816828
ast::OperationKind::Getter(_) | ast::OperationKind::Setter(_) => {

crates/macro/ui-tests/unsupported-options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl RustStruct {
1212
pub fn static_method() {}
1313

1414
#[wasm_bindgen(constructor)]
15-
pub fn new() -> Self {
15+
pub async fn new() -> Self {
1616
Self { data: 0 }
1717
}
1818

crates/macro/ui-tests/unsupported-options.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
warning: use of deprecated function `RustStruct::new::{closure#0}::_::constructor`: async constructors produce invalid TS code and support will be removed in the future
2+
--> ui-tests/unsupported-options.rs:15:18
3+
|
4+
15 | pub async fn new() -> Self {
5+
| ^^^
6+
|
7+
= note: `#[warn(deprecated)]` on by default
8+
19
error[E0277]: JavaScript constructors are not supported for `RustEnum`
210
--> ui-tests/unsupported-options.rs:56:12
311
|

tests/wasm/futures.rs

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub struct AsyncStruct;
124124
#[wasm_bindgen]
125125
impl AsyncStruct {
126126
#[wasm_bindgen(constructor)]
127+
#[allow(deprecated)]
127128
pub async fn new() -> AsyncStruct {
128129
AsyncStruct
129130
}

0 commit comments

Comments
 (0)