From 3c87687798a3cfb6c3cfcc231e6c60511e3341ab Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Tue, 20 Feb 2024 15:29:30 +0700 Subject: refactor(tvix/eval): add SourceCode directly into error types With this change it's no longer necessary to track the SourceCode struct separately from the evaluation for error reporting: It's just stored directly in the errors. This also ends up resolving an issue in compiler::bindings, where we cloned the Arc containing file references way too often. In fact those clones probably compensate for all additional SourceCode clones during error construction now. Change-Id: Ice93bf161e61f8ea3d48103435e20c53e6aa8c3a Reviewed-on: https://cl.tvl.fyi/c/depot/+/10986 Reviewed-by: flokli Tested-by: BuildkiteCI --- tvix/eval/src/vm/mod.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'tvix/eval/src/vm/mod.rs') diff --git a/tvix/eval/src/vm/mod.rs b/tvix/eval/src/vm/mod.rs index 0ca72bdf3072..32f0d0456f14 100644 --- a/tvix/eval/src/vm/mod.rs +++ b/tvix/eval/src/vm/mod.rs @@ -36,7 +36,7 @@ use crate::{ }, vm::generators::GenCo, warnings::{EvalWarning, WarningKind}, - NixString, + NixString, SourceCode, }; use generators::{call_functor, Generator, GeneratorState}; @@ -85,15 +85,18 @@ impl WithSpan for Result { match self { Ok(something) => Ok(something), Err(kind) => { - let mut error = Error::new(kind, top_span.get_span()); + let mut error = Error::new(kind, top_span.get_span(), vm.source.clone()); // Wrap the top-level error in chaining errors for each element // of the frame stack. for frame in vm.frames.iter().rev() { match frame { Frame::CallFrame { span, .. } => { - error = - Error::new(ErrorKind::BytecodeError(Box::new(error)), span.span()); + error = Error::new( + ErrorKind::BytecodeError(Box::new(error)), + span.span(), + vm.source.clone(), + ); } Frame::Generator { name, span, .. } => { error = Error::new( @@ -102,6 +105,7 @@ impl WithSpan for Result { gen_type: name, }, span.span(), + vm.source.clone(), ); } } @@ -275,6 +279,10 @@ struct VM<'o, IO> { // TODO: should probably be based on a file hash pub import_cache: ImportCache, + /// Data structure holding all source code evaluated in this VM, + /// used for pretty error reporting. + source: SourceCode, + /// Parsed Nix search path, which is used to resolve `<...>` /// references. nix_search_path: NixSearchPath, @@ -333,6 +341,7 @@ where nix_search_path: NixSearchPath, io_handle: IO, observer: &'o mut dyn RuntimeObserver, + source: SourceCode, globals: Rc, reasonable_span: LightSpan, ) -> Self { @@ -342,6 +351,7 @@ where observer, globals, reasonable_span, + source, frames: vec![], stack: vec![], with_stack: vec![], @@ -1315,6 +1325,7 @@ pub fn run_lambda( nix_search_path: NixSearchPath, io_handle: IO, observer: &mut dyn RuntimeObserver, + source: SourceCode, globals: Rc, lambda: Rc, strict: bool, @@ -1334,6 +1345,7 @@ where nix_search_path, io_handle, observer, + source, globals, root_span.into(), ); -- cgit 1.4.1