Skip to content

Commit

Permalink
Merge pull request #285 from NathanReb/fix-make-arguments-ordering
Browse files Browse the repository at this point in the history
Fix the ordering of `make`'s arguments
  • Loading branch information
NathanReb authored May 15, 2024
2 parents 35dfd4a + d69b55a commit d0bfca8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
unreleased
----------

* Fix ordering of derived `make`'s arguments
#285
(@NathanReb)

6.0.1 (17/04/2024)
------------------

Expand Down
8 changes: 6 additions & 2 deletions src_plugins/make/ppx_deriving_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ let str_of_record_type ~quoter ~loc labels =
| None ->
record fields
in
List.fold_left (add_str_label_arg ~quoter ~loc) fn labels
(* The labels list must be reversed here so that the arguments are in the
same order as the record fields. *)
List.fold_left (add_str_label_arg ~quoter ~loc) fn (List.rev labels)

let str_of_type ({ ptype_loc = loc } as type_decl) =
let quoter = Ppx_deriving.create_quoter () in
Expand Down Expand Up @@ -156,7 +158,9 @@ let sig_of_record_type ~loc ~typ labels =
| None when has_option -> Typ.arrow Label.nolabel (tconstr "unit" []) typ
| None -> typ
in
List.fold_left add_sig_label_arg typ labels
(* The labels list must be reversed here so that the arguments are in the
same order as the record fields. *)
List.fold_left add_sig_label_arg typ (List.rev labels)

let sig_of_type ({ ptype_loc = loc } as type_decl) =
let typ = Ppx_deriving.core_type_of_type_decl type_decl in
Expand Down
17 changes: 17 additions & 0 deletions src_test/make/test_deriving_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ end = struct
[@@deriving show]
end

(* This module is here to test that the ordering of the arguments
match the order of the record fields declarations. *)
module M2 : sig
type t =
{ first : int
; second : int
}

val make : first: int -> second: int -> t
end = struct
type t =
{ first : int
; second : int
}
[@@deriving make]
end

let test_no_main ctxt =
assert_equal ~printer:M.show_a
{ M.a1 = None; a2 = []; a3 = 42; a4s = 2, []; a5 = 1 }
Expand Down

0 comments on commit d0bfca8

Please sign in to comment.