Skip to content

Commit fca8ee2

Browse files
committed
wip remove Function$
1 parent 04f4e27 commit fca8ee2

File tree

8 files changed

+53
-73
lines changed

8 files changed

+53
-73
lines changed

compiler/frontend/ast_derive_js_mapper.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ let init () =
169169
in
170170
let to_js_body body =
171171
Ast_comb.single_non_rec_value pat_to_js
172-
(Ast_uncurried.uncurried_fun ~loc:Location.none ~arity:1
172+
(Ast_uncurried.uncurried_fun ~arity:1
173173
(Ast_compatible.fun_ ~arity:None
174174
(Pat.constraint_ (Pat.var pat_param) core_type)
175175
body))
@@ -214,7 +214,7 @@ let init () =
214214
in
215215
let from_js =
216216
Ast_comb.single_non_rec_value pat_from_js
217-
(Ast_uncurried.uncurried_fun ~loc:Location.none ~arity:1
217+
(Ast_uncurried.uncurried_fun ~arity:1
218218
(Ast_compatible.fun_ ~arity:(Some 1) (Pat.var pat_param)
219219
(if create_type then
220220
Exp.let_ Nonrecursive
@@ -260,7 +260,7 @@ let init () =
260260
app2 unsafe_index_get_exp exp_map exp_param
261261
else app1 erase_type_exp exp_param);
262262
Ast_comb.single_non_rec_value pat_from_js
263-
(Ast_uncurried.uncurried_fun ~loc:Location.none ~arity:1
263+
(Ast_uncurried.uncurried_fun ~arity:1
264264
(Ast_compatible.fun_ ~arity:(Some 1)
265265
(Pat.var pat_param)
266266
(let result =

compiler/frontend/ast_derive_projector.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ let init () =
2020
{
2121
structure_gen =
2222
(fun (tdcls : tdcls) _explict_nonrec ->
23-
let handle_uncurried_accessor_tranform ~loc ~arity accessor =
23+
let handle_uncurried_accessor_tranform ~arity accessor =
2424
(* Accessors with no params (arity of 0) are simply values and not functions *)
25-
if arity > 0 then Ast_uncurried.uncurried_fun ~loc ~arity accessor
25+
if arity > 0 then Ast_uncurried.uncurried_fun ~arity accessor
2626
else accessor
2727
in
2828
let handle_tdcl tdcl =
@@ -52,7 +52,7 @@ let init () =
5252
(Exp.ident {txt = Lident txt; loc})
5353
{txt = Longident.Lident pld_label; loc})
5454
(*arity will alwys be 1 since these are single param functions*)
55-
|> handle_uncurried_accessor_tranform ~arity:1 ~loc))
55+
|> handle_uncurried_accessor_tranform ~arity:1))
5656
| Ptype_variant constructor_declarations ->
5757
Ext_list.map constructor_declarations
5858
(fun
@@ -111,7 +111,7 @@ let init () =
111111
Ast_compatible.fun_ ~arity:(Some 1)
112112
(Pat.var {loc; txt = var})
113113
b)
114-
|> handle_uncurried_accessor_tranform ~loc ~arity))
114+
|> handle_uncurried_accessor_tranform ~arity))
115115
| Ptype_abstract | Ptype_open ->
116116
Ast_derive_util.not_applicable tdcl.ptype_loc deriving_name;
117117
[]

compiler/ml/ast_uncurried.ml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,23 @@ let uncurried_type ~loc ~arity t_arg =
1919
let t_arity = arity_type ~loc arity in
2020
Ast_helper.Typ.constr ~loc {txt = Lident "function$"; loc} [t_arg; t_arity]
2121

22-
let uncurried_fun ~loc ~arity fun_expr =
22+
let uncurried_fun ~arity fun_expr =
2323
let fun_expr =
2424
match fun_expr.Parsetree.pexp_desc with
2525
| Pexp_fun (l, eo, p, e, _) ->
2626
{fun_expr with pexp_desc = Pexp_fun (l, eo, p, e, Some arity)}
2727
| _ -> assert false
2828
in
29-
Ast_helper.Exp.construct ~loc
30-
(Location.mknoloc (Longident.Lident "Function$"))
31-
(Some fun_expr)
29+
fun_expr
3230

3331
let expr_is_uncurried_fun (expr : Parsetree.expression) =
3432
match expr.pexp_desc with
35-
| Pexp_construct ({txt = Lident "Function$"}, Some _) -> true
33+
| Pexp_fun (_, _, _, _, Some _) -> true
3634
| _ -> false
3735

3836
let expr_extract_uncurried_fun (expr : Parsetree.expression) =
3937
match expr.pexp_desc with
40-
| Pexp_construct ({txt = Lident "Function$"}, Some e) ->
41-
let () =
42-
match e.pexp_desc with
43-
| Pexp_fun (_, _, _, _, Some _arity) -> ()
44-
| _ -> assert false
45-
in
46-
e
38+
| Pexp_fun (_, _, _, _, Some _) -> expr
4739
| _ -> assert false
4840

4941
let remove_fun (expr : Parsetree.expression) =

compiler/ml/translcore.ml

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
674674
| Texp_constant cst -> Lconst (Const_base cst)
675675
| Texp_let (rec_flag, pat_expr_list, body) ->
676676
transl_let rec_flag pat_expr_list (transl_exp body)
677-
| Texp_function {arg_label = _; param; case; partial} ->
677+
| Texp_function {arg_label = _; arity; param; case; partial} -> (
678678
let async = has_async_attribute e in
679679
let directive =
680680
match extract_directive_for_fn e with
@@ -695,7 +695,22 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
695695
}
696696
in
697697
let loc = e.exp_loc in
698-
Lfunction {params; body; attr; loc}
698+
let lambda = Lfunction {params; body; attr; loc} in
699+
match arity with
700+
| Some arity ->
701+
let prim =
702+
match (Ctype.expand_head e.exp_env e.exp_type).desc with
703+
| Tarrow (Nolabel, t, _, _) -> (
704+
match (Ctype.expand_head e.exp_env t).desc with
705+
| Tconstr (Pident {name = "unit"}, [], _) -> Pjs_fn_make_unit
706+
| _ -> Pjs_fn_make arity)
707+
| _ -> Pjs_fn_make arity
708+
in
709+
Lprim
710+
( prim (* could be replaced with Opaque in the future except arity 0*),
711+
[lambda],
712+
loc )
713+
| None -> lambda)
699714
| Texp_apply
700715
( ({
701716
exp_desc = Texp_ident (_, _, {val_kind = Val_prim p});
@@ -781,27 +796,6 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda =
781796
with Not_constant -> Lprim (Pmakeblock Blk_tuple, ll, e.exp_loc))
782797
| Texp_construct ({txt = Lident "false"}, _, []) -> Lconst Const_false
783798
| Texp_construct ({txt = Lident "true"}, _, []) -> Lconst Const_true
784-
| Texp_construct
785-
({txt = Lident "Function$"}, _, [({exp_desc = Texp_function _} as expr)])
786-
->
787-
(* ReScript uncurried encoding *)
788-
let loc = expr.exp_loc in
789-
let lambda = transl_exp expr in
790-
let arity =
791-
Ast_uncurried.uncurried_type_get_arity ~env:e.exp_env e.exp_type
792-
in
793-
let prim =
794-
match (Ctype.expand_head expr.exp_env expr.exp_type).desc with
795-
| Tarrow (Nolabel, t, _, _) -> (
796-
match (Ctype.expand_head expr.exp_env t).desc with
797-
| Tconstr (Pident {name = "unit"}, [], _) -> Pjs_fn_make_unit
798-
| _ -> Pjs_fn_make arity)
799-
| _ -> Pjs_fn_make arity
800-
in
801-
Lprim
802-
( prim (* could be replaced with Opaque in the future except arity 0*),
803-
[lambda],
804-
loc )
805799
| Texp_construct (lid, cstr, args) -> (
806800
let ll = transl_list args in
807801
if cstr.cstr_inlined <> None then

compiler/ml/typecore.ml

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,9 +1913,12 @@ let rec approx_type env sty =
19131913
let rec type_approx env sexp =
19141914
match sexp.pexp_desc with
19151915
| Pexp_let (_, _, e) -> type_approx env e
1916-
| Pexp_fun (p, _, _, e, _arity) ->
1916+
| Pexp_fun (p, _, _, e, arity) -> (
19171917
let ty = if is_optional p then type_option (newvar ()) else newvar () in
1918-
newty (Tarrow (p, ty, type_approx env e, Cok))
1918+
let t = newty (Tarrow (p, ty, type_approx env e, Cok)) in
1919+
match arity with
1920+
| None -> t
1921+
| Some arity -> Ast_uncurried.make_uncurried_type ~env ~arity t)
19191922
| Pexp_match (_, {pc_rhs = e} :: _) -> type_approx env e
19201923
| Pexp_try (e, _) -> type_approx env e
19211924
| Pexp_tuple l -> newty (Ttuple (List.map (type_approx env) l))
@@ -2525,25 +2528,6 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
25252528
exp_attributes = sexp.pexp_attributes;
25262529
exp_env = env;
25272530
}
2528-
| Pexp_construct
2529-
( ({txt = Lident "Function$"} as lid),
2530-
(Some {pexp_desc = Pexp_fun (_, _, _, _, Some arity)} as sarg) ) ->
2531-
let state = Warnings.backup () in
2532-
let uncurried_typ =
2533-
Ast_uncurried.make_uncurried_type ~env ~arity (newvar ())
2534-
in
2535-
unify_exp_types loc env uncurried_typ ty_expected;
2536-
(* Disable Unerasable_optional_argument for uncurried functions *)
2537-
let unerasable_optional_argument =
2538-
Warnings.number Unerasable_optional_argument
2539-
in
2540-
Warnings.parse_options false
2541-
("-" ^ string_of_int unerasable_optional_argument);
2542-
let exp =
2543-
type_construct env loc lid sarg uncurried_typ sexp.pexp_attributes
2544-
in
2545-
Warnings.restore state;
2546-
exp
25472531
| Pexp_construct (lid, sarg) ->
25482532
type_construct env loc lid sarg ty_expected sexp.pexp_attributes
25492533
| Pexp_variant (l, sarg) -> (
@@ -3273,7 +3257,16 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
32733257
| Pexp_extension ext ->
32743258
raise (Error_forward (Builtin_attributes.error_of_extension ext))
32753259
3276-
and type_function ?in_function ~arity loc attrs env ty_expected l caselist =
3260+
and type_function ?in_function ~arity loc attrs env ty_expected_ l caselist =
3261+
let ty_expected =
3262+
match arity with
3263+
| None -> ty_expected_
3264+
| Some arity ->
3265+
let fun_t = newvar () in
3266+
let uncurried_typ = Ast_uncurried.make_uncurried_type ~env ~arity fun_t in
3267+
unify_exp_types loc env uncurried_typ ty_expected_;
3268+
fun_t
3269+
in
32773270
let loc_fun, ty_fun =
32783271
match in_function with
32793272
| Some p -> p
@@ -3311,12 +3304,18 @@ and type_function ?in_function ~arity loc attrs env ty_expected l caselist =
33113304
Location.prerr_warning case.c_lhs.pat_loc
33123305
Warnings.Unerasable_optional_argument;
33133306
let param = name_pattern "param" cases in
3307+
let exp_type = instance env (newgenty (Tarrow (l, ty_arg, ty_res, Cok))) in
3308+
let exp_type =
3309+
match arity with
3310+
| None -> exp_type
3311+
| Some arity -> Ast_uncurried.make_uncurried_type ~env ~arity exp_type
3312+
in
33143313
re
33153314
{
33163315
exp_desc = Texp_function {arg_label = l; arity; param; case; partial};
33173316
exp_loc = loc;
33183317
exp_extra = [];
3319-
exp_type = instance env (newgenty (Tarrow (l, ty_arg, ty_res, Cok)));
3318+
exp_type;
33203319
exp_attributes = attrs;
33213320
exp_env = env;
33223321
}

compiler/syntax/src/jsx_v4.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,8 +1028,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
10281028
in
10291029
let full_expression =
10301030
full_expression
1031-
|> Ast_uncurried.uncurried_fun ~loc:full_expression.pexp_loc
1032-
~arity:(if has_forward_ref then 2 else 1)
1031+
|> Ast_uncurried.uncurried_fun ~arity:(if has_forward_ref then 2 else 1)
10331032
in
10341033
let full_expression =
10351034
match full_module_name with

compiler/syntax/src/res_comments_table.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,8 +1451,6 @@ and walk_expression expr t comments =
14511451
attach t.leading expr.pexp_loc leading;
14521452
walk_expression expr t inside;
14531453
attach t.trailing expr.pexp_loc trailing
1454-
| Pexp_construct ({txt = Longident.Lident "Function$"}, Some return_expr) ->
1455-
walk_expression return_expr t comments
14561454
| _ ->
14571455
if is_block_expr return_expr then walk_expression return_expr t comments
14581456
else

compiler/syntax/src/res_core.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ let process_underscore_application args =
528528
let fun_expr =
529529
Ast_helper.Exp.fun_ ~loc ~arity:(Some 1) Nolabel None pattern exp_apply
530530
in
531-
Ast_uncurried.uncurried_fun ~loc ~arity:1 fun_expr
531+
Ast_uncurried.uncurried_fun ~arity:1 fun_expr
532532
| None -> exp_apply
533533
in
534534
(args, wrap)
@@ -1600,9 +1600,7 @@ and parse_es6_arrow_expression ?(arrow_attrs = []) ?(arrow_start_pos = None)
16001600
expr
16011601
in
16021602
if term_param_num = 1 then
1603-
( term_param_num - 1,
1604-
Ast_uncurried.uncurried_fun ~loc ~arity fun_expr,
1605-
1 )
1603+
(term_param_num - 1, Ast_uncurried.uncurried_fun ~arity fun_expr, 1)
16061604
else (term_param_num - 1, fun_expr, arity + 1)
16071605
| TypeParameter {attrs; locs = newtypes; pos = start_pos} ->
16081606
( term_param_num,

0 commit comments

Comments
 (0)