about summary refs log tree commit diff
path: root/tvix/eval/src/compiler/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tvix/eval/src/compiler/mod.rs')
-rw-r--r--tvix/eval/src/compiler/mod.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs
index 2f4fc4da64b6..9fb24aa98ec9 100644
--- a/tvix/eval/src/compiler/mod.rs
+++ b/tvix/eval/src/compiler/mod.rs
@@ -133,6 +133,11 @@ pub struct Compiler<'observer> {
     /// Carry an observer for the compilation process, which is called
     /// whenever a chunk is emitted.
     observer: &'observer mut dyn CompilerObserver,
+
+    /// Carry a count of nested scopes which have requested the
+    /// compiler not to emit anything. This used for compiling dead
+    /// code branches to catch errors & warnings in them.
+    dead_scope: usize,
 }
 
 impl Compiler<'_> {
@@ -185,6 +190,7 @@ impl<'observer> Compiler<'observer> {
             contexts: vec![LambdaCtx::new()],
             warnings: vec![],
             errors: vec![],
+            dead_scope: 0,
         })
     }
 }
@@ -216,6 +222,10 @@ impl Compiler<'_> {
     /// Push a single instruction to the current bytecode chunk and
     /// track the source span from which it was compiled.
     fn push_op<T: ToSpan>(&mut self, data: OpCode, node: &T) -> CodeIdx {
+        if self.dead_scope > 0 {
+            return CodeIdx(0);
+        }
+
         let span = self.span_for(node);
         self.chunk().push_op(data, span)
     }
@@ -223,6 +233,10 @@ impl Compiler<'_> {
     /// Emit a single constant to the current bytecode chunk and track
     /// the source span from which it was compiled.
     pub(super) fn emit_constant<T: ToSpan>(&mut self, value: Value, node: &T) {
+        if self.dead_scope > 0 {
+            return;
+        }
+
         let idx = self.chunk().push_constant(value);
         self.push_op(OpCode::OpConstant(idx), node);
     }
@@ -231,7 +245,7 @@ impl Compiler<'_> {
 // Actual code-emitting AST traversal methods.
 impl Compiler<'_> {
     fn compile(&mut self, slot: LocalIdx, expr: ast::Expr) {
-        let expr = optimiser::optimise_expr(self, expr);
+        let expr = optimiser::optimise_expr(self, slot, expr);
 
         match &expr {
             ast::Expr::Literal(literal) => self.compile_literal(literal),
@@ -289,6 +303,16 @@ impl Compiler<'_> {
         }
     }
 
+    /// Compiles an expression, but does not emit any code for it as
+    /// it is considered dead. This will still catch errors and
+    /// warnings in that expression.
+    fn compile_dead_code(&mut self, slot: LocalIdx, node: ast::Expr) {
+        self.dead_scope += 1;
+        self.emit_warning(&node, WarningKind::DeadCode);
+        self.compile(slot, node);
+        self.dead_scope -= 1;
+    }
+
     fn compile_literal(&mut self, node: &ast::Literal) {
         let value = match node.kind() {
             ast::LiteralKind::Float(f) => Value::Float(f.value().unwrap()),
@@ -956,7 +980,9 @@ impl Compiler<'_> {
         let mut compiled = self.contexts.pop().unwrap();
 
         // Check if tail-call optimisation is possible and perform it.
-        optimise_tail_call(&mut compiled.lambda.chunk);
+        if self.dead_scope == 0 {
+            optimise_tail_call(&mut compiled.lambda.chunk);
+        }
 
         // Capturing the with stack counts as an upvalue, as it is
         // emitted as an upvalue data instruction.