diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index 4bcaaf7a0..527a8f853 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -221,6 +221,16 @@ fn pure_builtins() -> Vec { x.as_str()[(beg as usize)..(end as usize)].into(), )) }), + Builtin::new("tail", 1, |args, vm| { + let xs = args[0].force(vm)?.to_list()?; + + if xs.len() == 0 { + Err(ErrorKind::TailEmptyList) + } else { + let output = xs.into_iter().skip(1).collect::>(); + Ok(Value::List(NixList::construct(output.len(), output))) + } + }), Builtin::new("throw", 1, |mut args, _| { return Err(ErrorKind::Throw( args.pop().unwrap().to_str()?.as_str().to_owned(), diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs index 6331ee64b..10bc9276a 100644 --- a/tvix/eval/src/errors.rs +++ b/tvix/eval/src/errors.rs @@ -31,6 +31,9 @@ pub enum ErrorKind { index: i64, }, + /// Attempted to call `builtins.tail` on an empty list. + TailEmptyList, + TypeError { expected: &'static str, actual: &'static str, @@ -150,6 +153,8 @@ impl Error { format!("list index '{}' is out of bounds", index) } + ErrorKind::TailEmptyList => format!("'tail' called on an empty list"), + ErrorKind::TypeError { expected, actual } => format!( "expected value of type '{}', but found a '{}'", expected, actual @@ -257,6 +262,7 @@ to a missing value in the attribute set(s) included via `with`."#, ErrorKind::NotAnAbsolutePath(_) => "E020", ErrorKind::ParseIntError(_) => "E021", ErrorKind::NegativeLength { .. } => "E022", + ErrorKind::TailEmptyList { .. } => "E023", ErrorKind::NotImplemented(_) => "E999", } } diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tail.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tail.exp new file mode 100644 index 000000000..b9e3aa1ef --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tail.exp @@ -0,0 +1 @@ +[ [ ] [ 2 3 ] ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tail.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tail.nix new file mode 100644 index 000000000..2be9496a9 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-tail.nix @@ -0,0 +1,4 @@ +[ + (builtins.tail [ "foo" ]) + (builtins.tail [ 1 2 3 ]) +]