Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only generate make functions for tagged types #13

Merged
merged 1 commit into from
Jan 19, 2024
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
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