about summary refs log tree commit diff
path: root/tvix/eval
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-03T00·40+0300
committertazjin <tazjin@tvl.su>2022-09-08T19·54+0000
commit0af1df4be2a7dd660cb9370b0bbf97f4fb7ab150 (patch)
tree836d3526a3e4c3701d8f0ee9596738adfeb785b7 /tvix/eval
parent23f248b530bedc556123033519514e03b1a8278f (diff)
refactor(tvix/eval): clean up logic in Compiler::end_scope r/4760
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 <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval')
-rw-r--r--tvix/eval/src/compiler/mod.rs8
1 files changed, 3 insertions, 5 deletions
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,