Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,18 @@ impl<'a> Interpreter<'a> {
});
}

if meta.kind != FunctionKind::Normal {
return self.call_builtin(function, arguments, location);
}

let parameters = meta.parameters.0.clone();
for ((parameter, typ, _), (argument, arg_location)) in parameters.iter().zip(arguments) {
self.define_pattern(parameter, typ, argument, arg_location)?;
}
let result = if meta.kind != FunctionKind::Normal {
self.call_builtin(function, arguments, location)?
} else {
let parameters = meta.parameters.0.clone();
for ((parameter, typ, _), (argument, arg_location)) in parameters.iter().zip(arguments)
{
self.define_pattern(parameter, typ, argument, arg_location)?;
}

let function_body = self.interner.function(&function).as_expr();
let result = self.evaluate(function_body)?;
let function_body = self.interner.function(&function).as_expr();
self.evaluate(function_body)?
};

self.exit_function(previous_state);
Ok(result)
Expand All @@ -102,6 +103,7 @@ impl<'a> Interpreter<'a> {
if let Some(builtin) = func_attrs.builtin() {
match builtin.as_str() {
"array_len" => builtin::array_len(&arguments),
"as_slice" => builtin::as_slice(arguments),
_ => {
let item = format!("Evaluation for builtin function {builtin}");
Err(InterpreterError::Unimplemented { item, location })
Expand Down
15 changes: 14 additions & 1 deletion compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use noirc_errors::Location;

use crate::hir::comptime::{errors::IResult, Value};
use crate::{
hir::comptime::{errors::IResult, Value},
Type,
};

pub(super) fn array_len(arguments: &[(Value, Location)]) -> IResult<Value> {
assert_eq!(arguments.len(), 1, "ICE: `array_len` should only receive a single argument");
Expand All @@ -10,3 +13,13 @@ pub(super) fn array_len(arguments: &[(Value, Location)]) -> IResult<Value> {
_ => unreachable!("ICE: Cannot query length of types other than arrays or slices"),
}
}

pub(super) fn as_slice(mut arguments: Vec<(Value, Location)>) -> IResult<Value> {
assert_eq!(arguments.len(), 1, "ICE: `as_slice` should only receive a single argument");
let (array, _) = arguments.pop().unwrap();
match array {
Value::Array(values, Type::Array(_, typ)) => Ok(Value::Slice(values, Type::Slice(typ))),
// Type checking should prevent this branch being taken.
_ => unreachable!("ICE: Cannot convert types other than arrays into slices"),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "array_to_slice"
type = "bin"
authors = [""]
compiler_version = ">=0.24.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
comptime {
let ws: [Field; 3] = [1; 3];
let ws_as_slice: [Field] = ws.as_slice();

assert_eq(ws[0], ws_as_slice[0]);
}
}