Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a deprecation notice about async constructors #4402

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/macro/ui-tests/unsupported-options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
8 changes: 8 additions & 0 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 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
|
Expand Down
1 change: 1 addition & 0 deletions tests/wasm/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub struct AsyncStruct;
#[wasm_bindgen]
impl AsyncStruct {
#[wasm_bindgen(constructor)]
daxpedda marked this conversation as resolved.
Show resolved Hide resolved
#[allow(deprecated)]
pub async fn new() -> AsyncStruct {
AsyncStruct
}
Expand Down
Loading