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

Fix: Inline auto curried async functions produce invalid javascript #6010

Merged
merged 1 commit into from
Feb 19, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ These are only breaking changes for unformatted code.
- GenType: fix issue with V3 compatibility mode (see https://github.com/rescript-lang/rescript-compiler/issues/5990) https://github.com/rescript-lang/rescript-compiler/pull/5991
- Fix issue in `Js.Promise2` where `then` and `catch` were returning `undefined` https://github.com/rescript-lang/rescript-compiler/pull/5997
- Fix formatting of props spread for multiline JSX expression https://github.com/rescript-lang/rescript-compiler/pull/6006
- Fix issue in the compiler back-end where async functions passed to an `@uncurry` external would be inlined and transformed in a way that loses async https://github.com/rescript-lang/rescript-compiler/pull/6010

#### :nail_care: Polish

Expand Down
9 changes: 6 additions & 3 deletions jscomp/core/lam_eta_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,16 @@ let unsafe_adjust_to_arity loc ~(to_ : int) ?(from : int option) (fn : Lam.t) :
let ap_info : Lam.ap_info =
{ ap_loc = loc; ap_inlined = Default_inline; ap_status = App_na }
in
let is_async_fn = match fn with
| Lfunction { attr = {async}} -> async
| _ -> false in
match (from, fn) with
| Some from, _ | None, Lfunction { arity = from } -> (
if from = to_ then fn
if from = to_ || is_async_fn then fn
else if to_ = 0 then
match fn with
| Lfunction { params = [ param ]; body; attr = {async} } ->
Lam.function_ ~arity:0 ~attr:{Lambda.default_function_attribute with async}
| Lfunction { params = [ param ]; body } ->
Lam.function_ ~arity:0 ~attr:Lambda.default_function_attribute
~params:[]
~body:(Lam.let_ Alias param Lam.unit body)
(* could be only introduced by
Expand Down
8 changes: 8 additions & 0 deletions jscomp/test/async_inline.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var Curry = require("../../lib/js/curry.js");
var React = require("react");

async function willBeInlined(param) {
return 3;
Expand Down Expand Up @@ -72,6 +73,12 @@ async function nested2(param) {
};
}

function onSubmit(param) {
return React.useCallback(async function (_a, b) {
return await b;
});
}

var tci = 3;

exports.willBeInlined = willBeInlined;
Expand All @@ -90,4 +97,5 @@ exports.tui = tui;
exports.tuia = tuia;
exports.nested1 = nested1;
exports.nested2 = nested2;
exports.onSubmit = onSubmit;
/* inlined Not a pure module */
10 changes: 10 additions & 0 deletions jscomp/test/async_inline.res
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ let tuia = uncurriedIdAsync(. 3)
let nested1 = () => async (y) => await y

let nested2 = async () => async (y) => await y

type callback<'input, 'output> = 'input => 'output

@module("react")
external useCallback: (@uncurry ('input => 'output)) => callback<'input, 'output> = "useCallback"

let onSubmit = () =>
useCallback(async (_a, b) => {
await b
})