From 0af1df4be2a7dd660cb9370b0bbf97f4fb7ab150 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 3 Sep 2022 03:40:53 +0300 Subject: refactor(tvix/eval): clean up logic in Compiler::end_scope The condition here was extremely hard to read prior to this change. As the locals vector is now guaranteed to never be empty (there is always at least a phantom for the current chunk's root expression), the logic here can be simplified to just dropping tailing locals entries while their depth matches that of the scope being closed. Change-Id: I24973e23bc2ad25e62ece64ab4d8624e6e274c16 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6427 Reviewed-by: sterni Tested-by: BuildkiteCI --- tvix/eval/src/compiler/mod.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'tvix/eval/src/compiler') diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs index f669e17ac2d9..731ba9179c4b 100644 --- a/tvix/eval/src/compiler/mod.rs +++ b/tvix/eval/src/compiler/mod.rs @@ -1053,8 +1053,6 @@ impl Compiler<'_> { let depth = self.scope().scope_depth; self.scope_mut().unpoison(depth); - self.scope_mut().scope_depth -= 1; - // When ending a scope, all corresponding locals need to be // removed, but the value of the body needs to remain on the // stack. This is implemented by a separate instruction. @@ -1062,9 +1060,7 @@ impl Compiler<'_> { // TL;DR - iterate from the back while things belonging to the // ended scope still exist. - while !self.scope().locals.is_empty() - && self.scope().locals[self.scope().locals.len() - 1].above(self.scope().scope_depth) - { + while self.scope().locals.last().unwrap().depth == depth { if let Some(local) = self.scope_mut().locals.pop() { // pop the local from the stack if it was actually // initialised @@ -1085,6 +1081,8 @@ impl Compiler<'_> { if pops > 0 { self.push_op(OpCode::OpCloseScope(Count(pops)), node); } + + self.scope_mut().scope_depth -= 1; } /// Open a new lambda context within which to compile a function, -- cgit 1.4.1