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

Conversation

danielronnkvist
Copy link
Contributor

Adds a deprecation attribute to the method marked as constructor if it is async.

Appreciate suggestions on wording in the deprecation notice.

Resolves #3976

Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test in crates/macro/ui-tests. We can take a second look at the wording then.

EDIT: A changelog entry is missing as well.

Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, unfortunately this doesn't seem to look right after all. Sorry!
The problem is we need a deprecation warning at the definition site, not the call site. Because realistically the call size will be in JS, not Rust.

This should work:

diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs
index c8decb4a1..19bc75afd 100644
--- a/crates/backend/src/codegen.rs
+++ b/crates/backend/src/codegen.rs
@@ -806,6 +806,14 @@ 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 =>
+                            let async_constructors_deprecated: ();
+                        })
+                        .to_tokens(into);
+                    }
                 }
                 ast::MethodKind::Operation(operation) => match operation.kind {
                     ast::OperationKind::Getter(_) | ast::OperationKind::Setter(_) => {

I wanted to push this directly but didn't have permissions, but I can open a PR in your repo if this makes things easier.

@daxpedda daxpedda added the waiting for author Waiting for author to respond label Jan 12, 2025
@danielronnkvist
Copy link
Contributor Author

@daxpedda I see, good point! But what do you think about still using the deprecated attribute? In my opinion, that outputs a better type of warning for this scenario than an unused variable.

diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs
index c8decb4a..e8ff0bcf 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(_) => {

@daxpedda
Copy link
Collaborator

Yes, definitely better!

Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the outstanding issue it LGTM!
Thank you again!

tests/wasm/futures.rs Show resolved Hide resolved
crates/macro/ui-tests/unsupported-options.rs Outdated Show resolved Hide resolved
crates/backend/src/codegen.rs Outdated Show resolved Hide resolved
crates/backend/src/codegen.rs Outdated Show resolved Hide resolved
@danielronnkvist danielronnkvist force-pushed the fail-async-constructors branch from df5a8bf to d0a68b6 Compare January 13, 2025 11:58
CHANGELOG.md Outdated Show resolved Hide resolved
@danielronnkvist danielronnkvist force-pushed the fail-async-constructors branch from d0a68b6 to 303fbf0 Compare January 13, 2025 13:42
Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@daxpedda daxpedda merged commit 6167f66 into rustwasm:main Jan 13, 2025
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for author Waiting for author to respond
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disallow async constructors
2 participants