Skip to content

Commit

Permalink
fix: disable warning 61 (unboxable-type-in-prim-decl) for deriving
Browse files Browse the repository at this point in the history
abstract externals
  • Loading branch information
anmonteiro committed Jun 27, 2023
1 parent a117f4c commit 7af41e3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 56 deletions.
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
35 changes: 2 additions & 33 deletions test/blackbox-tests/deriving-abstract-one-field.t
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ Record with two fields works fine
> type chartDataItemType = { height: int; foo: string } [@@deriving abstract]
> EOF

$ dune build --display=short ./.x.objs/melange/x.cmj
ocamlc .ppx/9b08511fbad9c15355a1e1f8e80934bc/dune__exe___ppx.{cmi,cmo}
ocamlopt .ppx/9b08511fbad9c15355a1e1f8e80934bc/build_info__Build_info_data.{cmx,o}
ocamlopt .ppx/9b08511fbad9c15355a1e1f8e80934bc/dune__exe___ppx.{cmx,o}
ocamlopt .ppx/9b08511fbad9c15355a1e1f8e80934bc/ppx.exe
ppx x.pp.ml
melc .x.objs/melange/x.{cmi,cmj,cmt}
$ dune build ./.x.objs/melange/x.cmj

Record with one field crashes

Expand All @@ -37,29 +31,4 @@ Record with one field crashes
> type chartDataItemType = { height: int; } [@@deriving abstract]
> EOF

$ dune build --display=short ./.x.objs/melange/x.cmj
ocamlc .ppx/9b08511fbad9c15355a1e1f8e80934bc/dune__exe___ppx.{cmi,cmo}
ocamlopt .ppx/9b08511fbad9c15355a1e1f8e80934bc/build_info__Build_info_data.{cmx,o}
ocamlopt .ppx/9b08511fbad9c15355a1e1f8e80934bc/dune__exe___ppx.{cmx,o}
ocamlopt .ppx/9b08511fbad9c15355a1e1f8e80934bc/ppx.exe
ppx x.pp.ml
melc .x.objs/melange/x.{cmi,cmj,cmt} (exit 2)
File "x.ml", line 1:
Error (warning 61 [unboxable-type-in-prim-decl]): This primitive declaration uses type chartDataItemType, whose representation
may be either boxed or unboxed. Without an annotation to indicate
which representation is intended, the boxed representation has been
selected by default. This default choice may change in future
versions of the compiler, breaking the primitive implementation.
You should explicitly annotate the declaration of chartDataItemType
with [@@boxed] or [@@unboxed], so that its external interface
remains stable in the future.
File "x.ml", line 1:
Error (warning 61 [unboxable-type-in-prim-decl]): This primitive declaration uses type chartDataItemType, whose representation
may be either boxed or unboxed. Without an annotation to indicate
which representation is intended, the boxed representation has been
selected by default. This default choice may change in future
versions of the compiler, breaking the primitive implementation.
You should explicitly annotate the declaration of chartDataItemType
with [@@boxed] or [@@unboxed], so that its external interface
remains stable in the future.
[1]
$ dune build ./.x.objs/melange/x.cmj

0 comments on commit 7af41e3

Please sign in to comment.