Skip to content

Preserve backtraces from failing Lazy_backtrack computations #805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions ocaml/utils/misc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ module EnvLazy = struct

and ('a,'b) eval =
| Done of 'b
| Raise of exn
| Raise of exn * Printexc.raw_backtrace
| Thunk of 'a

type undo =
Expand All @@ -976,14 +976,15 @@ module EnvLazy = struct
let force f x =
match !x with
| Done x -> x
| Raise e -> raise e
| Raise (e, bt) -> Printexc.raise_with_backtrace e bt
| Thunk e ->
match f e with
| y ->
x := Done y;
y
| exception e ->
x := Raise e;
let bt = Printexc.get_raw_backtrace () in
x := Raise (e, bt);
raise e

let get_arg x =
Expand All @@ -993,24 +994,34 @@ module EnvLazy = struct
match !x with
| Thunk a -> Either.Left a
| Done b -> Either.Right b
| Raise e -> raise e
| Raise (e, bt) -> Printexc.raise_with_backtrace e bt

let create x =
ref (Thunk x)

let create_forced y =
ref (Done y)

type does_not_return = |
exception I_just_want_a_backtrace

let create_failed e =
ref (Raise e)
let bt =
(* We want to call [Printexc.get_raw_backtrace], but that's only valid in
an exception handler. So let's handle an exception. *)
match (raise I_just_want_a_backtrace : does_not_return) with
| exception _ -> Printexc.get_raw_backtrace ()
| _ -> .
in
ref (Raise (e, bt))

let log () =
ref Nil

let force_logged log f x =
match !x with
| Done x -> x
| Raise e -> raise e
| Raise (e, bt) -> Printexc.raise_with_backtrace e bt
| Thunk e ->
match f e with
| (Error _ as err : _ result) ->
Expand All @@ -1021,7 +1032,8 @@ module EnvLazy = struct
x := Done res;
res
| exception e ->
x := Raise e;
let bt = Printexc.get_raw_backtrace () in
x := Raise (e, bt);
raise e

let backtrack log =
Expand Down