Skip to content

Commit 420eadc

Browse files
committed
add type constraint for better error message, simplify runtime support
1 parent 928a6c3 commit 420eadc

17 files changed

+2527
-2494
lines changed

jscomp/main/builtin_cmi_datasets.ml

+111-111
Large diffs are not rendered by default.

jscomp/main/builtin_cmj_datasets.ml

+29-29
Large diffs are not rendered by default.

jscomp/others/js_OO.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,17 @@ module Meth = struct
164164
}[@@unboxed]
165165
end
166166

167+
(**/**)
167168
module Internal = struct
168169
open Meth
169170
(* Use opaque instead of [._n] to prevent some optimizations happening *)
170-
external id : 'a -> 'a = "%opaque"
171171

172-
external run0 : 'a arity0 -> 'a = "#run" "0"
172+
external run : 'a arity0 -> 'a = "#run" "0"
173173
(*
174174
x##meth a b -->
175175
fullApppy (
176176
(id (unsafe_downgrade x)#meth).I_2)
177177
a b)
178178
*)
179-
end
179+
end
180+
(**/**)

jscomp/runtime/js.ml

+2-25
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,8 @@ module Internal = struct
133133
external unsafeInvariantApply : 'a -> 'a = "#full_apply"
134134

135135
(* Use opaque instead of [._n] to prevent some optimizations happening *)
136-
external run0 : 'a arity0 -> 'a = "#run" "0"
137-
external run1 : 'a arity1 -> 'a = "%opaque"
138-
external run2 : 'a arity2 -> 'a = "%opaque"
139-
external run3 : 'a arity3 -> 'a = "%opaque"
140-
external run4 : 'a arity4 -> 'a = "%opaque"
141-
external run5 : 'a arity5 -> 'a = "%opaque"
142-
external run6 : 'a arity6 -> 'a = "%opaque"
143-
external run7 : 'a arity7 -> 'a = "%opaque"
144-
external run8 : 'a arity8 -> 'a = "%opaque"
145-
external run9 : 'a arity9 -> 'a = "%opaque"
146-
external run10 : 'a arity10 -> 'a = "%opaque"
147-
external run11 : 'a arity11 -> 'a = "%opaque"
148-
external run12 : 'a arity12 -> 'a = "%opaque"
149-
external run13 : 'a arity13 -> 'a = "%opaque"
150-
external run14 : 'a arity14 -> 'a = "%opaque"
151-
external run15 : 'a arity15 -> 'a = "%opaque"
152-
external run16 : 'a arity16 -> 'a = "%opaque"
153-
external run17 : 'a arity17 -> 'a = "%opaque"
154-
external run18 : 'a arity18 -> 'a = "%opaque"
155-
external run19 : 'a arity19 -> 'a = "%opaque"
156-
external run20 : 'a arity20 -> 'a = "%opaque"
157-
external run21 : 'a arity21 -> 'a = "%opaque"
158-
external run22 : 'a arity22 -> 'a = "%opaque"
159-
160-
136+
external run : 'a arity0 -> 'a = "#run"
137+
external opaque : 'a -> 'a = "%opaque"
161138
external mk0 : (unit -> 'a0) -> 'a0 arity0 = "#fn_mk" "0"
162139
end
163140
(**/**)

jscomp/syntax/ast_literal.ml

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module Lid = struct
4848
(* TODO should be renamed in to {!Js.fn} *)
4949
(* TODO should be moved into {!Js.t} Later *)
5050
let js_internal : t = Ldot (Lident "Js", "Internal")
51+
let opaque : t = Ldot (js_internal, "opaque")
5152
let js_fn : t =
5253
Ldot (Lident "Js", "Fn")
5354
let js_oo : t = Lident "Js_OO"

jscomp/syntax/ast_literal.mli

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module Lid : sig
3535
val type_unit : t
3636
val type_int : t
3737
val js_fn : t
38+
val opaque : t
3839
val js_oo : t
3940
val js_meth : t
4041
val js_meth_callback : t

jscomp/syntax/ast_util.ml

+19-10
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,21 @@ let generic_apply loc
7575
let arity = List.length args in
7676
if arity = 0 then
7777
Parsetree.Pexp_apply
78-
(Exp.ident {txt = Ldot (jsInternal, "run0");loc}, [Nolabel,fn])
78+
(Exp.ident {txt = Ldot (jsInternal, "run");loc}, [Nolabel,fn])
7979
else
80-
let txt : Longident.t =
81-
Ldot (jsInternal, "run" ^ string_of_int arity) in
80+
let arity_s = string_of_int arity in
81+
8282
Parsetree.Pexp_apply (
8383
Exp.ident {txt = unsafeInvariantApply; loc},
8484
[Nolabel,
85-
Exp.apply (Exp.apply (Exp.ident {txt ; loc}) [(Nolabel,fn)])
86-
args])
85+
Exp.apply
86+
(Exp.apply
87+
(Exp.ident {txt = Ast_literal.Lid.opaque; loc})
88+
[(Nolabel, Exp.field ~loc
89+
(Exp.constraint_ ~loc fn
90+
(Typ.constr ~loc {txt = Ldot (Ast_literal.Lid.js_fn, "arity"^arity_s);loc}
91+
[Typ.any ~loc ()])) {txt = Longident.Lident ("I_"^ arity_s); loc})])
92+
args])
8793

8894
let method_apply loc
8995
(self : Bs_ast_mapper.mapper)
@@ -104,16 +110,19 @@ let method_apply loc
104110
let arity = List.length args in
105111
if arity = 0 then
106112
Parsetree.Pexp_apply
107-
(Exp.ident {txt = Ldot ((Ldot (Ast_literal.Lid.js_oo,"Internal")), "run0");loc}, [Nolabel,fn])
113+
(Exp.ident {txt = Ldot ((Ldot (Ast_literal.Lid.js_oo,"Internal")), "run");loc}, [Nolabel,fn])
108114
else
109-
let txt : Longident.t =
110-
Ldot (Ldot (Ast_literal.Lid.js_oo,"Internal"), "id") in
115+
let arity_s = string_of_int arity in
111116
Parsetree.Pexp_apply (
112117
Exp.ident {txt = unsafeInvariantApply; loc},
113118
[Nolabel,
114119
Exp.apply (
115-
Exp.apply (Exp.ident {txt ; loc})
116-
[(Nolabel,Exp.field fn {loc; txt = Ldot (Ast_literal.Lid.js_meth,"I_"^string_of_int arity)})])
120+
Exp.apply (Exp.ident {txt = Ast_literal.Lid.opaque; loc})
121+
[(Nolabel,
122+
Exp.field
123+
(Exp.constraint_ ~loc
124+
fn (Typ.constr ~loc {txt = Ldot (Ast_literal.Lid.js_meth,"arity"^arity_s);loc} [Typ.any ~loc ()]))
125+
{loc; txt = Lident ( "I_"^arity_s)})])
117126
args])
118127

119128

jscomp/test/arity.re

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ let u2 = ( ~f : t0 , a , b) => {
2020

2121
let f = (. ~x,y) => x + y ;
2222
let add = (+);
23-
// let u = f(.3,~x=2,1);
23+
// let u = f(.3,~x=2,1);
2424
// This function has arity2 but was expected arity3
2525

2626
// let h = f (1, 2) ;
2727
// This function has uncurried type, it needs to be applied in ucurried style
2828
// This function has uncurried type, it needs to be applied in ucurried style
2929

3030

31-
//let h = add(.1,2);
31+
// let h = add(.1,2);
3232
// This function is a curried function where an uncurried function is expected
3333

3434

0 commit comments

Comments
 (0)