Skip to content

Commit cbc9044

Browse files
committed
Ppx: add support for ppxlib.0.36
1 parent 2d7dd67 commit cbc9044

File tree

5 files changed

+114
-24
lines changed

5 files changed

+114
-24
lines changed

compiler/ppx/ppx_optcomp_light.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ let keep loc (attrs : attributes) =
158158
let rec eval = function
159159
| { pexp_desc = Pexp_ident { txt = Lident "ocaml_version"; _ }; _ } ->
160160
Version Version.current
161+
| { pexp_desc = Pexp_ident { txt = Lident "ast_version"; _ }; _ } ->
162+
Int Ppxlib.Selected_ast.version
161163
| { pexp_desc = Pexp_construct ({ txt = Lident "true"; _ }, None); _ } ->
162164
Bool true
163165
| { pexp_desc = Pexp_construct ({ txt = Lident "false"; _ }, None); _ } ->
@@ -275,11 +277,14 @@ let traverse =
275277
in
276278
super#structure items
277279

278-
method! cases =
279-
filter_map ~f:(fun case ->
280-
match filter_pattern case.pc_lhs with
281-
| None -> None
282-
| Some pattern -> Some { case with pc_lhs = pattern })
280+
method! cases cases =
281+
let cases =
282+
filter_map cases ~f:(fun case ->
283+
match filter_pattern case.pc_lhs with
284+
| None -> None
285+
| Some pattern -> Some { case with pc_lhs = pattern })
286+
in
287+
super#cases cases
283288
end
284289

285290
let () =

ppx/ppx_js/as-lib/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
(synopsis "Js_of_ocaml ppx")
55
(libraries compiler-libs.common ppxlib)
66
(preprocess
7-
(pps ppxlib.metaquot)))
7+
(pps ppx_optcomp_light ppxlib.metaquot)))
88

99
(rule
1010
(targets ppx_js_internal.ml)

ppx/ppx_js/lib_internal/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
(libraries compiler-libs.common ppxlib)
44
(kind ppx_rewriter)
55
(preprocess
6-
(pps ppxlib.metaquot)))
6+
(pps ppx_optcomp_light ppxlib.metaquot)))

ppx/ppx_js/lib_internal/ppx_js_internal.ml

Lines changed: 99 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,24 @@ let invoker ?(extra_types = []) uplift downlift body arguments =
250250
in
251251
let make_fun (label, pat) (label', typ) expr =
252252
assert (label' = label);
253-
Exp.fun_ label None (Pat.constraint_ pat typ) expr
253+
match expr.pexp_desc with
254+
| ((Pexp_function (params, c, b)) [@if ast_version >= 502]) ->
255+
let params =
256+
{ pparam_desc = Pparam_val (nolabel, None, Pat.constraint_ pat typ)
257+
; pparam_loc = { expr.pexp_loc with loc_ghost = true }
258+
}
259+
:: params
260+
in
261+
let c, b =
262+
match c, b with
263+
| ( None
264+
, Pfunction_body
265+
{ pexp_desc = Pexp_constraint (e, ty); pexp_attributes = []; _ } ) ->
266+
Some (Pconstraint ty), Pfunction_body e
267+
| _ -> c, b
268+
in
269+
{ expr with pexp_desc = Pexp_function (params, c, b) }
270+
| _ -> Exp.fun_ label None (Pat.constraint_ pat typ) expr
254271
in
255272
let invoker =
256273
List.fold_right2
@@ -504,7 +521,7 @@ type field_desc =
504521
string Asttypes.loc
505522
* Asttypes.private_flag
506523
* Asttypes.override_flag
507-
* Parsetree.expression
524+
* (Parsetree.expression * Parsetree.core_type option)
508525
* Arg.t list
509526
| Val of
510527
string Asttypes.loc * Prop_kind.t * Asttypes.override_flag * Parsetree.expression
@@ -518,6 +535,29 @@ let filter_map f l =
518535
in
519536
List.rev l
520537

538+
let rec create_meth_ty exp =
539+
match exp.pexp_desc with
540+
| Pexp_fun (label, _, _, body) -> label :: create_meth_ty body
541+
| Pexp_function _ -> [ nolabel ]
542+
| Pexp_newtype (_, body) -> create_meth_ty body
543+
| _ -> []
544+
[@@if ast_version < 502]
545+
546+
let rec create_meth_ty exp =
547+
match exp.pexp_desc with
548+
| Pexp_function (params, _, body) -> (
549+
List.filter_map params ~f:(function
550+
| { pparam_desc = Pparam_newtype _; _ } -> None
551+
| { pparam_desc = Pparam_val (label, _, _arg); _ } -> Some label)
552+
@
553+
match body with
554+
| Pfunction_cases _ -> [ nolabel ]
555+
| Pfunction_body e ->
556+
(* TODO: should we recurse or not ? *)
557+
create_meth_ty e)
558+
| _ -> []
559+
[@@if ast_version >= 502]
560+
521561
let preprocess_literal_object mappper fields :
522562
[ `Fields of field_desc list | `Error of _ ] =
523563
let check_name id names =
@@ -581,22 +621,16 @@ let preprocess_literal_object mappper fields :
581621
| Pcf_method (id, priv, Cfk_concrete (bang, body)) ->
582622
let names = check_name id names in
583623
let body, body_ty = drop_pexp_poly (mappper body) in
584-
let rec create_meth_ty exp =
585-
match exp.pexp_desc with
586-
| Pexp_fun (label, _, _, body) -> label :: create_meth_ty body
587-
| Pexp_function _ -> [ nolabel ]
588-
| Pexp_newtype (_, body) -> create_meth_ty body
589-
| _ -> []
590-
in
591624
let fun_ty =
592625
List.map ~f:(fun label -> Arg.make ~label ()) (create_meth_ty body)
593626
in
627+
594628
let body =
595629
match body_ty with
596-
| None -> body
630+
| None -> body, None
597631
| Some { ptyp_desc = Ptyp_poly _; _ } ->
598632
raise_errorf ~loc:exp.pcf_loc "Polymorphic method not supported."
599-
| Some ty -> Exp.constraint_ body ty
633+
| Some ty -> body, Some ty
600634
in
601635
names, Meth (id, priv, bang, body, fun_ty) :: fields
602636
| _ ->
@@ -649,8 +683,43 @@ let literal_object self_id (fields : field_desc list) =
649683
in
650684
let body = function
651685
| Val (_, _, _, body) -> body
652-
| Meth (_, _, _, body, _) ->
653-
Exp.fun_ ~loc:{ body.pexp_loc with loc_ghost = true } Nolabel None self_id body
686+
| Meth (_, _, _, (body, ty), _) -> (
687+
match body.pexp_desc, ty with
688+
| ((Pexp_function (params, c, b), None) [@if ast_version >= 502]) ->
689+
let params =
690+
{ pparam_desc = Pparam_val (nolabel, None, self_id)
691+
; pparam_loc = { body.pexp_loc with loc_ghost = true }
692+
}
693+
:: params
694+
in
695+
{ body with pexp_desc = Pexp_function (params, c, b) }
696+
| ((_, Some ty) [@if ast_version >= 502]) -> (
697+
let e =
698+
Exp.fun_
699+
~loc:{ body.pexp_loc with loc_ghost = true }
700+
Nolabel
701+
None
702+
self_id
703+
body
704+
in
705+
match e.pexp_desc with
706+
| Pexp_function (params, None, b) ->
707+
{ e with pexp_desc = Pexp_function (params, Some (Pconstraint ty), b) }
708+
| _ -> assert false)
709+
| ((_, Some ty) [@if ast_version < 502]) ->
710+
Exp.fun_
711+
~loc:{ body.pexp_loc with loc_ghost = true }
712+
Nolabel
713+
None
714+
self_id
715+
(Exp.constraint_ body ty)
716+
| _, None ->
717+
Exp.fun_
718+
~loc:{ body.pexp_loc with loc_ghost = true }
719+
Nolabel
720+
None
721+
self_id
722+
body)
654723
in
655724
let extra_types =
656725
List.concat
@@ -738,7 +807,23 @@ let literal_object self_id (fields : field_desc list) =
738807
(self :: List.map fields ~f:(fun f -> (name f).txt))
739808
~init:fake_object
740809
~f:(fun name fun_ ->
741-
Exp.fun_ ~loc:gloc nolabel None (Pat.var ~loc:gloc (mknoloc name)) fun_))
810+
match fun_.pexp_desc with
811+
| ((Pexp_function (params, c, b)) [@if ast_version >= 502]) ->
812+
let params =
813+
{ pparam_desc =
814+
Pparam_val (nolabel, None, Pat.var ~loc:gloc (mknoloc name))
815+
; pparam_loc = { fun_.pexp_loc with loc_ghost = true }
816+
}
817+
:: params
818+
in
819+
{ fun_ with pexp_desc = Pexp_function (params, c, b) }
820+
| _ ->
821+
Exp.fun_
822+
~loc:gloc
823+
nolabel
824+
None
825+
(Pat.var ~loc:gloc (mknoloc name))
826+
fun_))
742827
with
743828
pexp_attributes = [ merlin_hide ]
744829
}

ppx/ppx_js/tests/gen.mlt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ Error: Polymorphic method not supported.
369369

370370

371371
let o () =
372-
object%js
372+
object%js
373373
method m1 : 'a -> unit = fun (type a) (a : a) -> ignore a
374374
method m2 : int -> unit = fun (type a) (a : a) -> ignore a
375375
method m3 : 'b -> unit = fun (a : 'b) -> ignore a
@@ -409,8 +409,8 @@ let o () =
409409
("m3",
410410
(Js_of_ocaml.Js.Unsafe.inject
411411
(Js_of_ocaml.Js.wrap_meth_callback t31)))|])
412-
(fun _ : 'a -> unit-> fun (type a) -> fun (a : a) -> ignore a)
413-
(fun _ : int -> unit-> fun (type a) -> fun (a : a) -> ignore a)
412+
(fun _ : 'a -> unit-> fun (type a) (a : a) -> ignore a)
413+
(fun _ : int -> unit-> fun (type a) (a : a) -> ignore a)
414414
(fun _ : 'b -> unit-> fun (a : 'b) -> ignore a)
415415
((fun self m1 m2 m3 ->
416416
object method m1 = m1 self method m2 = m2 self method m3 = m3 self

0 commit comments

Comments
 (0)