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 <tazjin@tvl.su>
Autosubmit: Adam Joseph <adam@westernsemico.com>
Tested-by: BuildkiteCI
This commit is contained in:
Adam Joseph 2023-09-09 22:02:56 -07:00 committed by clbot
parent 926459ce69
commit 05f42519b5
16 changed files with 320 additions and 247 deletions

View file

@ -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<Value> {
/// 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<NixString, CatchableErrorKind> {
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