From ba138712e4c4a067e438a62ad20d54091f5f4446 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 18 Mar 2023 00:10:29 +0300 Subject: feat(tvix/eval): add Evaluation::strict to toggle top-level deepseq This makes it possible for callers to control whether they can receive partially evaluated values from an evaluation or not. We're actually flipping the default behaviour to non-strict top-level evaluation, which means that callers have to set `strict = true` on the Evaluation to get the previous behaviour. Change-Id: Ic048e9ba09c88866d4c3177d5fa07db11c4eb20e Reviewed-on: https://cl.tvl.fyi/c/depot/+/8325 Autosubmit: tazjin Tested-by: BuildkiteCI Reviewed-by: sterni --- tvix/eval/src/lib.rs | 16 +++++++++++++++- tvix/eval/src/tests/mod.rs | 2 ++ tvix/eval/src/vm/mod.rs | 9 ++++++--- tvix/eval/tests/nix_oracle.rs | 1 + tvix/serde/src/de.rs | 3 ++- 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/tvix/eval/src/lib.rs b/tvix/eval/src/lib.rs index c6bef65c72..deb48b5007 100644 --- a/tvix/eval/src/lib.rs +++ b/tvix/eval/src/lib.rs @@ -104,6 +104,11 @@ pub struct Evaluation<'code, 'co, 'ro> { /// able to read the files specified as arguments to `import`. pub enable_import: bool, + /// Determines whether the returned value should be strictly + /// evaluated, that is whether its list and attribute set elements + /// should be forced recursively. + pub strict: bool, + /// (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, @@ -161,6 +166,7 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> { src_builtins: vec![], io_handle: Box::new(DummyIO {}), enable_import: false, + strict: false, nix_path: None, compiler_observer: None, runtime_observer: None, @@ -256,7 +262,15 @@ impl<'code, 'co, 'ro> Evaluation<'code, 'co, 'ro> { .unwrap_or_default(); let runtime_observer = self.runtime_observer.take().unwrap_or(&mut noop_observer); - let vm_result = run_lambda(nix_path, self.io_handle, runtime_observer, globals, lambda); + + let vm_result = run_lambda( + nix_path, + self.io_handle, + runtime_observer, + globals, + lambda, + self.strict, + ); match vm_result { Ok(mut runtime_result) => { diff --git a/tvix/eval/src/tests/mod.rs b/tvix/eval/src/tests/mod.rs index b998600cda..f800baf050 100644 --- a/tvix/eval/src/tests/mod.rs +++ b/tvix/eval/src/tests/mod.rs @@ -52,6 +52,7 @@ fn eval_test(code_path: &str, expect_success: bool) { } let mut eval = crate::Evaluation::new_impure(&code, Some(code_path.into())); + eval.strict = true; eval.builtins.extend(mock_builtins::builtins()); let result = eval.evaluate(); @@ -100,6 +101,7 @@ fn identity(code_path: &str) { let code = std::fs::read_to_string(code_path).expect("should be able to read test code"); let mut eval = crate::Evaluation::new(&code, None); + eval.strict = true; eval.io_handle = Box::new(crate::StdIO); let result = eval.evaluate(); diff --git a/tvix/eval/src/vm/mod.rs b/tvix/eval/src/vm/mod.rs index 0fb1f9cd8c..13158619c6 100644 --- a/tvix/eval/src/vm/mod.rs +++ b/tvix/eval/src/vm/mod.rs @@ -1190,6 +1190,7 @@ pub fn run_lambda( observer: &mut dyn RuntimeObserver, globals: Rc, lambda: Rc, + strict: bool, ) -> EvalResult { // Retain the top-level span of the expression in this lambda, as // synthetic "calls" in deep_force will otherwise not have a span @@ -1207,9 +1208,11 @@ pub fn run_lambda( root_span.into(), ); - // Synthesise a frame that will instruct the VM to deep-force the final - // value before returning it. - vm.enqueue_generator("final_deep_force", root_span.into(), final_deep_force); + // When evaluating strictly, synthesise a frame that will instruct + // the VM to deep-force the final value before returning it. + if strict { + vm.enqueue_generator("final_deep_force", root_span.into(), final_deep_force); + } vm.frames.push(Frame::CallFrame { span: root_span.into(), diff --git a/tvix/eval/tests/nix_oracle.rs b/tvix/eval/tests/nix_oracle.rs index e241a26f8f..b3ef08f872 100644 --- a/tvix/eval/tests/nix_oracle.rs +++ b/tvix/eval/tests/nix_oracle.rs @@ -41,6 +41,7 @@ fn nix_eval(expr: &str) -> String { fn compare_eval(expr: &str) { let nix_result = nix_eval(expr); let mut eval = tvix_eval::Evaluation::new(expr, None); + eval.strict = true; eval.io_handle = Box::new(tvix_eval::StdIO); let tvix_result = eval diff --git a/tvix/serde/src/de.rs b/tvix/serde/src/de.rs index 0285c386ca..43efc71c6f 100644 --- a/tvix/serde/src/de.rs +++ b/tvix/serde/src/de.rs @@ -33,7 +33,8 @@ where T: serde::Deserialize<'code>, { // First step is to evaluate the Nix code ... - let eval = tvix_eval::Evaluation::new(src, None); + let mut eval = tvix_eval::Evaluation::new(src, None); + eval.strict = true; let source = eval.source_map(); let result = eval.evaluate(); -- cgit 1.4.1