Skip to content

Commit

Permalink
Only generate make functions for tagged types
Browse files Browse the repository at this point in the history
  • Loading branch information
bn-d committed Jan 19, 2024
1 parent a155e13 commit 447d662
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,24 @@ let strip_option (ct : core_type) =
let unsupported_error str { txt; loc } =
Location.raise_errorf ~loc "%s %s cannot be derived" str txt

let has_make_attr ({ ptype_attributes; _ } : type_declaration) =
let is_make = function [%stri make] -> true | _ -> false in
List.exists
(fun (attr : attribute) ->
(attr.attr_name.txt = "deriving" || attr.attr_name.txt = "deriving_inline")
&&
match attr.attr_payload with
| PStr items -> List.exists is_make items
| _ -> false)
ptype_attributes

let make_type_decl_generator f =
Deriving.Generator.V2.make_noarg (fun ~ctxt (rec_flag, tds) ->
let loc = Expansion_context.Deriver.derived_item_loc ctxt in
tds |> List.map (f ~loc rec_flag) |> List.concat)
tds
|> List.filter has_make_attr
|> List.map (f ~loc rec_flag)
|> List.concat)

let gen_make_name { txt = name; loc } = { txt = "make_" ^ name; loc }

Expand Down
3 changes: 3 additions & 0 deletions test/misc_types.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(* https://github.com/bn-d/ppx_make/issues/12 *)
type a = { i : int } [@@deriving make]
and b = int

0 comments on commit 447d662

Please sign in to comment.