Skip to content

Commit f189aa9

Browse files
committed
Support @uncurry externals with uncurried types.
So existing externals can be used without change in uncurried mode, without giving an error. That said, the `@uncurry` annotation becomes redundant in those cases.
1 parent 60f6c9d commit f189aa9

File tree

5 files changed

+46
-11
lines changed

5 files changed

+46
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
- Add support for unary uncurried pipe in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/5804
2020
- Add support for partial application of uncurried functions: with uncurried application one can provide a
2121
subset of the arguments, and return a curried type with the remaining ones https://github.com/rescript-lang/rescript-compiler/pull/5805
22-
- Add support for uncurried externals https://github.com/rescript-lang/rescript-compiler/pull/5815 https://github.com/rescript-lang/rescript-compiler/pull/5819 https://github.com/rescript-lang/rescript-compiler/pull/5830
22+
- Add support for uncurried externals https://github.com/rescript-lang/rescript-compiler/pull/5815 https://github.com/rescript-lang/rescript-compiler/pull/5819 https://github.com/rescript-lang/rescript-compiler/pull/5830 https://github.com/rescript-lang/rescript-compiler/pull/5894
2323
- Parser/Printer: unify uncurried functions of arity 0, and of arity 1 taking unit. There's now only arity 1 in the source language. https://github.com/rescript-lang/rescript-compiler/pull/5825
2424
- Add support for default arguments in uncurried functions https://github.com/rescript-lang/rescript-compiler/pull/5835
2525
- Inline uncurried application when it is safe https://github.com/rescript-lang/rescript-compiler/pull/5847

jscomp/frontend/ast_core_type.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ let get_uncurry_arity (ty : t) =
131131
| _ -> None
132132

133133
let get_curry_arity (ty : t) =
134-
match ty.ptyp_desc with
135-
| Ptyp_constr ({ txt = Lident "function$" }, [ t; _ ]) ->
136-
get_uncurry_arity_aux t 0
137-
| _ -> get_uncurry_arity_aux ty 0
134+
if Ast_uncurried.typeIsUncurriedFun ty then
135+
let arity, _ = Ast_uncurried.typeExtractUncurriedFun ty in
136+
arity
137+
else get_uncurry_arity_aux ty 0
138138

139139
(* add hoc for bs.send.pipe *)
140140
let rec get_curry_labels (ty : t) acc =

jscomp/frontend/ast_external_process.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ let spec_of_ptyp (nolabel : bool) (ptyp : Parsetree.core_type) :
6767
(* Unwrap attribute can only be attached to things like `[a of a0 | b of b0]` *)
6868
| _ -> Bs_syntaxerr.err ptyp.ptyp_loc Invalid_bs_unwrap_type)
6969
| `Uncurry opt_arity -> (
70-
let real_arity = Ast_core_type.get_uncurry_arity ptyp in
70+
let real_arity =
71+
if Ast_uncurried.typeIsUncurriedFun ptyp then
72+
let arity, _ = Ast_uncurried.typeExtractUncurriedFun ptyp in
73+
Some arity
74+
else
75+
Ast_core_type.get_uncurry_arity ptyp in
7176
match (opt_arity, real_arity) with
7277
| Some arity, None -> Fn_uncurry_arity arity
7378
| None, None -> Bs_syntaxerr.err ptyp.ptyp_loc Canot_infer_arity_by_syntax

jscomp/test/UncurriedExternals.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
var React = require("react");
34

45
function dd(param) {
56
throw {
@@ -46,6 +47,14 @@ function tsiU(c) {
4647
});
4748
}
4849

50+
var match = React.useState(function () {
51+
return 3;
52+
});
53+
54+
var StandardNotation_get = match[0];
55+
56+
var StandardNotation_set = match[1];
57+
4958
var StandardNotation = {
5059
dd: dd,
5160
h: h,
@@ -57,7 +66,9 @@ var StandardNotation = {
5766
te: te,
5867
tcr: tcr,
5968
tsiC: tsiC,
60-
tsiU: tsiU
69+
tsiU: tsiU,
70+
get: StandardNotation_get,
71+
set: StandardNotation_set
6172
};
6273

6374
function dd$1(param) {
@@ -105,6 +116,14 @@ function tsiU$1(c) {
105116
});
106117
}
107118

119+
var match$1 = React.useState(function (param) {
120+
return 3;
121+
});
122+
123+
var get = match$1[0];
124+
125+
var set = match$1[1];
126+
108127
exports.StandardNotation = StandardNotation;
109128
exports.dd = dd$1;
110129
exports.h = h$1;
@@ -117,4 +136,6 @@ exports.te = te$1;
117136
exports.tcr = tcr$1;
118137
exports.tsiC = tsiC$1;
119138
exports.tsiU = tsiU$1;
139+
exports.get = get;
140+
exports.set = set;
120141
/* h Not a pure module */

jscomp/test/UncurriedExternals.res

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ module StandardNotation = {
2424
external toException: (. exn) => exn = "%identity"
2525
let te = toException(. Not_found)
2626

27-
@obj external ccreate : (. unit) => string = ""
27+
@obj external ccreate: (. unit) => string = ""
2828
let tcr = ccreate(.)
2929

3030
type counter
3131
@set external setIncrementC: (counter, @this (counter, int) => unit) => unit = "increment"
3232
let tsiC = c => setIncrementC(c, @this (me, amount) => Js.log(me))
3333
@set external setIncrementU: (. counter, @this (. counter, int) => unit) => unit = "increment"
34-
let tsiU = c => setIncrementU(. c, @this (. me, amount) => Js.log(me))
34+
let tsiU = c => setIncrementU(.c, @this (. me, amount) => Js.log(me))
35+
36+
@module("react")
37+
external useState: (@uncurry (unit => 'state)) => ('state, ('state => 'state) => unit) =
38+
"useState"
39+
let (get, set) = useState(() => 3)
3540
}
3641

3742
@@uncurried
@@ -61,11 +66,15 @@ let tc = copy("abc")
6166
external toException: exn => exn = "%identity"
6267
let te = toException(Not_found)
6368

64-
@obj external ucreate : unit => string = ""
69+
@obj external ucreate: unit => string = ""
6570
let tcr = ucreate()
6671

6772
type counter
6873
@set external setIncrementC: (. counter, @this (. counter, int) => unit) => unit = "increment"
69-
let tsiC = c => setIncrementC(. c, @this (. me, amount) => Js.log(. me))
74+
let tsiC = c => setIncrementC(.c, @this (. me, amount) => Js.log(. me))
7075
@set external setIncrementU: (counter, @this (counter, int) => unit) => unit = "increment"
7176
let tsiU = c => setIncrementU(c, @this (me, amount) => Js.log(. me))
77+
78+
@module("react")
79+
external useState: (@uncurry (unit => 'state)) => ('state, ('state => 'state) => unit) = "useState"
80+
let (get, set) = useState(() => 3)

0 commit comments

Comments
 (0)