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
10 changes: 5 additions & 5 deletions compiler/frontend/ast_core_type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ let from_labels ~loc arity labels : t =
(Ext_list.map2 labels tyvars (fun x y -> Parsetree.Otag (x, [], y)))
Closed
in
Ext_list.fold_right2 labels tyvars result_type
(fun label (* {loc ; txt = label }*) tyvar acc ->
Ast_helper.Typ.arrow ~loc:label.loc ~arity:(Some arity)
{lbl = Asttypes.Labelled label; typ = tyvar}
acc)
let args =
Ext_list.map2 labels tyvars (fun label tyvar ->
{Parsetree.lbl = Asttypes.Labelled label; typ = tyvar})
in
Typ.arrows ~loc args result_type

let make_obj ~loc xs = Typ.object_ ~loc xs Closed

Expand Down
9 changes: 9 additions & 0 deletions compiler/ml/ast_helper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ module Typ = struct
let var ?loc ?attrs a = mk ?loc ?attrs (Ptyp_var a)
let arrow ?loc ?attrs ~arity arg ret =
mk ?loc ?attrs (Ptyp_arrow {arg; ret; arity})
let arrows ?loc ?attrs args ret =
let arity = Some (List.length args) in
let rec build_arrows arity_to_use = function
| [] -> ret
| [arg] -> arrow ?loc ?attrs ~arity:arity_to_use arg ret
| arg :: rest ->
arrow ?loc ?attrs ~arity:arity_to_use arg (build_arrows None rest)
in
build_arrows arity args
let tuple ?loc ?attrs a = mk ?loc ?attrs (Ptyp_tuple a)
let constr ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_constr (a, b))
let object_ ?loc ?attrs a b = mk ?loc ?attrs (Ptyp_object (a, b))
Expand Down
1 change: 1 addition & 0 deletions compiler/ml/ast_helper.mli
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module Typ : sig
val var : ?loc:loc -> ?attrs:attrs -> string -> core_type
val arrow :
?loc:loc -> ?attrs:attrs -> arity:arity -> arg -> core_type -> core_type
val arrows : ?loc:loc -> ?attrs:attrs -> arg list -> core_type -> core_type
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This begins to run into trouble when one would like to use it for examples such as this one:

@obj
external renamed_make: (
  @as("type") ~_type: string,
  @as("WIDTH") ~width: int=?,
  ~normal: float,
) => (_ as 'event) = ""

Currently the @as attributes are stored in each ast node, and they are not really part of arg, so we can't use the current arrows function.

I think we'll need to first change the AST so attributes can be stored in arguments, then store them there instead of in the AST nodes themselves.

I think it makes sense to support type @attr (@attr1 ~x1: t1, @attr2 ~x2:t2) => t, where @attr goes in the attributes of top-level node, and the rest go in the args. Then change the rest of the compiler to pick up attributes from the argument.

@zth I guess this rings a bell with other issue found in the past?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely! I agree.

val tuple : ?loc:loc -> ?attrs:attrs -> core_type list -> core_type
val constr : ?loc:loc -> ?attrs:attrs -> lid -> core_type list -> core_type
val object_ :
Expand Down
Loading