diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs index 3648881e50..c473ac7f8a 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs @@ -31,6 +31,7 @@ use self::unquote::UnquoteArgs; use super::errors::{IResult, InterpreterError}; use super::value::Value; +mod builtin; mod unquote; #[allow(unused)] @@ -99,8 +100,13 @@ impl<'a> Interpreter<'a> { .expect("all builtin functions must contain a function attribute which contains the opcode which it links to"); if let Some(builtin) = func_attrs.builtin() { - let item = format!("Evaluation for builtin functions like {builtin}"); - Err(InterpreterError::Unimplemented { item, location }) + match builtin.as_str() { + "array_len" => builtin::array_len(&arguments), + _ => { + let item = format!("Evaluation for builtin function {builtin}"); + Err(InterpreterError::Unimplemented { item, location }) + } + } } else if let Some(foreign) = func_attrs.foreign() { let item = format!("Evaluation for foreign functions like {foreign}"); Err(InterpreterError::Unimplemented { item, location }) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs new file mode 100644 index 0000000000..3e5ad20771 --- /dev/null +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -0,0 +1,12 @@ +use noirc_errors::Location; + +use crate::hir::comptime::{errors::IResult, Value}; + +pub(super) fn array_len(arguments: &[(Value, Location)]) -> IResult { + assert_eq!(arguments.len(), 1, "ICE: `array_len` should only receive a single argument"); + match &arguments[0].0 { + Value::Array(values, _) | Value::Slice(values, _) => Ok(Value::U32(values.len() as u32)), + // Type checking should prevent this branch being taken. + _ => unreachable!("ICE: Cannot query length of types other than arrays or slices"), + } +} diff --git a/test_programs/compile_success_empty/comptime_array_len/Nargo.toml b/test_programs/compile_success_empty/comptime_array_len/Nargo.toml new file mode 100644 index 0000000000..c07deddc6c --- /dev/null +++ b/test_programs/compile_success_empty/comptime_array_len/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "comptime_array_len" +type = "bin" +authors = [""] + +[dependencies] diff --git a/test_programs/compile_success_empty/comptime_array_len/src/main.nr b/test_programs/compile_success_empty/comptime_array_len/src/main.nr new file mode 100644 index 0000000000..c98a3de01d --- /dev/null +++ b/test_programs/compile_success_empty/comptime_array_len/src/main.nr @@ -0,0 +1,6 @@ +fn main() { + comptime + { + assert_eq([1, 2, 3].len(), 3); + } +}