Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Allow count of elements in repeated-element array syntax to be any static Field #2386

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "slice_as_array"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {
let slice = [1, 2, 3];
let array: [Field; 3] = slice.as_array();

assert(array[0] == 1);
assert(array[1] == 2);
assert(array[2] == 3);
}
6 changes: 4 additions & 2 deletions crates/noirc_evaluator/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ impl From<RuntimeError> for FileDiagnostic {
impl RuntimeError {
fn into_diagnostic(self) -> Diagnostic {
match self {
RuntimeError::InternalError(_) => {
This conversation was marked as resolved.
Show resolved Hide resolved
RuntimeError::InternalError(err) => {
dbg!(err);
Diagnostic::simple_error(
"Internal Consistency Evaluators Errors: \n
This is likely a bug. Consider Opening an issue at https://github.com/noir-lang/noir/issues".to_owned(),
Expand All @@ -104,7 +105,8 @@ impl RuntimeError {
}
_ => {
let message = self.to_string();
let location = self.call_stack().back().expect("Expected RuntimeError to have a location");
let location =
self.call_stack().back().expect("Expected RuntimeError to have a location");

Diagnostic::simple_error(message, String::new(), location.span)
}
Expand Down
4 changes: 4 additions & 0 deletions crates/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub(crate) enum Intrinsic {
SlicePopFront,
SliceInsert,
SliceRemove,
SliceAsArray,
StrAsBytes,
ToBits(Endian),
ToRadix(Endian),
Expand All @@ -64,6 +65,7 @@ impl std::fmt::Display for Intrinsic {
Intrinsic::ToRadix(Endian::Big) => write!(f, "to_be_radix"),
Intrinsic::ToRadix(Endian::Little) => write!(f, "to_le_radix"),
Intrinsic::BlackBox(function) => write!(f, "{function}"),
Intrinsic::SliceAsArray => write!(f, "unsafe_slice_as_array"),
}
}
}
Expand All @@ -84,6 +86,7 @@ impl Intrinsic {
| Intrinsic::SlicePopFront
| Intrinsic::SliceInsert
| Intrinsic::SliceRemove
| Intrinsic::SliceAsArray
| Intrinsic::StrAsBytes
| Intrinsic::ToBits(_)
| Intrinsic::ToRadix(_) => false,
Expand All @@ -106,6 +109,7 @@ impl Intrinsic {
"slice_pop_front" => Some(Intrinsic::SlicePopFront),
"slice_insert" => Some(Intrinsic::SliceInsert),
"slice_remove" => Some(Intrinsic::SliceRemove),
"unsafe_slice_as_array" => Some(Intrinsic::SliceAsArray),
"str_as_bytes" => Some(Intrinsic::StrAsBytes),
"to_le_radix" => Some(Intrinsic::ToRadix(Endian::Little)),
"to_be_radix" => Some(Intrinsic::ToRadix(Endian::Big)),
Expand Down
7 changes: 7 additions & 0 deletions crates/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ pub(super) fn simplify_call(
}
Intrinsic::BlackBox(bb_func) => simplify_black_box_func(bb_func, arguments, dfg),
Intrinsic::Sort => simplify_sort(dfg, arguments),
Intrinsic::SliceAsArray => {
if let Some((slice, index)) = dfg.get_array_constant(arguments[1]) {
SimplifyResult::SimplifiedTo(dfg.make_array(slice, index))
} else {
SimplifyResult::None
}
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ impl<'a> FunctionContext<'a> {
let slice_contents = self.codegen_array(elements, typ[1].clone());
Tree::Branch(vec![slice_length.into(), slice_contents])
}
_ => unreachable!("ICE: array literal type must be an array or a slice, but got {}", array.typ),
_ => unreachable!(
"ICE: array literal type must be an array or a slice, but got {}",
array.typ
),
}
}
ast::Literal::Integer(value, typ) => {
Expand Down
8 changes: 8 additions & 0 deletions noir_stdlib/src/array.nr
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ impl<T, N> [T; N] {
ret
}
}

fn repeated<T>(element: T, length: Field) -> [T] {
let mut result = [];
for _ in 0..length {
result = result.push_back(element);
}
result
}
8 changes: 8 additions & 0 deletions noir_stdlib/src/slice.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

impl<T> [T] {
fn as_array<N>(self) -> [T; N] {
assert(self.len() == N);
unsafe_slice_as_array(self)
}

/// Push a new element to the end of the slice, returning a
/// new slice with a length one greater than the
/// original unmodified slice.
Expand Down Expand Up @@ -42,3 +47,6 @@ impl<T> [T] {
self
}
}

#[builtin(unsafe_slice_as_array)]
fn unsafe_slice_as_array<T, N>(_slice: [T]) -> [T; N] {}
This conversation was marked as resolved.
Show resolved Hide resolved