about summary refs log tree commit diff
path: root/tvix/eval
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-01T15·02+0300
committertazjin <tazjin@tvl.su>2022-09-07T20·04+0000
commit3efed26940cf24676e6de9b329d24b2a555f2330 (patch)
treeaabb2ad78a89f2d2569574b797e0f34eddf95d00 /tvix/eval
parentb8874f8d352f1f255a9eed0fdadc31d45de85e46 (diff)
refactor(tvix/eval): split out Upvalue struct & UpvalueKind enum r/4732
This separation makes it possible to annotate the upvalue itself with
the span that created it, which (due to upvalue reuse) is only the
first one for an instance of the given UpvalueKind.

Change-Id: I9a991da6a3e8d71a92f981314bed900bcf434d44
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6399
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval')
-rw-r--r--tvix/eval/src/compiler/mod.rs26
-rw-r--r--tvix/eval/src/compiler/scope.rs7
2 files changed, 19 insertions, 14 deletions
diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs
index e664ceaf5b..07b68061c8 100644
--- a/tvix/eval/src/compiler/mod.rs
+++ b/tvix/eval/src/compiler/mod.rs
@@ -29,7 +29,7 @@ use crate::opcode::{CodeIdx, Count, JumpOffset, OpCode, UpvalueIdx};
 use crate::value::{Closure, Lambda, Thunk, Value};
 use crate::warnings::{EvalWarning, WarningKind};
 
-use self::scope::{Local, LocalIdx, LocalPosition, Scope, Upvalue};
+use self::scope::{Local, LocalIdx, LocalPosition, Scope, Upvalue, UpvalueKind};
 
 /// Represents the result of compiling a piece of Nix code. If
 /// compilation was successful, the resulting bytecode can be passed
@@ -816,7 +816,7 @@ impl Compiler<'_> {
             // must be thunked.
             LocalPosition::Recursive(idx) => self.thunk(slot, &node, move |compiler, node, _| {
                 let upvalue_idx =
-                    compiler.add_upvalue(compiler.contexts.len() - 1, Upvalue::Local(idx));
+                    compiler.add_upvalue(compiler.contexts.len() - 1, UpvalueKind::Local(idx));
                 compiler.push_op(OpCode::OpGetUpvalue(upvalue_idx), node);
             }),
         };
@@ -956,13 +956,13 @@ impl Compiler<'_> {
     /// assemble the provided upvalues array.
     fn emit_upvalue_data(&mut self, slot: Option<LocalIdx>, upvalues: Vec<Upvalue>) {
         for upvalue in upvalues {
-            match upvalue {
-                Upvalue::Local(idx) if slot.is_none() => {
+            match upvalue.kind {
+                UpvalueKind::Local(idx) if slot.is_none() => {
                     let stack_idx = self.scope().stack_index(idx);
                     self.push_op_old(OpCode::DataLocalIdx(stack_idx));
                 }
 
-                Upvalue::Local(idx) => {
+                UpvalueKind::Local(idx) => {
                     let stack_idx = self.scope().stack_index(idx);
 
                     // If the upvalue slot is located *after* the
@@ -977,10 +977,10 @@ impl Compiler<'_> {
                     }
                 }
 
-                Upvalue::Upvalue(idx) => {
+                UpvalueKind::Upvalue(idx) => {
                     self.push_op_old(OpCode::DataUpvalueIdx(idx));
                 }
-                Upvalue::Dynamic { name, up } => {
+                UpvalueKind::Dynamic { name, up } => {
                     let idx = self.chunk().push_constant(Value::String(name.into()));
                     self.push_op_old(OpCode::DataDynamicIdx(idx));
                     if let Some(up) = up {
@@ -1119,7 +1119,7 @@ impl Compiler<'_> {
             // 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::Local(idx)))
+                return Some(self.add_upvalue(ctx_idx, UpvalueKind::Local(idx)))
             }
 
             LocalPosition::Unknown => { /* continue below */ }
@@ -1129,7 +1129,7 @@ impl Compiler<'_> {
         // recurse to make sure that the upvalues are created at each
         // level.
         if let Some(idx) = self.resolve_upvalue(ctx_idx - 1, name) {
-            return Some(self.add_upvalue(ctx_idx, Upvalue::Upvalue(idx)));
+            return Some(self.add_upvalue(ctx_idx, UpvalueKind::Upvalue(idx)));
         }
 
         None
@@ -1168,7 +1168,7 @@ impl Compiler<'_> {
             for idx in lowest_idx..=at {
                 upvalue_idx = Some(self.add_upvalue(
                     idx,
-                    Upvalue::Dynamic {
+                    UpvalueKind::Dynamic {
                         name: name.clone(),
                         up: upvalue_idx,
                     },
@@ -1183,16 +1183,16 @@ impl Compiler<'_> {
         None
     }
 
-    fn add_upvalue(&mut self, ctx_idx: usize, upvalue: Upvalue) -> UpvalueIdx {
+    fn add_upvalue(&mut self, ctx_idx: usize, kind: UpvalueKind) -> UpvalueIdx {
         // If there is already an upvalue closing over the specified
         // index, retrieve that instead.
         for (idx, existing) in self.contexts[ctx_idx].scope.upvalues.iter().enumerate() {
-            if *existing == upvalue {
+            if existing.kind == kind {
                 return UpvalueIdx(idx);
             }
         }
 
-        self.contexts[ctx_idx].scope.upvalues.push(upvalue);
+        self.contexts[ctx_idx].scope.upvalues.push(Upvalue { kind });
 
         let idx = UpvalueIdx(self.contexts[ctx_idx].lambda.upvalue_count);
         self.contexts[ctx_idx].lambda.upvalue_count += 1;
diff --git a/tvix/eval/src/compiler/scope.rs b/tvix/eval/src/compiler/scope.rs
index 87b43ad1ca..01e0801e65 100644
--- a/tvix/eval/src/compiler/scope.rs
+++ b/tvix/eval/src/compiler/scope.rs
@@ -72,7 +72,7 @@ pub enum LocalPosition {
 /// Represents the different ways in which upvalues can be captured in
 /// closures or thunks.
 #[derive(Clone, Debug, PartialEq, Eq)]
-pub enum Upvalue {
+pub enum UpvalueKind {
     /// This upvalue captures a local from the stack.
     Local(LocalIdx),
 
@@ -91,6 +91,11 @@ pub enum Upvalue {
     },
 }
 
+#[derive(Clone, Debug)]
+pub struct Upvalue {
+    pub kind: UpvalueKind,
+}
+
 /// Represents the index of a local in the scope's local array, which
 /// is subtly different from its `StackIdx` (which excludes
 /// uninitialised values in between).