Skip to content

Commit

Permalink
flambda-backend: Preserve backtraces from failing Lazy_backtrack co…
Browse files Browse the repository at this point in the history
…mputations (ocaml-flambda#805)

* Preserve backtraces from failing `EnvLazy` computations

Capture the backtrace whenever a `Misc.EnvLazy.t` fails (or when
it's created with `create_failed`), then use
`Printexc.raise_with_backtrace` so that anyone forcing the value
gets an accurate backtrace.

* Use `Printexc.get_callstack`
  • Loading branch information
lukemaurer authored Feb 28, 2023
1 parent 56d014e commit 777fda7
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions utils/lazy_backtrack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ('a,'b) t = ('a,'b) eval ref

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

type undo =
Expand All @@ -29,14 +29,15 @@ type log = undo ref
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 @@ -46,24 +47,27 @@ let get_contents x =
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)

let backtrace_size = 64

let create_failed e =
ref (Raise e)
let bt = Printexc.get_callstack backtrace_size 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 @@ -74,7 +78,8 @@ let force_logged log f x =
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

0 comments on commit 777fda7

Please sign in to comment.