about summary refs log tree commit diff
path: root/tvix
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-17T23·52+0300
committertazjin <tazjin@tvl.su>2022-09-18T21·12+0000
commit0e5baae7ad16ca2a5a70ba34d922cabcaa68d45e (patch)
tree02efc5d294da14df71e8bebc93de97a5d1d2835b /tvix
parent5dd5c7e254f01f96a2ddfcf2c163c08833ec0061 (diff)
refactor(tvix/eval): clone the Arc<codemap::File> for the compiler r/4907
This disconnects ownership of the `File` reference in a compiler from
the calling scope, which is required for when we implement `import`.

`import` will need to carry an `Rc<RefCell<CodeMap>>` (or maybe, in
the future, Arc) to give us the ability to add new detected code
files at runtime.

Note that the choice of `Arc` over `Rc` here is not ours - it's the
codemap crate's.

Change-Id: I3aeca4ffc167acbd1701846a332d93550b56ba7d
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6630
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
Diffstat (limited to 'tvix')
-rw-r--r--tvix/eval/src/compiler/attrs.rs2
-rw-r--r--tvix/eval/src/compiler/mod.rs11
-rw-r--r--tvix/eval/src/compiler/spans.rs4
-rw-r--r--tvix/eval/src/eval.rs4
4 files changed, 11 insertions, 10 deletions
diff --git a/tvix/eval/src/compiler/attrs.rs b/tvix/eval/src/compiler/attrs.rs
index f1441cff68..1584d5017a 100644
--- a/tvix/eval/src/compiler/attrs.rs
+++ b/tvix/eval/src/compiler/attrs.rs
@@ -3,7 +3,7 @@
 
 use super::*;
 
-impl Compiler<'_, '_> {
+impl Compiler<'_> {
     pub(super) fn compile_attr(&mut self, slot: LocalIdx, node: ast::Attr) {
         match node {
             ast::Attr::Dynamic(dynamic) => {
diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs
index 4d6728b6d6..160433904d 100644
--- a/tvix/eval/src/compiler/mod.rs
+++ b/tvix/eval/src/compiler/mod.rs
@@ -24,6 +24,7 @@ use smol_str::SmolStr;
 use std::collections::HashMap;
 use std::path::{Path, PathBuf};
 use std::rc::Rc;
+use std::sync::Arc;
 
 use crate::chunk::Chunk;
 use crate::errors::{Error, ErrorKind, EvalResult};
@@ -73,7 +74,7 @@ impl LambdaCtx {
 /// implicitly be resolvable in the global scope.
 type GlobalsMap = HashMap<&'static str, Rc<dyn Fn(&mut Compiler, rnix::ast::Ident)>>;
 
-struct Compiler<'code, 'observer> {
+struct Compiler<'observer> {
     contexts: Vec<LambdaCtx>,
     warnings: Vec<EvalWarning>,
     errors: Vec<Error>,
@@ -90,7 +91,7 @@ struct Compiler<'code, 'observer> {
     /// File reference in the codemap contains all known source code
     /// and is used to track the spans from which instructions where
     /// derived.
-    file: &'code codemap::File,
+    file: Arc<codemap::File>,
 
     /// Carry an observer for the compilation process, which is called
     /// whenever a chunk is emitted.
@@ -99,7 +100,7 @@ struct Compiler<'code, 'observer> {
 
 // Helper functions for emitting code and metadata to the internal
 // structures of the compiler.
-impl Compiler<'_, '_> {
+impl Compiler<'_> {
     fn context(&self) -> &LambdaCtx {
         &self.contexts[self.contexts.len() - 1]
     }
@@ -137,7 +138,7 @@ impl Compiler<'_, '_> {
 }
 
 // Actual code-emitting AST traversal methods.
-impl Compiler<'_, '_> {
+impl Compiler<'_> {
     fn compile(&mut self, slot: LocalIdx, expr: ast::Expr) {
         match expr {
             ast::Expr::Literal(literal) => self.compile_literal(literal),
@@ -1386,7 +1387,7 @@ fn prepare_globals(additional: HashMap<&'static str, Value>) -> GlobalsMap {
 pub fn compile(
     expr: ast::Expr,
     location: Option<PathBuf>,
-    file: &codemap::File,
+    file: Arc<codemap::File>,
     globals: HashMap<&'static str, Value>,
     observer: &mut dyn Observer,
 ) -> EvalResult<CompilationOutput> {
diff --git a/tvix/eval/src/compiler/spans.rs b/tvix/eval/src/compiler/spans.rs
index a972a17edd..6c11961e0e 100644
--- a/tvix/eval/src/compiler/spans.rs
+++ b/tvix/eval/src/compiler/spans.rs
@@ -77,8 +77,8 @@ expr_to_span!(ast::Str);
 expr_to_span!(ast::UnaryOp);
 expr_to_span!(ast::With);
 
-impl Compiler<'_, '_> {
+impl Compiler<'_> {
     pub(super) fn span_for<S: ToSpan>(&self, to_span: &S) -> Span {
-        to_span.span_for(self.file)
+        to_span.span_for(&self.file)
     }
 }
diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs
index 4ed97b329d..6510ef0afb 100644
--- a/tvix/eval/src/eval.rs
+++ b/tvix/eval/src/eval.rs
@@ -45,7 +45,7 @@ pub fn interpret(code: &str, location: Option<PathBuf>) -> EvalResult<Value> {
         crate::compiler::compile(
             root_expr,
             location,
-            &file,
+            file.clone(),
             global_builtins(),
             &mut DisassemblingObserver::new(codemap.clone(), std::io::stderr()),
         )
@@ -53,7 +53,7 @@ pub fn interpret(code: &str, location: Option<PathBuf>) -> EvalResult<Value> {
         crate::compiler::compile(
             root_expr,
             location,
-            &file,
+            file.clone(),
             global_builtins(),
             &mut NoOpObserver::default(),
         )