From 712fc1e2798ddf56172aafaf8253b49ed13a8e94 Mon Sep 17 00:00:00 2001 From: Louis Gesbert Date: Fri, 21 Jun 2024 11:35:21 +0200 Subject: [PATCH] Support closure calls across modules First I was disappointed that we couldn't convert closure environment properly because of their opaque nature (native/interpreted conversion is based on the Catala types) ; but after more thought it's actually unnecessary to convert them at all since we are guaranteed that they can't be consumed outside of their realm. --- compiler/shared_ast/interpreter.ml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/compiler/shared_ast/interpreter.ml b/compiler/shared_ast/interpreter.ml index 88837758..171dce2d 100644 --- a/compiler/shared_ast/interpreter.ml +++ b/compiler/shared_ast/interpreter.ml @@ -566,7 +566,11 @@ let rec runtime_to_val : let e = runtime_to_val eval_expr ctx m ty (Obj.field o 0) in EInj { name = Expr.option_enum; cons = Expr.some_constr; e }, m | _ -> assert false) - | TClosureEnv -> assert false + | TClosureEnv -> + (* By construction, a closure environment can only be consumed from the same + scope where it was built (compiled or not) ; for this reason, we can + safely avoid converting in depth here *) + Obj.obj o, m | TArray ty -> ( EArray (List.map @@ -656,6 +660,11 @@ and val_to_runtime : in curry [] targs | TDefault ty, _ -> val_to_runtime eval_expr ctx ty v + | TClosureEnv, v -> + (* By construction, a closure environment can only be consumed from the same + scope where it was built (compiled or not) ; for this reason, we can + safely avoid converting in depth here *) + Obj.repr v | _ -> Message.error ~internal:true "Could not convert value of type %a@ to@ runtime:@ %a" (Print.typ ctx) ty