From d2715c469e8cf8bf8cff81b897c212094a9517c5 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Tue, 16 Jan 2024 21:21:41 -0800 Subject: [PATCH] feat: allow melange to work with OCaml 4.14 (#1028) * 4.14 conditionals * 4.14 AST * wip * wip --- bin/melc.ml | 8 +-- flake.lock | 30 +++++------ jscomp/core/{ast_io.ml => ast_io.cppo.ml} | 10 +++- jscomp/core/ast_io.mli | 1 + jscomp/core/dune | 52 +++++++++++++++++++ jscomp/core/gen/record_fold.ml | 4 +- jscomp/core/gen/record_iter.ml | 4 +- jscomp/core/gen/record_map.ml | 4 +- ...itialization.ml => initialization.cppo.ml} | 3 ++ .../{lam_convert.ml => lam_convert.cppo.ml} | 6 +++ ...match.ml => polyvar_pattern_match.cppo.ml} | 4 ++ playground/mel_playground.ml | 2 +- vendor/melange-compiler-libs | 2 +- 13 files changed, 103 insertions(+), 27 deletions(-) rename jscomp/core/{ast_io.ml => ast_io.cppo.ml} (97%) rename jscomp/core/{initialization.ml => initialization.cppo.ml} (98%) rename jscomp/core/{lam_convert.ml => lam_convert.cppo.ml} (99%) rename jscomp/core/{polyvar_pattern_match.ml => polyvar_pattern_match.cppo.ml} (97%) diff --git a/bin/melc.ml b/bin/melc.ml index 3e6296df4e..917171d38a 100644 --- a/bin/melc.ml +++ b/bin/melc.ml @@ -84,9 +84,11 @@ module As_ppx = struct Cmd_ppx_apply.apply_rewriters ~tool_name:"melppx" kind ast +module Melange_ast_version = Melangelib.Ast_io.Melange_ast_version + module Convert = Ppxlib_ast.Convert - (Ppxlib_ast__.Versions.OCaml_501) + (Melange_ast_version) (Ppxlib_ast__.Versions.OCaml_current) let apply_lazy ~source ~target = @@ -98,7 +100,7 @@ let apply_lazy ~source ~target = | Intf ast -> let ast: Ppxlib_ast__.Versions.OCaml_current.Ast.Parsetree.signature = let ast = apply ~kind:Ml_binary.Mli ast in - let ppxlib_ast: Ppxlib_ast__.Versions.OCaml_501.Ast.Parsetree.signature = + let ppxlib_ast: Melange_ast_version.Ast.Parsetree.signature = Obj.magic ast in Convert.copy_signature ppxlib_ast @@ -112,7 +114,7 @@ let apply_lazy ~source ~target = let ast: Melange_compiler_libs.Parsetree.structure = apply ~kind:Ml_binary.Ml ast in - let ppxlib_ast: Ppxlib_ast__.Versions.OCaml_501.Ast.Parsetree.structure = + let ppxlib_ast: Melange_ast_version.Ast.Parsetree.structure = Obj.magic ast in Convert.copy_structure ppxlib_ast diff --git a/flake.lock b/flake.lock index a3cfac1022..4a403bb3fe 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1701680307, - "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", "owner": "numtide", "repo": "flake-utils", - "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", "type": "github" }, "original": { @@ -28,11 +28,11 @@ ] }, "locked": { - "lastModified": 1704861910, + "lastModified": 1705466801, "narHash": "sha256-1dub8rJZkAjZiKR0EopdXVP3JCbSq/8fAw2Fei0Hh+E=", "owner": "melange-re", "repo": "melange-compiler-libs", - "rev": "61653c60844cd04af5733512768618fa9b05ec71", + "rev": "87bb98024b1669766f9c8aec3bbbf73ef332dcb6", "type": "github" }, "original": { @@ -43,11 +43,11 @@ }, "nix-filter": { "locked": { - "lastModified": 1701697642, - "narHash": "sha256-L217WytWZHSY8GW9Gx1A64OnNctbuDbfslaTEofXXRw=", + "lastModified": 1705332318, + "narHash": "sha256-kcw1yFeJe9N4PjQji9ZeX47jg0p9A0DuU4djKvg1a7I=", "owner": "numtide", "repo": "nix-filter", - "rev": "c843418ecfd0344ecb85844b082ff5675e02c443", + "rev": "3449dc925982ad46246cfc36469baf66e1b64f17", "type": "github" }, "original": { @@ -64,11 +64,11 @@ "nixpkgs": "nixpkgs_2" }, "locked": { - "lastModified": 1705277551, - "narHash": "sha256-T/HpuRjBj4CRGYSLvs3ezlzcp2WlRr5eBUrGfrCBGKc=", + "lastModified": 1705460136, + "narHash": "sha256-4bl+nspzOzkCt1m+jo8KkT2G+ckSUekyr8lY56oklCc=", "owner": "nix-ocaml", "repo": "nix-overlays", - "rev": "4798c7a11836ba876bf5e0402d4dd730139c8ebb", + "rev": "e44898b2f22f6cac71a8a8f687d1b2ff52d12a79", "type": "github" }, "original": { @@ -79,17 +79,17 @@ }, "nixpkgs_2": { "locked": { - "lastModified": 1705069489, - "narHash": "sha256-kXk4DUZS5uEdowgcv5PWVJ1s37xZKPvhpD/CFNdWMbs=", + "lastModified": 1705293701, + "narHash": "sha256-yJs738MxB+RsxGETqESof15lRJ5za6s3NmhjbXt8Kt4=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "391d29cb04fe2ca9a4744c10d6b8a7783f6b0f6d", + "rev": "715fac4e39626ca0d24481f3d1fdd54dbeeaced8", "type": "github" }, "original": { "owner": "NixOS", "repo": "nixpkgs", - "rev": "391d29cb04fe2ca9a4744c10d6b8a7783f6b0f6d", + "rev": "715fac4e39626ca0d24481f3d1fdd54dbeeaced8", "type": "github" } }, diff --git a/jscomp/core/ast_io.ml b/jscomp/core/ast_io.cppo.ml similarity index 97% rename from jscomp/core/ast_io.ml rename to jscomp/core/ast_io.cppo.ml index 4ad98a00dd..1760244b7c 100644 --- a/jscomp/core/ast_io.ml +++ b/jscomp/core/ast_io.cppo.ml @@ -23,6 +23,14 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) open Import + +module Melange_ast_version = +#if OCAML_VERSION >= (5, 1, 0) + Ppxlib_ast__.Versions.OCaml_501 +#else + Ppxlib_ast__.Versions.OCaml_414 +#endif + module Compiler_version = Ppxlib_ast.Compiler_version module type OCaml_version = Ppxlib_ast.OCaml_version @@ -35,7 +43,7 @@ module Intf_or_impl = struct module Convert = Ppxlib_ast.Convert (Ppxlib_ast.Selected_ast) - (Ppxlib_ast__.Versions.OCaml_501) + (Melange_ast_version) let ppxlib_impl : Ppxlib_ast.Ast.structure -> t = fun stru -> diff --git a/jscomp/core/ast_io.mli b/jscomp/core/ast_io.mli index e3417504d9..5637cf7857 100644 --- a/jscomp/core/ast_io.mli +++ b/jscomp/core/ast_io.mli @@ -23,6 +23,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US. *) open Import +module Melange_ast_version : Ppxlib_ast.OCaml_version module Compiler_version = Ppxlib_ast.Compiler_version module type OCaml_version = Ppxlib_ast.OCaml_version diff --git a/jscomp/core/dune b/jscomp/core/dune index b047f6923f..400bcf183e 100644 --- a/jscomp/core/dune +++ b/jscomp/core/dune @@ -8,6 +8,58 @@ ppxlib.ast dune-build-info)) +(rule + (targets lam_convert.ml) + (deps lam_convert.cppo.ml) + (action + (run + cppo + -V + OCAML:%{ocaml_version} + %{env:CPPO_FLAGS=} + %{deps} + -o + %{targets}))) + +(rule + (targets polyvar_pattern_match.ml) + (deps polyvar_pattern_match.cppo.ml) + (action + (run + cppo + -V + OCAML:%{ocaml_version} + %{env:CPPO_FLAGS=} + %{deps} + -o + %{targets}))) + +(rule + (targets initialization.ml) + (deps initialization.cppo.ml) + (action + (run + cppo + -V + OCAML:%{ocaml_version} + %{env:CPPO_FLAGS=} + %{deps} + -o + %{targets}))) + +(rule + (targets ast_io.ml) + (deps ast_io.cppo.ml) + (action + (run + cppo + -V + OCAML:%{ocaml_version} + %{env:CPPO_FLAGS=} + %{deps} + -o + %{targets}))) + (rule (target include_dirs.ml) (deps include_dirs.dev.ml include_dirs.release.ml) diff --git a/jscomp/core/gen/record_fold.ml b/jscomp/core/gen/record_fold.ml index 12e0109100..851e48d8fa 100644 --- a/jscomp/core/gen/record_fold.ml +++ b/jscomp/core/gen/record_fold.ml @@ -202,7 +202,7 @@ let make type_declaration = [ Typ.var "state"; Typ.constr { txt = Lident name; loc } []; ]))) - (StringSet.to_list customNames)) + (StringSet.to_seq customNames |> List.of_seq)) in let iter = Ast_helper.Type.mk @@ -228,7 +228,7 @@ let make type_declaration = ~f:(fun s -> let lid = { Asttypes.txt = Longident.Lident s; loc } in (lid, Ast_helper.Exp.ident lid)) - (StringSet.to_list customNames)) + (StringSet.to_seq customNames |> List.of_seq)) None in [%stri let super : 'state iter = [%e super]] diff --git a/jscomp/core/gen/record_iter.ml b/jscomp/core/gen/record_iter.ml index fe3ab92b20..47ad162b1a 100644 --- a/jscomp/core/gen/record_iter.ml +++ b/jscomp/core/gen/record_iter.ml @@ -189,7 +189,7 @@ let make type_declaration = Type.field { txt = name; loc } (Typ.constr { txt = Lident "fn"; loc } [ Typ.constr { txt = Lident name; loc } [] ]))) - (StringSet.to_list customNames)) + (StringSet.to_seq customNames |> List.of_seq)) in let iter = Ast_helper.Type.mk ~kind:record { txt = "iter"; loc } in let fn = @@ -206,7 +206,7 @@ let make type_declaration = ~f:(fun s -> let lid = { Asttypes.txt = Longident.Lident s; loc } in (lid, Ast_helper.Exp.ident lid)) - (StringSet.to_list customNames)) + (StringSet.to_seq customNames |> List.of_seq)) None in [%stri let super : iter = [%e super]] diff --git a/jscomp/core/gen/record_map.ml b/jscomp/core/gen/record_map.ml index 00ccf56d29..ec2ada4c95 100644 --- a/jscomp/core/gen/record_map.ml +++ b/jscomp/core/gen/record_map.ml @@ -219,7 +219,7 @@ let make type_declaration = Type.field { txt = name; loc } (Typ.constr { txt = Lident "fn"; loc } [ Typ.constr { txt = Lident name; loc } [] ]))) - (StringSet.to_list customNames)) + (StringSet.to_seq customNames |> List.of_seq)) in let iter = Ast_helper.Type.mk ~kind:record { txt = "iter"; loc } in let fn = @@ -236,7 +236,7 @@ let make type_declaration = ~f:(fun s -> let lid = { Asttypes.txt = Longident.Lident s; loc } in (lid, Ast_helper.Exp.ident lid)) - (StringSet.to_list customNames)) + (StringSet.to_seq customNames |> List.of_seq)) None in [%stri let super : iter = [%e super]] diff --git a/jscomp/core/initialization.ml b/jscomp/core/initialization.cppo.ml similarity index 98% rename from jscomp/core/initialization.ml rename to jscomp/core/initialization.cppo.ml index f12af7a45e..eff0c3d155 100644 --- a/jscomp/core/initialization.ml +++ b/jscomp/core/initialization.cppo.ml @@ -105,6 +105,9 @@ module Perfile = struct Typemod.initial_env ~loc:(Location.in_file "command line") ~initially_opened_module +#if OCAML_VERSION < (5,0,0) + ~safe_string:true +#endif ~open_implicit_modules:(List.rev !Clflags.open_modules) end diff --git a/jscomp/core/lam_convert.ml b/jscomp/core/lam_convert.cppo.ml similarity index 99% rename from jscomp/core/lam_convert.ml rename to jscomp/core/lam_convert.cppo.ml index 380a45c85a..d8aed3d2df 100644 --- a/jscomp/core/lam_convert.ml +++ b/jscomp/core/lam_convert.cppo.ml @@ -274,7 +274,11 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t = | Blk_na s -> let info : Lam.Tag_info.t = Blk_na s in prim ~primitive:(Pmakeblock (tag, info, mutable_flag)) ~args loc) +#if OCAML_VERSION >= (5, 1, 0) | Pfield (id, _ptr, _mut, info) -> +#else + | Pfield (id, info) -> +#endif prim ~primitive:(Pfield (id, info)) ~args loc | Psetfield (id, _, _initialization_or_assignment, info) -> prim ~primitive:(Psetfield (id, info)) ~args loc @@ -428,11 +432,13 @@ let lam_prim ~primitive:(p : Lambda.primitive) ~args loc : Lam.t = | Pbbswap i -> prim ~primitive:(Pbbswap i) ~args loc | Pbswap16 -> prim ~primitive:Pbswap16 ~args loc | Pduparray _ -> assert false +#if OCAML_VERSION >= (5, 1, 0) | Prunstack | Pperform | Presume | Preperform | Patomic_exchange | Patomic_cas | Patomic_fetch_add | Pdls_get | Patomic_load _ -> Location.raise_errorf ~loc "OCaml 5 multicore primitives (Effect, Condition, Semaphore) are not \ currently supported in Melange" +#endif (* Does not exist since we compile array in js backend unlike native backend *) diff --git a/jscomp/core/polyvar_pattern_match.ml b/jscomp/core/polyvar_pattern_match.cppo.ml similarity index 97% rename from jscomp/core/polyvar_pattern_match.ml rename to jscomp/core/polyvar_pattern_match.cppo.ml index bf3cb957ca..fe49b0b43f 100644 --- a/jscomp/core/polyvar_pattern_match.ml +++ b/jscomp/core/polyvar_pattern_match.cppo.ml @@ -125,5 +125,9 @@ let call_switcher_variant_constr (loc : Lambda.scoped_location) ( Alias, Pgenval, v, +#if OCAML_VERSION >= (5, 1, 0) Lprim (Pfield (0, Pointer, Immutable, Fld_poly_var_tag), [ arg ], loc), +#else + Lprim (Pfield (0, Fld_poly_var_tag), [ arg ], loc), +#endif call_switcher_variant_constant loc fail (Lvar v) int_lambda_list names ) diff --git a/playground/mel_playground.ml b/playground/mel_playground.ml index 2468a828ca..359a7dbf9c 100644 --- a/playground/mel_playground.ml +++ b/playground/mel_playground.ml @@ -26,7 +26,7 @@ open Melstd open Melangelib open Melange_compiler_libs module Js = Jsoo_runtime.Js -module Melange_OCaml_version = Ppxlib_ast__.Versions.OCaml_501 +module Melange_OCaml_version = Ast_io.Melange_ast_version module Melange_ast = struct external to_ppxlib : diff --git a/vendor/melange-compiler-libs b/vendor/melange-compiler-libs index 61653c6084..87bb98024b 160000 --- a/vendor/melange-compiler-libs +++ b/vendor/melange-compiler-libs @@ -1 +1 @@ -Subproject commit 61653c60844cd04af5733512768618fa9b05ec71 +Subproject commit 87bb98024b1669766f9c8aec3bbbf73ef332dcb6