Skip to content

Commit 31fe30b

Browse files
committed
AST: use inline record for Ptyp_arrow.
1 parent 52d1cfb commit 31fe30b

26 files changed

+123
-138
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- AST cleanup: Remove `structure_item_desc.Pstr_class`, `signature_item_desc.Psig_class`, `structure_item_desc.Pstr_class_type`, `signature_item_desc.Psig_class_type`, `structure_item_desc.Tstr_class`, `structure_item_desc.Tstr_class_type`, `signature_item_desc.Tsig_class`, `signature_item_desc.Tsig_class_type` from AST as it is unused. https://github.com/rescript-lang/rescript/pull/7242
3030
- AST cleanup: remove "|." and rename "|." to "->" in the internal representation for the pipe operator. https://github.com/rescript-lang/rescript/pull/7244
3131
- AST cleanup: represent concatenation (`++`) and (dis)equality operators (`==`, `===`, `!=`, `!==`) just like in the syntax. https://github.com/rescript-lang/rescript/pull/7248
32+
- AST cleanup: use inline record for `Ptyp_arrow`. https://github.com/rescript-lang/rescript/pull/7250
3233

3334
# 12.0.0-alpha.7
3435

analysis/src/SignatureHelp.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ let extractParameters ~signature ~typeStrForParser ~labelPrefixLen =
113113
| {
114114
(* Gotcha: functions with multiple arugments are modelled as a series of single argument functions. *)
115115
Parsetree.ptyp_desc =
116-
Ptyp_arrow (argumentLabel, argumentTypeExpr, nextFunctionExpr, _);
116+
Ptyp_arrow
117+
{lbl = argumentLabel; arg = argumentTypeExpr; ret = nextFunctionExpr};
117118
ptyp_loc;
118119
} ->
119120
let startOffset =

compiler/frontend/ast_compatible.ml

+5-4
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,17 @@ let apply_labels ?(loc = default_loc) ?(attrs = []) fn
122122
};
123123
}
124124

125-
let label_arrow ?(loc = default_loc) ?(attrs = []) ~arity s a b : core_type =
125+
let label_arrow ?(loc = default_loc) ?(attrs = []) ~arity s arg ret : core_type
126+
=
126127
{
127-
ptyp_desc = Ptyp_arrow (Asttypes.Labelled s, a, b, arity);
128+
ptyp_desc = Ptyp_arrow {lbl = Labelled s; arg; ret; arity};
128129
ptyp_loc = loc;
129130
ptyp_attributes = attrs;
130131
}
131132

132-
let opt_arrow ?(loc = default_loc) ?(attrs = []) ~arity s a b : core_type =
133+
let opt_arrow ?(loc = default_loc) ?(attrs = []) ~arity s arg ret : core_type =
133134
{
134-
ptyp_desc = Ptyp_arrow (Asttypes.Optional s, a, b, arity);
135+
ptyp_desc = Ptyp_arrow {lbl = Asttypes.Optional s; arg; ret; arity};
135136
ptyp_loc = loc;
136137
ptyp_attributes = attrs;
137138
}

compiler/frontend/ast_core_type.ml

+9-9
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let make_obj ~loc xs = Typ.object_ ~loc xs Closed
109109
*)
110110
let rec get_uncurry_arity_aux (ty : t) acc =
111111
match ty.ptyp_desc with
112-
| Ptyp_arrow (_, _, new_ty, _) -> get_uncurry_arity_aux new_ty (succ acc)
112+
| Ptyp_arrow {ret = new_ty} -> get_uncurry_arity_aux new_ty (succ acc)
113113
| Ptyp_poly (_, ty) -> get_uncurry_arity_aux ty acc
114114
| _ -> acc
115115

@@ -120,12 +120,12 @@ let rec get_uncurry_arity_aux (ty : t) acc =
120120
*)
121121
let get_uncurry_arity (ty : t) =
122122
match ty.ptyp_desc with
123-
| Ptyp_arrow (_, _, rest, _) -> Some (get_uncurry_arity_aux rest 1)
123+
| Ptyp_arrow {ret = rest} -> Some (get_uncurry_arity_aux rest 1)
124124
| _ -> None
125125

126126
let get_curry_arity (ty : t) =
127127
match ty.ptyp_desc with
128-
| Ptyp_arrow (_, _, _, Some arity) -> arity
128+
| Ptyp_arrow {arity = Some arity} -> arity
129129
| _ -> get_uncurry_arity_aux ty 0
130130

131131
let is_arity_one ty = get_curry_arity ty = 1
@@ -142,23 +142,23 @@ let mk_fn_type (new_arg_types_ty : param_type list) (result : t) : t =
142142
Ext_list.fold_right new_arg_types_ty result
143143
(fun {label; ty; attr; loc} acc ->
144144
{
145-
ptyp_desc = Ptyp_arrow (label, ty, acc, None);
145+
ptyp_desc = Ptyp_arrow {lbl = label; arg = ty; ret = acc; arity = None};
146146
ptyp_loc = loc;
147147
ptyp_attributes = attr;
148148
})
149149
in
150150
match t.ptyp_desc with
151-
| Ptyp_arrow (l, t1, t2, _arity) ->
151+
| Ptyp_arrow arr ->
152152
let arity = List.length new_arg_types_ty in
153-
{t with ptyp_desc = Ptyp_arrow (l, t1, t2, Some arity)}
153+
{t with ptyp_desc = Ptyp_arrow {arr with arity = Some arity}}
154154
| _ -> t
155155

156156
let list_of_arrow (ty : t) : t * param_type list =
157157
let rec aux (ty : t) acc =
158158
match ty.ptyp_desc with
159-
| Ptyp_arrow (label, t1, t2, arity) when arity = None || acc = [] ->
160-
aux t2
161-
(({label; ty = t1; attr = ty.ptyp_attributes; loc = ty.ptyp_loc}
159+
| Ptyp_arrow {lbl = label; arg; ret; arity} when arity = None || acc = [] ->
160+
aux ret
161+
(({label; ty = arg; attr = ty.ptyp_attributes; loc = ty.ptyp_loc}
162162
: param_type)
163163
:: acc)
164164
| Ptyp_poly (_, ty) ->

compiler/frontend/ast_core_type_class_type.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ 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 (label, args, body, _)
70+
| Ptyp_arrow {lbl = label; arg = args; ret = body}
7171
(* let it go without regard label names,
7272
it will report error later when the label is not empty
7373
*) -> (

compiler/frontend/ast_external_process.ml

+19-22
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
[@@@warning "+9"]
26-
(* record pattern match complete checker*)
27-
2825
let rec variant_can_unwrap_aux (row_fields : Parsetree.row_field list) : bool =
2926
match row_fields with
3027
| [] -> true
@@ -68,7 +65,7 @@ let spec_of_ptyp (nolabel : bool) (ptyp : Parsetree.core_type) :
6865
| _ -> Bs_syntaxerr.err ptyp.ptyp_loc Invalid_bs_unwrap_type)
6966
| `Nothing -> (
7067
match ptyp_desc with
71-
| Ptyp_constr ({txt = Lident "unit"; _}, []) ->
68+
| Ptyp_constr ({txt = Lident "unit"}, []) ->
7269
if nolabel then Extern_unit else Nothing
7370
| _ -> Nothing)
7471

@@ -257,7 +254,7 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
257254
{
258255
pstr_desc =
259256
Pstr_eval
260-
({pexp_loc; pexp_desc = Pexp_record (fields, _); _}, _);
257+
({pexp_loc; pexp_desc = Pexp_record (fields, _)}, _);
261258
_;
262259
};
263260
] -> (
@@ -270,10 +267,10 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
270267
Longident.t Location.loc * Parsetree.expression * bool)
271268
->
272269
match (l, exp.pexp_desc) with
273-
| ( {txt = Lident "from"; _},
270+
| ( {txt = Lident "from"},
274271
Pexp_constant (Pconst_string (s, _)) ) ->
275272
from_name := Some s
276-
| {txt = Lident "with"; _}, Pexp_record (fields, _) ->
273+
| {txt = Lident "with"}, Pexp_record (fields, _) ->
277274
with_ := Some fields
278275
| _ -> ());
279276
match (!from_name, !with_) with
@@ -395,7 +392,7 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
395392
| "return" -> (
396393
let actions = Ast_payload.ident_or_record_as_config loc payload in
397394
match actions with
398-
| [({txt; _}, None)] ->
395+
| [({txt}, None)] ->
399396
{st with return_wrapper = return_wrapper loc txt}
400397
| _ -> Bs_syntaxerr.err loc Not_supported_directive_in_bs_return)
401398
| _ -> raise_notrace Not_handled_external_attribute
@@ -467,7 +464,7 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
467464
match arg_label with
468465
| Nolabel -> (
469466
match ty.ptyp_desc with
470-
| Ptyp_constr ({txt = Lident "unit"; _}, []) ->
467+
| Ptyp_constr ({txt = Lident "unit"}, []) ->
471468
( External_arg_spec.empty_kind Extern_unit,
472469
param_type :: arg_types,
473470
result_types )
@@ -550,7 +547,7 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
550547
| Nothing ->
551548
let for_sure_not_nested =
552549
match ty.ptyp_desc with
553-
| Ptyp_constr ({txt = Lident txt; _}, []) ->
550+
| Ptyp_constr ({txt = Lident txt}, []) ->
554551
Ast_core_type.is_builtin_rank0_type txt
555552
| _ -> false
556553
in
@@ -643,7 +640,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
643640
else
644641
Location.raise_errorf ~loc
645642
"Ill defined attribute %@set_index (arity of 3)"
646-
| {set_index = true; _} ->
643+
| {set_index = true} ->
647644
Bs_syntaxerr.err loc
648645
(Conflict_ffi_attribute "Attribute found that conflicts with %@set_index")
649646
| {
@@ -669,7 +666,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
669666
Location.raise_errorf ~loc
670667
"Ill defined attribute %@get_index (arity expected 2 : while %d)"
671668
arg_type_specs_length
672-
| {get_index = true; _} ->
669+
| {get_index = true} ->
673670
Bs_syntaxerr.err loc
674671
(Conflict_ffi_attribute "Attribute found that conflicts with %@get_index")
675672
| {
@@ -702,7 +699,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
702699
Location.raise_errorf ~loc
703700
"Incorrect FFI attribute found: (%@new should not carry a payload here)"
704701
)
705-
| {module_as_val = Some _; get_index; val_send; _} ->
702+
| {module_as_val = Some _; get_index; val_send} ->
706703
let reason =
707704
match (get_index, val_send) with
708705
| true, _ ->
@@ -770,7 +767,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
770767
Js_var {name; external_module_name; scopes}
771768
(*FIXME: splice is not supported here *)
772769
else Js_call {splice; name; external_module_name; scopes; tagged_template}
773-
| {call_name = Some _; _} ->
770+
| {call_name = Some _} ->
774771
Bs_syntaxerr.err loc
775772
(Conflict_ffi_attribute "Attribute found that conflicts with %@val")
776773
| {
@@ -797,7 +794,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
797794
]}
798795
*)
799796
Js_var {name; external_module_name; scopes}
800-
| {val_name = Some _; _} ->
797+
| {val_name = Some _} ->
801798
Bs_syntaxerr.err loc
802799
(Conflict_ffi_attribute "Attribute found that conflicts with %@val")
803800
| {
@@ -855,7 +852,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
855852
Location.raise_errorf ~loc
856853
"Ill defined attribute %@send(first argument can't be const)"
857854
| _ :: _ -> Js_send {splice; name; js_send_scopes = scopes})
858-
| {val_send = Some _; _} ->
855+
| {val_send = Some _} ->
859856
Location.raise_errorf ~loc
860857
"You used a FFI attribute that can't be used with %@send"
861858
| {
@@ -876,7 +873,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
876873
tagged_template = _;
877874
} ->
878875
Js_new {name; external_module_name; splice; scopes}
879-
| {new_name = Some _; _} ->
876+
| {new_name = Some _} ->
880877
Bs_syntaxerr.err loc
881878
(Conflict_ffi_attribute "Attribute found that conflicts with %@new")
882879
| {
@@ -901,7 +898,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
901898
else
902899
Location.raise_errorf ~loc
903900
"Ill defined attribute %@set (two args required)"
904-
| {set_name = Some _; _} ->
901+
| {set_name = Some _} ->
905902
Location.raise_errorf ~loc "conflict attributes found with %@set"
906903
| {
907904
get_name = Some {name; source = _};
@@ -925,7 +922,7 @@ let external_desc_of_non_obj (loc : Location.t) (st : external_desc)
925922
else
926923
Location.raise_errorf ~loc
927924
"Ill defined attribute %@get (only one argument)"
928-
| {get_name = Some _; _} ->
925+
| {get_name = Some _} ->
929926
Location.raise_errorf ~loc "Attribute found that conflicts with %@get"
930927

931928
(** Note that the passed [type_annotation] is already processed by visitor pattern before*)
@@ -935,8 +932,8 @@ let handle_attributes (loc : Bs_loc.t) (type_annotation : Parsetree.core_type)
935932
let prim_name_with_source = {name = prim_name; source = External} in
936933
let type_annotation, build_uncurried_type =
937934
match type_annotation with
938-
| {ptyp_desc = Ptyp_arrow (_, _, _, Some _); _} as t ->
939-
( t,
935+
| {ptyp_desc = Ptyp_arrow {arity = Some _}} ->
936+
( type_annotation,
940937
fun ~arity (x : Parsetree.core_type) ->
941938
Ast_uncurried.uncurried_type ~arity x )
942939
| _ -> (type_annotation, fun ~arity:_ x -> x)
@@ -978,7 +975,7 @@ let handle_attributes (loc : Bs_loc.t) (type_annotation : Parsetree.core_type)
978975
Location.raise_errorf ~loc
979976
"%@variadic expect the last type to be an array";
980977
match ty.ptyp_desc with
981-
| Ptyp_constr ({txt = Lident "array"; _}, [_]) -> ()
978+
| Ptyp_constr ({txt = Lident "array"}, [_]) -> ()
982979
| _ ->
983980
Location.raise_errorf ~loc
984981
"%@variadic expect the last type to be an array"));

compiler/frontend/ast_typ_uncurry.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ let to_uncurry_type loc (mapper : Bs_ast_mapper.mapper)
6161
let arity = Ast_core_type.get_uncurry_arity fn_type in
6262
let fn_type =
6363
match fn_type.ptyp_desc with
64-
| Ptyp_arrow (l, t1, t2, _) ->
65-
{fn_type with ptyp_desc = Ptyp_arrow (l, t1, t2, arity)}
64+
| Ptyp_arrow arr -> {fn_type with ptyp_desc = Ptyp_arrow {arr with arity}}
6665
| _ -> assert false
6766
in
6867
match arity with

compiler/frontend/bs_ast_mapper.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ module T = struct
101101
match desc with
102102
| Ptyp_any -> any ~loc ~attrs ()
103103
| Ptyp_var s -> var ~loc ~attrs s
104-
| Ptyp_arrow (lab, t1, t2, arity) ->
105-
arrow ~loc ~attrs ~arity lab (sub.typ sub t1) (sub.typ sub t2)
104+
| Ptyp_arrow {lbl; arg; ret; arity} ->
105+
arrow ~loc ~attrs ~arity lbl (sub.typ sub arg) (sub.typ sub ret)
106106
| Ptyp_tuple tyl -> tuple ~loc ~attrs (List.map (sub.typ sub) tyl)
107107
| Ptyp_constr (lid, tl) ->
108108
constr ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tl)

compiler/ml/ast_helper.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ module Typ = struct
5454

5555
let any ?loc ?attrs () = mk ?loc ?attrs Ptyp_any
5656
let var ?loc ?attrs a = mk ?loc ?attrs (Ptyp_var a)
57-
let arrow ?loc ?attrs ~arity a b c =
58-
mk ?loc ?attrs (Ptyp_arrow (a, b, c, arity))
57+
let arrow ?loc ?attrs ~arity lbl arg ret =
58+
mk ?loc ?attrs (Ptyp_arrow {lbl; arg; ret; arity})
5959
let tuple ?loc ?attrs a = mk ?loc ?attrs (Ptyp_tuple a)
6060
let constr ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_constr (a, b))
6161
let object_ ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_object (a, b))
@@ -82,8 +82,8 @@ module Typ = struct
8282
| Ptyp_var x ->
8383
check_variable var_names t.ptyp_loc x;
8484
Ptyp_var x
85-
| Ptyp_arrow (label, core_type, core_type', a) ->
86-
Ptyp_arrow (label, loop core_type, loop core_type', a)
85+
| Ptyp_arrow {lbl = label; arg; ret; arity = a} ->
86+
Ptyp_arrow {lbl = label; arg = loop arg; ret = loop ret; arity = a}
8787
| Ptyp_tuple lst -> Ptyp_tuple (List.map loop lst)
8888
| Ptyp_constr ({txt = Longident.Lident s}, []) when List.mem s var_names
8989
->

compiler/ml/ast_iterator.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ module T = struct
9696
sub.attributes sub attrs;
9797
match desc with
9898
| Ptyp_any | Ptyp_var _ -> ()
99-
| Ptyp_arrow (_lab, t1, t2, _) ->
100-
sub.typ sub t1;
101-
sub.typ sub t2
99+
| Ptyp_arrow {arg; ret} ->
100+
sub.typ sub arg;
101+
sub.typ sub ret
102102
| Ptyp_tuple tyl -> List.iter (sub.typ sub) tyl
103103
| Ptyp_constr (lid, tl) ->
104104
iter_loc sub lid;

compiler/ml/ast_mapper.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ module T = struct
9393
match desc with
9494
| Ptyp_any -> any ~loc ~attrs ()
9595
| Ptyp_var s -> var ~loc ~attrs s
96-
| Ptyp_arrow (lab, t1, t2, arity) ->
97-
arrow ~loc ~attrs ~arity lab (sub.typ sub t1) (sub.typ sub t2)
96+
| Ptyp_arrow {lbl; arg; ret; arity} ->
97+
arrow ~loc ~attrs ~arity lbl (sub.typ sub arg) (sub.typ sub ret)
9898
| Ptyp_tuple tyl -> tuple ~loc ~attrs (List.map (sub.typ sub) tyl)
9999
| Ptyp_constr (lid, tl) ->
100100
constr ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tl)

compiler/ml/ast_mapper_from0.ml

+2-3
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ module T = struct
106106
constr ~loc ~attrs (map_loc sub lid) (List.map (sub.typ sub) tl)
107107
in
108108
match typ0.ptyp_desc with
109-
| Ptyp_constr
110-
(lid, [({ptyp_desc = Ptyp_arrow (lbl, t1, t2, _)} as fun_t); t_arity])
109+
| Ptyp_constr (lid, [({ptyp_desc = Ptyp_arrow arr} as fun_t); t_arity])
111110
when lid.txt = Lident "function$" ->
112111
let decode_arity_string arity_s =
113112
int_of_string
@@ -120,7 +119,7 @@ module T = struct
120119
| _ -> assert false
121120
in
122121
let arity = arity_from_type t_arity in
123-
{fun_t with ptyp_desc = Ptyp_arrow (lbl, t1, t2, Some arity)}
122+
{fun_t with ptyp_desc = Ptyp_arrow {arr with arity = Some arity}}
124123
| _ -> typ0)
125124
| Ptyp_object (l, o) ->
126125
object_ ~loc ~attrs (List.map (object_field sub) l) o

compiler/ml/ast_mapper_to0.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ module T = struct
9898
match desc with
9999
| Ptyp_any -> any ~loc ~attrs ()
100100
| Ptyp_var s -> var ~loc ~attrs s
101-
| Ptyp_arrow (lab, t1, t2, arity) -> (
102-
let typ0 = arrow ~loc ~attrs lab (sub.typ sub t1) (sub.typ sub t2) in
101+
| Ptyp_arrow {lbl; arg; ret; arity} -> (
102+
let typ0 = arrow ~loc ~attrs lbl (sub.typ sub arg) (sub.typ sub ret) in
103103
match arity with
104104
| None -> typ0
105105
| Some arity ->

compiler/ml/ast_uncurried.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
let uncurried_type ~arity (t_arg : Parsetree.core_type) =
44
match t_arg.ptyp_desc with
5-
| Ptyp_arrow (l, t1, t2, _) ->
6-
{t_arg with ptyp_desc = Ptyp_arrow (l, t1, t2, Some arity)}
5+
| Ptyp_arrow arr ->
6+
{t_arg with ptyp_desc = Ptyp_arrow {arr with arity = Some arity}}
77
| _ -> assert false
88

99
let uncurried_fun ?(async = false) ~arity fun_expr =

compiler/ml/depend.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ let rec add_type bv ty =
105105
match ty.ptyp_desc with
106106
| Ptyp_any -> ()
107107
| Ptyp_var _ -> ()
108-
| Ptyp_arrow (_, t1, t2, _) ->
109-
add_type bv t1;
110-
add_type bv t2
108+
| Ptyp_arrow {arg; ret} ->
109+
add_type bv arg;
110+
add_type bv ret
111111
| Ptyp_tuple tl -> List.iter (add_type bv) tl
112112
| Ptyp_constr (c, tl) ->
113113
add bv c;

0 commit comments

Comments
 (0)