Skip to content

Commit

Permalink
feat: Sync from noir (AztecProtocol/aztec-packages#5234)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Álvaro Rodríguez <[email protected]>
  • Loading branch information
AztecBot and sirasistant committed Mar 15, 2024
2 parents 824e6d2 + d44bec3 commit ef01b8e
Show file tree
Hide file tree
Showing 74 changed files with 211 additions and 53 deletions.
65 changes: 60 additions & 5 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ use crate::{
StatementKind,
};
use crate::{
ArrayLiteral, Distinctness, ForRange, FunctionDefinition, FunctionReturnType, Generics,
ItemVisibility, LValue, NoirStruct, NoirTypeAlias, Param, Path, PathKind, Pattern, Shared,
StructType, Type, TypeAlias, TypeVariable, TypeVariableKind, UnaryOp, UnresolvedGenerics,
UnresolvedTraitConstraint, UnresolvedType, UnresolvedTypeData, UnresolvedTypeExpression,
Visibility, ERROR_IDENT,
ArrayLiteral, BinaryOpKind, Distinctness, ForRange, FunctionDefinition, FunctionReturnType,
Generics, ItemVisibility, LValue, NoirStruct, NoirTypeAlias, Param, Path, PathKind, Pattern,
Shared, StructType, Type, TypeAlias, TypeVariable, TypeVariableKind, UnaryOp,
UnresolvedGenerics, UnresolvedTraitConstraint, UnresolvedType, UnresolvedTypeData,
UnresolvedTypeExpression, Visibility, ERROR_IDENT,
};
use fm::FileId;
use iter_extended::vecmap;
Expand Down Expand Up @@ -1943,10 +1943,65 @@ impl<'a> Resolver<'a> {
rhs: ExprId,
span: Span,
) -> Result<u128, Option<ResolverError>> {
// Arbitrary amount of recursive calls to try before giving up
let fuel = 100;
self.try_eval_array_length_id_with_fuel(rhs, span, fuel)
}

fn try_eval_array_length_id_with_fuel(
&self,
rhs: ExprId,
span: Span,
fuel: u32,
) -> Result<u128, Option<ResolverError>> {
if fuel == 0 {
// If we reach here, it is likely from evaluating cyclic globals. We expect an error to
// be issued for them after name resolution so issue no error now.
return Err(None);
}

match self.interner.expression(&rhs) {
HirExpression::Literal(HirLiteral::Integer(int, false)) => {
int.try_into_u128().ok_or(Some(ResolverError::IntegerTooLarge { span }))
}
HirExpression::Ident(ident) => {
let definition = self.interner.definition(ident.id);
match definition.kind {
DefinitionKind::Global(global_id) => {
let let_statement = self.interner.get_global_let_statement(global_id);
if let Some(let_statement) = let_statement {
let expression = let_statement.expression;
self.try_eval_array_length_id_with_fuel(expression, span, fuel - 1)
} else {
Err(Some(ResolverError::InvalidArrayLengthExpr { span }))
}
}
_ => Err(Some(ResolverError::InvalidArrayLengthExpr { span })),
}
}
HirExpression::Infix(infix) => {
let lhs = self.try_eval_array_length_id_with_fuel(infix.lhs, span, fuel - 1)?;
let rhs = self.try_eval_array_length_id_with_fuel(infix.rhs, span, fuel - 1)?;

match infix.operator.kind {
BinaryOpKind::Add => Ok(lhs + rhs),
BinaryOpKind::Subtract => Ok(lhs - rhs),
BinaryOpKind::Multiply => Ok(lhs * rhs),
BinaryOpKind::Divide => Ok(lhs / rhs),
BinaryOpKind::Equal => Ok((lhs == rhs) as u128),
BinaryOpKind::NotEqual => Ok((lhs != rhs) as u128),
BinaryOpKind::Less => Ok((lhs < rhs) as u128),
BinaryOpKind::LessEqual => Ok((lhs <= rhs) as u128),
BinaryOpKind::Greater => Ok((lhs > rhs) as u128),
BinaryOpKind::GreaterEqual => Ok((lhs >= rhs) as u128),
BinaryOpKind::And => Ok(lhs & rhs),
BinaryOpKind::Or => Ok(lhs | rhs),
BinaryOpKind::Xor => Ok(lhs ^ rhs),
BinaryOpKind::ShiftRight => Ok(lhs >> rhs),
BinaryOpKind::ShiftLeft => Ok(lhs << rhs),
BinaryOpKind::Modulo => Ok(lhs % rhs),
}
}
_other => Err(Some(ResolverError::InvalidArrayLengthExpr { span })),
}
}
Expand Down
2 changes: 1 addition & 1 deletion noir_stdlib/src/array.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cmp::{Ord};
use crate::cmp::Ord;

// TODO: Once we fully move to the new SSA pass this module can be removed and replaced
// by the methods in the `slice` module
Expand Down
101 changes: 97 additions & 4 deletions noir_stdlib/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod poseidon2;
mod pedersen;

use crate::default::Default;
use crate::uint128::U128;

#[foreign(sha256)]
// docs:start:sha256
Expand Down Expand Up @@ -120,10 +121,102 @@ where
}
}

// TODO: add implementations for the remainder of primitive types.
impl Hash for Field{
impl Hash for Field {
fn hash<H>(self, state: &mut H) where H: Hasher{
let input: [Field] = [self];
H::write(state, input);
H::write(state, [self]);
}
}

impl Hash for u8 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, [self as Field]);
}
}

impl Hash for u32 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, [self as Field]);
}
}

impl Hash for u64 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, [self as Field]);
}
}

impl Hash for i8 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, [self as Field]);
}
}

impl Hash for i32 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, [self as Field]);
}
}

impl Hash for i64 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, [self as Field]);
}
}

impl Hash for bool {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, [self as Field]);
}
}

impl Hash for () {
fn hash<H>(_self: Self, _state: &mut H) where H: Hasher {}
}

impl Hash for U128 {
fn hash<H>(self, state: &mut H) where H: Hasher{
H::write(state, [self.lo as Field, self.hi as Field]);
}
}

impl<T, N> Hash for [T; N] where T: Hash {
fn hash<H>(self, state: &mut H) where H: Hasher{
for elem in self {
elem.hash(state);
}
}
}

impl<A, B> Hash for (A, B) where A: Hash, B: Hash {
fn hash<H>(self, state: &mut H) where H: Hasher{
self.0.hash(state);
self.1.hash(state);
}
}

impl<A, B, C> Hash for (A, B, C) where A: Hash, B: Hash, C: Hash {
fn hash<H>(self, state: &mut H) where H: Hasher{
self.0.hash(state);
self.1.hash(state);
self.2.hash(state);
}
}

impl<A, B, C, D> Hash for (A, B, C, D) where A: Hash, B: Hash, C: Hash, D: Hash {
fn hash<H>(self, state: &mut H) where H: Hasher{
self.0.hash(state);
self.1.hash(state);
self.2.hash(state);
self.3.hash(state);
}
}

impl<A, B, C, D, E> Hash for (A, B, C, D, E) where A: Hash, B: Hash, C: Hash, D: Hash, E: Hash {
fn hash<H>(self, state: &mut H) where H: Hasher{
self.0.hash(state);
self.1.hash(state);
self.2.hash(state);
self.3.hash(state);
self.4.hash(state);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion test_programs/compile_failure/depend_on_bin/Prover.toml

This file was deleted.

Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
26 changes: 0 additions & 26 deletions test_programs/compile_failure/hashmap_load_factor/Prover.toml

This file was deleted.

Empty file.
2 changes: 0 additions & 2 deletions test_programs/compile_failure/orphaned_trait_impl/Prover.toml

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "nargo_cli"
description = "Noir's package manager"
default-run = "nargo"
version.workspace = true
authors.workspace = true
edition.workspace = true
Expand Down
41 changes: 40 additions & 1 deletion tooling/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn main() {
println!("cargo:rerun-if-changed={}", test_dir.as_os_str().to_str().unwrap());

generate_execution_success_tests(&mut test_file, &test_dir);
generate_execution_failure_tests(&mut test_file, &test_dir);
generate_noir_test_success_tests(&mut test_file, &test_dir);
generate_noir_test_failure_tests(&mut test_file, &test_dir);
generate_compile_success_empty_tests(&mut test_file, &test_dir);
Expand Down Expand Up @@ -86,6 +87,44 @@ fn execution_success_{test_name}() {{
}
}

fn generate_execution_failure_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "execution_failure";
let test_data_dir = test_data_dir.join(test_sub_dir);

let test_case_dirs =
fs::read_dir(test_data_dir).unwrap().flatten().filter(|c| c.path().is_dir());

for test_dir in test_case_dirs {
let test_name =
test_dir.file_name().into_string().expect("Directory can't be converted to string");
if test_name.contains('-') {
panic!(
"Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`"
);
};
let test_dir = &test_dir.path();

write!(
test_file,
r#"
#[test]
fn execution_failure_{test_name}() {{
let test_program_dir = PathBuf::from("{test_dir}");
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.env("NARGO_BACKEND_PATH", path_to_mock_backend());
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("execute").arg("--force");
cmd.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());
}}
"#,
test_dir = test_dir.display(),
)
.expect("Could not write templated test file.");
}
}

fn generate_noir_test_success_tests(test_file: &mut File, test_data_dir: &Path) {
let test_sub_dir = "noir_test_success";
let test_data_dir = test_data_dir.join(test_sub_dir);
Expand Down Expand Up @@ -281,7 +320,7 @@ fn compile_failure_{test_name}() {{
let mut cmd = Command::cargo_bin("nargo").unwrap();
cmd.env("NARGO_BACKEND_PATH", path_to_mock_backend());
cmd.arg("--program-dir").arg(test_program_dir);
cmd.arg("execute").arg("--force");
cmd.arg("compile").arg("--force");
cmd.assert().failure().stderr(predicate::str::contains("The application panicked (crashed).").not());
}}
Expand Down
9 changes: 8 additions & 1 deletion tooling/nargo_fmt/src/rewrite/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ impl UseTree {

let mut iter = self.path.iter().peekable();
while let Some(segment) = iter.next() {
let segment_str = segment.rewrite(visitor, shape);
let mut segment_str = segment.rewrite(visitor, shape);
if segment_str.contains('{')
&& !segment_str.contains(',')
&& !segment_str.contains("::")
{
let empty = "";
segment_str = segment_str.replace(['{', '}'], empty);
}
result.push_str(&segment_str);

if iter.peek().is_some() {
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_fmt/tests/expected/contract.nr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract Benchmarking {
context::Context, note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader},
log::emit_unencrypted_log, state_vars::{Map, PublicMutable, PrivateSet},
types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN},
types::address::{AztecAddress}
types::address::AztecAddress
};

struct Storage {
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/expected/import_braces.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use dep::std::hash::sha256;
2 changes: 1 addition & 1 deletion tooling/nargo_fmt/tests/input/contract.nr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract Benchmarking {
context::Context, note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader},
log::emit_unencrypted_log, state_vars::{Map, PublicMutable, PrivateSet},
types::type_serialization::field_serialization::{FieldSerializationMethods, FIELD_SERIALIZED_LEN},
types::address::{AztecAddress}
types::address::AztecAddress
};

struct Storage {
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/input/import_braces.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use dep::std::hash::{sha256};

0 comments on commit ef01b8e

Please sign in to comment.