Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#### :nail_care: Polish

- Add missing backtick and spaces to `Belt.Map.map` doc comment. https://github.com/rescript-lang/rescript/pull/7632
- AST: store the attributes directly on function arguments. https://github.com/rescript-lang/rescript/pull/7660

#### :house: Internal

Expand Down
12 changes: 4 additions & 8 deletions compiler/frontend/ast_core_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ let from_labels ~loc arity labels : t =
in
let args =
Ext_list.map2 labels tyvars (fun label tyvar ->
{Parsetree.lbl = Asttypes.Labelled label; typ = tyvar})
{Parsetree.attrs = []; lbl = Asttypes.Labelled label; typ = tyvar})
in
Typ.arrows ~loc args result_type

Expand Down Expand Up @@ -143,7 +143,8 @@ let mk_fn_type (new_arg_types_ty : param_type list) (result : t) : t =
Ext_list.fold_right new_arg_types_ty result
(fun {label; ty; attr; loc} acc ->
Ast_helper.Typ.arrow ~loc ~attrs:attr ~arity:None
{lbl = label; typ = ty} acc)
{attrs = []; lbl = label; typ = ty}
acc)
in
match t.ptyp_desc with
| Ptyp_arrow arr ->
Expand All @@ -156,12 +157,7 @@ let list_of_arrow (ty : t) : t * param_type list =
match ty.ptyp_desc with
| Ptyp_arrow {arg; ret; arity} when arity = None || acc = [] ->
aux ret
(({
label = arg.lbl;
ty = arg.typ;
attr = ty.ptyp_attributes;
loc = ty.ptyp_loc;
}
(({label = arg.lbl; ty = arg.typ; attr = arg.attrs; loc = ty.ptyp_loc}
: param_type)
:: acc)
| Ptyp_poly (_, ty) ->
Expand Down
14 changes: 7 additions & 7 deletions compiler/frontend/ast_derive_abstract.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let handle_tdcl light (tdcl : Parsetree.type_declaration) :
( [],
(if has_optional_field then
Ast_helper.Typ.arrow ~loc ~arity:None
{lbl = Nolabel; typ = Ast_literal.type_unit ()}
{attrs = []; lbl = Nolabel; typ = Ast_literal.type_unit ()}
core_type
else core_type),
[] )
Expand Down Expand Up @@ -116,19 +116,19 @@ let handle_tdcl light (tdcl : Parsetree.type_declaration) :
if is_optional then
let optional_type = Ast_core_type.lift_option_type pld_type in
( Ast_helper.Typ.arrow ~loc:pld_loc ~arity
{lbl = Asttypes.Optional pld_name; typ = pld_type}
{attrs = []; lbl = Asttypes.Optional pld_name; typ = pld_type}
maker,
Val.mk ~loc:pld_loc
(if light then pld_name
else {pld_name with txt = pld_name.txt ^ "Get"})
~attrs:get_optional_attrs ~prim
(Ast_helper.Typ.arrow ~loc ~arity:(Some 1)
{lbl = Nolabel; typ = core_type}
{attrs = []; lbl = Nolabel; typ = core_type}
optional_type)
:: acc )
else
( Ast_helper.Typ.arrow ~loc:pld_loc ~arity
{lbl = Asttypes.Labelled pld_name; typ = pld_type}
{attrs = []; lbl = Asttypes.Labelled pld_name; typ = pld_type}
maker,
Val.mk ~loc:pld_loc
(if light then pld_name
Expand All @@ -140,7 +140,7 @@ let handle_tdcl light (tdcl : Parsetree.type_declaration) :
[External_arg_spec.dummy] Return_identity
(Js_get {js_get_name = prim_as_name; js_get_scopes = []}))
(Ast_helper.Typ.arrow ~loc ~arity:(Some 1)
{lbl = Nolabel; typ = core_type}
{attrs = []; lbl = Nolabel; typ = core_type}
pld_type)
:: acc )
in
Expand All @@ -149,9 +149,9 @@ let handle_tdcl light (tdcl : Parsetree.type_declaration) :
if is_current_field_mutable then
let setter_type =
Ast_helper.Typ.arrow ~arity:(Some 2)
{lbl = Nolabel; typ = core_type}
{attrs = []; lbl = Nolabel; typ = core_type}
(Ast_helper.Typ.arrow ~arity:None
{lbl = Nolabel; typ = pld_type} (* setter *)
{attrs = []; lbl = Nolabel; typ = pld_type} (* setter *)
(Ast_literal.type_unit ()))
in
Val.mk ~loc:pld_loc
Expand Down
15 changes: 10 additions & 5 deletions compiler/frontend/ast_derive_js_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ let erase_type_str =
Str.primitive
(Val.mk ~prim:["%identity"]
{loc = noloc; txt = erase_type_lit}
(Ast_helper.Typ.arrow ~arity:(Some 1) {lbl = Nolabel; typ = any} any))
(Ast_helper.Typ.arrow ~arity:(Some 1)
{attrs = []; lbl = Nolabel; typ = any}
any))

let unsafe_index = "_index"

Expand All @@ -79,8 +81,11 @@ let unsafe_index_get =
(Val.mk ~prim:[""]
{loc = noloc; txt = unsafe_index}
~attrs:[Ast_attributes.get_index]
(Ast_helper.Typ.arrow ~arity:None {lbl = Nolabel; typ = any}
(Ast_helper.Typ.arrow ~arity:None {lbl = Nolabel; typ = any} any)))
(Ast_helper.Typ.arrow ~arity:None
{attrs = []; lbl = Nolabel; typ = any}
(Ast_helper.Typ.arrow ~arity:None
{attrs = []; lbl = Nolabel; typ = any}
any)))

let unsafe_index_get_exp = Exp.ident {loc = noloc; txt = Lident unsafe_index}

Expand Down Expand Up @@ -132,7 +137,7 @@ let app1 = Ast_compatible.app1
let app2 = Ast_compatible.app2

let ( ->~ ) a b =
Ast_helper.Typ.arrow ~arity:(Some 1) {lbl = Nolabel; typ = a} b
Ast_helper.Typ.arrow ~arity:(Some 1) {attrs = []; lbl = Nolabel; typ = a} b

let raise_when_not_found_ident =
Longident.Ldot (Lident Primitive_modules.util, "raiseWhenNotFound")
Expand Down Expand Up @@ -305,7 +310,7 @@ let init () =
let to_js_type result =
Ast_comb.single_non_rec_val pat_to_js
(Ast_helper.Typ.arrow ~arity:(Some 1)
{lbl = Nolabel; typ = core_type}
{attrs = []; lbl = Nolabel; typ = core_type}
result)
in
let new_type, new_tdcl =
Expand Down
5 changes: 3 additions & 2 deletions compiler/frontend/ast_derive_projector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ let init () =
Ext_list.map label_declarations (fun {pld_name; pld_type} ->
Ast_comb.single_non_rec_val ?attrs:gentype_attrs pld_name
(Ast_helper.Typ.arrow ~arity:(Some 1)
{lbl = Nolabel; typ = core_type}
{attrs = []; lbl = Nolabel; typ = core_type}
pld_type
(*arity will alwys be 1 since these are single param functions*)))
| Ptype_variant constructor_declarations ->
Expand Down Expand Up @@ -170,7 +170,8 @@ let init () =
{loc; txt = Ext_string.uncapitalize_ascii con_name}
(Ext_list.fold_right pcd_args annotate_type (fun x acc ->
Ast_helper.Typ.arrow ~arity:None
{lbl = Nolabel; typ = x} acc)
{attrs = []; lbl = Nolabel; typ = x}
acc)
|> add_arity ~arity))
| Ptype_open | Ptype_abstract ->
Ast_derive_util.not_applicable tdcl.ptype_loc deriving_name;
Expand Down
21 changes: 13 additions & 8 deletions compiler/frontend/ast_exp_handle_external.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let handle_external loc (x : string) : Parsetree.expression =
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_expr"]
~pval_type:
(Ast_helper.Typ.arrow ~arity:(Some 1)
{lbl = Nolabel; typ = Ast_helper.Typ.any ()}
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}
(Ast_helper.Typ.any ()))
[str_exp];
}
Expand Down Expand Up @@ -72,7 +72,7 @@ let handle_debugger loc (payload : Ast_payload.t) =
Ast_external_mk.local_external_apply loc ~pval_prim:["%debugger"]
~pval_type:
(Ast_helper.Typ.arrow ~arity:(Some 1)
{lbl = Nolabel; typ = Ast_helper.Typ.any ()}
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}
(Ast_literal.type_unit ()))
[Ast_literal.val_unit ~loc ()]
| _ ->
Expand All @@ -99,7 +99,7 @@ let handle_raw ~kind loc payload =
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_expr"]
~pval_type:
(Ast_helper.Typ.arrow ~arity:(Some 1)
{lbl = Nolabel; typ = Ast_helper.Typ.any ()}
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}
(Ast_helper.Typ.any ()))
[exp];
pexp_attributes =
Expand Down Expand Up @@ -128,11 +128,16 @@ let handle_ffi ~loc ~payload =
let unit = Ast_literal.type_unit ~loc () in
let rec arrow ~arity =
if arity = 0 then
Ast_helper.Typ.arrow ~arity:None ~loc {lbl = Nolabel; typ = unit} any
Ast_helper.Typ.arrow ~arity:None ~loc
{attrs = []; lbl = Nolabel; typ = unit}
any
else if arity = 1 then
Ast_helper.Typ.arrow ~arity:None ~loc {lbl = Nolabel; typ = any} any
Ast_helper.Typ.arrow ~arity:None ~loc
{attrs = []; lbl = Nolabel; typ = any}
any
else
Ast_helper.Typ.arrow ~loc ~arity:None {lbl = Nolabel; typ = any}
Ast_helper.Typ.arrow ~loc ~arity:None
{attrs = []; lbl = Nolabel; typ = any}
(arrow ~arity:(arity - 1))
in
match !is_function with
Expand All @@ -152,7 +157,7 @@ let handle_ffi ~loc ~payload =
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_expr"]
~pval_type:
(Ast_helper.Typ.arrow ~arity:(Some 1)
{lbl = Nolabel; typ = Ast_helper.Typ.any ()}
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}
(Ast_helper.Typ.any ()))
[exp];
pexp_attributes =
Expand All @@ -171,7 +176,7 @@ let handle_raw_structure loc payload =
Ast_external_mk.local_external_apply loc ~pval_prim:["#raw_stmt"]
~pval_type:
(Ast_helper.Typ.arrow ~arity:(Some 1)
{lbl = Nolabel; typ = Ast_helper.Typ.any ()}
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}
(Ast_helper.Typ.any ()))
[exp];
}
Expand Down
8 changes: 6 additions & 2 deletions compiler/frontend/ast_typ_uncurry.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ let to_method_callback_type loc (mapper : Bs_ast_mapper.mapper)
let first_arg = mapper.typ mapper first_arg in
let typ = mapper.typ mapper typ in
let meth_type =
Ast_helper.Typ.arrow ~loc ~arity:None {lbl = label; typ = first_arg} typ
Ast_helper.Typ.arrow ~loc ~arity:None
{attrs = []; lbl = label; typ = first_arg}
typ
in
let arity = Ast_core_type.get_uncurry_arity meth_type in
match arity with
Expand All @@ -58,7 +60,9 @@ let to_uncurry_type loc (mapper : Bs_ast_mapper.mapper)
let typ = mapper.typ mapper typ in

let fn_type =
Ast_helper.Typ.arrow ~loc ~arity:None {lbl = label; typ = first_arg} typ
Ast_helper.Typ.arrow ~loc ~arity:None
{attrs = []; lbl = label; typ = first_arg}
typ
in
let arity = Ast_core_type.get_uncurry_arity fn_type in
let fn_type =
Expand Down
4 changes: 2 additions & 2 deletions compiler/ml/ast_mapper_from0.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ module T = struct
| Ptyp_var s -> Typ.var ~loc ~attrs s
| Ptyp_arrow (lbl, t1, t2) ->
let lbl = Asttypes.to_arg_label lbl in
Typ.arrow ~loc ~attrs ~arity:None
{lbl; typ = sub.typ sub t1}
Typ.arrow ~loc ~arity:None
{attrs; lbl; typ = sub.typ sub t1}
(sub.typ sub t2)
| Ptyp_tuple tyl -> Typ.tuple ~loc ~attrs (List.map (sub.typ sub) tyl)
| Ptyp_constr (lid, tl) -> (
Expand Down
2 changes: 1 addition & 1 deletion compiler/ml/parsetree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ and core_type = {
ptyp_attributes: attributes; (* ... [@id1] [@id2] *)
}

and arg = {lbl: arg_label; typ: core_type}
and arg = {attrs: attributes; lbl: arg_label; typ: core_type}

and core_type_desc =
| Ptyp_any (* _ *)
Expand Down
16 changes: 10 additions & 6 deletions compiler/ml/pprintast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,15 @@ let tyvar f str = pp f "'%s" str
let tyvar_loc f str = pp f "'%s" str.txt
let string_quot f x = pp f "`%s" x

let rec type_with_label ctxt f (label, c) =
match label with
| Nolabel -> core_type1 ctxt f c (* otherwise parenthesize *)
| Labelled {txt = s} -> pp f "%s:%a" s (core_type1 ctxt) c
| Optional {txt = s} -> pp f "?%s:%a" s (core_type1 ctxt) c
let rec type_with_label ctxt f arg =
match arg.lbl with
| Nolabel ->
pp f "%a%a" (core_type1 ctxt) arg.typ (attributes ctxt) arg.attrs
(* otherwise parenthesize *)
| Labelled {txt = s} ->
pp f "%s:%a%a" s (core_type1 ctxt) arg.typ (attributes ctxt) arg.attrs
| Optional {txt = s} ->
pp f "?%s:%a%a" s (core_type1 ctxt) arg.typ (attributes ctxt) arg.attrs

and core_type ctxt f x =
if x.ptyp_attributes <> [] then
Expand All @@ -300,7 +304,7 @@ and core_type ctxt f x =
match x.ptyp_desc with
| Ptyp_arrow {arg; ret; arity} ->
pp f "@[<2>%a@;->@;%a%s@]" (* FIXME remove parens later *)
(type_with_label ctxt) (arg.lbl, arg.typ) (core_type ctxt) ret
(type_with_label ctxt) arg (core_type ctxt) ret
(match arity with
| None -> ""
| Some n -> " (a:" ^ string_of_int n ^ ")")
Expand Down
17 changes: 5 additions & 12 deletions compiler/syntax/src/jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -918,20 +918,16 @@ let transform_structure_item ~config item =
|> Option.map Jsx_common.typ_vars_of_core_type
|> Option.value ~default:[]
in
let rec get_prop_types types
({ptyp_loc; ptyp_desc; ptyp_attributes} as full_type) =
let rec get_prop_types types ({ptyp_loc; ptyp_desc} as full_type) =
match ptyp_desc with
| Ptyp_arrow {arg; ret = {ptyp_desc = Ptyp_arrow _} as typ2}
when is_labelled arg.lbl || is_optional arg.lbl ->
get_prop_types
((arg.lbl, ptyp_attributes, ptyp_loc, arg.typ) :: types)
typ2
get_prop_types ((arg.lbl, arg.attrs, ptyp_loc, arg.typ) :: types) typ2
| Ptyp_arrow {arg = {lbl = Nolabel}; ret} -> get_prop_types types ret
| Ptyp_arrow {arg; ret = return_value}
when is_labelled arg.lbl || is_optional arg.lbl ->
( return_value,
(arg.lbl, ptyp_attributes, return_value.ptyp_loc, arg.typ) :: types
)
(arg.lbl, arg.attrs, return_value.ptyp_loc, arg.typ) :: types )
| _ -> (full_type, types)
in
let inner_type, prop_types = get_prop_types [] pval_type in
Expand Down Expand Up @@ -1027,9 +1023,7 @@ let transform_signature_item ~config item =
match ptyp_desc with
| Ptyp_arrow {arg; ret = {ptyp_desc = Ptyp_arrow _} as rest}
when is_optional arg.lbl || is_labelled arg.lbl ->
get_prop_types
((arg.lbl, arg.typ.ptyp_attributes, ptyp_loc, arg.typ) :: types)
rest
get_prop_types ((arg.lbl, arg.attrs, ptyp_loc, arg.typ) :: types) rest
| Ptyp_arrow
{
arg =
Expand All @@ -1045,8 +1039,7 @@ let transform_signature_item ~config item =
| Ptyp_arrow {arg; ret = return_value}
when is_optional arg.lbl || is_labelled arg.lbl ->
( return_value,
(arg.lbl, arg.typ.ptyp_attributes, return_value.ptyp_loc, arg.typ)
:: types )
(arg.lbl, arg.attrs, return_value.ptyp_loc, arg.typ) :: types )
| _ -> (full_type, types)
in
let inner_type, prop_types = get_prop_types [] pval_type in
Expand Down
14 changes: 9 additions & 5 deletions compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4039,7 +4039,8 @@ and parse_poly_type_expr ?current_type_name_path ?inline_types_context p =
let typ = Ast_helper.Typ.var ~loc:var.loc var.txt in
let return_type = parse_typ_expr ~alias:false p in
let loc = mk_loc typ.Parsetree.ptyp_loc.loc_start p.prev_end_pos in
Ast_helper.Typ.arrow ~loc ~arity:(Some 1) {lbl = Nolabel; typ}
Ast_helper.Typ.arrow ~loc ~arity:(Some 1)
{attrs = []; lbl = Nolabel; typ}
return_type
| _ -> Ast_helper.Typ.var ~loc:var.loc var.txt)
| _ -> assert false)
Expand Down Expand Up @@ -4400,7 +4401,7 @@ and parse_es6_arrow_type ~attrs p =
Parser.expect EqualGreater p;
let return_type = parse_typ_expr ~alias:false p in
let loc = mk_loc start_pos p.prev_end_pos in
Ast_helper.Typ.arrow ~loc ~attrs ~arity:None {lbl; typ} return_type
Ast_helper.Typ.arrow ~loc ~arity:None {attrs; lbl; typ} return_type
| DocComment _ -> assert false
| _ ->
let parameters = parse_type_parameters p in
Expand Down Expand Up @@ -4429,7 +4430,7 @@ and parse_es6_arrow_type ~attrs p =
| _ -> arity
in
let t_arg =
Ast_helper.Typ.arrow ~loc ~attrs ~arity:None {lbl = arg_lbl; typ} t
Ast_helper.Typ.arrow ~loc ~arity:None {attrs; lbl = arg_lbl; typ} t
in
if param_num = 1 then
(param_num - 1, Ast_uncurried.uncurried_type ~arity t_arg, 1)
Expand Down Expand Up @@ -4493,7 +4494,9 @@ and parse_arrow_type_rest ~es6_arrow ~start_pos typ p =
Parser.next p;
let return_type = parse_typ_expr ~alias:false p in
let loc = mk_loc start_pos p.prev_end_pos in
Ast_helper.Typ.arrow ~loc ~arity:(Some 1) {lbl = Nolabel; typ} return_type
Ast_helper.Typ.arrow ~loc ~arity:(Some 1)
{attrs = []; lbl = Nolabel; typ}
return_type
| _ -> typ

and parse_typ_expr_region p =
Expand Down Expand Up @@ -5151,7 +5154,8 @@ and parse_type_equation_or_constr_decl p =
let return_type = parse_typ_expr ~alias:false p in
let loc = mk_loc uident_start_pos p.prev_end_pos in
let arrow_type =
Ast_helper.Typ.arrow ~loc ~arity:(Some 1) {lbl = Nolabel; typ}
Ast_helper.Typ.arrow ~loc ~arity:(Some 1)
{attrs = []; lbl = Nolabel; typ}
return_type
in
let typ = parse_type_alias p arrow_type in
Expand Down
Loading