From 05f42519b53575ad3235b5e0a0cd7d71f04076a5 Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Sat, 9 Sep 2023 22:02:56 -0700 Subject: fix(tvix/eval): fix b/281 by adding Value::Catchable This commit makes catchable errors a variant of Value. The main downside of this approach is that we lose the ability to use Rust's `?` syntax for propagating catchable errors. Change-Id: Ibe89438d8a70dcec29e016df692b5bf88a5cad13 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9289 Reviewed-by: tazjin Autosubmit: Adam Joseph Tested-by: BuildkiteCI --- tvix/eval/src/vm/generators.rs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) (limited to 'tvix/eval/src/vm/generators.rs') diff --git a/tvix/eval/src/vm/generators.rs b/tvix/eval/src/vm/generators.rs index a195dac24f53..f9c5786d8f8f 100644 --- a/tvix/eval/src/vm/generators.rs +++ b/tvix/eval/src/vm/generators.rs @@ -190,10 +190,6 @@ pub enum VMResponse { /// VM response with a span to use at the current point. Span(LightSpan), - - /// Message returned by the VM when a catchable error is encountered during - /// the evaluation of `builtins.tryEval`. - ForceError, } impl Display for VMResponse { @@ -204,7 +200,6 @@ impl Display for VMResponse { VMResponse::Path(p) => write!(f, "path({})", p.to_string_lossy()), VMResponse::Directory(d) => write!(f, "dir(len = {})", d.len()), VMResponse::Span(_) => write!(f, "span"), - VMResponse::ForceError => write!(f, "force_error"), } } } @@ -539,20 +534,18 @@ pub async fn request_force(co: &GenCo, val: Value) -> Value { } } -/// Force a value, but inform the caller (by returning `None`) if a catchable -/// error occured. -pub(crate) async fn request_try_force(co: &GenCo, val: Value) -> Option { +/// Force a value +pub(crate) async fn request_try_force(co: &GenCo, val: Value) -> Value { if let Value::Thunk(_) = val { match co.yield_(VMRequest::TryForce(val)).await { - VMResponse::Value(value) => Some(value), - VMResponse::ForceError => None, + VMResponse::Value(value) => value, msg => panic!( "Tvix bug: VM responded with incorrect generator message: {}", msg ), } } else { - Some(val) + val } } @@ -592,13 +585,18 @@ where callable } -pub async fn request_string_coerce(co: &GenCo, val: Value, kind: CoercionKind) -> NixString { +pub async fn request_string_coerce( + co: &GenCo, + val: Value, + kind: CoercionKind, +) -> Result { match val { - Value::String(s) => s, + Value::String(s) => Ok(s), _ => match co.yield_(VMRequest::StringCoerce(val, kind)).await { - VMResponse::Value(value) => value + VMResponse::Value(Value::Catchable(c)) => Err(c), + VMResponse::Value(value) => Ok(value .to_str() - .expect("coerce_to_string always returns a string"), + .expect("coerce_to_string always returns a string")), msg => panic!( "Tvix bug: VM responded with incorrect generator message: {}", msg -- cgit 1.4.1