feat(tvix/eval): implement correct toString behavior

Implement C++ Nix's `EvalState::coerceToString` minus some of the Path
/ store handling. This is currently only used for `toString` which does
all possible coercions, but we've already prepared the weaker coercion
variant which is e.g. used for builtins that expect string arguments.

`EvalState::coerceToPath` is still missing for builtins that need a
path, but it'll be easy to build on top of this.

Change-Id: I78d15576b18921791d04b6b1e964b951fdef22c6
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6571
Autosubmit: sterni <sternenseemann@systemli.org>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
This commit is contained in:
sterni 2022-09-13 15:37:19 +02:00
parent 16da548f93
commit da1d71a4e8
6 changed files with 196 additions and 14 deletions

View file

@ -1,3 +1,4 @@
use crate::value::CoercionKind;
use std::fmt::Display;
use codemap::{CodeMap, Span};
@ -61,6 +62,12 @@ pub enum ErrorKind {
/// chained up.
ThunkForce(Box<Error>),
/// Given type can't be coerced to a string in the respective context
NotCoercibleToString {
from: &'static str,
kind: CoercionKind,
},
/// Tvix internal warning for features triggered by users that are
/// not actually implemented yet, and without which eval can not
/// proceed.
@ -158,6 +165,21 @@ to a missing value in the attribute set(s) included via `with`."#,
// delegating to the inner error
ErrorKind::ThunkForce(err) => err.message(codemap),
ErrorKind::NotCoercibleToString { kind, from } => {
let kindly = match kind {
CoercionKind::Strong => "strongly",
CoercionKind::Weak => "weakly",
};
let hint = if *from == "set" {
", missing a `__toString` or `outPath` attribute"
} else {
""
};
format!("cannot ({kindly}) coerce {from} to a string{hint}")
}
ErrorKind::NotImplemented(feature) => {
format!("feature not yet implemented in Tvix: {}", feature)
}
@ -185,6 +207,7 @@ to a missing value in the attribute set(s) included via `with`."#,
ErrorKind::ParseErrors(_) => "E015",
ErrorKind::DuplicateAttrsKey { .. } => "E016",
ErrorKind::ThunkForce(_) => "E017",
ErrorKind::NotCoercibleToString { .. } => "E018",
ErrorKind::NotImplemented(_) => "E999",
}
}