Skip to content

Fix issue with @this and async functions #6105

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

Merged
merged 2 commits into from
Apr 3, 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 @@ -67,6 +67,7 @@ These are only breaking changes for unformatted code.
- Fix printing of uncurried application when the lhs is a function definition https://github.com/rescript-lang/rescript-compiler/pull/6084
- Fix parsing uncurried type starting with path https://github.com/rescript-lang/rescript-compiler/pull/6089
- Fix bigInt comparison https://github.com/rescript-lang/rescript-compiler/pull/6097
- 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

#### :nail_care: Polish

Expand Down
10 changes: 5 additions & 5 deletions jscomp/frontend/ast_uncurry_gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
Bs_syntaxerr.optional_err loc label;
let rec aux acc (body : Parsetree.expression) =
match Ast_attributes.process_attributes_rev body.pexp_attributes with
| Nothing, _ -> (
| Nothing, attrs -> (
match body.pexp_desc with
| Pexp_fun (arg_label, _, arg, body) ->
Bs_syntaxerr.optional_err loc arg_label;
aux ((arg_label, self.pat self arg) :: acc) body
aux ((arg_label, self.pat self arg, attrs) :: acc) body
| _ -> (self.expr self body, acc))
| _, _ -> (self.expr self body, acc)
in
let result, rev_extra_args = aux [ (label, self_pat) ] body in
let result, rev_extra_args = aux [ (label, self_pat, []) ] body in
let body =
Ext_list.fold_left rev_extra_args result (fun e (label, p) ->
Ast_helper.Exp.fun_ ~loc label None p e)
Ext_list.fold_left rev_extra_args result (fun e (label, p, attrs) ->
Ast_helper.Exp.fun_ ~loc ~attrs label None p e)
in
let arity = List.length rev_extra_args in
let arity_s = string_of_int arity in
Expand Down
16 changes: 16 additions & 0 deletions jscomp/test/UncurriedExternals.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ var StandardNotation = {
set: StandardNotation_set
};

function methodWithAsync(param) {
var $$this = this ;
return (async function (arg) {
return $$this + arg | 0;
})(param);
}

function dd$1(param) {
throw {
RE_EXN_ID: "Not_found",
Expand Down Expand Up @@ -120,11 +127,19 @@ var match$1 = React.useState(function (param) {
return 3;
});

function methodWithAsyncU() {
var $$this = this ;
return async function (arg) {
return $$this + arg | 0;
};
}

var get = match$1[0];

var set = match$1[1];

exports.StandardNotation = StandardNotation;
exports.methodWithAsync = methodWithAsync;
exports.dd = dd$1;
exports.h = h$1;
exports.M = M$1;
Expand All @@ -138,4 +153,5 @@ exports.tsiC = tsiC$1;
exports.tsiU = tsiU$1;
exports.get = get;
exports.set = set;
exports.methodWithAsyncU = methodWithAsyncU;
/* h Not a pure module */
4 changes: 4 additions & 0 deletions jscomp/test/UncurriedExternals.res
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ module StandardNotation = {
let (get, set) = useState(() => 3)
}

let methodWithAsync = @this this => async arg => this + arg

@@uncurried.swap

external raise: exn => 'a = "%raise"
Expand Down Expand Up @@ -78,3 +80,5 @@ let tsiU = c => setIncrementU(c, @this (me, amount) => Js.log(. me))
@module("react")
external useState: (@uncurry (unit => 'state)) => ('state, ('state => 'state) => unit) = "useState"
let (get, set) = useState(() => 3)

let methodWithAsyncU = @this this => async arg => this + arg