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

ppx_deriving.make breaks on mutually recurive types #272

Closed
shonfeder opened this issue Jan 16, 2024 · 0 comments · Fixed by #281
Closed

ppx_deriving.make breaks on mutually recurive types #272

shonfeder opened this issue Jan 16, 2024 · 0 comments · Fixed by #281

Comments

@shonfeder
Copy link
Contributor

shonfeder commented Jan 16, 2024

In ex.ml

module Ex = struct
  type t = {a : int; b: s}
  [@@deriving make]
  and s = t list
end

With the dune file:

(library
 (name ex)
 (modules ex)
 (preprocess (pps ppx_deriving.make)))

dune build gives

$ dune build                    
File "ex.ml", line 5, characters 2-16:
5 |   and s = t list
      ^^^^^^^^^^^^^^
Error: make can be derived only for record types

If we put another deriving annotation on s and swap the order the types are declared, the compilation goes thru but the make annotation is ignored:

module Ex = struct
  type s = t list
  [@@deriving show]

  and t = {a : int; b: s}
  [@@deriving make]
end

Which produces

[@@@ocaml.ppx.context
  {
    tool_name = "ppx_driver";
    include_dirs = [];
    load_path = [];
    open_modules = [];
    for_package = None;
    debug = false;
    use_threads = false;
    use_vmthreads = false;
    recursive_types = false;
    principal = false;
    transparent_modules = false;
    unboxed_types = false;
    unsafe_string = false;
    cookies = [("library-name", "ex")]
  }]
module Ex =
  struct
    type s = t list[@@deriving show]
    and t = {
      a: int ;
      b: s }[@@deriving make]
    let rec pp_s :
      Ppx_deriving_runtime.Format.formatter -> s -> Ppx_deriving_runtime.unit
      =
      let __0 () = pp in
      ((let open! ((Ppx_deriving_runtime)[@ocaml.warning "-A"]) in
          fun fmt ->
            fun x ->
              Ppx_deriving_runtime.Format.fprintf fmt "@[<2>[";
              ignore
                (List.fold_left
                   (fun sep ->
                      fun x ->
                        if sep
                        then Ppx_deriving_runtime.Format.fprintf fmt ";@ ";
                        ((__0 ()) fmt) x;
                        true) false x);
              Ppx_deriving_runtime.Format.fprintf fmt "@,]@]")
        [@ocaml.warning "-A"])
    and show_s : s -> Ppx_deriving_runtime.string =
      fun x -> Ppx_deriving_runtime.Format.asprintf "%a" pp_s x[@@ocaml.warning
                                                                 "-32"]
    and pp :
      Ppx_deriving_runtime.Format.formatter -> t -> Ppx_deriving_runtime.unit
      =
      let __0 () = pp_s in
      ((let open! ((Ppx_deriving_runtime)[@ocaml.warning "-A"]) in
          fun fmt ->
            fun x ->
              Ppx_deriving_runtime.Format.fprintf fmt "@[<2>{ ";
              ((Ppx_deriving_runtime.Format.fprintf fmt "@[%s =@ " "Ex.Ex.a";
                (Ppx_deriving_runtime.Format.fprintf fmt "%d") x.a;
                Ppx_deriving_runtime.Format.fprintf fmt "@]");
               Ppx_deriving_runtime.Format.fprintf fmt ";@ ";
               Ppx_deriving_runtime.Format.fprintf fmt "@[%s =@ " "b";
               ((__0 ()) fmt) x.b;
               Ppx_deriving_runtime.Format.fprintf fmt "@]");
              Ppx_deriving_runtime.Format.fprintf fmt "@ }@]")
        [@ocaml.warning "-A"])
    and show : t -> Ppx_deriving_runtime.string =
      fun x -> Ppx_deriving_runtime.Format.asprintf "%a" pp x[@@ocaml.warning
                                                               "-32"]
  end

As per dune describe pp ex.ml.

If the additional deriving annotation is not added, we get the same error.

shonfeder added a commit to shonfeder/ppx_deriving that referenced this issue Feb 24, 2024
Tests behavior when deriving on mutually recursive types

ocaml-ppx#272
NathanReb pushed a commit to NathanReb/ppx_deriving that referenced this issue Apr 8, 2024
Tests behavior when deriving on mutually recursive types

ocaml-ppx#272
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant