Skip to content

Commit c2c4bc4

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

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src_plugins/ord/ppx_deriving_ord.cppo.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,15 @@ let reduce_compare l =
3737

3838
let wildcard_case ~typ int_cases =
3939
let loc = !Ast_helper.default_loc in
40+
let to_int_fun = [%expr fun (x: [%t typ]) -> [%e Exp.match_ [%expr x] int_cases]] in
41+
(* Need explicit polymorphic type for pre-OCaml 4.11 compatibility,
42+
but just using Ppx_deriving.strong_type_of_type doesn't work,
43+
so adding explicit polymorphism using (type a) arguments instead.
44+
See: https://github.com/ocaml-ppx/ppx_deriving/pull/260#issuecomment-1072995847. *)
45+
let free_vars = Ppx_deriving.free_vars_in_core_type typ in
46+
let poly_to_int_fun = List.fold_right Exp.newtype free_vars to_int_fun in
4047
Exp.case [%pat? _] [%expr
41-
let to_int: [%t typ] -> Ppx_deriving_runtime.int = [%e Exp.function_ int_cases] in
48+
let to_int = [%e poly_to_int_fun] in
4249
Ppx_deriving_runtime.compare (to_int lhs) (to_int rhs)]
4350

4451
let pattn side typs =

0 commit comments

Comments
 (0)