ppx_deriving icon indicating copy to clipboard operation
ppx_deriving copied to clipboard

Deriving show: Functor application not allowed here.

Open brokenpylons opened this issue 4 years ago • 2 comments

I have this code:

module type LABEL = sig
  type t
  val equal: t -> t -> bool
  val compare: t -> t -> int
  val to_string: t -> string
  val pp: Format.formatter -> t -> unit
end
module Make(Label: LABEL) = struct 
  type t = {
    label: Set.Make(Label).t 
  }
  [@@deriving show]
end

And I get:

Error: broken invariant in parsetree: Functor application not allowed here.

Is there a reason why this shouldn't work? The code compiles if i create the module beforehand module S = Set.Make(Label) and then use S.t instead.

brokenpylons avatar Sep 23 '20 16:09 brokenpylons

Meh, I think I figured it out, Set.Make(Label).pp is then called in the body of the generated function, right? So the generator could do let module S = Set.Make(Label) in for each module in the type, however if the module has side effects, this probably wouldn't be a good idea...

brokenpylons avatar Sep 24 '20 12:09 brokenpylons

You figured it out correctly indeed.

It may be possible to name the module carefully: if each occurrence of a functor application (in a module path that we want to derive from) is turned into a binding on the fly, we should be able to preserve the evaluation behavior of the original program. (What we don't want is to evaluate the functor application several times in the generated code.) But this is a bit delicate, and there may be case where this would change the type signature of the program (type equalities lost in the renaming, this kind of thing).

I think that the more robust approach is to bail out when we see this, and ask the user to do the let-binding themselves, so that they understand this is happening. The error message could be better than it is today (it looks like ppx_deriving is not saying anything here, the error in the generated code is caught by the compiler later on), but it would be the same behavior.

gasche avatar Sep 24 '20 12:09 gasche