From 6d3d2379233109d3d8b122b7dfe80dd4f4557d2e Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 9 Dec 2022 13:16:01 +0300 Subject: refactor(tvix/eval): consume `self` in Evaluation::evaluate This simplifies lifetime management for observers in callers of tvix_eval. Change-Id: I2f47c8d89f22b1c766526e5d1483c0d026b500ae Reviewed-on: https://cl.tvl.fyi/c/depot/+/7546 Autosubmit: tazjin Reviewed-by: grfn Tested-by: BuildkiteCI --- tvix/eval/src/lib.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'tvix/eval') diff --git a/tvix/eval/src/lib.rs b/tvix/eval/src/lib.rs index aef6f5f06b..4a5ae6f3e8 100644 --- a/tvix/eval/src/lib.rs +++ b/tvix/eval/src/lib.rs @@ -85,9 +85,6 @@ pub struct Evaluation<'code, 'co, 'ro> { /// Top-level file reference for this code inside the source map. file: Arc, - /// Root expression of the Nix code after parsing. - expr: Option, - /// (optional) Nix search path, e.g. the value of `NIX_PATH` used /// for resolving items on the search path (such as ``). pub nix_path: Option, @@ -115,6 +112,9 @@ pub struct EvaluationResult { /// Warnings that occured during evaluation. Warnings are not critical, but /// should be addressed either to modernise code or improve performance. pub warnings: Vec, + + /// AST node that was parsed from the code (on success only). + pub expr: Option, } impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> { @@ -136,7 +136,6 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> { location, source_map, file, - expr: None, nix_path: None, compiler_observer: None, runtime_observer: None, @@ -150,7 +149,7 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> { } /// Evaluate the provided source code. - pub fn evaluate(&mut self) -> EvaluationResult { + pub fn evaluate(mut self) -> EvaluationResult { let mut result = EvaluationResult::default(); let parsed = rnix::ast::Root::parse(self.code); let parse_errors = parsed.errors(); @@ -168,7 +167,7 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> { // // The root expression is persisted in self in case the caller wants // access to the parsed expression. - self.expr = parsed.tree().expr(); + result.expr = parsed.tree().expr(); let builtins = crate::compiler::prepare_globals(Box::new(global_builtins(self.source_map()))); @@ -177,7 +176,7 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> { let compiler_observer = self.compiler_observer.take().unwrap_or(&mut noop_observer); let compiler_result = match compiler::compile( - self.expr.as_ref().unwrap(), + result.expr.as_ref().unwrap(), self.location.take(), self.file.clone(), builtins, -- cgit 1.4.1