From 3a67f912284e22f50bb54a6f6a533b2caf8929f8 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 14 Aug 2022 14:38:19 +0300 Subject: fix(tvix/eval): `?` operator should use OpAttrsOrNotFound Using `OpAttrSelect`, the ? operator will fail when encountering a nested value that is not an attribute set. This however breaks valid code, such as: { bs = 42; } ? bs.a.b The fix is simply to use the same operator used in the `or` statement, which leaves a sentinal on the stack if a field is not found or the value is not an attribute set. Change-Id: Ib28fc8a96e6d592b4cdbc3e65ba129ad8faecd66 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6211 Reviewed-by: grfn Tested-by: BuildkiteCI --- tvix/eval/src/compiler.rs | 2 +- tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.nix | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.nix diff --git a/tvix/eval/src/compiler.rs b/tvix/eval/src/compiler.rs index 6bb75f92c009..4b7a7b44a522 100644 --- a/tvix/eval/src/compiler.rs +++ b/tvix/eval/src/compiler.rs @@ -568,7 +568,7 @@ impl Compiler { self.compile_with_literal_ident(next)?; for fragment in fragments.into_iter().rev() { - self.chunk.push_op(OpCode::OpAttrsSelect); + self.chunk.push_op(OpCode::OpAttrOrNotFound); self.compile_with_literal_ident(fragment)?; } diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.exp new file mode 100644 index 000000000000..c508d5366f70 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.exp @@ -0,0 +1 @@ +false diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.nix new file mode 100644 index 000000000000..361ba9144594 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-contains-nested-non-set.nix @@ -0,0 +1,3 @@ +# ? operator should work even if encountering a non-set value on the +# walk +{ a.b = 42; } ? a.b.c -- cgit 1.4.1