Skip to content

Commit

Permalink
Upgrade to OCaml 5.3
Browse files Browse the repository at this point in the history
compat with < 5.3
  • Loading branch information
anmonteiro committed Oct 6, 2024
1 parent ff79856 commit c734f38
Show file tree
Hide file tree
Showing 20 changed files with 276 additions and 82 deletions.
7 changes: 6 additions & 1 deletion bin/melc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,12 @@ let file_level_flags_handler (e : Parsetree.expression option) =
let args =
( List.map ~f:(fun (e: Parsetree.expression) ->
match e.pexp_desc with
| Pexp_constant (Pconst_string(name,_,_)) -> name
#if OCAML_VERSION >= (5, 3, 0)
| Pexp_constant { pconst_desc = Pconst_string(name,_,_); _ }
#else
| Pexp_constant (Pconst_string(name,_,_))
#endif
-> name
| _ -> Location.raise_errorf ~loc:e.pexp_loc "Flags must be a literal array of strings") args)
in
let argv = Melc_cli.normalize_argv (Array.of_list (Sys.argv.(0) :: args)) in
Expand Down
14 changes: 7 additions & 7 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
} // (flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages."${system}".extend (self: super: {
ocamlPackages = super.ocaml-ng.ocamlPackages_5_2;
ocamlPackages = super.ocaml-ng.ocamlPackages_5_3;
});

packages =
Expand Down
13 changes: 13 additions & 0 deletions jscomp/common/dune
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,16 @@
(deps oprint_mel_primitive_name.dev.ml oprint_mel_primitive_name.release.ml)
(action
(copy# oprint_mel_primitive_name.%{profile}.ml %{target})))

(rule
(targets utf8_string.ml)
(deps utf8_string.cppo.ml)
(action
(run
cppo
-V
OCAML:%{ocaml_version}
%{env:CPPO_FLAGS=}
%{deps}
-o
%{targets})))
70 changes: 42 additions & 28 deletions jscomp/common/utf8_string.ml → jscomp/common/utf8_string.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,30 @@

open Import

#if OCAML_VERSION < (5, 3, 0)
module Format_doc = Format
#endif

type byte = Single of int | Cont of int | Leading of int * int | Invalid

(** [classify chr] returns the {!byte} corresponding to [chr] *)
let classify chr =
let c = int_of_char chr in
(* Classify byte according to leftmost 0 bit *)
if c land 0b1000_0000 = 0 then Single c
else if
(* c 0b0____*)
c land 0b0100_0000 = 0
then Cont (c land 0b0011_1111)
else if
(* c 0b10___*)
c land 0b0010_0000 = 0
then Leading (1, c land 0b0001_1111)
else if
(* c 0b110__*)
c land 0b0001_0000 = 0
then Leading (2, c land 0b0000_1111)
else if
(* c 0b1110_ *)
c land 0b0000_1000 = 0
then Leading (3, c land 0b0000_0111)
else if
(* c 0b1111_0___*)
c land 0b0000_0100 = 0
then Leading (4, c land 0b0000_0011)
else if
(* c 0b1111_10__*)
c land 0b0000_0010 = 0
then Leading (5, c land 0b0000_0001) (* c 0b1111_110__ *)
else if (* c 0b0____*)
c land 0b0100_0000 = 0 then Cont (c land 0b0011_1111)
else if (* c 0b10___*)
c land 0b0010_0000 = 0 then Leading (1, c land 0b0001_1111)
else if (* c 0b110__*)
c land 0b0001_0000 = 0 then Leading (2, c land 0b0000_1111)
else if (* c 0b1110_ *)
c land 0b0000_1000 = 0 then Leading (3, c land 0b0000_0111)
else if (* c 0b1111_0___*)
c land 0b0000_0100 = 0 then Leading (4, c land 0b0000_0011)
else if (* c 0b1111_10__*)
c land 0b0000_0010 = 0 then Leading (5, c land 0b0000_0001)
(* c 0b1111_110__ *)
else Invalid

let rec next s ~remaining offset =
Expand Down Expand Up @@ -104,7 +97,7 @@ let pp_error fmt err =
| Invalid_hex_escape -> "Invalid \\x escape"
| Invalid_unicode_escape -> "Invalid \\u escape"
in
Format.pp_print_string fmt msg
Format_doc.pp_print_string fmt msg

type exn += Error of int (* offset *) * error

Expand Down Expand Up @@ -225,7 +218,15 @@ let transform =
e with
pexp_desc =
Pexp_constant
(Pconst_string (transform ~loc s, loc, escaped_j_delimiter));
#if OCAML_VERSION >= (5, 3, 0)
{
pconst_desc =
Pconst_string (transform ~loc s, loc, escaped_j_delimiter);
pconst_loc = loc;
};
#else
(Pconst_string (transform ~loc s, loc, escaped_j_delimiter));
#endif
}
| false -> (
match String.equal delim unescaped_j_delimiter with
Expand All @@ -236,7 +237,15 @@ let transform =
e with
pexp_desc =
Pexp_constant
(Pconst_string (transform ~loc s, loc, escaped_j_delimiter));
#if OCAML_VERSION >= (5, 3, 0)
{
pconst_desc =
Pconst_string (transform ~loc s, loc, escaped_j_delimiter);
pconst_loc = loc;
};
#else
(Pconst_string (transform ~loc s, loc, escaped_j_delimiter));
#endif
}
| false -> e)
in
Expand All @@ -250,7 +259,12 @@ let rewrite_structure =
expr =
(fun self e ->
match e.pexp_desc with
| Pexp_constant (Pconst_string (s, loc, Some delim)) ->
| Pexp_constant
#if OCAML_VERSION >= (5, 3, 0)
{ pconst_desc = Pconst_string (s, loc, Some delim); _ } ->
#else
Pconst_string (s, loc, Some delim) ->
#endif
transform e s ~loc ~delim
| _ -> super.expr self e);
}
Expand Down
4 changes: 3 additions & 1 deletion jscomp/core/ast_io.cppo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
open Import

module Melange_ast_version =
#if OCAML_VERSION >= (5, 2, 0)
#if OCAML_VERSION >= (5, 3, 0)
Ppxlib_ast__.Versions.OCaml_503
#elif OCAML_VERSION >= (5, 2, 0)
Ppxlib_ast__.Versions.OCaml_502
#elif OCAML_VERSION >= (5, 1, 0)
Ppxlib_ast__.Versions.OCaml_501
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ let report_error ppf = function

let () =
Location.register_error_of_exn (function
| Error err -> Some (Location.error_of_printer_file report_error err)
| Error err ->
#if OCAML_VERSION >= (5, 3, 0)
let f (fmt : Format_doc.formatter) err =
let doc_f =
Format_doc.deprecated_printer (fun fmt ->
Format.fprintf fmt "%a" report_error err)
in
doc_f fmt
in
Some (Location.error_of_printer_file f err)
#else
Some (Location.error_of_printer_file report_error err)
#endif
| _ -> None)

let cannot_run comm = raise (Error (CannotRun comm))
Expand Down
52 changes: 52 additions & 0 deletions jscomp/core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
-o
%{targets})))

(rule
(targets cmd_ast_exception.ml)
(deps cmd_ast_exception.cppo.ml)
(action
(run
cppo
-V
OCAML:%{ocaml_version}
%{env:CPPO_FLAGS=}
%{deps}
-o
%{targets})))

(rule
(targets compile_rec_module.ml)
(deps compile_rec_module.cppo.ml)
Expand All @@ -54,6 +67,45 @@
-o
%{targets})))

(rule
(targets record_attributes_check.ml)
(deps record_attributes_check.cppo.ml)
(action
(run
cppo
-V
OCAML:%{ocaml_version}
%{env:CPPO_FLAGS=}
%{deps}
-o
%{targets})))

(rule
(targets mel_ast_invariant.ml)
(deps mel_ast_invariant.cppo.ml)
(action
(run
cppo
-V
OCAML:%{ocaml_version}
%{env:CPPO_FLAGS=}
%{deps}
-o
%{targets})))

(rule
(targets mel_exception.ml)
(deps mel_exception.cppo.ml)
(action
(run
cppo
-V
OCAML:%{ocaml_version}
%{env:CPPO_FLAGS=}
%{deps}
-o
%{targets})))

(rule
(targets matching_polyfill.ml)
(deps matching_polyfill.cppo.ml)
Expand Down
14 changes: 5 additions & 9 deletions jscomp/core/js_cmj_format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type cmj_value = {
(** Either constant or closed functor *)
}

type effect = string option

let single_na = Single Lam_arity.na

type keyed_cmj_value = {
Expand All @@ -53,7 +51,7 @@ type t = {
delayed_program : J.deps_program;
}

let make ~(values : cmj_value String.Map.t) ~effect ~package_spec ~case
let make ~(values : cmj_value String.Map.t) ~effect_ ~package_spec ~case
~delayed_program : t =
{
values =
Expand All @@ -63,7 +61,7 @@ let make ~(values : cmj_value String.Map.t) ~effect ~package_spec ~case
arity = v.arity;
persistent_closed_lambda = v.persistent_closed_lambda;
});
pure = effect = None;
pure = effect_ = None;
package_spec;
case;
delayed_program;
Expand Down Expand Up @@ -117,7 +115,7 @@ let get_result midVal =
match midVal.persistent_closed_lambda with
| Some
(Lconst
(Const_js_null | Const_js_undefined | Const_js_true | Const_js_false))
(Const_js_null | Const_js_undefined | Const_js_true | Const_js_false))
| None ->
midVal
| Some _ ->
Expand All @@ -135,10 +133,8 @@ let rec binarySearchAux arr lo hi (key : string) =
let loVal = Array.unsafe_get arr lo in
if loVal.name = key then get_result loVal else not_found key
else binarySearchAux arr lo mid key
else if
(* a[lo] =< a[mid] < key <= a[hi] *)
lo = mid
then
else if (* a[lo] =< a[mid] < key <= a[hi] *)
lo = mid then
let hiVal = Array.unsafe_get arr hi in
if hiVal.name = key then get_result hiVal else not_found key
else binarySearchAux arr mid hi key
Expand Down
6 changes: 2 additions & 4 deletions jscomp/core/js_cmj_format.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

open Import

(** Define intemediate format to be serialized for cross module optimization *)
(** Define intermediate format to be serialized for cross module optimization *)

(** In this module,
currently only arity information is exported,
Expand Down Expand Up @@ -54,8 +54,6 @@ type cmj_value = {
(* Either constant or closed functor *)
}

type effect = string option

type keyed_cmj_value = {
name : string;
arity : arity;
Expand All @@ -72,7 +70,7 @@ type t = {

val make :
values:cmj_value String.Map.t ->
effect:effect ->
effect_:string option ->
package_spec:Js_packages_info.t ->
case:Js_packages_info.file_case ->
delayed_program:J.deps_program ->
Expand Down
Loading

0 comments on commit c734f38

Please sign in to comment.