From ae531a2245e83d44ce58ba6c588713265984dbbf Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 27 Aug 2022 22:58:39 +0300 Subject: feat(tvix/eval): implement capture of self-recursive upvalues With this change, it becomes possible for functions to call themselves as they are being defined in local bindings. Change-Id: Ib46a39ba17b1452b5673d96fa729d633d237241a Reviewed-on: https://cl.tvl.fyi/c/depot/+/6314 Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/compiler/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'tvix/eval/src/compiler') diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index 520eb3bd6404..6ba470a7af9f 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -709,9 +709,9 @@ impl Compiler { self.chunk().push_op(OpCode::OpResolveWith) } - LocalPosition::Recursive(_) => todo!("self-recursive upvalue"), - LocalPosition::Known(idx) => self.chunk().push_op(OpCode::OpGetLocal(idx)), + + LocalPosition::Recursive(_) => panic!("TODO: unclear if this can happen"), }; } @@ -981,10 +981,14 @@ impl Compiler { // Determine whether the upvalue is a local in the enclosing context. match self.contexts[ctx_idx - 1].scope.resolve_local(name) { - LocalPosition::Known(idx) => { + // recursive upvalues are dealt with the same way as + // standard known ones, as thunks and closures are + // guaranteed to be placed on the stack (i.e. in the right + // position) *during* their runtime construction + LocalPosition::Known(idx) | LocalPosition::Recursive(idx) => { return Some(self.add_upvalue(ctx_idx, Upvalue::Stack(idx))) } - LocalPosition::Recursive(_) => todo!("self-recursive upvalue"), + LocalPosition::Unknown => { /* continue below */ } }; -- cgit 1.4.1