Compile a typedtree from a .cmt to Lambda

Hi,

I am trying to compile a Typedtree from a .cmt file to a Lambda (using ocaml-4.08.1) with this minimal example:

let () =
  let cmt = Cmt_format.read_cmt Sys.argv.(1) in
  match cmt.cmt_annots with
  | Implementation structure ->
     let tstructure = (structure, Typedtree.Tcoerce_none) in
     Load_path.init cmt.cmt_loadpath;
     ignore (Translmod.transl_implementation cmt.cmt_modname tstructure)
  | _ -> failwith "not a structure"

Sadly, transl_implementation fails when the source file corresponding to the .cmt had a definition with a local variable.

For example using the .cmt (produced with ocamlopt -bin-annot) from:

let f = let x = 42 in x

the first code fails with:

>> Fatal error: Cannot find address for: x
Fatal error: exception Misc.Fatal_error

Did I miss something? It seems to be a problem from Env which is not set properly so the local variable x is not found.
Thanks in advance for your time!

The environments stored in the .cmt are in “summarized” form (a compact format suitable for serialization) and they need to be restored before they can be used for translation. The following should work:

let () =
  let cmt = Cmt_format.read_cmt Sys.argv.(1) in
  match cmt.cmt_annots with
  | Implementation structure ->
     Load_path.init cmt.cmt_loadpath;
     let map = {Tast_mapper.default with env = fun _ env -> Envaux.env_of_only_summary env} in
     let structure = map.structure map structure in
     let tstructure = (structure, Typedtree.Tcoerce_none) in
     Format.printf "%a@." Printlambda.program (Translmod.transl_implementation cmt.cmt_modname tstructure)
  | _ -> failwith "not a structure"

It worked, many thanks for the quick answer !