Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Unreleased
- `Dom.*`: we now ship a `melange.dom` library that includes the modules
containing Node.js bindings. This library is included by default so the
`Dom` module will always be available in Melange projects.
- Disable warning 61 (`unboxable-type-in-prim-decl`) for externals generated by
`[@@deriving abstract]`
([#641](https://github.com/melange-re/melange/pull/641),

1.0.0 2023-05-31
---------------
Expand Down
18 changes: 18 additions & 0 deletions ppx/ast_attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,21 @@ let is_inline : attr -> bool =
fun { attr_name = { txt; _ }; _ } -> txt = "bs.inline" || txt = "inline"

let has_inline_payload (attrs : t) = Ext_list.find_first attrs is_inline

(* We disable warning 61 in Melange externals since they're substantially
different from OCaml externals. This warning doesn't make sense for a JS
runtime *)
let unboxable_type_in_prim_decl : Parsetree.attribute =
let open Ast_helper in
{
attr_name = { txt = "ocaml.warning"; loc = Location.none };
attr_payload =
PStr
[
Str.eval
(Exp.constant
(Pconst_string
("-unboxable-type-in-prim-decl", Location.none, None)));
];
attr_loc = Location.none;
}
1 change: 1 addition & 0 deletions ppx/ast_attributes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ val rs_externals : t -> string list -> bool
type as_const_payload = Int of int | Str of string | Js_literal_str of string

val iter_process_bs_string_or_int_as : t -> as_const_payload option
val unboxable_type_in_prim_decl : attr
8 changes: 5 additions & 3 deletions ppx/ast_derive/ast_derive_abstract.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ open Ast_helper
let get_optional_attrs =
[ Ast_attributes.bs_get; Ast_attributes.bs_return_undefined ]

let get_attrs = [ Ast_attributes.bs_get_arity ]
let set_attrs = [ Ast_attributes.bs_set ]
let get_attrs = Ast_attributes.[ bs_get_arity; unboxable_type_in_prim_decl ]
let set_attrs = Ast_attributes.[ bs_set; unboxable_type_in_prim_decl ]

let get_pld_type pld_type ~attrs =
let is_optional = Ast_attributes.has_bs_optional attrs in
Expand Down Expand Up @@ -142,7 +142,9 @@ let handleTdcl light (tdcl : Parsetree.type_declaration) :
Ast_external_mk.pval_prim_of_option_labels labels has_optional_field
in
let myMaker =
Val.mk ~loc { loc; txt = type_name } ~prim:myPrims makeType
Val.mk ~loc { loc; txt = type_name }
~attrs:[ Ast_attributes.unboxable_type_in_prim_decl ]
~prim:myPrims makeType
in
myMaker :: setter_accessor )
| Ptype_abstract | Ptype_variant _ | Ptype_open ->
Expand Down
24 changes: 4 additions & 20 deletions ppx/ast_external.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,6 @@

open Ppxlib

(* We disable warning 61 in Melange externals since they're substantially
different from OCaml externals. This warning doesn't make sense for a JS
runtime *)
let unboxable_type_in_prim_decl : Parsetree.attribute =
let open Ast_helper in
{
attr_name = { txt = "ocaml.warning"; loc = Location.none };
attr_payload =
PStr
[
Str.eval
(Exp.constant
(Pconst_string
("-unboxable-type-in-prim-decl", Location.none, None)));
];
attr_loc = Location.none;
}

let handleExternalInSig (self : Ast_traverse.map)
(prim : Parsetree.value_description) (sigi : Parsetree.signature_item) :
Parsetree.signature_item =
Expand Down Expand Up @@ -79,7 +61,8 @@ let handleExternalInSig (self : Ast_traverse.map)
prim with
pval_type;
pval_prim = (if no_inline_cross_module then [] else pval_prim);
pval_attributes = unboxable_type_in_prim_decl :: pval_attributes;
pval_attributes =
Ast_attributes.unboxable_type_in_prim_decl :: pval_attributes;
};
}

Expand Down Expand Up @@ -120,7 +103,8 @@ let handleExternalInStru (self : Ast_traverse.map)
prim with
pval_type;
pval_prim;
pval_attributes = unboxable_type_in_prim_decl :: pval_attributes;
pval_attributes =
Ast_attributes.unboxable_type_in_prim_decl :: pval_attributes;
};
}
in
Expand Down
34 changes: 34 additions & 0 deletions test/blackbox-tests/deriving-abstract-one-field.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Showcase an error with deriving abstract when the record has one field

$ . ./setup.sh

$ cat > dune-project <<EOF
> (lang dune 3.8)
> (using melange 0.1)
> EOF

$ cat > dune <<EOF
> (library
> (modes melange)
> (name x)
> (preprocess
> (pps melange.ppx)))
> EOF

Record with two fields works fine

$ cat > x.ml <<EOF
> type chartDataItemType = { height: int; foo: string } [@@deriving abstract]
> EOF

$ dune build ./.x.objs/melange/x.cmj

Record with one field crashes

$ dune clean

$ cat > x.ml <<EOF
> type chartDataItemType = { height: int; } [@@deriving abstract]
> EOF

$ dune build ./.x.objs/melange/x.cmj