From 5eb523e882264ab3491ae2c6ce318e00a3fb3dfd Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 11 Aug 2022 15:40:29 +0300 Subject: fix(tvix/compiler): support identifier literals in select expression With this change, attribute set access is working as intended. Change-Id: Ic5dbbd68aa59156106069289e7375a696909f78b Reviewed-on: https://cl.tvl.fyi/c/depot/+/6159 Tested-by: BuildkiteCI Reviewed-by: sterni Reviewed-by: grfn --- tvix/eval/src/compiler.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'tvix/eval/src/compiler.rs') diff --git a/tvix/eval/src/compiler.rs b/tvix/eval/src/compiler.rs index 3a6c685c7d29..7ab1dce19367 100644 --- a/tvix/eval/src/compiler.rs +++ b/tvix/eval/src/compiler.rs @@ -94,6 +94,24 @@ impl Compiler { } } + /// Compiles nodes the same way that `Self::compile` does, with + /// the exception of identifiers which are added literally to the + /// stack as string values. + /// + /// This is needed for correctly accessing attribute sets. + fn compile_with_literal_ident(&mut self, node: rnix::SyntaxNode) -> EvalResult<()> { + if node.kind() == rnix::SyntaxKind::NODE_IDENT { + let ident = rnix::types::Ident::cast(node).unwrap(); + let idx = self + .chunk + .add_constant(Value::String(ident.as_str().to_string().into())); + self.chunk.add_op(OpCode::OpConstant(idx)); + return Ok(()); + } + + self.compile(node) + } + fn compile_literal(&mut self, value: rnix::value::Value) -> EvalResult<()> { match value { rnix::NixValue::Float(f) => { @@ -292,7 +310,7 @@ impl Compiler { // // This order matters because the key needs to be evaluated // first to fail in the correct order on type errors. - self.compile(node.index().unwrap())?; + self.compile_with_literal_ident(node.index().unwrap())?; self.chunk.add_op(OpCode::OpAttrsSelect); Ok(()) -- cgit 1.4.1