Skip to content

Commit ceed17b

Browse files
committed
Fix: Inline auto curried async functions produce invalid javascript
Fixes #6007
1 parent 2cd2228 commit ceed17b

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ These are only breaking changes for unformatted code.
6464
- 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
6565
- Fix issue in `Js.Promise2` where `then` and `catch` were returning `undefined` https://github.com/rescript-lang/rescript-compiler/pull/5997
6666
- Fix formatting of props spread for multiline JSX expression https://github.com/rescript-lang/rescript-compiler/pull/6006
67+
- 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
6768

6869
#### :nail_care: Polish
6970

jscomp/core/lam_eta_conversion.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,16 @@ let unsafe_adjust_to_arity loc ~(to_ : int) ?(from : int option) (fn : Lam.t) :
113113
let ap_info : Lam.ap_info =
114114
{ ap_loc = loc; ap_inlined = Default_inline; ap_status = App_na }
115115
in
116+
let is_async_fn = match fn with
117+
| Lfunction { attr = {async}} -> async
118+
| _ -> false in
116119
match (from, fn) with
117120
| Some from, _ | None, Lfunction { arity = from } -> (
118-
if from = to_ then fn
121+
if from = to_ || is_async_fn then fn
119122
else if to_ = 0 then
120123
match fn with
121-
| Lfunction { params = [ param ]; body; attr = {async} } ->
122-
Lam.function_ ~arity:0 ~attr:{Lambda.default_function_attribute with async}
124+
| Lfunction { params = [ param ]; body } ->
125+
Lam.function_ ~arity:0 ~attr:Lambda.default_function_attribute
123126
~params:[]
124127
~body:(Lam.let_ Alias param Lam.unit body)
125128
(* could be only introduced by

jscomp/test/async_inline.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var Curry = require("../../lib/js/curry.js");
4+
var React = require("react");
45

56
async function willBeInlined(param) {
67
return 3;
@@ -72,6 +73,12 @@ async function nested2(param) {
7273
};
7374
}
7475

76+
function onSubmit(param) {
77+
return React.useCallback(async function (_a, b) {
78+
return await b;
79+
});
80+
}
81+
7582
var tci = 3;
7683

7784
exports.willBeInlined = willBeInlined;
@@ -90,4 +97,5 @@ exports.tui = tui;
9097
exports.tuia = tuia;
9198
exports.nested1 = nested1;
9299
exports.nested2 = nested2;
100+
exports.onSubmit = onSubmit;
93101
/* inlined Not a pure module */

jscomp/test/async_inline.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,13 @@ let tuia = uncurriedIdAsync(. 3)
5050
let nested1 = () => async (y) => await y
5151

5252
let nested2 = async () => async (y) => await y
53+
54+
type callback<'input, 'output> = 'input => 'output
55+
56+
@module("react")
57+
external useCallback: (@uncurry ('input => 'output)) => callback<'input, 'output> = "useCallback"
58+
59+
let onSubmit = () =>
60+
useCallback(async (_a, b) => {
61+
await b
62+
})

0 commit comments

Comments
 (0)