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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
* `console.*()` calls in tests are now always intercepted by default. To show them use `--nocapture`. When shown they are always printed in-place instead of after test results, analogous to `cargo test`.
[#4356](https://github.com/rustwasm/wasm-bindgen/pull/4356)

* Deprecation warning when using an async function as a constructor
[#4402](https://github.com/rustwasm/wasm-bindgen/pull/4402)

### Fixed

- Fixed using [JavaScript keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords) as identifiers not being handled correctly.
Expand Down
5 changes: 5 additions & 0 deletions crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,11 @@ 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
8 changes: 6 additions & 2 deletions 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 Expand Up @@ -79,4 +79,8 @@ impl NonWasmType {
pub fn static_method() {}
}

fn main() {}
fn main() {
async {
RustStruct::new().await;
};
}
8 changes: 8 additions & 0 deletions crates/macro/ui-tests/unsupported-options.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,11 @@ 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
Loading