Skip to content

Commit e6a4b8c

Browse files
authored
Fix issue with @this and async functions (#6105)
* Fix issue with `@this` and async functions * Asked a "friend" to improve the changelog.
1 parent 77b6a3b commit e6a4b8c

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ These are only breaking changes for unformatted code.
6767
- Fix printing of uncurried application when the lhs is a function definition https://github.com/rescript-lang/rescript-compiler/pull/6084
6868
- Fix parsing uncurried type starting with path https://github.com/rescript-lang/rescript-compiler/pull/6089
6969
- Fix bigInt comparison https://github.com/rescript-lang/rescript-compiler/pull/6097
70+
- Fixed a bug where the async attribute was not preserved when using the `@this` decorator in ReScript functions. This fix allows proper handling of async functions with the `@this` decorator. Issue: https://github.com/rescript-lang/rescript-compiler/issues/6100
7071

7172
#### :nail_care: Polish
7273

jscomp/frontend/ast_uncurry_gen.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
3434
Bs_syntaxerr.optional_err loc label;
3535
let rec aux acc (body : Parsetree.expression) =
3636
match Ast_attributes.process_attributes_rev body.pexp_attributes with
37-
| Nothing, _ -> (
37+
| Nothing, attrs -> (
3838
match body.pexp_desc with
3939
| Pexp_fun (arg_label, _, arg, body) ->
4040
Bs_syntaxerr.optional_err loc arg_label;
41-
aux ((arg_label, self.pat self arg) :: acc) body
41+
aux ((arg_label, self.pat self arg, attrs) :: acc) body
4242
| _ -> (self.expr self body, acc))
4343
| _, _ -> (self.expr self body, acc)
4444
in
45-
let result, rev_extra_args = aux [ (label, self_pat) ] body in
45+
let result, rev_extra_args = aux [ (label, self_pat, []) ] body in
4646
let body =
47-
Ext_list.fold_left rev_extra_args result (fun e (label, p) ->
48-
Ast_helper.Exp.fun_ ~loc label None p e)
47+
Ext_list.fold_left rev_extra_args result (fun e (label, p, attrs) ->
48+
Ast_helper.Exp.fun_ ~loc ~attrs label None p e)
4949
in
5050
let arity = List.length rev_extra_args in
5151
let arity_s = string_of_int arity in

jscomp/test/UncurriedExternals.js

+16
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ var StandardNotation = {
7171
set: StandardNotation_set
7272
};
7373

74+
function methodWithAsync(param) {
75+
var $$this = this ;
76+
return (async function (arg) {
77+
return $$this + arg | 0;
78+
})(param);
79+
}
80+
7481
function dd$1(param) {
7582
throw {
7683
RE_EXN_ID: "Not_found",
@@ -120,11 +127,19 @@ var match$1 = React.useState(function (param) {
120127
return 3;
121128
});
122129

130+
function methodWithAsyncU() {
131+
var $$this = this ;
132+
return async function (arg) {
133+
return $$this + arg | 0;
134+
};
135+
}
136+
123137
var get = match$1[0];
124138

125139
var set = match$1[1];
126140

127141
exports.StandardNotation = StandardNotation;
142+
exports.methodWithAsync = methodWithAsync;
128143
exports.dd = dd$1;
129144
exports.h = h$1;
130145
exports.M = M$1;
@@ -138,4 +153,5 @@ exports.tsiC = tsiC$1;
138153
exports.tsiU = tsiU$1;
139154
exports.get = get;
140155
exports.set = set;
156+
exports.methodWithAsyncU = methodWithAsyncU;
141157
/* h Not a pure module */

jscomp/test/UncurriedExternals.res

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module StandardNotation = {
3939
let (get, set) = useState(() => 3)
4040
}
4141

42+
let methodWithAsync = @this this => async arg => this + arg
43+
4244
@@uncurried.swap
4345

4446
external raise: exn => 'a = "%raise"
@@ -78,3 +80,5 @@ let tsiU = c => setIncrementU(c, @this (me, amount) => Js.log(. me))
7880
@module("react")
7981
external useState: (@uncurry (unit => 'state)) => ('state, ('state => 'state) => unit) = "useState"
8082
let (get, set) = useState(() => 3)
83+
84+
let methodWithAsyncU = @this this => async arg => this + arg

0 commit comments

Comments
 (0)