fix(tvix/eval): remove incorrect imports when coercing

The default behavior of string coercion in C++ Nix is to weakly coerce
and import to store if necessary. There is a flag to make it strongly
coerce (coerceMore) and a flag that controls whether path values have
the corresponding file/directory imported into the store before
returning the (store) path as a string (copyToStore). We need to
implement our equivalent to the copyToStore (import_paths) flag for the
benefit of weak coercions that don't import into the store (dirOf,
baseNameOf, readFile, ...) and strong coercions that don't import into
the store (toString).

This makes coerce_to_string as well as CoercionKind weirder and more
versatile, but prevents us from reimplementing parts of the coercion
logic constantly as can be seen in the case of baseNameOf.

Note that it is not possible to test this properly in //tvix/eval tests
due to the lack of an appropriate EvalIO implementation being available.
Tests should be added to //tvix/glue down the line.

Change-Id: I8fb8ab99c7fe08e311d2ba1c36960746bf22f566
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10361
Autosubmit: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
Reviewed-by: Adam Joseph <adam@westernsemico.com>
This commit is contained in:
sterni 2023-12-13 14:52:07 +01:00 committed by clbot
parent a30dd0905a
commit 7165ebc43b
8 changed files with 207 additions and 54 deletions

View file

@ -780,7 +780,14 @@ impl<'o> VM<'o> {
self.push_call_frame(span, frame);
self.enqueue_generator("coerce_to_string", gen_span.clone(), |co| {
value.coerce_to_string(co, CoercionKind::Weak, gen_span)
value.coerce_to_string(
co,
CoercionKind {
strong: false,
import_paths: true,
},
gen_span,
)
});
return Ok(false);
@ -1206,7 +1213,23 @@ async fn add_values(co: GenCo, a: Value, b: Value) -> Result<Value, ErrorKind> {
let result = match (a, b) {
(Value::Path(p), v) => {
let mut path = p.to_string_lossy().into_owned();
match generators::request_string_coerce(&co, v, CoercionKind::Weak).await {
match generators::request_string_coerce(
&co,
v,
CoercionKind {
strong: false,
// Concatenating a Path with something else results in a
// Path, so we don't need to import any paths (paths
// imported by Nix always exist as a string, unless
// converted by the user). In C++ Nix they even may not
// contain any string context, the resulting error of such a
// case can not be replicated by us.
import_paths: false,
},
)
.await
{
Ok(vs) => {
path.push_str(vs.as_str());
crate::value::canon_path(PathBuf::from(path)).into()
@ -1215,14 +1238,38 @@ async fn add_values(co: GenCo, a: Value, b: Value) -> Result<Value, ErrorKind> {
}
}
(Value::String(s1), Value::String(s2)) => Value::String(s1.concat(&s2)),
(Value::String(s1), v) => generators::request_string_coerce(&co, v, CoercionKind::Weak)
.await
.map(|s2| Value::String(s1.concat(&s2)))
.into(),
(Value::String(s1), v) => generators::request_string_coerce(
&co,
v,
CoercionKind {
strong: false,
// Behaves the same as string interpolation
import_paths: true,
},
)
.await
.map(|s2| Value::String(s1.concat(&s2)))
.into(),
(a @ Value::Integer(_), b) | (a @ Value::Float(_), b) => arithmetic_op!(&a, &b, +)?,
(a, b) => {
let r1 = generators::request_string_coerce(&co, a, CoercionKind::Weak).await;
let r2 = generators::request_string_coerce(&co, b, CoercionKind::Weak).await;
let r1 = generators::request_string_coerce(
&co,
a,
CoercionKind {
strong: false,
import_paths: false,
},
)
.await;
let r2 = generators::request_string_coerce(
&co,
b,
CoercionKind {
strong: false,
import_paths: false,
},
)
.await;
match (r1, r2) {
(Ok(s1), Ok(s2)) => Value::String(s1.concat(&s2)),
(Err(c), _) => return Ok(Value::Catchable(c)),