Skip to content

Commit 8a7ac4a

Browse files
committed
Refactor method callback and uncurry type handling
Simplifies and updates the handling of method callback and uncurry types in the AST frontend. Removes the to_uncurry_type function and refactors to_method_callback_type to accept arity and the full method type, improving compatibility and maintainability. Updates related type signatures and usage in core type class and uncurry generation modules.
1 parent d228c78 commit 8a7ac4a

File tree

4 files changed

+19
-62
lines changed

4 files changed

+19
-62
lines changed

compiler/frontend/ast_core_type_class_type.ml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,15 @@ let default_typ_mapper = Bs_ast_mapper.default_mapper.typ
6767
let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
6868
let loc = ty.ptyp_loc in
6969
match ty.ptyp_desc with
70-
| Ptyp_arrow {arg; ret = body}
70+
| Ptyp_arrow {arity}
7171
(* let it go without regard label names,
7272
it will report error later when the label is not empty
7373
*)
7474
-> (
7575
match fst (Ast_attributes.process_attributes_rev ty.ptyp_attributes) with
7676
| Meth_callback _ ->
77-
Ast_typ_uncurry.to_method_callback_type loc self arg.lbl arg.typ body
78-
| Method _ ->
79-
(* Treat @meth as making the type uncurried, for backwards compatibility *)
80-
Ast_typ_uncurry.to_uncurry_type loc self arg.lbl arg.typ body
77+
Ast_typ_uncurry.to_method_callback_type loc self ~arity ty
78+
| Method _ -> Bs_ast_mapper.default_mapper.typ self ty
8179
| Nothing -> Bs_ast_mapper.default_mapper.typ self ty)
8280
| Ptyp_object (methods, closed_flag) ->
8381
let ( +> ) attr (typ : Parsetree.core_type) =
@@ -107,7 +105,8 @@ let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
107105
| Meth_callback attr, attrs -> (attrs, attr +> ty)
108106
in
109107
Ast_compatible.object_field name attrs
110-
(Ast_typ_uncurry.to_uncurry_type loc self Nolabel core_type
108+
(Ast_helper.Typ.arrows ~loc
109+
[{attrs = []; lbl = Nolabel; typ = self.typ self core_type}]
111110
(Ast_literal.type_unit ~loc ()))
112111
in
113112
let not_getter_setter ty =

compiler/frontend/ast_typ_uncurry.ml

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,10 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
type typ = Parsetree.core_type
26-
type 'a cxt = Ast_helper.loc -> Bs_ast_mapper.mapper -> 'a
27-
type uncurry_type_gen = (Asttypes.arg_label -> typ -> typ -> typ) cxt
2826

29-
let to_method_callback_type loc (mapper : Bs_ast_mapper.mapper)
30-
(label : Asttypes.arg_label) (first_arg : Parsetree.core_type)
31-
(typ : Parsetree.core_type) =
32-
let first_arg = mapper.typ mapper first_arg in
33-
let typ = mapper.typ mapper typ in
34-
let meth_type =
35-
Ast_helper.Typ.arrow ~loc ~arity:None
36-
{attrs = []; lbl = label; typ = first_arg}
37-
typ
38-
in
39-
let arity = Ast_core_type.get_uncurry_arity meth_type in
27+
let to_method_callback_type loc (mapper : Bs_ast_mapper.mapper) ~arity
28+
(meth_type : Parsetree.core_type) =
29+
let meth_type = Bs_ast_mapper.default_mapper.typ mapper meth_type in
4030
match arity with
4131
| Some n ->
4232
Ast_helper.Typ.constr
@@ -46,30 +36,3 @@ let to_method_callback_type loc (mapper : Bs_ast_mapper.mapper)
4636
}
4737
[meth_type]
4838
| None -> assert false
49-
50-
let to_uncurry_type loc (mapper : Bs_ast_mapper.mapper)
51-
(label : Asttypes.arg_label) (first_arg : Parsetree.core_type)
52-
(typ : Parsetree.core_type) =
53-
(* no need to error for optional here,
54-
since we can not make it
55-
TODO: still error out for external?
56-
Maybe no need to error on optional at all
57-
it just does not make sense
58-
*)
59-
let first_arg = mapper.typ mapper first_arg in
60-
let typ = mapper.typ mapper typ in
61-
62-
let fn_type =
63-
Ast_helper.Typ.arrow ~loc ~arity:None
64-
{attrs = []; lbl = label; typ = first_arg}
65-
typ
66-
in
67-
let arity = Ast_core_type.get_uncurry_arity fn_type in
68-
let fn_type =
69-
match fn_type.ptyp_desc with
70-
| Ptyp_arrow arr -> {fn_type with ptyp_desc = Ptyp_arrow {arr with arity}}
71-
| _ -> assert false
72-
in
73-
match arity with
74-
| Some arity -> Ast_uncurried.uncurried_type ~arity fn_type
75-
| None -> assert false

compiler/frontend/ast_typ_uncurry.mli

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,14 @@
3737

3838
type typ = Parsetree.core_type
3939

40-
type 'a cxt = Ast_helper.loc -> Bs_ast_mapper.mapper -> 'a
41-
42-
type uncurry_type_gen =
43-
(Asttypes.arg_label ->
44-
(* label for error checking *)
45-
typ ->
46-
(* First arg *)
40+
val to_method_callback_type :
41+
Ast_helper.loc ->
42+
Bs_ast_mapper.mapper ->
43+
arity:int option ->
4744
typ ->
48-
(* Tail *)
49-
typ)
50-
cxt
51-
52-
val to_uncurry_type : uncurry_type_gen
53-
(** syntax :
54-
{[ int -> int -> int [@bs]]}
55-
*)
45+
(* Method type *)
46+
typ
5647

57-
val to_method_callback_type : uncurry_type_gen
5848
(** syntax:
5949
{[ 'obj -> int -> int [@this] ]}
6050
*)

compiler/frontend/ast_uncurry_gen.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
4848
Ast_helper.Exp.fun_ ~loc ~attrs ~arity:None ~async label None p e)
4949
in
5050
let arity = List.length rev_extra_args in
51+
let body =
52+
match body.pexp_desc with
53+
| Pexp_fun f -> {body with pexp_desc = Pexp_fun {f with arity = Some arity}}
54+
| _ -> body
55+
in
5156
let arity_s = string_of_int arity in
5257
Stack.pop Js_config.self_stack |> ignore;
5358
Parsetree.Pexp_apply

0 commit comments

Comments
 (0)