Skip to content

Commit 9afac86

Browse files
authored
Add support for functions in untagged variants. (#6279)
* Add support for functions in untagged variants. This was done at speed: need to double check that there are no corner cases missing. Fixes #6278 * test gentype * Add parens as required for TS case with function type. * Update CHANGELOG.md
1 parent 22e69d2 commit 9afac86

File tree

16 files changed

+92
-15
lines changed

16 files changed

+92
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :rocket: New Feature
1616

1717
- Introduced a new `%ffi` extension that provides a more robust mechanism for JavaScript function interoperation by considering function arity in type constraints. This enhancement improves safety when dealing with JavaScript functions by enforcing type constraints based on the arity of the function. [PR #6251](https://github.com/rescript-lang/rescript-compiler/pull/6251)
18+
- Extended untagged variants with function types https://github.com/rescript-lang/rescript-compiler/pull/6279
1819

1920
#### :bug: Bug Fix
2021

jscomp/core/js_exp_make.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ let tag_type = function
769769
| Undefined -> undefined
770770
| Untagged IntType -> str "number"
771771
| Untagged FloatType -> str "number"
772+
| Untagged FunctionType -> str "function"
772773
| Untagged StringType -> str "string"
773774
| Untagged ArrayType -> str "Array" ~delim:DNoQuotes
774775
| Untagged ObjectType -> str "object"

jscomp/frontend/ast_core_type.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ let get_uncurry_arity (ty : t) =
125125
| _ -> None
126126

127127
let get_curry_arity (ty : t) =
128-
if Ast_uncurried.typeIsUncurriedFun ty then
128+
if Ast_uncurried.coreTypeIsUncurriedFun ty then
129129
let arity, _ = Ast_uncurried.typeExtractUncurriedFun ty in
130130
arity
131131
else get_uncurry_arity_aux ty 0

jscomp/frontend/ast_external_process.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let spec_of_ptyp (nolabel : bool) (ptyp : Parsetree.core_type) :
6868
| _ -> Bs_syntaxerr.err ptyp.ptyp_loc Invalid_bs_unwrap_type)
6969
| `Uncurry opt_arity -> (
7070
let real_arity =
71-
if Ast_uncurried.typeIsUncurriedFun ptyp then
71+
if Ast_uncurried.coreTypeIsUncurriedFun ptyp then
7272
let arity, _ = Ast_uncurried.typeExtractUncurriedFun ptyp in
7373
Some arity
7474
else Ast_core_type.get_uncurry_arity ptyp

jscomp/gentype/EmitType.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,14 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
184184
|> field ~name:(Runtime.jsVariantTag ~polymorphic:false)
185185
in
186186
match (unboxed, type_) with
187-
| true, type_ -> type_ |> render
187+
| true, type_ ->
188+
let needParens =
189+
match type_ with
190+
| Function _ -> true
191+
| _ -> false
192+
in
193+
let t = type_ |> render in
194+
if needParens then EmitText.parens [t] else t
188195
| false, type_ when polymorphic ->
189196
(* poly variant *)
190197
[

jscomp/gentype_tests/typescript-react-example/bsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"subdirs": true
2525
}
2626
],
27+
"uncurried": false,
2728
"package-specs": {
2829
"module": "es6",
2930
"in-source": true

jscomp/gentype_tests/typescript-react-example/package-lock.json

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/gentype_tests/typescript-react-example/src/Unboxed.gen.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export type r1 = number;
1818
// tslint:disable-next-line:interface-over-type-literal
1919
export type r2 = string;
2020

21+
// tslint:disable-next-line:interface-over-type-literal
22+
export type t = number[] | number | ((_1:number) => number);
23+
2124
export const testV1: (x:v1) => v1 = UnboxedBS.testV1;
2225

2326
export const r2Test: (x:r2) => r2 = UnboxedBS.r2Test;

jscomp/gentype_tests/typescript-react-example/src/Unboxed.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ type r1 = {x: int}
1313
type r2 = B({g: string})
1414

1515
@genType let r2Test = (x: r2) => x
16+
17+
@genType @unboxed
18+
type t = Array(array<int>) | Record({x:int}) | Function((. int) => int)

jscomp/ml/ast_uncurried.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,19 @@ let exprExtractUncurriedFun (expr : Parsetree.expression) =
6363
| Pexp_construct ({ txt = Lident "Function$" }, Some e) -> e
6464
| _ -> assert false
6565

66-
let typeIsUncurriedFun (typ : Parsetree.core_type) =
66+
let coreTypeIsUncurriedFun (typ : Parsetree.core_type) =
6767
match typ.ptyp_desc with
6868
| Ptyp_constr ({txt = Lident "function$"}, [{ptyp_desc = Ptyp_arrow _}; _]) ->
6969
true
7070
| _ -> false
7171

72+
let typeIsUncurriedFun (typ : Types.type_expr) =
73+
match typ.desc with
74+
| Tconstr (Pident {name = "function$"}, [{desc = Tarrow _}; _], _) ->
75+
true
76+
| _ -> false
77+
78+
7279
let typeExtractUncurriedFun (typ : Parsetree.core_type) =
7380
match typ.ptyp_desc with
7481
| Ptyp_constr ({txt = Lident "function$"}, [tArg; tArity]) ->

0 commit comments

Comments
 (0)