Skip to content

Commit

Permalink
Bump compatibility to 4.11+ (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Jul 3, 2023
1 parent 40c1b32 commit 21439f5
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 150 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -4,6 +4,8 @@
* Remove deprecated values `prompt_continue`, `prompt_comment`, `smart_accept`,
`new_prompt_hooks`, `at_new_prompt` (#..., @emillon)

* Require OCaml 4.11.0 or newer. (#444, @emillon)

2.12.1 (2023-04-21)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion dune-project
Expand Up @@ -15,7 +15,7 @@
(description
"utop is an improved toplevel (i.e., Read-Eval-Print Loop or REPL) for OCaml. It can run in a terminal or in Emacs. It supports line edition, history, real-time and context sensitive completion, colors, and more. It integrates with the Tuareg mode in Emacs.")
(depends
(ocaml (>= 4.08.0))
(ocaml (>= 4.11.0))
base-unix
base-threads
(ocamlfind (>= 1.7.2))
Expand Down
8 changes: 1 addition & 7 deletions src/lib/uTop.ml
Expand Up @@ -359,13 +359,7 @@ let check_phrase phrase =
(fun () ->
Str.eval
(Exp.fun_ Nolabel None (Pat.construct unit None)
(Exp.letmodule (with_loc loc
#if OCAML_VERSION >= (4, 10, 0)
(Some "_")
#else
"_"
#endif
)
(Exp.letmodule (with_loc loc (Some "_"))
(Mod.structure (item :: items))
(Exp.construct unit None))))
in
Expand Down
84 changes: 0 additions & 84 deletions src/lib/uTop_compat.ml
@@ -1,10 +1,3 @@
let lookup_value =
#if OCAML_VERSION >= (4, 10, 0)
Env.find_value_by_name
#else
Env.lookup_value
#endif

let get_desc x =
#if OCAML_VERSION >= (4, 14, 0)
Types.get_desc x
Expand All @@ -19,67 +12,13 @@ let toploop_get_directive name =
try Some (Hashtbl.find Toploop.directive_table name) with Not_found -> None
#endif

let lookup_module name env =
#if OCAML_VERSION >= (4, 10, 0)
let path, md = Env.find_module_by_name name env in
#else
let path = Env.lookup_module name env ~load:true in
let md = Env.find_module path env in
#endif
(path, md.md_type)

let lookup_label =
#if OCAML_VERSION >= (4, 10, 0)
Env.find_label_by_name
#else
Env.lookup_label
#endif

let lookup_modtype =
#if OCAML_VERSION >= (4, 10, 0)
Env.find_modtype_by_name
#else
Env.lookup_modtype
#endif

let lookup_constructor =
#if OCAML_VERSION >= (4, 10, 0)
Env.find_constructor_by_name
#else
Env.lookup_constructor
#endif

let lookup_class=
#if OCAML_VERSION >= (4, 10, 0)
Env.find_class_by_name
#else
Env.lookup_class
#endif

let longident_parse str =
#if OCAML_VERSION >= (4, 11, 0)
let lexbuf = Lexing.from_string str in
Parse.longident lexbuf
#else
Longident.parse str
#endif

let toploop_all_directive_names () =
#if OCAML_VERSION >= (4, 13, 0)
Toploop.all_directive_names ()
#else
Hashtbl.fold (fun dir _ acc -> dir::acc) Toploop.directive_table []
#endif

#if OCAML_VERSION >= (4, 10, 0)
let lookup_type longident env =
Env.find_type_by_name longident env
#else
let lookup_type longident env =
let path = Env.lookup_type longident env in
(path, Env.find_type path env)
#endif

let set_load_path path =
#if OCAML_VERSION >= (5, 0, 0)
Load_path.init path ~auto_include:Load_path.no_auto_include
Expand All @@ -94,13 +33,6 @@ let toploop_use_silently fmt name =
Toploop.use_silently fmt name
#endif

module Persistent_signature =
#if OCAML_VERSION >= (4, 09, 0)
Persistent_env.Persistent_signature
#else
Env.Persistent_signature
#endif

let toploop_set_paths () =
#if OCAML_VERSION >= (5, 0, 0)
Toploop.set_paths ~auto_include:Load_path.no_auto_include ()
Expand All @@ -115,22 +47,6 @@ let toploop_load_file ppf fn =
Topdirs.load_file ppf fn
#endif

let iter_structure expr =
#if OCAML_VERSION >= (4,09,0)
let next iterator e = Tast_iterator.default_iterator.expr iterator e in
let expr iterator = expr (next iterator) in
let iter = { Tast_iterator.default_iterator with expr } in
iter.structure iter
#else
let module Search =
TypedtreeIter.MakeIterator(struct
include TypedtreeIter.DefaultIteratorArgument

let enter_expression = expr ignore
end) in
Search.iter_structure
#endif

(** Returns whether the given path is persistent. *)
let rec is_persistent_path = function
| Path.Pident id -> Ident.persistent id
Expand Down
34 changes: 10 additions & 24 deletions src/lib/uTop_complete.ml
Expand Up @@ -494,9 +494,9 @@ let names_of_module longident =
try
Longident_map.find longident !local_names_by_longident
with Not_found ->
match lookup_env lookup_module longident !Toploop.toplevel_env with
| Some(path, module_type) ->
let names = names_of_module_type module_type in
match lookup_env Env.find_module_by_name longident !Toploop.toplevel_env with
| Some(path, {md_type; _}) ->
let names = names_of_module_type md_type in
local_names_by_path := Path_map.add path names !local_names_by_path;
local_names_by_longident := Longident_map.add longident names !local_names_by_longident;
names
Expand All @@ -508,9 +508,9 @@ let fields_of_module longident =
try
Longident_map.find longident !local_fields_by_longident
with Not_found ->
match lookup_env lookup_module longident !Toploop.toplevel_env with
| Some(path, module_type) ->
let fields = fields_of_module_type module_type in
match lookup_env Env.find_module_by_name longident !Toploop.toplevel_env with
| Some(path, {md_type; _}) ->
let fields = fields_of_module_type md_type in
local_fields_by_path := Path_map.add path fields !local_fields_by_path;
local_fields_by_longident := Longident_map.add longident fields !local_fields_by_longident;
fields
Expand All @@ -521,10 +521,8 @@ let fields_of_module longident =
let list_global_names () =
let rec loop acc = function
| Env.Env_empty -> acc
#if OCAML_VERSION >= (4, 10, 0)
| Env.Env_value_unbound _-> acc
| Env.Env_module_unbound _-> acc
#endif
| Env.Env_value(summary, id, _) ->
loop (add (Ident.name id) acc) summary
| Env.Env_type(summary, id, decl) ->
Expand All @@ -545,13 +543,8 @@ let list_global_names () =
loop (add (Ident.name id) acc) summary
| Env.Env_constraints (summary, _) ->
loop acc summary
#if OCAML_VERSION >= (4, 10, 0)
| Env.Env_copy_types summary ->
loop acc summary
#else
| Env.Env_copy_types (summary, _) ->
loop acc summary
#endif
| Env.Env_open(summary, path) ->
match try Some (Path_map.find path !local_names_by_path) with Not_found -> None with
| Some names ->
Expand Down Expand Up @@ -582,10 +575,8 @@ let replace x y set =
let list_global_fields () =
let rec loop acc = function
| Env.Env_empty -> acc
#if OCAML_VERSION >= (4, 10, 0)
| Env.Env_value_unbound _-> acc
| Env.Env_module_unbound _-> acc
#endif
| Env.Env_value(summary, id, _) ->
loop (add (Ident.name id) acc) summary
| Env.Env_type(summary, id, decl) ->
Expand All @@ -606,13 +597,8 @@ let list_global_fields () =
loop (add (Ident.name id) acc) summary
| Env.Env_constraints (summary, _) ->
loop acc summary
#if OCAML_VERSION >= (4, 10, 0)
| Env.Env_copy_types summary ->
loop acc summary
#else
| Env.Env_copy_types (summary, _) ->
loop acc summary
#endif
| Env.Env_open(summary, path) ->
match try Some (Path_map.find path !local_fields_by_path) with Not_found -> None with
| Some fields ->
Expand Down Expand Up @@ -695,7 +681,7 @@ let rec find_object meths type_expr =
None

let methods_of_object longident meths =
match lookup_env lookup_value longident !Toploop.toplevel_env with
match lookup_env Env.find_value_by_name longident !Toploop.toplevel_env with
| None ->
[]
| Some (path, { val_type = type_expr }) ->
Expand Down Expand Up @@ -735,7 +721,7 @@ let rec labels_of_type acc type_expr =
String_map.bindings acc

let labels_of_function longident meths =
match lookup_env lookup_value longident !Toploop.toplevel_env with
match lookup_env Env.find_value_by_name longident !Toploop.toplevel_env with
| None ->
[]
| Some (path, { val_type = type_expr }) ->
Expand All @@ -746,7 +732,7 @@ let labels_of_function longident meths =
labels_of_type String_map.empty type_expr

let labels_of_newclass longident =
match lookup_env lookup_class longident !Toploop.toplevel_env with
match lookup_env Env.find_class_by_name longident !Toploop.toplevel_env with
| None ->
[]
| Some (path, { cty_new = None }) ->
Expand Down Expand Up @@ -830,7 +816,7 @@ let complete ~phrase_terminator ~input =

| [(Symbol "#", _); (Lident "typeof", _); (String (tlen, false), loc)] ->
let prefix = String.sub input (loc.ofs1 + tlen) (String.length input - loc.ofs1 - tlen) in
begin match longident_parse prefix with
begin match Parse.longident (Lexing.from_string prefix) with
| Longident.Ldot (lident, last_prefix) ->
let set = names_of_module lident in
let compls = lookup last_prefix (String_set.elements set) in
Expand Down

0 comments on commit 21439f5

Please sign in to comment.