Skip to content

Commit 3e43c96

Browse files
committed
Fix ord variant wildcard_case to_int type pre-OCaml 4.11 (PR #260)
1 parent 6eb5745 commit 3e43c96

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

src_plugins/ord/ppx_deriving_ord.cppo.ml

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ let reduce_compare l =
3535
| [] -> [%expr 0]
3636
| x :: xs -> List.fold_left compare_reduce x xs
3737

38-
let wildcard_case ~typ int_cases =
38+
let wildcard_case ?typ int_cases =
3939
let loc = !Ast_helper.default_loc in
40+
let typ = match typ with
41+
| Some typ -> typ
42+
| None -> [%type: _] (* don't constrain *)
43+
in
4044
Exp.case [%pat? _] [%expr
41-
let to_int: [%t typ] -> Ppx_deriving_runtime.int = [%e Exp.function_ int_cases] in
45+
let to_int (x: [%t typ]) = [%e Exp.match_ [%expr x] int_cases] in
4246
Ppx_deriving_runtime.compare (to_int lhs) (to_int rhs)]
4347

4448
let pattn side typs =
@@ -163,7 +167,7 @@ and expr_of_typ quoter typ =
163167
| _ -> assert false)
164168
in
165169
[%expr fun lhs rhs ->
166-
[%e Exp.match_ [%expr lhs, rhs] (cases @ [wildcard_case ~typ int_cases])]]
170+
[%e Exp.match_ [%expr lhs, rhs] (cases @ [wildcard_case int_cases])]]
167171
| { ptyp_desc = Ptyp_var name } -> evar ("poly_"^name)
168172
| { ptyp_desc = Ptyp_alias (typ, _) } -> expr_of_typ typ
169173
| { ptyp_loc } ->
@@ -185,6 +189,24 @@ let sig_of_type ~options ~path type_decl =
185189
let str_of_type ~options ~path ({ ptype_loc = loc } as type_decl) =
186190
parse_options options;
187191
let quoter = Ppx_deriving.create_quoter () in
192+
(* Capture type in helper module outside Ppx_deriving_runtime wrapper (added by sanitize).
193+
Required for to_int constraint in variant type wildcard_case if the type name
194+
conflicts with a Stdlib type from Ppx_deriving_runtime (e.g. bool in test).
195+
In that case we must refer to the type being declared, not the one opened by Ppx_deriving_runtime. *)
196+
let helper_type =
197+
Type.mk ~loc ~attrs:[Ppx_deriving.attr_warning [%expr "-unused-type-declaration"]]
198+
~params:type_decl.ptype_params
199+
~manifest:(Ppx_deriving.core_type_of_type_decl type_decl)
200+
(mkloc "t" loc)
201+
in
202+
let helper_typ =
203+
let name = mkloc (Longident.parse "Ppx_deriving_ord_helper.t") loc in
204+
let params = match helper_type.ptype_params with
205+
| [] -> []
206+
| _ :: _ -> [Typ.any ()] (* match all params with single wildcard *)
207+
in
208+
Typ.constr name params
209+
in
188210
let comparator =
189211
match type_decl.ptype_kind, type_decl.ptype_manifest with
190212
| Ptype_abstract, Some manifest -> expr_of_typ quoter manifest
@@ -208,7 +230,7 @@ let str_of_type ~options ~path ({ ptype_loc = loc } as type_decl) =
208230
)
209231
in
210232
[%expr fun lhs rhs ->
211-
[%e Exp.match_ [%expr lhs, rhs] (cases @ [wildcard_case ~typ:[%type: Ppx_deriving_ord_helper.t] int_cases])]]
233+
[%e Exp.match_ [%expr lhs, rhs] (cases @ [wildcard_case ~typ:helper_typ int_cases])]]
212234
| Ptype_record labels, _ ->
213235
let exprs =
214236
labels |> List.map (fun ({ pld_name = { txt = name }; _ } as pld) ->
@@ -235,16 +257,6 @@ let str_of_type ~options ~path ({ ptype_loc = loc } as type_decl) =
235257
core_type_of_decl ~options ~path type_decl in
236258
let out_var =
237259
pvar (Ppx_deriving.mangle_type_decl (`Prefix "compare") type_decl) in
238-
(* Capture type in helper module outside Ppx_deriving_runtime wrapper (added by sanitize).
239-
Required for to_int constraint in variant type wildcard_case if the type name
240-
conflicts with a Stdlib type from Ppx_deriving_runtime (e.g. bool in test).
241-
In that case we must refer to the type being declared, not the one opened by Ppx_deriving_runtime. *)
242-
let helper_type =
243-
Type.mk ~loc ~attrs:[Ppx_deriving.attr_warning [%expr "-unused-type-declaration"]]
244-
~params:type_decl.ptype_params
245-
~manifest:(Ppx_deriving.core_type_of_type_decl type_decl)
246-
(mkloc "t" loc)
247-
in
248260
let comparator_with_helper =
249261
[%expr let module Ppx_deriving_ord_helper = struct [%%i Str.type_ Nonrecursive [helper_type]] end in
250262
[%e Ppx_deriving.sanitize ~quoter (eta_expand (polymorphize comparator))]]

src_test/eq/test_deriving_eq.cppo.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ and 'a poly_abs_custom = 'a
131131
module List = struct
132132
type 'a t = [`Cons of 'a | `Nil]
133133
[@@deriving eq]
134+
type 'a u = Cons of 'a | Nil
135+
[@@deriving eq]
134136
end
135137
type 'a std_clash = 'a List.t option
136138
[@@deriving eq]

src_test/ord/test_deriving_ord.cppo.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ and 'a poly_abs_custom = 'a
158158
module List = struct
159159
type 'a t = [`Cons of 'a | `Nil]
160160
[@@deriving ord]
161+
type 'a u = Cons of 'a | Nil
162+
[@@deriving ord]
161163
end
162164
type 'a std_clash = 'a List.t option
163165
[@@deriving ord]

0 commit comments

Comments
 (0)