Skip to content

Commit b2ebcc0

Browse files
committed
remove explicit uses of function$ in preparation for removing the type entirely
1 parent 1551925 commit b2ebcc0

33 files changed

+216
-284
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- Build with OCaml 5.2.1. https://github.com/rescript-lang/rescript-compiler/pull/7201
3232
- AST cleanup: Remove `Function$` entirely for function definitions. https://github.com/rescript-lang/rescript/pull/7200
3333
- AST cleanup: store arity in function type https://github.com/rescript-lang/rescript/pull/7195
34+
- AST cleanup: remove explicit uses of `function$` in preparation for removing the type entirely. https://github.com/rescript-lang/rescript/pull/7206
3435

3536
# 12.0.0-alpha.5
3637

analysis/src/CompletionBackEnd.ml

+2-6
Original file line numberDiff line numberDiff line change
@@ -1358,12 +1358,8 @@ let rec completeTypedValue ?(typeArgContext : typeArgContext option) ~rawOpens
13581358
in
13591359
(* Find all functions in the module that returns type t *)
13601360
let rec fnReturnsTypeT t =
1361-
match t.Types.desc with
1362-
| Tlink t1
1363-
| Tsubst t1
1364-
| Tpoly (t1, [])
1365-
| Tconstr (Pident {name = "function$"}, [t1], _) ->
1366-
fnReturnsTypeT t1
1361+
match (Ast_uncurried.remove_function_dollar t).desc with
1362+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> fnReturnsTypeT t1
13671363
| Tarrow _ -> (
13681364
match TypeUtils.extractFunctionType ~env ~package:full.package t with
13691365
| ( (Nolabel, {desc = Tconstr (Path.Pident {name = "t"}, _, _)}) :: _,

analysis/src/CompletionJsx.ml

+2-6
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,8 @@ let getJsxLabels ~componentPath ~findTypeOfValue ~package =
234234
| _ -> []
235235
in
236236
let rec getLabels (t : Types.type_expr) =
237-
match t.desc with
238-
| Tlink t1
239-
| Tsubst t1
240-
| Tpoly (t1, [])
241-
| Tconstr (Pident {name = "function$"}, [t1], _) ->
242-
getLabels t1
237+
match (Ast_uncurried.remove_function_dollar t).desc with
238+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> getLabels t1
243239
| Tconstr (p, [propsType], _) when Path.name p = "React.component" -> (
244240
let rec getPropsType (t : Types.type_expr) =
245241
match t.desc with

analysis/src/CreateInterface.ml

+2-3
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,11 @@ let printSignature ~extractor ~signature =
118118

119119
let buf = Buffer.create 10 in
120120

121-
let rec getComponentType (typ : Types.type_expr) =
121+
let getComponentType (typ : Types.type_expr) =
122122
let reactElement =
123123
Ctype.newconstr (Pdot (Pident (Ident.create "React"), "element", 0)) []
124124
in
125-
match typ.desc with
126-
| Tconstr (Pident {name = "function$"}, [typ], _) -> getComponentType typ
125+
match (Ast_uncurried.remove_function_dollar typ).desc with
127126
| Tarrow
128127
(_, {desc = Tconstr (Path.Pident propsId, typeArgs, _)}, retType, _, _)
129128
when Ident.name propsId = "props" ->

analysis/src/SignatureHelp.ml

+7-19
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,13 @@ let findFunctionType ~currentFile ~debug ~path ~pos =
104104
(* Extracts all parameters from a parsed function signature *)
105105
let extractParameters ~signature ~typeStrForParser ~labelPrefixLen =
106106
match signature with
107-
| [
108-
( {
109-
Parsetree.psig_desc =
110-
Psig_value {pval_type = {ptyp_desc = Ptyp_arrow _} as expr};
111-
}
112-
| {
113-
psig_desc =
114-
Psig_value
115-
{
116-
pval_type =
117-
{
118-
ptyp_desc =
119-
Ptyp_constr
120-
( {txt = Lident "function$"},
121-
[({ptyp_desc = Ptyp_arrow _} as expr)] );
122-
};
123-
};
124-
} );
125-
] ->
107+
| [{Parsetree.psig_desc = Psig_value {pval_type = expr}}]
108+
when match
109+
(Ast_uncurried.core_type_remove_function_dollar expr).ptyp_desc
110+
with
111+
| Ptyp_arrow _ -> true
112+
| _ -> false ->
113+
let expr = Ast_uncurried.core_type_remove_function_dollar expr in
126114
let rec extractParams expr params =
127115
match expr with
128116
| {

analysis/src/TypeUtils.ml

+9-24
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ let findTypeViaLoc ~full ~debug (loc : Location.t) =
3434
| Some {locType = Typed (_, typExpr, _)} -> Some typExpr
3535
| _ -> None
3636

37-
let rec pathFromTypeExpr (t : Types.type_expr) =
38-
match t.desc with
39-
| Tconstr (Pident {name = "function$"}, [t], _) -> pathFromTypeExpr t
37+
let pathFromTypeExpr (t : Types.type_expr) =
38+
match (Ast_uncurried.remove_function_dollar t).desc with
4039
| Tconstr (path, _typeArgs, _)
4140
| Tlink {desc = Tconstr (path, _typeArgs, _)}
4241
| Tsubst {desc = Tconstr (path, _typeArgs, _)}
@@ -238,13 +237,11 @@ let rec extractObjectType ~env ~package (t : Types.type_expr) =
238237
| _ -> None)
239238
| _ -> None
240239

241-
let rec extractFunctionType ~env ~package typ =
240+
let extractFunctionType ~env ~package typ =
242241
let rec loop ~env acc (t : Types.type_expr) =
243-
match t.desc with
242+
match (Ast_uncurried.remove_function_dollar t).desc with
244243
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> loop ~env acc t1
245244
| Tarrow (label, tArg, tRet, _, _) -> loop ~env ((label, tArg) :: acc) tRet
246-
| Tconstr (Pident {name = "function$"}, [t], _) ->
247-
extractFunctionType ~env ~package t
248245
| Tconstr (path, typeArgs, _) -> (
249246
match References.digConstructor ~env ~package path with
250247
| Some
@@ -277,14 +274,12 @@ let maybeSetTypeArgCtx ?typeArgContextFromTypeManifest ~typeParams ~typeArgs env
277274
typeArgContext
278275

279276
(* TODO(env-stuff) Maybe this could be removed entirely if we can guarantee that we don't have to look up functions from in here. *)
280-
let rec extractFunctionType2 ?typeArgContext ~env ~package typ =
277+
let extractFunctionType2 ?typeArgContext ~env ~package typ =
281278
let rec loop ?typeArgContext ~env acc (t : Types.type_expr) =
282-
match t.desc with
279+
match (Ast_uncurried.remove_function_dollar t).desc with
283280
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> loop ?typeArgContext ~env acc t1
284281
| Tarrow (label, tArg, tRet, _, _) ->
285282
loop ?typeArgContext ~env ((label, tArg) :: acc) tRet
286-
| Tconstr (Pident {name = "function$"}, [t], _) ->
287-
extractFunctionType2 ?typeArgContext ~env ~package t
288283
| Tconstr (path, typeArgs, _) -> (
289284
match References.digConstructor ~env ~package path with
290285
| Some
@@ -317,7 +312,7 @@ let rec extractType ?(printOpeningDebug = true)
317312
Printf.printf "[extract_type]--> %s"
318313
(debugLogTypeArgContext typeArgContext));
319314
let instantiateType = instantiateType2 in
320-
match t.desc with
315+
match (Ast_uncurried.remove_function_dollar t).desc with
321316
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) ->
322317
extractType ?typeArgContext ~printOpeningDebug:false ~env ~package t1
323318
| Tconstr (Path.Pident {name = "option"}, [payloadTypeExpr], _) ->
@@ -334,13 +329,6 @@ let rec extractType ?(printOpeningDebug = true)
334329
Some (Tstring env, typeArgContext)
335330
| Tconstr (Path.Pident {name = "exn"}, [], _) ->
336331
Some (Texn env, typeArgContext)
337-
| Tconstr (Pident {name = "function$"}, [t], _) -> (
338-
match extractFunctionType2 ?typeArgContext t ~env ~package with
339-
| args, tRet, typeArgContext when args <> [] ->
340-
Some
341-
( Tfunction {env; args; typ = t; uncurried = true; returnType = tRet},
342-
typeArgContext )
343-
| _args, _tRet, _typeArgContext -> None)
344332
| Tarrow _ -> (
345333
match extractFunctionType2 ?typeArgContext t ~env ~package with
346334
| args, tRet, typeArgContext when args <> [] ->
@@ -906,11 +894,8 @@ let rec resolveNestedPatternPath (typ : innerType) ~env ~full ~nested =
906894
let getArgs ~env (t : Types.type_expr) ~full =
907895
let rec getArgsLoop ~env (t : Types.type_expr) ~full ~currentArgumentPosition
908896
=
909-
match t.desc with
910-
| Tlink t1
911-
| Tsubst t1
912-
| Tpoly (t1, [])
913-
| Tconstr (Pident {name = "function$"}, [t1], _) ->
897+
match (Ast_uncurried.remove_function_dollar t).desc with
898+
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) ->
914899
getArgsLoop ~full ~env ~currentArgumentPosition t1
915900
| Tarrow (Labelled l, tArg, tRet, _, _) ->
916901
(SharedTypes.Completable.Labelled l, tArg)

compiler/frontend/ast_core_type.ml

+18-10
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ let get_uncurry_arity (ty : t) =
124124
| _ -> None
125125

126126
let get_curry_arity (ty : t) =
127-
if Ast_uncurried.core_type_is_uncurried_fun ty then
128-
let arity, _ = Ast_uncurried.core_type_extract_uncurried_fun ty in
129-
arity
130-
else get_uncurry_arity_aux ty 0
127+
match Ast_uncurried.core_type_remove_function_dollar ty with
128+
| {ptyp_desc = Ptyp_arrow (_, _, _, Some arity)} -> arity
129+
| _ -> get_uncurry_arity_aux ty 0
130+
131131
let is_arity_one ty = get_curry_arity ty = 1
132132

133133
type param_type = {
@@ -138,12 +138,20 @@ type param_type = {
138138
}
139139

140140
let mk_fn_type (new_arg_types_ty : param_type list) (result : t) : t =
141-
Ext_list.fold_right new_arg_types_ty result (fun {label; ty; attr; loc} acc ->
142-
{
143-
ptyp_desc = Ptyp_arrow (label, ty, acc, None);
144-
ptyp_loc = loc;
145-
ptyp_attributes = attr;
146-
})
141+
let t =
142+
Ext_list.fold_right new_arg_types_ty result
143+
(fun {label; ty; attr; loc} acc ->
144+
{
145+
ptyp_desc = Ptyp_arrow (label, ty, acc, None);
146+
ptyp_loc = loc;
147+
ptyp_attributes = attr;
148+
})
149+
in
150+
match t.ptyp_desc with
151+
| Ptyp_arrow (l, t1, t2, _arity) ->
152+
let arity = List.length new_arg_types_ty in
153+
{t with ptyp_desc = Ptyp_arrow (l, t1, t2, Some arity)}
154+
| _ -> t
147155

148156
let list_of_arrow (ty : t) : t * param_type list =
149157
let rec aux (ty : t) acc =

compiler/frontend/ast_core_type_class_type.ml

+8-16
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,20 @@ let default_typ_mapper = Bs_ast_mapper.default_mapper.typ
6565
*)
6666

6767
let typ_mapper (self : Bs_ast_mapper.mapper) (ty : Parsetree.core_type) =
68-
match ty with
69-
| {
70-
ptyp_attributes;
71-
ptyp_desc =
72-
( Ptyp_arrow (label, args, body, _)
73-
| Ptyp_constr
74-
(* function$<...> is re-wrapped around only in case Nothing below *)
75-
( {txt = Lident "function$"},
76-
[{ptyp_desc = Ptyp_arrow (label, args, body, _)}] ) );
77-
(* let it go without regard label names,
78-
it will report error later when the label is not empty
79-
*)
80-
ptyp_loc = loc;
81-
} -> (
82-
match fst (Ast_attributes.process_attributes_rev ptyp_attributes) with
68+
let loc = ty.ptyp_loc in
69+
match (Ast_uncurried.core_type_remove_function_dollar ty).ptyp_desc with
70+
| Ptyp_arrow (label, args, body, _)
71+
(* let it go without regard label names,
72+
it will report error later when the label is not empty
73+
*) -> (
74+
match fst (Ast_attributes.process_attributes_rev ty.ptyp_attributes) with
8375
| Meth_callback _ ->
8476
Ast_typ_uncurry.to_method_callback_type loc self label args body
8577
| Method _ ->
8678
(* Treat @meth as making the type uncurried, for backwards compatibility *)
8779
Ast_typ_uncurry.to_uncurry_type loc self label args body
8880
| Nothing -> Bs_ast_mapper.default_mapper.typ self ty)
89-
| {ptyp_desc = Ptyp_object (methods, closed_flag); ptyp_loc = loc} ->
81+
| Ptyp_object (methods, closed_flag) ->
9082
let ( +> ) attr (typ : Parsetree.core_type) =
9183
{typ with ptyp_attributes = attr :: typ.ptyp_attributes}
9284
in

compiler/frontend/ast_derive_abstract.ml

+13-6
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,25 @@ let handle_tdcl light (tdcl : Parsetree.type_declaration) :
105105
let is_optional = Ast_attributes.has_bs_optional pld_attributes in
106106

107107
let maker, acc =
108+
let arity =
109+
if List.length labels = List.length label_declarations - 1 then
110+
(* toplevel type *)
111+
Some ((if has_optional_field then 2 else 1) + List.length labels)
112+
else None
113+
in
108114
if is_optional then
109115
let optional_type = Ast_core_type.lift_option_type pld_type in
110-
( Ast_compatible.opt_arrow ~loc:pld_loc ~arity:None label_name
111-
pld_type maker,
116+
( Ast_compatible.opt_arrow ~loc:pld_loc ~arity label_name pld_type
117+
maker,
112118
Val.mk ~loc:pld_loc
113119
(if light then pld_name
114120
else {pld_name with txt = pld_name.txt ^ "Get"})
115121
~attrs:get_optional_attrs ~prim
116-
(Ast_compatible.arrow ~loc ~arity:None core_type optional_type)
122+
(Ast_compatible.arrow ~loc ~arity:(Some 1) core_type
123+
optional_type)
117124
:: acc )
118125
else
119-
( Ast_compatible.label_arrow ~loc:pld_loc ~arity:None label_name
126+
( Ast_compatible.label_arrow ~loc:pld_loc ~arity label_name
120127
pld_type maker,
121128
Val.mk ~loc:pld_loc
122129
(if light then pld_name
@@ -127,14 +134,14 @@ let handle_tdcl light (tdcl : Parsetree.type_declaration) :
127134
External_ffi_types.ffi_bs_as_prims
128135
[External_arg_spec.dummy] Return_identity
129136
(Js_get {js_get_name = prim_as_name; js_get_scopes = []}))
130-
(Ast_compatible.arrow ~loc ~arity:None core_type pld_type)
137+
(Ast_compatible.arrow ~loc ~arity:(Some 1) core_type pld_type)
131138
:: acc )
132139
in
133140
let is_current_field_mutable = pld_mutable = Mutable in
134141
let acc =
135142
if is_current_field_mutable then
136143
let setter_type =
137-
Ast_compatible.arrow ~arity:None core_type
144+
Ast_compatible.arrow ~arity:(Some 2) core_type
138145
(Ast_compatible.arrow ~arity:None pld_type (* setter *)
139146
(Ast_literal.type_unit ()))
140147
in

compiler/frontend/ast_derive_js_mapper.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ let erase_type_str =
6767
Str.primitive
6868
(Val.mk ~prim:["%identity"]
6969
{loc = noloc; txt = erase_type_lit}
70-
(Ast_compatible.arrow ~arity:None any any))
70+
(Ast_compatible.arrow ~arity:(Some 1) any any))
7171

7272
let unsafe_index = "_index"
7373

compiler/frontend/ast_exp_handle_external.ml

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ let handle_external loc (x : string) : Parsetree.expression =
4343
str_exp with
4444
pexp_desc =
4545
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_expr"]
46-
~pval_type:(Typ.arrow ~arity:None Nolabel (Typ.any ()) (Typ.any ()))
46+
~pval_type:
47+
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ()) (Typ.any ()))
4748
[str_exp];
4849
}
4950
in
@@ -69,7 +70,8 @@ let handle_debugger loc (payload : Ast_payload.t) =
6970
| PStr [] ->
7071
Ast_external_mk.local_external_apply loc ~pval_prim:["%debugger"]
7172
~pval_type:
72-
(Typ.arrow ~arity:None Nolabel (Typ.any ()) (Ast_literal.type_unit ()))
73+
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ())
74+
(Ast_literal.type_unit ()))
7375
[Ast_literal.val_unit ~loc ()]
7476
| _ ->
7577
Location.raise_errorf ~loc "%%debugger extension doesn't accept arguments"
@@ -93,7 +95,8 @@ let handle_raw ~kind loc payload =
9395
exp with
9496
pexp_desc =
9597
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_expr"]
96-
~pval_type:(Typ.arrow ~arity:None Nolabel (Typ.any ()) (Typ.any ()))
98+
~pval_type:
99+
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ()) (Typ.any ()))
97100
[exp];
98101
pexp_attributes =
99102
(match !is_function with
@@ -142,7 +145,8 @@ let handle_ffi ~loc ~payload =
142145
exp with
143146
pexp_desc =
144147
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_expr"]
145-
~pval_type:(Typ.arrow ~arity:None Nolabel (Typ.any ()) (Typ.any ()))
148+
~pval_type:
149+
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ()) (Typ.any ()))
146150
[exp];
147151
pexp_attributes =
148152
(match !is_function with
@@ -158,7 +162,8 @@ let handle_raw_structure loc payload =
158162
exp with
159163
pexp_desc =
160164
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_stmt"]
161-
~pval_type:(Typ.arrow ~arity:None Nolabel (Typ.any ()) (Typ.any ()))
165+
~pval_type:
166+
(Typ.arrow ~arity:(Some 1) Nolabel (Typ.any ()) (Typ.any ()))
162167
[exp];
163168
}
164169
| None ->

compiler/frontend/ast_external_process.ml

+5-14
Original file line numberDiff line numberDiff line change
@@ -934,17 +934,11 @@ let handle_attributes (loc : Bs_loc.t) (type_annotation : Parsetree.core_type)
934934
Parsetree.core_type * External_ffi_types.t * Parsetree.attributes * bool =
935935
let prim_name_with_source = {name = prim_name; source = External} in
936936
let type_annotation, build_uncurried_type =
937-
match type_annotation.ptyp_desc with
938-
| Ptyp_constr (({txt = Lident "function$"; _} as lid), [t]) ->
937+
match Ast_uncurried.core_type_remove_function_dollar type_annotation with
938+
| {ptyp_desc = Ptyp_arrow (_, _, _, Some _); _} as t ->
939939
( t,
940940
fun ~arity (x : Parsetree.core_type) ->
941-
let x =
942-
match x.ptyp_desc with
943-
| Ptyp_arrow (l, t1, t2, _) ->
944-
{x with ptyp_desc = Ptyp_arrow (l, t1, t2, arity)}
945-
| _ -> x
946-
in
947-
{x with Parsetree.ptyp_desc = Ptyp_constr (lid, [x])} )
941+
Ast_uncurried.uncurried_type ~loc ~arity x )
948942
| _ -> (type_annotation, fun ~arity:_ x -> x)
949943
in
950944
let result_type, arg_types_ty =
@@ -961,10 +955,7 @@ let handle_attributes (loc : Bs_loc.t) (type_annotation : Parsetree.core_type)
961955
let arity, new_type, spec =
962956
process_obj loc external_desc prim_name arg_types_ty result_type
963957
in
964-
( build_uncurried_type ~arity:(Some arity) new_type,
965-
spec,
966-
unused_attrs,
967-
false )
958+
(build_uncurried_type ~arity new_type, spec, unused_attrs, false)
968959
else
969960
let splice = external_desc.splice in
970961
let arg_type_specs, new_arg_types_ty, arg_type_specs_length =
@@ -1036,7 +1027,7 @@ let handle_attributes (loc : Bs_loc.t) (type_annotation : Parsetree.core_type)
10361027
check_return_wrapper loc external_desc.return_wrapper result_type
10371028
in
10381029
let fn_type = Ast_core_type.mk_fn_type new_arg_types_ty result_type in
1039-
( build_uncurried_type ~arity:(Some (List.length new_arg_types_ty)) fn_type,
1030+
( build_uncurried_type ~arity:(List.length new_arg_types_ty) fn_type,
10401031
External_ffi_types.ffi_bs arg_type_specs return_wrapper ffi,
10411032
unused_attrs,
10421033
relative )

0 commit comments

Comments
 (0)