Skip to content

Commit c520c0a

Browse files
Add support for load_rec and patch load conditionally
OCaml 4.13 introduces a few deprecations that we need to work around.
1 parent 6fd673c commit c520c0a

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

lib/top/compat_top.ml

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,19 @@ let action_on_suberror b =
195195
if not b && not !Sys.interactive then
196196
raise (Exit_with_status 125)
197197

198-
(* [load] cannot be patched to return errors because the underlying code is not exposed:
199-
It would require [Topdirs.load_file] with the first argument to be [false] but the exposed
200-
version hardcodes it to [true].
201-
*)
202-
let mdx_load = patch_directive "load" (Directive_string (Topdirs.dir_load std_err))
203-
204198
let dir_use ppf name =
205199
action_on_suberror (Toploop.use_file ppf name)
206200

207201
let mdx_use = patch_directive "use" (Directive_string (dir_use std_err))
202+
208203
let mdx_install_printer = patch_directive "install_printer" (Directive_ident (Topdirs.dir_install_printer std_err))
209204
let mdx_remove_printer = patch_directive "remove_printer" (Directive_ident (Topdirs.dir_remove_printer std_err))
210205
#endif
211206

212207
#if OCAML_VERSION > (4, 11, 0) && OCAML_VERSION < (4, 14, 0)
213208

214-
let dir_use_output ppf name = action_on_suberror (Toploop.use_output ppf name)
209+
let dir_use_output ppf name =
210+
action_on_suberror (Toploop.use_output ppf name)
215211

216212
let mdx_use_output = patch_directive "use_output" (Directive_string (dir_use_output std_err))
217213
#endif
@@ -222,10 +218,39 @@ let mdx_untrace = patch_directive "untrace" (Directive_ident (Topdirs.dir_untrac
222218
let mdx_untrace_all = patch_directive "untrace_all" (Directive_none (Topdirs.dir_untrace_all std_err))
223219
#endif
224220

221+
#if OCAML_VERSION < (4, 13, 0)
222+
(* [load] cannot be patched to return errors because the underlying code is not exposed:
223+
It would require [Topdirs.load_file] with the first argument to be [false] but the exposed
224+
version hardcodes it to [true].
225+
*)
226+
let mdx_load = patch_directive "load" (Directive_string (Topdirs.dir_load std_err))
227+
228+
(* On the other hand, [load_rec] can be patched because the curried [true] is the only
229+
difference between these directives *)
230+
let dir_load_rec ppf name =
231+
action_on_suberror (Topdirs.load_file ppf name)
232+
233+
let mdx_load_rec = patch_directive "load_rec" (Directive_string (dir_load_rec std_err))
234+
235+
#elif OCAML_VERSION > (4, 13, 0) && OCAML_VERSION < (4, 14, 0)
236+
(* OCaml 4.13 exposes [Topeval.load_file] which allows us to patch [#load] too *)
237+
let dir_load ppf name =
238+
action_on_suberror (Topeval.load_file false ppf name)
239+
240+
let mdx_load = patch_directive "load" (Directive_string (dir_load std_err))
241+
242+
(* This uses [Topeval.load_file] because [Topdirs.load_file] is deprecated on 4.13 *)
243+
let dir_load_rec ppf name =
244+
action_on_suberror (Topeval.load_file true ppf name)
245+
246+
let mdx_load_rec = patch_directive "load_rec" (Directive_string (dir_load_rec std_err))
247+
#endif
248+
225249
let redirect_directive directive =
226250
match directive with
227251
#if OCAML_VERSION < (4, 14, 0)
228252
| "load" -> mdx_load
253+
| "load_rec" -> mdx_load_rec
229254
| "use" -> mdx_use
230255
| "install_printer" -> mdx_install_printer
231256
| "remove_printer" -> mdx_remove_printer

0 commit comments

Comments
 (0)