ppx_deriving icon indicating copy to clipboard operation
ppx_deriving copied to clipboard

Unused values when deriving show for private types

Open emillon opened this issue 6 years ago • 3 comments

Hi,

The following piece of code triggers warning 32 (unused value show_x). This is a common pattern, it happens whenever an exposed type uses a type that is not exposed.

module M : sig
  type t
  [@@deriving show]

  val make : int -> t
end = struct
  type x = int
  [@@deriving show]

  type t = C of x
  [@@deriving show]

  let make n = C n
end

The problem is the following:

  • 4 bindings are created: pp_x, show_x, pp, and show.
  • pp, and show are exposed
  • pp_x is used in the generated definition for pp
  • show_x is not, so a warning is triggered.

The root cause is similar to ocaml-ppx/ppx_deriving_yojson#76: there is a "convenience function" (here show) that is not meant to be used recursively.

I am not sure how to best fix this without breaking existing code, but here are some possibilities:

  • generate let _ = show_x to suppress the warning
  • similarly, wrap the generated structure within [@@@warning "-32"]
  • add an option so that we can [@@derive show { only_pp = true }]
  • introduce a [@@deriving pp] that only derives pp. This would enable [%pp: t] as well.

What do you think?

Thanks!

emillon avatar Jun 25 '18 15:06 emillon

I think [@@warning "-32"] and [@deriving pp] both seem like good options; we can simply do both of them.

whitequark avatar Jun 27 '18 09:06 whitequark

Hi,

What's the status on this issue?

I'm happy to contribute for the [@deriving pp] if that's fine by you!

NathanReb avatar Aug 07 '18 16:08 NathanReb

@NathanReb As mentioned above in https://github.com/ocaml-ppx/ppx_deriving/issues/171#issuecomment-400598844, I think both [@@warning "-32"] and [@deriving pp] ought to be implemented. If you can do one of them, great!

whitequark avatar Aug 07 '18 20:08 whitequark