Skip to content

Commit 79a3876

Browse files
committed
wip remove Function$
1 parent 233db91 commit 79a3876

10 files changed

+46
-95
lines changed

compiler/frontend/ast_derive_js_mapper.ml

+3-3
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

+4-4
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

+4-12
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 core_type_is_uncurried_fun (typ : Parsetree.core_type) =

compiler/ml/translcore.ml

+17-23
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

+13-20
Original file line numberDiff line numberDiff line change
@@ -2525,25 +2525,6 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
25252525
exp_attributes = sexp.pexp_attributes;
25262526
exp_env = env;
25272527
}
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
25472528
| Pexp_construct (lid, sarg) ->
25482529
type_construct env loc lid sarg ty_expected sexp.pexp_attributes
25492530
| Pexp_variant (l, sarg) -> (
@@ -3273,7 +3254,19 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
32733254
| Pexp_extension ext ->
32743255
raise (Error_forward (Builtin_attributes.error_of_extension ext))
32753256
3276-
and type_function ?in_function ~arity loc attrs env ty_expected l caselist =
3257+
and type_function ?in_function ~arity loc attrs env ty_expected_ l caselist =
3258+
let ty_expected =
3259+
match arity with
3260+
| None ->
3261+
ty_expected_
3262+
| Some arity ->
3263+
let fun_t = newvar() in
3264+
let uncurried_typ =
3265+
Ast_uncurried.make_uncurried_type ~env ~arity fun_t
3266+
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

compiler/syntax/src/jsx_v4.ml

+1-2
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

-2
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

+3-5
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)
@@ -1596,13 +1596,11 @@ and parse_es6_arrow_expression ?(arrow_attrs = []) ?(arrow_start_pos = None)
15961596
{attrs; label = lbl; expr = default_expr; pat; pos = start_pos} ->
15971597
let loc = mk_loc start_pos end_pos in
15981598
let fun_expr =
1599-
Ast_helper.Exp.fun_ ~loc ~attrs ~arity:(Some arity) lbl default_expr
1599+
Ast_helper.Exp.fun_ ~loc ~attrs ~arity:None lbl default_expr
16001600
pat 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,

compiler/syntax/src/res_parens.ml

-11
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ let unary_expr_operand expr =
111111
Parenthesized
112112
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
113113
Parenthesized
114-
| {pexp_desc = Pexp_construct ({txt = Lident "Function$"}, Some expr)}
115-
when ParsetreeViewer.is_underscore_apply_sugar expr ->
116-
Nothing
117-
| {pexp_desc = Pexp_construct ({txt = Lident "Function$"}, Some _)} ->
118-
Parenthesized
119114
| _ -> Nothing)
120115

121116
let binary_expr_operand ~is_lhs expr =
@@ -183,7 +178,6 @@ let flatten_operand_rhs parent_operator rhs =
183178
let prec_parent = ParsetreeViewer.operator_precedence parent_operator in
184179
let prec_child = ParsetreeViewer.operator_precedence operator in
185180
prec_parent >= prec_child || rhs.pexp_attributes <> []
186-
| Pexp_construct ({txt = Lident "Function$"}, Some _) -> true
187181
| Pexp_constraint ({pexp_desc = Pexp_pack _}, {ptyp_desc = Ptyp_package _}) ->
188182
false
189183
| Pexp_fun _ when ParsetreeViewer.is_underscore_apply_sugar rhs -> false
@@ -279,11 +273,6 @@ let field_expr expr =
279273
Parenthesized
280274
| _ when ParsetreeViewer.has_await_attribute expr.pexp_attributes ->
281275
Parenthesized
282-
| {pexp_desc = Pexp_construct ({txt = Lident "Function$"}, Some expr)}
283-
when ParsetreeViewer.is_underscore_apply_sugar expr ->
284-
Nothing
285-
| {pexp_desc = Pexp_construct ({txt = Lident "Function$"}, Some _)} ->
286-
Parenthesized
287276
| _ -> Nothing)
288277

289278
let set_field_expr_rhs expr =

compiler/syntax/src/res_printer.ml

+1-13
Original file line numberDiff line numberDiff line change
@@ -2793,19 +2793,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
27932793
None,
27942794
{ppat_desc = Ppat_var {txt = "__x"}},
27952795
{pexp_desc = Pexp_apply _},
2796-
_ )
2797-
| Pexp_construct
2798-
( {txt = Lident "Function$"},
2799-
Some
2800-
{
2801-
pexp_desc =
2802-
Pexp_fun
2803-
( Nolabel,
2804-
None,
2805-
{ppat_desc = Ppat_var {txt = "__x"}},
2806-
{pexp_desc = Pexp_apply _},
2807-
_ );
2808-
} ) ->
2796+
_ ) ->
28092797
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
28102798
print_expression_with_comments ~state
28112799
(ParsetreeViewer.rewrite_underscore_apply e_fun)

0 commit comments

Comments
 (0)