Skip to content

Commit d0bfca8

Browse files
authored
Merge pull request #285 from NathanReb/fix-make-arguments-ordering
Fix the ordering of `make`'s arguments
2 parents 35dfd4a + d69b55a commit d0bfca8

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
unreleased
2+
----------
3+
4+
* Fix ordering of derived `make`'s arguments
5+
#285
6+
(@NathanReb)
7+
18
6.0.1 (17/04/2024)
29
------------------
310

src_plugins/make/ppx_deriving_make.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ let str_of_record_type ~quoter ~loc labels =
9797
| None ->
9898
record fields
9999
in
100-
List.fold_left (add_str_label_arg ~quoter ~loc) fn labels
100+
(* The labels list must be reversed here so that the arguments are in the
101+
same order as the record fields. *)
102+
List.fold_left (add_str_label_arg ~quoter ~loc) fn (List.rev labels)
101103

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

161165
let sig_of_type ({ ptype_loc = loc } as type_decl) =
162166
let typ = Ppx_deriving.core_type_of_type_decl type_decl in

src_test/make/test_deriving_make.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ end = struct
6464
[@@deriving show]
6565
end
6666

67+
(* This module is here to test that the ordering of the arguments
68+
match the order of the record fields declarations. *)
69+
module M2 : sig
70+
type t =
71+
{ first : int
72+
; second : int
73+
}
74+
75+
val make : first: int -> second: int -> t
76+
end = struct
77+
type t =
78+
{ first : int
79+
; second : int
80+
}
81+
[@@deriving make]
82+
end
83+
6784
let test_no_main ctxt =
6885
assert_equal ~printer:M.show_a
6986
{ M.a1 = None; a2 = []; a3 = 42; a4s = 2, []; a5 = 1 }

0 commit comments

Comments
 (0)