about summary refs log tree commit diff
path: root/tvix/eval/src/compiler/scope.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-28T12·34+0300
committertazjin <tazjin@tvl.su>2022-09-06T07·45+0000
commit47b356286751bff8f41930761a402564d54d1898 (patch)
tree3dad60022ee4229c90bf26463a8418113989e23c /tvix/eval/src/compiler/scope.rs
parent705c1b9d535a2a57d3f7f2208818015adaa255a8 (diff)
refactor(tvix/eval): decouple local depth & initialisation tracking r/4661
In order to resolve recursive references correctly, these two can not
be initialised the same way as a potentially large number of (nested!)
locals can be declared without initialising their depth.

This would lead to issues with detecting things like shadowed
variables, so making both bits explicit is preferable.

Change-Id: I100cdf1724faa4a2b5a0748429841cf8ef206252
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6325
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'tvix/eval/src/compiler/scope.rs')
-rw-r--r--tvix/eval/src/compiler/scope.rs62
1 files changed, 22 insertions, 40 deletions
diff --git a/tvix/eval/src/compiler/scope.rs b/tvix/eval/src/compiler/scope.rs
index 3061e08ead25..a76a411b461b 100644
--- a/tvix/eval/src/compiler/scope.rs
+++ b/tvix/eval/src/compiler/scope.rs
@@ -16,34 +16,6 @@ use smol_str::SmolStr;
 
 use crate::opcode::{StackIdx, UpvalueIdx};
 
-/// Represents the initialisation status of a variable, tracking
-/// whether it is only known or also already defined.
-pub enum Depth {
-    /// Variable is defined and located at the given depth.
-    At(usize),
-
-    /// Variable is known but not yet defined.
-    Unitialised,
-}
-
-impl Depth {
-    /// Does this variable live above the other given depth?
-    pub fn above(&self, theirs: usize) -> bool {
-        match self {
-            Depth::Unitialised => false,
-            Depth::At(ours) => *ours > theirs,
-        }
-    }
-
-    /// Does this variable live below the other given depth?
-    pub fn below(&self, theirs: usize) -> bool {
-        match self {
-            Depth::Unitialised => false,
-            Depth::At(ours) => *ours < theirs,
-        }
-    }
-}
-
 /// Represents a single local already known to the compiler.
 pub struct Local {
     // Definition name, which can be different kinds of tokens (plain
@@ -55,7 +27,10 @@ pub struct Local {
     pub node: Option<rnix::SyntaxNode>,
 
     // Scope depth of this local.
-    pub depth: Depth,
+    pub depth: usize,
+
+    // Is this local initialised?
+    pub initialised: bool,
 
     // Phantom locals are not actually accessible by users (e.g.
     // intermediate values used for `with`).
@@ -65,6 +40,13 @@ pub struct Local {
     pub used: bool,
 }
 
+impl Local {
+    /// Does this local live above the other given depth?
+    pub fn above(&self, theirs: usize) -> bool {
+        self.depth > theirs
+    }
+}
+
 /// Represents the current position of a local as resolved in a scope.
 pub enum LocalPosition {
     /// Local is not known in this scope.
@@ -175,17 +157,17 @@ impl Scope {
             if !local.phantom && local.name == name {
                 local.used = true;
 
-                match local.depth {
-                    // This local is still being initialised, meaning
-                    // that we know its final runtime stack position,
-                    // but it is not yet on the stack.
-                    Depth::Unitialised => return LocalPosition::Recursive(StackIdx(idx)),
-
-                    // This local is known, but we need to account for
-                    // uninitialised variables in this "initialiser
-                    // stack".
-                    Depth::At(_) => return LocalPosition::Known(self.resolve_uninit(idx)),
+                // This local is still being initialised, meaning that
+                // we know its final runtime stack position, but it is
+                // not yet on the stack.
+                if !local.initialised {
+                    return LocalPosition::Recursive(StackIdx(idx));
                 }
+
+                // This local is known, but we need to account for
+                // uninitialised variables in this "initialiser
+                // stack".
+                return LocalPosition::Known(self.resolve_uninit(idx));
             }
         }
 
@@ -200,7 +182,7 @@ impl Scope {
         StackIdx(
             self.locals[..locals_idx]
                 .iter()
-                .filter(|local| matches!(local.depth, Depth::At(_)))
+                .filter(|local| local.initialised)
                 .count(),
         )
     }