Skip to content

Commit 91fc35f

Browse files
authored
Uncurried types: distinguish 0 args, and 1 arg of type unit. (#5821)
* Uncurried types: distinguish 0 args, and 1 arg of type unit. * Update CHANGELOG.md
1 parent f5fb4a6 commit 91fc35f

File tree

13 files changed

+157
-21
lines changed

13 files changed

+157
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ subset of the arguments, and return a curried type with the remaining ones https
3232
- Curried after uncurried is not fused anymore: `(. x) => y => 3` is not equivalent to `(. x, y) => 3` anymore. It's instead equivalent to `(. x) => { y => 3 }`.
3333
Also, `(. int) => string => bool` is not equivalen to `(. int, string) => bool` anymore.
3434
These are only breaking changes for unformatted code.
35+
- Distinguish between uncurried type `(. ()) => int`, whch takes 0 arguments, and `(. unit) => int` which takes 1 argument of type `unit` https://github.com/rescript-lang/rescript-compiler/pull/5821
3536

3637
#### :bug: Bug Fix
3738

jscomp/test/poly_variant_test.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ external on: (
8181
@string
8282
[
8383
| #line((. string) => unit)
84-
| #close((. unit) => unit)
84+
| #close((. ()) => unit)
8585
],
8686
) => unit = "on"
8787

jscomp/test/reactTestUtils.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ type undefined = Js.undefined<unit>
33
let undefined: undefined = Js.Undefined.empty
44

55
@module("react-dom/test-utils")
6-
external reactAct: ((. unit) => undefined) => unit = "act"
6+
external reactAct: ((. ()) => undefined) => unit = "act"
77

88
let act: (unit => unit) => unit = func => {
99
let reactFunc = (. ()) => {
@@ -14,7 +14,7 @@ let act: (unit => unit) => unit = func => {
1414
}
1515

1616
@module("react-dom/test-utils")
17-
external reactActAsync: ((. unit) => Js.Promise.t<'a>) => Js.Promise.t<unit> = "act"
17+
external reactActAsync: ((. ()) => Js.Promise.t<'a>) => Js.Promise.t<unit> = "act"
1818

1919
let actAsync = func => {
2020
let reactFunc = (. ()) => func()

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54752,9 +54752,10 @@ and printTypExpr ~state (typExpr : Parsetree.core_type) cmtTbl =
5475254752
| Ptyp_object (fields, openFlag) ->
5475354753
printObject ~state ~inline:false fields openFlag cmtTbl
5475454754
| Ptyp_arrow _ -> printArrow ~uncurried:false typExpr
54755+
| Ptyp_constr ({txt = Lident "()"}, []) -> Doc.text "()"
5475554756
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), "arity0")}, [tArg]) ->
54756-
let unitConstr = Location.mkloc (Longident.Lident "unit") tArg.ptyp_loc in
54757-
let tUnit = Ast_helper.Typ.constr unitConstr [] in
54757+
let parensConstr = Location.mkloc (Longident.Lident "()") tArg.ptyp_loc in
54758+
let tUnit = Ast_helper.Typ.constr parensConstr [] in
5475854759
printArrow ~uncurried:true ~arity:1
5475954760
{tArg with ptyp_desc = Ptyp_arrow (Nolabel, tUnit, tArg)}
5476054761
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), arity)}, [tArg])

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54752,9 +54752,10 @@ and printTypExpr ~state (typExpr : Parsetree.core_type) cmtTbl =
5475254752
| Ptyp_object (fields, openFlag) ->
5475354753
printObject ~state ~inline:false fields openFlag cmtTbl
5475454754
| Ptyp_arrow _ -> printArrow ~uncurried:false typExpr
54755+
| Ptyp_constr ({txt = Lident "()"}, []) -> Doc.text "()"
5475554756
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), "arity0")}, [tArg]) ->
54756-
let unitConstr = Location.mkloc (Longident.Lident "unit") tArg.ptyp_loc in
54757-
let tUnit = Ast_helper.Typ.constr unitConstr [] in
54757+
let parensConstr = Location.mkloc (Longident.Lident "()") tArg.ptyp_loc in
54758+
let tUnit = Ast_helper.Typ.constr parensConstr [] in
5475854759
printArrow ~uncurried:true ~arity:1
5475954760
{tArg with ptyp_desc = Ptyp_arrow (Nolabel, tUnit, tArg)}
5476054761
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), arity)}, [tArg])
@@ -166636,14 +166637,15 @@ and parseEs6ArrowType ~attrs p =
166636166637
if p.uncurried_by_default then not dotted else dotted
166637166638
in
166638166639
if uncurried && (paramNum = 1 || not p.uncurried_by_default) then
166639-
let isUnit =
166640+
let isParens =
166640166641
match typ.ptyp_desc with
166641-
| Ptyp_constr ({txt = Lident "unit"}, []) -> true
166642+
| Ptyp_constr ({txt = Lident "unit"; loc}, []) ->
166643+
loc.loc_end.pos_cnum - loc.loc_start.pos_cnum = 2 (* () *)
166642166644
| _ -> false
166643166645
in
166644166646
let loc = mkLoc startPos endPos in
166645166647
let fnArity, tArg =
166646-
if isUnit && arity = 1 then (0, t)
166648+
if isParens && arity = 1 then (0, t)
166647166649
else (arity, Ast_helper.Typ.arrow ~loc ~attrs argLbl typ t)
166648166650
in
166649166651
( paramNum - 1,

lib/4.06.1/whole_compiler.ml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109750,9 +109750,10 @@ and printTypExpr ~state (typExpr : Parsetree.core_type) cmtTbl =
109750109750
| Ptyp_object (fields, openFlag) ->
109751109751
printObject ~state ~inline:false fields openFlag cmtTbl
109752109752
| Ptyp_arrow _ -> printArrow ~uncurried:false typExpr
109753+
| Ptyp_constr ({txt = Lident "()"}, []) -> Doc.text "()"
109753109754
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), "arity0")}, [tArg]) ->
109754-
let unitConstr = Location.mkloc (Longident.Lident "unit") tArg.ptyp_loc in
109755-
let tUnit = Ast_helper.Typ.constr unitConstr [] in
109755+
let parensConstr = Location.mkloc (Longident.Lident "()") tArg.ptyp_loc in
109756+
let tUnit = Ast_helper.Typ.constr parensConstr [] in
109756109757
printArrow ~uncurried:true ~arity:1
109757109758
{tArg with ptyp_desc = Ptyp_arrow (Nolabel, tUnit, tArg)}
109758109759
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), arity)}, [tArg])
@@ -180068,14 +180069,15 @@ and parseEs6ArrowType ~attrs p =
180068180069
if p.uncurried_by_default then not dotted else dotted
180069180070
in
180070180071
if uncurried && (paramNum = 1 || not p.uncurried_by_default) then
180071-
let isUnit =
180072+
let isParens =
180072180073
match typ.ptyp_desc with
180073-
| Ptyp_constr ({txt = Lident "unit"}, []) -> true
180074+
| Ptyp_constr ({txt = Lident "unit"; loc}, []) ->
180075+
loc.loc_end.pos_cnum - loc.loc_start.pos_cnum = 2 (* () *)
180074180076
| _ -> false
180075180077
in
180076180078
let loc = mkLoc startPos endPos in
180077180079
let fnArity, tArg =
180078-
if isUnit && arity = 1 then (0, t)
180080+
if isParens && arity = 1 then (0, t)
180079180081
else (arity, Ast_helper.Typ.arrow ~loc ~attrs argLbl typ t)
180080180082
in
180081180083
( paramNum - 1,

res_syntax/src/res_core.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,14 +4250,15 @@ and parseEs6ArrowType ~attrs p =
42504250
if p.uncurried_by_default then not dotted else dotted
42514251
in
42524252
if uncurried && (paramNum = 1 || not p.uncurried_by_default) then
4253-
let isUnit =
4253+
let isParens =
42544254
match typ.ptyp_desc with
4255-
| Ptyp_constr ({txt = Lident "unit"}, []) -> true
4255+
| Ptyp_constr ({txt = Lident "unit"; loc}, []) ->
4256+
loc.loc_end.pos_cnum - loc.loc_start.pos_cnum = 2 (* () *)
42564257
| _ -> false
42574258
in
42584259
let loc = mkLoc startPos endPos in
42594260
let fnArity, tArg =
4260-
if isUnit && arity = 1 then (0, t)
4261+
if isParens && arity = 1 then (0, t)
42614262
else (arity, Ast_helper.Typ.arrow ~loc ~attrs argLbl typ t)
42624263
in
42634264
( paramNum - 1,

res_syntax/src/res_printer.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,9 +1653,10 @@ and printTypExpr ~state (typExpr : Parsetree.core_type) cmtTbl =
16531653
| Ptyp_object (fields, openFlag) ->
16541654
printObject ~state ~inline:false fields openFlag cmtTbl
16551655
| Ptyp_arrow _ -> printArrow ~uncurried:false typExpr
1656+
| Ptyp_constr ({txt = Lident "()"}, []) -> Doc.text "()"
16561657
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), "arity0")}, [tArg]) ->
1657-
let unitConstr = Location.mkloc (Longident.Lident "unit") tArg.ptyp_loc in
1658-
let tUnit = Ast_helper.Typ.constr unitConstr [] in
1658+
let parensConstr = Location.mkloc (Longident.Lident "()") tArg.ptyp_loc in
1659+
let tUnit = Ast_helper.Typ.constr parensConstr [] in
16591660
printArrow ~uncurried:true ~arity:1
16601661
{tArg with ptyp_desc = Ptyp_arrow (Nolabel, tUnit, tArg)}
16611662
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), arity)}, [tArg])

res_syntax/tests/parsing/grammar/expressions/UncurriedByDefault.res

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ type mixTyp = (string, .string, string) => (string, string, string) => (string,
1414
type bTyp = (. string) => string => int
1515
type cTyp2 = (string, string) => int
1616
type uTyp2 = (.string, string) => int
17+
type cu = unit => int
18+
type cp = () => int
19+
type cuu = unit => unit => int
20+
type cpu = () => unit => int
21+
type cup = unit => () => int
22+
type cpp = () => () => int
23+
type cu2 = (unit, unit) => unit
24+
type cp2 = ((), ()) => unit
25+
type uu = (. unit) => int
26+
type up = (. ()) => int
27+
type uuu = (. unit) => (. unit) => int
28+
type upu = (. ()) => (. unit) => int
29+
type uup = (. unit) => (. ()) => int
30+
type upp = (. ()) => (. ()) => int
31+
type uu2 = (. unit, unit) => unit
32+
type up2 = (. (), ()) => unit
1733

1834
@@uncurried
1935

@@ -34,5 +50,21 @@ type mixTyp = (.string) => (string, string) => (.string, string, string, string)
3450
type bTyp = string => (. string) => int
3551
type cTyp2 = (.string, string) => int
3652
type uTyp2 = (string, string) => int
53+
type cu = (. unit) => int
54+
type cp = (. ()) => int
55+
type cuu = (. unit) => (. unit) => int
56+
type cpu = (. ()) => (. unit) => int
57+
type cup = (. unit) => (. ()) => int
58+
type cpp = (. ()) => (. ()) => int
59+
type cu2 = (. unit, unit) => unit
60+
type cp2 = (. (), ()) => unit
61+
type uu = unit => int
62+
type up = () => int
63+
type uuu = unit => unit => int
64+
type upu = () => unit => int
65+
type uup = unit => () => int
66+
type upp = () => () => int
67+
type uu2 = (unit, unit) => unit
68+
type up2 = ((), ()) => unit
3769

3870
let pipe1 = 3->f

res_syntax/tests/parsing/grammar/expressions/expected/UncurriedByDefault.res.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ type nonrec mixTyp =
2424
type nonrec bTyp = (string -> string -> int) Js.Fn.arity1
2525
type nonrec cTyp2 = string -> string -> int
2626
type nonrec uTyp2 = (string -> string -> int) Js.Fn.arity2
27+
type nonrec cu = unit -> int
28+
type nonrec cp = unit -> int
29+
type nonrec cuu = unit -> unit -> int
30+
type nonrec cpu = unit -> unit -> int
31+
type nonrec cup = unit -> unit -> int
32+
type nonrec cpp = unit -> unit -> int
33+
type nonrec cu2 = unit -> unit -> unit
34+
type nonrec cp2 = unit -> unit -> unit
35+
type nonrec uu = (unit -> int) Js.Fn.arity1
36+
type nonrec up = int Js.Fn.arity0
37+
type nonrec uuu = (unit -> (unit -> int) Js.Fn.arity1) Js.Fn.arity1
38+
type nonrec upu = (unit -> int) Js.Fn.arity1 Js.Fn.arity0
39+
type nonrec uup = (unit -> int Js.Fn.arity0) Js.Fn.arity1
40+
type nonrec upp = int Js.Fn.arity0 Js.Fn.arity0
41+
type nonrec uu2 = (unit -> unit -> unit) Js.Fn.arity2
42+
type nonrec up2 = (unit -> unit -> unit) Js.Fn.arity2
2743
[@@@uncurried ]
2844
let cApp = foo 3
2945
let uApp = ((foo 3)[@bs ])
@@ -52,4 +68,20 @@ type nonrec mixTyp =
5268
type nonrec bTyp = (string -> string -> int) Js.Fn.arity1
5369
type nonrec cTyp2 = string -> string -> int
5470
type nonrec uTyp2 = (string -> string -> int) Js.Fn.arity2
71+
type nonrec cu = unit -> int
72+
type nonrec cp = unit -> int
73+
type nonrec cuu = unit -> unit -> int
74+
type nonrec cpu = unit -> unit -> int
75+
type nonrec cup = unit -> unit -> int
76+
type nonrec cpp = unit -> unit -> int
77+
type nonrec cu2 = unit -> unit -> unit
78+
type nonrec cp2 = unit -> unit -> unit
79+
type nonrec uu = (unit -> int) Js.Fn.arity1
80+
type nonrec up = int Js.Fn.arity0
81+
type nonrec uuu = (unit -> (unit -> int) Js.Fn.arity1) Js.Fn.arity1
82+
type nonrec upu = (unit -> int) Js.Fn.arity1 Js.Fn.arity0
83+
type nonrec uup = (unit -> int Js.Fn.arity0) Js.Fn.arity1
84+
type nonrec upp = int Js.Fn.arity0 Js.Fn.arity0
85+
type nonrec uu2 = (unit -> unit -> unit) Js.Fn.arity2
86+
type nonrec up2 = (unit -> unit -> unit) Js.Fn.arity2
5587
let pipe1 = 3 |.u f

res_syntax/tests/parsing/grammar/typexpr/uncurried.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ type t = @attr (. float) => @attr2 int => @attr3 (. bool) => @attr4 string => un
99
type t = (. (@attr float), (@attr2 int), . (@attr3 bool), (@attr4 string)) => unit
1010

1111
@bs.val
12-
external setTimeout : ((. unit) => unit, int) => timerId = "setTimeout"
12+
external setTimeout : ((. ()) => unit, int) => timerId = "setTimeout"
1313
// totally different meaning
1414
external setTimeout : (. unit => unit, int) => timerId = "setTimeout"

res_syntax/tests/printer/expr/UncurriedByDefault.res

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ type mixTyp = (string, .string, string) => (string, string, string) => (string,
1414
type bTyp = (. string) => string => int
1515
type cTyp2 = (string, string) => int
1616
type uTyp2 = (.string, string) => int
17+
type cu = unit => int
18+
type cp = () => int
19+
type cuu = unit => unit => int
20+
type cpu = () => unit => int
21+
type cup = unit => () => int
22+
type cpp = () => () => int
23+
type cu2 = (unit, unit) => unit
24+
type cp2 = ((), ()) => unit
25+
type uu = (. unit) => int
26+
type up = (. ()) => int
27+
type uuu = (. unit) => (. unit) => int
28+
type upu = (. ()) => (. unit) => int
29+
type uup = (. unit) => (. ()) => int
30+
type upp = (. ()) => (. ()) => int
31+
type uu2 = (. unit, unit) => unit
32+
type up2 = (. (), ()) => unit
1733

1834
let pipe = a->foo(. b, c)
1935

@@ -36,5 +52,21 @@ type mixTyp = (.string) => (string, string) => (.string, string, string, string)
3652
type bTyp = string => (. string) => int
3753
type cTyp2 = (. string, string) => int
3854
type uTyp2 = (string, string) => int
55+
type cu = (. unit) => int
56+
type cp = (. ()) => int
57+
type cuu = (. unit) => (. unit) => int
58+
type cpu = (. ()) => (. unit) => int
59+
type cup = (. unit) => (. ()) => int
60+
type cpp = (. ()) => (. ()) => int
61+
type cu2 = (. unit, unit) => unit
62+
type cp2 = (. (), ()) => unit
63+
type uu = unit => int
64+
type up = () => int
65+
type uuu = unit => unit => int
66+
type upu = () => unit => int
67+
type uup = unit => () => int
68+
type upp = () => () => int
69+
type uu2 = (unit, unit) => unit
70+
type up2 = ((), ()) => unit
3971

4072
let pipe = a->foo(b, c)

res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ type mixTyp = string => (. string, string) => (string, string, string, string) =
1414
type bTyp = (. string) => string => int
1515
type cTyp2 = (string, string) => int
1616
type uTyp2 = (. string, string) => int
17+
type cu = unit => int
18+
type cp = unit => int
19+
type cuu = (unit, unit) => int
20+
type cpu = (unit, unit) => int
21+
type cup = (unit, unit) => int
22+
type cpp = (unit, unit) => int
23+
type cu2 = (unit, unit) => unit
24+
type cp2 = (unit, unit) => unit
25+
type uu = (. unit) => int
26+
type up = (. ()) => int
27+
type uuu = (. unit) => (. unit) => int
28+
type upu = (. ()) => (. unit) => int
29+
type uup = (. unit) => (. ()) => int
30+
type upp = (. ()) => (. ()) => int
31+
type uu2 = (. unit, unit) => unit
32+
type up2 = (. unit, unit) => unit
1733

1834
let pipe = a->foo(. b, c)
1935

@@ -36,5 +52,21 @@ type mixTyp = (. string) => (string, string) => (. string, string, string, strin
3652
type bTyp = string => (. string) => int
3753
type cTyp2 = (. string, string) => int
3854
type uTyp2 = (string, string) => int
55+
type cu = (. unit) => int
56+
type cp = (. unit) => int
57+
type cuu = (. unit, unit) => int
58+
type cpu = (. unit, unit) => int
59+
type cup = (. unit, unit) => int
60+
type cpp = (. unit, unit) => int
61+
type cu2 = (. unit, unit) => unit
62+
type cp2 = (. unit, unit) => unit
63+
type uu = unit => int
64+
type up = () => int
65+
type uuu = unit => unit => int
66+
type upu = () => unit => int
67+
type uup = unit => () => int
68+
type upp = () => () => int
69+
type uu2 = (unit, unit) => unit
70+
type up2 = (unit, unit) => unit
3971

4072
let pipe = a->foo(b, c)

0 commit comments

Comments
 (0)