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

show: handle [@printer] in polymorphic variants #267

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 15 additions & 4 deletions src_plugins/show/ppx_deriving_show.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,31 @@ let rec expr_of_typ quoter typ =
| { ptyp_desc = Ptyp_variant (fields, _, _); ptyp_loc } ->
let cases =
fields |> List.map (fun field ->
match field.prf_desc with
| Rtag(label, true (*empty*), []) ->
match attr_printer field.prf_attributes, field.prf_desc with
| None, Rtag(label, true (*empty*), []) ->
let label = label.txt in
Exp.case (Pat.variant label None)
[%expr Ppx_deriving_runtime.Format.pp_print_string fmt [%e str ("`" ^ label)]]
| Rtag(label, false, [typ]) ->
| Some printer, Rtag(label, true (*empty*), []) ->
let label = label.txt in
Exp.case (Pat.variant label None)
[%expr [%e printer] fmt ()]
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be easier to factorize the two cases better, the code is identical except for the second argument of Exp.case that depends on the optional printer attribute.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am worried about the use of printer directly when all other such patterns in the code use wrap_printer quoter printer. I don't remember the details of how this works, but I would follow the rest of the code here.

| None, Rtag(label, false, [typ]) ->
let label = label.txt in
Exp.case (Pat.variant label (Some [%pat? x]))
[%expr Ppx_deriving_runtime.Format.fprintf fmt [%e str ("`" ^ label ^ " (@[<hov>")];
[%e expr_of_typ typ] x;
Ppx_deriving_runtime.Format.fprintf fmt "@])"]
| Rinherit({ ptyp_desc = Ptyp_constr (tname, _) } as typ) ->
| Some printer, Rtag(label, false, [typ]) ->
let label = label.txt in
Exp.case (Pat.variant label (Some [%pat? x]))
[%expr [%e printer] fmt x]
Copy link
Contributor

Choose a reason for hiding this comment

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

Same remarks: you could share more code (but not doing so is okay), and printer should be wrapped.

| None, Rinherit({ ptyp_desc = Ptyp_constr (tname, _) } as typ) ->
Exp.case [%pat? [%p Pat.type_ tname] as x]
[%expr [%e expr_of_typ typ] x]
| Some printer, Rinherit({ ptyp_desc = Ptyp_constr (tname, _) }) ->
Exp.case [%pat? [%p Pat.type_ tname] as x]
[%expr [%e printer] fmt x]
| _ ->
raise_errorf ~loc:ptyp_loc "%s cannot be derived for %s"
deriver (Ppx_deriving.string_of_core_type typ))
Expand Down
12 changes: 12 additions & 0 deletions src_test/show/test_deriving_show.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ let test_variant_printer ctxt =
assert_equal ~printer
"fourth: 8 4" (show_variant_printer (Fourth(8,4)))

type polyvar_printer = [
| `First [@printer fun fmt _ -> Format.pp_print_string fmt "first"]
| `Second of int [@printer fun fmt i -> Format.fprintf fmt "second: %d" i]
] [@@deriving show]

let test_polyvar_printer ctxt =
assert_equal ~printer
"first" (show_polyvar_printer `First);
assert_equal ~printer
"second: 42" (show_polyvar_printer (`Second 42));

type no_full = NoFull of int [@@deriving show { with_path = false }]
type with_full = WithFull of int [@@deriving show { with_path = true }]
module WithFull = struct
Expand Down Expand Up @@ -265,6 +276,7 @@ let suite = "Test deriving(show)" >::: [
"test_std_shadowing" >:: test_std_shadowing;
"test_poly_app" >:: test_poly_app;
"test_variant_printer" >:: test_variant_printer;
"test_polyvar_printer" >:: test_polyvar_printer;
"test_paths" >:: test_paths_printer;
"test_result" >:: test_result;
"test_result_result" >:: test_result_result;
Expand Down