From 03c7923ce7825b1f963c345edf236b62c8ff801c Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:11:30 +0100 Subject: [PATCH 01/11] chore: codegen formatter test cases (#3006) --- tooling/nargo_fmt/build.rs | 68 +++++++++++++++++++ tooling/nargo_fmt/src/lib.rs | 48 ------------- tooling/nargo_fmt/tests/execute.rs | 5 ++ .../{nested-if-else.nr => nested_if_else.nr} | 0 .../{nested-if-else.nr => nested_if_else.nr} | 0 5 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 tooling/nargo_fmt/build.rs create mode 100644 tooling/nargo_fmt/tests/execute.rs rename tooling/nargo_fmt/tests/expected/{nested-if-else.nr => nested_if_else.nr} (100%) rename tooling/nargo_fmt/tests/input/{nested-if-else.nr => nested_if_else.nr} (100%) diff --git a/tooling/nargo_fmt/build.rs b/tooling/nargo_fmt/build.rs new file mode 100644 index 00000000000..0ff39e99129 --- /dev/null +++ b/tooling/nargo_fmt/build.rs @@ -0,0 +1,68 @@ +use std::fs::File; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::{env, fs}; + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + let destination = Path::new(&out_dir).join("execute.rs"); + let mut test_file = File::create(destination).unwrap(); + + // Try to find the directory that Cargo sets when it is running; otherwise fallback to assuming the CWD + // is the root of the repository and append the crate path + let manifest_dir = match std::env::var("CARGO_MANIFEST_DIR") { + Ok(dir) => PathBuf::from(dir), + Err(_) => std::env::current_dir().unwrap().join("crates").join("nargo_cli"), + }; + let test_dir = manifest_dir.join("tests"); + + generate_formatter_tests(&mut test_file, &test_dir); +} + +fn generate_formatter_tests(test_file: &mut File, test_data_dir: &Path) { + let inputs_dir = test_data_dir.join("input"); + let outputs_dir = test_data_dir.join("expected"); + + let test_case_files = + fs::read_dir(inputs_dir).unwrap().flatten().filter(|c| c.path().is_file()); + + for file in test_case_files { + let file_path = file.path(); + let file_name = file_path.file_name().unwrap(); + let test_name = file_path.file_stem().unwrap().to_str().unwrap(); + + if test_name.contains('-') { + panic!( + "Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`" + ); + }; + + let input_source_path = file.path(); + let input_source = std::fs::read_to_string(input_source_path).unwrap(); + + let output_source_path = outputs_dir.join(file_name); + let output_source = std::fs::read_to_string(output_source_path).unwrap(); + + write!( + test_file, + r##" +#[test] +fn format_{test_name}() {{ + let input = r#"{input_source}"#; + let expected_output = r#"{output_source}"#; + + + let (parsed_module, errors) = noirc_frontend::parse_program(&input); + assert!(errors.is_empty()); + + let config = nargo_fmt::Config::default(); + let fmt_text = nargo_fmt::format(&input, parsed_module, &config); + + + assert_eq!(fmt_text, expected_output); +}} + "## + ) + .expect("Could not write templated test file."); + } +} diff --git a/tooling/nargo_fmt/src/lib.rs b/tooling/nargo_fmt/src/lib.rs index 754449aa425..c938a70e7a5 100644 --- a/tooling/nargo_fmt/src/lib.rs +++ b/tooling/nargo_fmt/src/lib.rs @@ -33,51 +33,3 @@ pub fn format(source: &str, parsed_module: ParsedModule, config: &Config) -> Str fmt.visit_module(parsed_module); fmt.finish() } - -#[cfg(test)] -mod tests { - use std::{ffi::OsStr, path::PathBuf}; - - use crate::Config; - - #[test] - fn test() { - let files = std::fs::read_dir("tests/input").unwrap(); - for file in files { - let file = file.unwrap(); - - let config = Config::default(); - - let source_path = file.path(); - let source = std::fs::read_to_string(&source_path).unwrap(); - - let (parsed_module, errors) = noirc_frontend::parse_program(&source); - let fmt_text = crate::format(&source, parsed_module, &config); - - assert!(errors.is_empty()); - - let target_path: PathBuf = source_path - .components() - .map(|component| { - if component.as_os_str() == "input" { - OsStr::new("expected") - } else { - component.as_os_str() - } - }) - .collect(); - - let target = match std::fs::read_to_string(&target_path) { - Ok(t) => t, - Err(err) if err.kind() == std::io::ErrorKind::NotFound => { - std::fs::write(target_path, fmt_text.clone()).unwrap(); - fmt_text.clone() - } - Err(err) => unreachable!("{err}"), - }; - - // FIXME: better diff - assert_eq!(fmt_text, target); - } - } -} diff --git a/tooling/nargo_fmt/tests/execute.rs b/tooling/nargo_fmt/tests/execute.rs new file mode 100644 index 00000000000..d8802fd926b --- /dev/null +++ b/tooling/nargo_fmt/tests/execute.rs @@ -0,0 +1,5 @@ +#[cfg(test)] +mod tests { + // include tests generated by `build.rs` + include!(concat!(env!("OUT_DIR"), "/execute.rs")); +} diff --git a/tooling/nargo_fmt/tests/expected/nested-if-else.nr b/tooling/nargo_fmt/tests/expected/nested_if_else.nr similarity index 100% rename from tooling/nargo_fmt/tests/expected/nested-if-else.nr rename to tooling/nargo_fmt/tests/expected/nested_if_else.nr diff --git a/tooling/nargo_fmt/tests/input/nested-if-else.nr b/tooling/nargo_fmt/tests/input/nested_if_else.nr similarity index 100% rename from tooling/nargo_fmt/tests/input/nested-if-else.nr rename to tooling/nargo_fmt/tests/input/nested_if_else.nr From ae8d0e9013f26b52e8f0bdc9f84866ffec50872d Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 9 Oct 2023 12:26:05 +0100 Subject: [PATCH 02/11] fix: include .nr and .sol files in builds (#3039) --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 4334cb8a3f0..0a9bf65322d 100644 --- a/flake.nix +++ b/flake.nix @@ -81,7 +81,7 @@ # Custom filter with various file extensions that we rely upon to build packages # Currently: `.nr`, `.sol`, `.sh`, `.json`, `.md` filter = path: type: - (builtins.match ".*\.(sh|json|md)$" path != null) || (craneLib.filterCargoSources path type); + (builtins.match ".*\.(nr|sol|sh|json|md)$" path != null) || (craneLib.filterCargoSources path type); }; # TODO(#1198): It'd be nice to include these flags when running `cargo clippy` in a devShell. From 1f1039529f722b8dcef2e375fd2249083e0caa15 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 9 Oct 2023 12:53:00 +0100 Subject: [PATCH 03/11] chore: make paths in `wasm` tests absolute (#3040) --- compiler/wasm/test/node/index.test.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/compiler/wasm/test/node/index.test.ts b/compiler/wasm/test/node/index.test.ts index 78aed535df7..4ec6d83c3c3 100644 --- a/compiler/wasm/test/node/index.test.ts +++ b/compiler/wasm/test/node/index.test.ts @@ -4,19 +4,18 @@ import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { compile } from '@noir-lang/noir_wasm'; -async function getFileContent(path: string): Promise { - return readFileSync(join(__dirname, path)).toString(); -} +const absoluteNoirSourcePath = join(__dirname, noirSourcePath); +const absoluteNargoArtifactPath = join(__dirname, nargoArtifactPath); // eslint-disable-next-line @typescript-eslint/no-explicit-any async function getPrecompiledSource(): Promise { - const compiledData = await getFileContent(nargoArtifactPath); + const compiledData = readFileSync(absoluteNargoArtifactPath).toString(); return JSON.parse(compiledData); } describe('noir wasm compilation', () => { it('matches nargos compilation', async () => { - const wasmCircuit = await compile(noirSourcePath); + const wasmCircuit = await compile(absoluteNoirSourcePath); const cliCircuit = await getPrecompiledSource(); // We don't expect the hashes to match due to how `noir_wasm` handles dependencies From 082a6d02dad67a25692bed15c340a16a848a320e Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:54:41 +0100 Subject: [PATCH 04/11] fix: prevent duplicated assert message transformation (#3038) Co-authored-by: kevaundray --- acvm-repo/acvm/src/compiler/mod.rs | 10 ++++++-- acvm-repo/acvm/src/compiler/optimizers/mod.rs | 13 +++++++--- .../acvm/src/compiler/transformers/mod.rs | 24 +++++++++---------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/acvm-repo/acvm/src/compiler/mod.rs b/acvm-repo/acvm/src/compiler/mod.rs index 5b090e4603f..4abf94a2e78 100644 --- a/acvm-repo/acvm/src/compiler/mod.rs +++ b/acvm-repo/acvm/src/compiler/mod.rs @@ -11,6 +11,7 @@ mod optimizers; mod transformers; pub use optimizers::optimize; +use optimizers::optimize_internal; pub use transformers::transform; use transformers::transform_internal; @@ -73,7 +74,12 @@ pub fn compile( np_language: Language, is_opcode_supported: impl Fn(&Opcode) -> bool, ) -> Result<(Circuit, AcirTransformationMap), CompileError> { - let (acir, AcirTransformationMap { acir_opcode_positions }) = optimize(acir); + let (acir, AcirTransformationMap { acir_opcode_positions }) = optimize_internal(acir); - transform_internal(acir, np_language, is_opcode_supported, acir_opcode_positions) + let (mut acir, transformation_map) = + transform_internal(acir, np_language, is_opcode_supported, acir_opcode_positions)?; + + acir.assert_messages = transform_assert_messages(acir.assert_messages, &transformation_map); + + Ok((acir, transformation_map)) } diff --git a/acvm-repo/acvm/src/compiler/optimizers/mod.rs b/acvm-repo/acvm/src/compiler/optimizers/mod.rs index d04d21039f9..c63cfdf9c82 100644 --- a/acvm-repo/acvm/src/compiler/optimizers/mod.rs +++ b/acvm-repo/acvm/src/compiler/optimizers/mod.rs @@ -13,6 +13,15 @@ use super::{transform_assert_messages, AcirTransformationMap}; /// Applies [`ProofSystemCompiler`][crate::ProofSystemCompiler] independent optimizations to a [`Circuit`]. pub fn optimize(acir: Circuit) -> (Circuit, AcirTransformationMap) { + let (mut acir, transformation_map) = optimize_internal(acir); + + acir.assert_messages = transform_assert_messages(acir.assert_messages, &transformation_map); + + (acir, transformation_map) +} + +/// Applies [`ProofSystemCompiler`][crate::ProofSystemCompiler] independent optimizations to a [`Circuit`]. +pub(super) fn optimize_internal(acir: Circuit) -> (Circuit, AcirTransformationMap) { // General optimizer pass let mut opcodes: Vec = Vec::new(); for opcode in acir.opcodes { @@ -36,12 +45,10 @@ pub fn optimize(acir: Circuit) -> (Circuit, AcirTransformationMap) { // Range optimization pass let range_optimizer = RangeOptimizer::new(acir); - let (mut acir, acir_opcode_positions) = + let (acir, acir_opcode_positions) = range_optimizer.replace_redundant_ranges(acir_opcode_positions); let transformation_map = AcirTransformationMap { acir_opcode_positions }; - acir.assert_messages = transform_assert_messages(acir.assert_messages, &transformation_map); - (acir, transformation_map) } diff --git a/acvm-repo/acvm/src/compiler/transformers/mod.rs b/acvm-repo/acvm/src/compiler/transformers/mod.rs index b909bc54662..3a3a013e8eb 100644 --- a/acvm-repo/acvm/src/compiler/transformers/mod.rs +++ b/acvm-repo/acvm/src/compiler/transformers/mod.rs @@ -27,7 +27,12 @@ pub fn transform( // by applying the modifications done to the circuit opcodes and also to the opcode_positions (delete and insert) let acir_opcode_positions = acir.opcodes.iter().enumerate().map(|(i, _)| i).collect(); - transform_internal(acir, np_language, is_opcode_supported, acir_opcode_positions) + let (mut acir, transformation_map) = + transform_internal(acir, np_language, is_opcode_supported, acir_opcode_positions)?; + + acir.assert_messages = transform_assert_messages(acir.assert_messages, &transformation_map); + + Ok((acir, transformation_map)) } /// Applies [`ProofSystemCompiler`][crate::ProofSystemCompiler] specific optimizations to a [`Circuit`]. @@ -40,14 +45,12 @@ pub(super) fn transform_internal( acir_opcode_positions: Vec, ) -> Result<(Circuit, AcirTransformationMap), CompileError> { // Fallback transformer pass - let (mut acir, acir_opcode_positions) = + let (acir, acir_opcode_positions) = FallbackTransformer::transform(acir, is_opcode_supported, acir_opcode_positions)?; let mut transformer = match &np_language { crate::Language::R1CS => { let transformation_map = AcirTransformationMap { acir_opcode_positions }; - acir.assert_messages = - transform_assert_messages(acir.assert_messages, &transformation_map); let transformer = R1CSTransformer::new(acir); return Ok((transformer.transform(), transformation_map)); } @@ -200,18 +203,15 @@ pub(super) fn transform_internal( let current_witness_index = next_witness_index - 1; - let transformation_map = - AcirTransformationMap { acir_opcode_positions: new_acir_opcode_positions }; - let acir = Circuit { current_witness_index, opcodes: transformed_opcodes, - // The optimizer does not add new public inputs - private_parameters: acir.private_parameters, - public_parameters: acir.public_parameters, - return_values: acir.return_values, - assert_messages: transform_assert_messages(acir.assert_messages, &transformation_map), + // The transformer does not add new public inputs + ..acir }; + let transformation_map = + AcirTransformationMap { acir_opcode_positions: new_acir_opcode_positions }; + Ok((acir, transformation_map)) } From f8990d75b948ce0a6968db659370f7ece7f5db08 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 9 Oct 2023 13:56:07 +0100 Subject: [PATCH 05/11] fix: add `pub` modifier to grumpkin functions (#3036) Co-authored-by: kevaundray --- noir_stdlib/src/grumpkin_scalar.nr | 6 +++--- noir_stdlib/src/grumpkin_scalar_mul.nr | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/noir_stdlib/src/grumpkin_scalar.nr b/noir_stdlib/src/grumpkin_scalar.nr index e3cc2a9a8ed..d05158488f4 100644 --- a/noir_stdlib/src/grumpkin_scalar.nr +++ b/noir_stdlib/src/grumpkin_scalar.nr @@ -4,7 +4,7 @@ struct GrumpkinScalar { } impl GrumpkinScalar { - fn new(low: Field, high: Field) -> Self { + pub fn new(low: Field, high: Field) -> Self { // TODO: check that the low and high value fit within the grumpkin modulus GrumpkinScalar { low, high } } @@ -12,10 +12,10 @@ impl GrumpkinScalar { global GRUMPKIN_SCALAR_SERIALIZED_LEN: Field = 2; -fn deserialize_grumpkin_scalar(fields: [Field; GRUMPKIN_SCALAR_SERIALIZED_LEN]) -> GrumpkinScalar { +pub fn deserialize_grumpkin_scalar(fields: [Field; GRUMPKIN_SCALAR_SERIALIZED_LEN]) -> GrumpkinScalar { GrumpkinScalar { low: fields[0], high: fields[1] } } -fn serialize_grumpkin_scalar(scalar: GrumpkinScalar) -> [Field; GRUMPKIN_SCALAR_SERIALIZED_LEN] { +pub fn serialize_grumpkin_scalar(scalar: GrumpkinScalar) -> [Field; GRUMPKIN_SCALAR_SERIALIZED_LEN] { [scalar.low, scalar.high] } diff --git a/noir_stdlib/src/grumpkin_scalar_mul.nr b/noir_stdlib/src/grumpkin_scalar_mul.nr index 0d73b9dd194..06d30d62332 100644 --- a/noir_stdlib/src/grumpkin_scalar_mul.nr +++ b/noir_stdlib/src/grumpkin_scalar_mul.nr @@ -1,7 +1,7 @@ use crate::grumpkin_scalar::GrumpkinScalar; use crate::scalar_mul::fixed_base_embedded_curve; -fn grumpkin_fixed_base(scalar: GrumpkinScalar) -> [Field; 2] { +pub fn grumpkin_fixed_base(scalar: GrumpkinScalar) -> [Field; 2] { // TODO: this should use both the low and high limbs to do the scalar multiplication fixed_base_embedded_curve(scalar.low, scalar.high) } From a902bdb72a478a75ace353fb7fefa3bc1be777ee Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Mon, 9 Oct 2023 05:57:04 -0700 Subject: [PATCH 06/11] chore: Add a flag to nargo to silence warnings (#3032) Co-authored-by: kevaundray --- compiler/noirc_driver/src/lib.rs | 6 +++- compiler/noirc_errors/src/reporter.rs | 6 +++- tooling/nargo_cli/src/cli/check_cmd.rs | 15 ++++++++-- tooling/nargo_cli/src/cli/compile_cmd.rs | 37 ++++++++++++++++++++---- tooling/nargo_cli/src/cli/fmt_cmd.rs | 7 ++++- tooling/nargo_cli/src/cli/test_cmd.rs | 9 +++++- 6 files changed, 69 insertions(+), 11 deletions(-) diff --git a/compiler/noirc_driver/src/lib.rs b/compiler/noirc_driver/src/lib.rs index d797d498572..44e50f94874 100644 --- a/compiler/noirc_driver/src/lib.rs +++ b/compiler/noirc_driver/src/lib.rs @@ -42,8 +42,12 @@ pub struct CompileOptions { pub print_acir: bool, /// Treat all warnings as errors - #[arg(long)] + #[arg(long, conflicts_with = "silence_warnings")] pub deny_warnings: bool, + + /// Suppress warnings + #[arg(long, conflicts_with = "deny_warnings")] + pub silence_warnings: bool, } /// Helper type used to signify where only warnings are expected in file diagnostics diff --git a/compiler/noirc_errors/src/reporter.rs b/compiler/noirc_errors/src/reporter.rs index ba5ac450f56..cb5abbe2079 100644 --- a/compiler/noirc_errors/src/reporter.rs +++ b/compiler/noirc_errors/src/reporter.rs @@ -115,11 +115,15 @@ pub fn report_all<'files>( files: &'files impl Files<'files, FileId = fm::FileId>, diagnostics: &[FileDiagnostic], deny_warnings: bool, + silence_warnings: bool, ) -> ReportedErrors { // Report warnings before any errors - let (mut diagnostics, mut errors): (Vec<_>, _) = + let (warnings, mut errors): (Vec<_>, _) = diagnostics.iter().partition(|item| item.diagnostic.is_warning()); + + let mut diagnostics = if silence_warnings { Vec::new() } else { warnings }; diagnostics.append(&mut errors); + let error_count = diagnostics.iter().map(|error| error.report(files, deny_warnings) as u32).sum(); diff --git a/tooling/nargo_cli/src/cli/check_cmd.rs b/tooling/nargo_cli/src/cli/check_cmd.rs index 58e0e5d9697..30309f88f32 100644 --- a/tooling/nargo_cli/src/cli/check_cmd.rs +++ b/tooling/nargo_cli/src/cli/check_cmd.rs @@ -51,7 +51,12 @@ pub(crate) fn run( fn check_package(package: &Package, compile_options: &CompileOptions) -> Result<(), CompileError> { let (mut context, crate_id) = prepare_package(package, Box::new(|path| std::fs::read_to_string(path))); - check_crate_and_report_errors(&mut context, crate_id, compile_options.deny_warnings)?; + check_crate_and_report_errors( + &mut context, + crate_id, + compile_options.deny_warnings, + compile_options.silence_warnings, + )?; if package.is_library() || package.is_contract() { // Libraries do not have ABIs while contracts have many, so we cannot generate a `Prover.toml` file. @@ -171,7 +176,13 @@ pub(crate) fn check_crate_and_report_errors( context: &mut Context, crate_id: CrateId, deny_warnings: bool, + silence_warnings: bool, ) -> Result<(), CompileError> { let result = check_crate(context, crate_id, deny_warnings); - super::compile_cmd::report_errors(result, &context.file_manager, deny_warnings) + super::compile_cmd::report_errors( + result, + &context.file_manager, + deny_warnings, + silence_warnings, + ) } diff --git a/tooling/nargo_cli/src/cli/compile_cmd.rs b/tooling/nargo_cli/src/cli/compile_cmd.rs index edbc3e984db..a332d63e062 100644 --- a/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -130,13 +130,23 @@ pub(super) fn compile_workspace( let compiled_programs: Vec = program_results .into_iter() .map(|(file_manager, compilation_result)| { - report_errors(compilation_result, &file_manager, compile_options.deny_warnings) + report_errors( + compilation_result, + &file_manager, + compile_options.deny_warnings, + compile_options.silence_warnings, + ) }) .collect::>()?; let compiled_contracts: Vec = contract_results .into_iter() .map(|(file_manager, compilation_result)| { - report_errors(compilation_result, &file_manager, compile_options.deny_warnings) + report_errors( + compilation_result, + &file_manager, + compile_options.deny_warnings, + compile_options.silence_warnings, + ) }) .collect::>()?; @@ -164,7 +174,12 @@ pub(crate) fn compile_bin_package( &is_opcode_supported, ); - let program = report_errors(compilation_result, &file_manager, compile_options.deny_warnings)?; + let program = report_errors( + compilation_result, + &file_manager, + compile_options.deny_warnings, + compile_options.silence_warnings, + )?; Ok(program) } @@ -323,11 +338,23 @@ pub(crate) fn report_errors( result: CompilationResult, file_manager: &FileManager, deny_warnings: bool, + silence_warnings: bool, ) -> Result { let (t, warnings) = result.map_err(|errors| { - noirc_errors::reporter::report_all(file_manager.as_file_map(), &errors, deny_warnings) + noirc_errors::reporter::report_all( + file_manager.as_file_map(), + &errors, + deny_warnings, + silence_warnings, + ) })?; - noirc_errors::reporter::report_all(file_manager.as_file_map(), &warnings, deny_warnings); + noirc_errors::reporter::report_all( + file_manager.as_file_map(), + &warnings, + deny_warnings, + silence_warnings, + ); + Ok(t) } diff --git a/tooling/nargo_cli/src/cli/fmt_cmd.rs b/tooling/nargo_cli/src/cli/fmt_cmd.rs index bfe0b1948ad..31b45cc092a 100644 --- a/tooling/nargo_cli/src/cli/fmt_cmd.rs +++ b/tooling/nargo_cli/src/cli/fmt_cmd.rs @@ -37,7 +37,12 @@ pub(crate) fn run(_args: FormatCommand, config: NargoConfig) -> Result<(), CliEr }) .collect(); - let _ = super::compile_cmd::report_errors::<()>(Err(errors), &file_manager, false); + let _ = super::compile_cmd::report_errors::<()>( + Err(errors), + &file_manager, + false, + false, + ); return Ok(()); } diff --git a/tooling/nargo_cli/src/cli/test_cmd.rs b/tooling/nargo_cli/src/cli/test_cmd.rs index e281411be8c..4ba2fc35766 100644 --- a/tooling/nargo_cli/src/cli/test_cmd.rs +++ b/tooling/nargo_cli/src/cli/test_cmd.rs @@ -84,7 +84,12 @@ fn run_tests( ) -> Result<(), CliError> { let (mut context, crate_id) = prepare_package(package, Box::new(|path| std::fs::read_to_string(path))); - check_crate_and_report_errors(&mut context, crate_id, compile_options.deny_warnings)?; + check_crate_and_report_errors( + &mut context, + crate_id, + compile_options.deny_warnings, + compile_options.silence_warnings, + )?; let test_functions = context.get_all_test_functions_in_crate_matching(&crate_id, test_name); @@ -119,6 +124,7 @@ fn run_tests( context.file_manager.as_file_map(), &[diag], compile_options.deny_warnings, + compile_options.silence_warnings, ); } failing += 1; @@ -128,6 +134,7 @@ fn run_tests( context.file_manager.as_file_map(), &[err], compile_options.deny_warnings, + compile_options.silence_warnings, ); failing += 1; } From 42bbb38e6f1dc40eb965465fed2579108995a1ec Mon Sep 17 00:00:00 2001 From: Yordan Madzhunkov <52652109+yordanmadzhunkov@users.noreply.github.com> Date: Mon, 9 Oct 2023 17:23:04 +0300 Subject: [PATCH 07/11] chore(tests): Test trait override of MutableReference of struct (#3011) Co-authored-by: Yordan Madzhunkov --- .../trait_override_implementation/src/main.nr | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tooling/nargo_cli/tests/execution_success/trait_override_implementation/src/main.nr b/tooling/nargo_cli/tests/execution_success/trait_override_implementation/src/main.nr index 18f51032bcf..f5f01c79ad6 100644 --- a/tooling/nargo_cli/tests/execution_success/trait_override_implementation/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/trait_override_implementation/src/main.nr @@ -40,6 +40,11 @@ impl F for Bar { fn f3(self) -> Field { 30 } } +impl F for &mut Bar { + fn f1(self) -> Field { 101 } + fn f5(self) -> Field { 505 } +} + fn main(x: Field) { let first = Foo::method2(x); assert(first == 3 * x); @@ -50,4 +55,17 @@ fn main(x: Field) { assert(bar.f3() == 30); assert(bar.f4() == 4); assert(bar.f5() == 50); + + let mut bar_mut = Bar{}; + assert((&mut bar_mut).f1() == 101); + assert((&mut bar_mut).f2() == 2); + assert((&mut bar_mut).f3() == 3); + assert((&mut bar_mut).f4() == 4); + assert((&mut bar_mut).f5() == 505); + + assert(bar_mut.f1() == 10); + assert(bar_mut.f2() == 2); + assert(bar_mut.f3() == 30); + assert(bar_mut.f4() == 4); + assert(bar_mut.f5() == 50); } \ No newline at end of file From bb712fdb658917f637cf38ba6ea6acd562bdfb76 Mon Sep 17 00:00:00 2001 From: Koby Hall <102518238+kobyhallx@users.noreply.github.com> Date: Mon, 9 Oct 2023 18:23:27 +0200 Subject: [PATCH 08/11] chore: publish to npm nightly (#3029) Co-authored-by: kevaundray Co-authored-by: Tom French Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> --- .github/workflows/publish-abi_wasm.yml | 51 ------------------ .github/workflows/publish-acvm-js.yml | 51 ------------------ .github/workflows/publish-es-packages.yml | 29 +++++++++-- .github/workflows/publish-nargo.yml | 13 ++--- .github/workflows/publish-nightly.yml | 26 ++++++++++ .github/workflows/publish-noir-js.yml | 52 ------------------- .github/workflows/publish-noir-wasm.yml | 47 ----------------- .github/workflows/publish-source-resolver.yml | 30 ----------- .github/workflows/release.yml | 3 +- acvm-repo/acvm_js/package.json | 1 + compiler/source-resolver/package.json | 1 + compiler/wasm/package.json | 1 + package.json | 1 + tooling/noir_js/package.json | 1 + .../noir_js_backend_barretenberg/package.json | 1 + tooling/noir_js_types/package.json | 1 + tooling/noirc_abi_wasm/package.json | 1 + 17 files changed, 69 insertions(+), 241 deletions(-) delete mode 100644 .github/workflows/publish-abi_wasm.yml delete mode 100644 .github/workflows/publish-acvm-js.yml create mode 100644 .github/workflows/publish-nightly.yml delete mode 100644 .github/workflows/publish-noir-js.yml delete mode 100644 .github/workflows/publish-noir-wasm.yml delete mode 100644 .github/workflows/publish-source-resolver.yml diff --git a/.github/workflows/publish-abi_wasm.yml b/.github/workflows/publish-abi_wasm.yml deleted file mode 100644 index c84a8f0688b..00000000000 --- a/.github/workflows/publish-abi_wasm.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: Publish ABI Wasm - -on: - workflow_dispatch: - inputs: - noir-ref: - description: The noir reference to checkout - required: true - -jobs: - noirc-abi-wasm-build: - runs-on: ubuntu-latest - env: - CACHED_PATH: /tmp/nix-cache - - steps: - - name: Checkout sources - uses: actions/checkout@v3 - with: - repository: "noir-lang/noir" - ref: ${{ inputs.noir-ref }} - - - name: Setup Node.js - uses: actions/setup-node@v3 - with: - registry-url: "https://registry.npmjs.org" - node-version: 18.15 - - - uses: cachix/install-nix-action@v22 - with: - nix_path: nixpkgs=channel:nixos-23.05 - github_access_token: ${{ secrets.GITHUB_TOKEN }} - - - name: Build noirc_abi_wasm - run: nix build .#noirc_abi_wasm - - - name: Discover Build Output Path - run: echo "BUILD_OUTPUT_PATH=$(readlink -f ./result/noirc_abi_wasm)" >> $GITHUB_ENV - - - name: Copy Build Output to Temporary Directory - run: | - mkdir temp_publish_dir - cp -r ${{ env.BUILD_OUTPUT_PATH }}/* temp_publish_dir/ - touch temp_publish_dir/yarn.lock - - - name: Publish to NPM - working-directory: ./temp_publish_dir - run: yarn npm publish --tag latest - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - diff --git a/.github/workflows/publish-acvm-js.yml b/.github/workflows/publish-acvm-js.yml deleted file mode 100644 index 997cdbd565e..00000000000 --- a/.github/workflows/publish-acvm-js.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: Publish acvm-js - -on: - workflow_dispatch: - inputs: - acvm-ref: - description: The acvm reference to checkout - required: true - -jobs: - publish-acvm-js-package: - runs-on: ubuntu-latest - steps: - - name: Checkout sources - uses: actions/checkout@v4 - with: - ref: ${{ inputs.acvm-ref }} - - - name: Setup Node.js - uses: actions/setup-node@v3 - with: - registry-url: "https://registry.npmjs.org" - node-version: 18.15 - - - uses: cachix/install-nix-action@v22 - with: - nix_path: nixpkgs=channel:nixos-23.05 - github_access_token: ${{ secrets.GITHUB_TOKEN }} - - - uses: cachix/cachix-action@v12 - with: - name: barretenberg - authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}" - - - name: Build acvm-js - run: nix build .#acvm_js - - - name: Discover Build Output Path - run: echo "BUILD_OUTPUT_PATH=$(readlink -f ./result/acvm_js)" >> $GITHUB_ENV - - - name: Copy Build Output to Temporary Directory - run: | - mkdir temp_publish_dir - cp -r ${{ env.BUILD_OUTPUT_PATH }}/* temp_publish_dir/ - touch temp_publish_dir/yarn.lock - - - name: Publish to NPM - working-directory: ./temp_publish_dir - run: yarn npm publish --tag latest - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/publish-es-packages.yml b/.github/workflows/publish-es-packages.yml index 7eec3939f8d..b29740d5502 100644 --- a/.github/workflows/publish-es-packages.yml +++ b/.github/workflows/publish-es-packages.yml @@ -2,13 +2,24 @@ name: Publish Noir ES Packages on: workflow_dispatch: - + inputs: + noir-ref: + description: The noir reference to checkout + required: false + nightly: + description: Indicates this is a nightly release + required: false + type: boolean + default: true + jobs: build-noir_wasm: runs-on: ubuntu-latest steps: - name: Checkout sources uses: actions/checkout@v4 + with: + ref: ${{ inputs.noir-ref || 'master' }} - name: Setup Nix uses: ./.github/actions/nix @@ -26,6 +37,8 @@ jobs: steps: - name: Checkout sources uses: actions/checkout@v4 + with: + ref: ${{ inputs.noir-ref || 'master' }} - name: Setup Nix uses: ./.github/actions/nix @@ -43,6 +56,8 @@ jobs: steps: - name: Checkout sources uses: actions/checkout@v4 + with: + ref: ${{ inputs.noir-ref || 'master' }} - name: Setup Nix uses: ./.github/actions/nix @@ -61,6 +76,8 @@ jobs: steps: - name: Checkout sources uses: actions/checkout@v4 + with: + ref: ${{ inputs.noir-ref || 'master' }} - name: Setup Nix uses: ./.github/actions/nix @@ -75,8 +92,14 @@ jobs: - name: Build ES Packages run: yarn prepare:publish + - name: Prepare nightly version + if: ${{ inputs.nightly }} + run: | + sudo apt-get install jq + yarn nightly:version + - name: Authenticate with npm run: "echo npmAuthToken: ${{ secrets.NPM_TOKEN }} > ~/.yarnrc.yml" - + - name: Publish ES Packages - run: yarn publish:all --access public \ No newline at end of file + run: yarn publish:all --access public ${{ inputs.nightly && ' --tag nightly' }} \ No newline at end of file diff --git a/.github/workflows/publish-nargo.yml b/.github/workflows/publish-nargo.yml index 15ed04f3f71..fa642de8a4f 100644 --- a/.github/workflows/publish-nargo.yml +++ b/.github/workflows/publish-nargo.yml @@ -14,9 +14,6 @@ on: description: Whether to publish the build artifacts type: boolean default: false - schedule: - # Run a nightly release at 2 AM UTC - - cron: "0 2 * * *" merge_group: pull_request: @@ -29,6 +26,7 @@ jobs: runs-on: macos-latest env: CROSS_CONFIG: ${{ github.workspace }}/.github/Cross.toml + NIGHTLY_RELEASE: ${{ inputs.tag == '' }} strategy: matrix: target: [x86_64-apple-darwin, aarch64-apple-darwin] @@ -117,6 +115,7 @@ jobs: runs-on: ubuntu-22.04 env: CROSS_CONFIG: ${{ github.workspace }}/.github/Cross.toml + NIGHTLY_RELEASE: ${{ inputs.tag == '' }} strategy: fail-fast: false matrix: @@ -172,7 +171,7 @@ jobs: - name: Upload binaries to release tag uses: svenstaro/upload-release-action@v2 - if: ${{ inputs.publish || github.event_name == 'schedule' }} + if: ${{ inputs.publish }} with: repo_name: noir-lang/noir repo_token: ${{ secrets.GITHUB_TOKEN }} @@ -183,12 +182,12 @@ jobs: - name: Get formatted date id: date - if: ${{ inputs.tag == '' && inputs.publish || github.event_name == 'schedule' }} + if: ${{ env.NIGHTLY_RELEASE && inputs.publish }} run: echo "date=$(date '+%Y-%m-%d')" >> $GITHUB_OUTPUT - name: Upload binaries to release with date tag uses: svenstaro/upload-release-action@v2 - if: ${{ inputs.tag == '' && inputs.publish || github.event_name == 'schedule' }} + if: ${{ env.NIGHTLY_RELEASE && inputs.publish }} with: repo_name: noir-lang/noir repo_token: ${{ secrets.GITHUB_TOKEN }} @@ -198,3 +197,5 @@ jobs: make_latest: false overwrite: true tag: ${{ format('{0}-{1}', 'nightly', steps.date.outputs.date) }} + + \ No newline at end of file diff --git a/.github/workflows/publish-nightly.yml b/.github/workflows/publish-nightly.yml new file mode 100644 index 00000000000..9e8299bc597 --- /dev/null +++ b/.github/workflows/publish-nightly.yml @@ -0,0 +1,26 @@ +name: Publish Nightly release +on: + workflow_dispatch: + schedule: + # Run a nightly release at 2 AM UTC + - cron: "0 2 * * *" + +jobs: + dispatch-publish-es: + runs-on: ubuntu-latest + steps: + - name: Dispatch to publish-nargo + uses: benc-uk/workflow-dispatch@v1 + with: + workflow: publish-nargo.yml + token: ${{ secrets.NOIR_REPO_TOKEN }} + # Omitting a tag results in a nightly + inputs: "{ \"publish\": true }" + + + - name: Dispatch to publish-es-packages + uses: benc-uk/workflow-dispatch@v1 + with: + workflow: publish-es-packages.yml + token: ${{ secrets.NOIR_REPO_TOKEN }} + inputs: "{ \"noir-ref\": \"${{ env.GITHUB_REF }}\", \"nightly\": true }" diff --git a/.github/workflows/publish-noir-js.yml b/.github/workflows/publish-noir-js.yml deleted file mode 100644 index f10f0181fb6..00000000000 --- a/.github/workflows/publish-noir-js.yml +++ /dev/null @@ -1,52 +0,0 @@ -name: Publish Noir Js - -on: - workflow_dispatch: - -jobs: - release-noir-js: - name: Release and Publish Noir Js - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Install dependencies - run: yarn install --immutable - - - name: Install jq - run: sudo apt-get install jq - - - name: Setup toolchain - uses: dtolnay/rust-toolchain@1.66.0 - - - name: Install wasm-bindgen-cli - uses: taiki-e/install-action@v2 - with: - tool: wasm-bindgen-cli@0.2.86 - - - name: Install wasm-opt - run: | - npm i wasm-opt -g - - - name: Install wasm32-unknown-unknwown target - run: | - rustup target add wasm32-unknown-unknown - - - name: Build noirc_abi - run: yarn workspace @noir-lang/noirc_abi build - - - name: Build acvm_js - run: yarn workspace @noir-lang/acvm_js build - - - name: Build noir_js - run: yarn workspace @noir-lang/noir_js build - - - name: Authenticate with npm - run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc - - - name: Publish to NPM - working-directory: ./tooling/noir_js - run: yarn npm publish --access public - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/publish-noir-wasm.yml b/.github/workflows/publish-noir-wasm.yml deleted file mode 100644 index c434fa5077a..00000000000 --- a/.github/workflows/publish-noir-wasm.yml +++ /dev/null @@ -1,47 +0,0 @@ -name: Publish Noir Wasm - -on: - workflow_dispatch: - -jobs: - publish-noir-wasm: - runs-on: ubuntu-22.04 - - steps: - - name: Checkout - uses: actions/checkout@v3 - - - uses: actions/cache@v3 - with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - target/ - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }} - - - uses: cachix/install-nix-action@v22 - with: - nix_path: nixpkgs=channel:nixos-23.05 - - - name: Build with Nix - run: nix build .#noir_wasm - - - name: Discover Build Output Path - run: echo "BUILD_OUTPUT_PATH=$(readlink -f ./result/noir_wasm)" >> $GITHUB_ENV - - - name: Copy Build Output to Temporary Directory - run: | - mkdir temp_publish_dir - cp -r ${{ env.BUILD_OUTPUT_PATH }}/* temp_publish_dir/ - touch temp_publish_dir/yarn.lock - - - name: Authenticate with npm - run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc - - - name: Publish to NPM - working-directory: ./temp_publish_dir - run: yarn npm publish --tag latest - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/publish-source-resolver.yml b/.github/workflows/publish-source-resolver.yml deleted file mode 100644 index 399e72a7b96..00000000000 --- a/.github/workflows/publish-source-resolver.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Publish Source Resolver - -on: - workflow_dispatch: - -jobs: - release-source-resolver: - name: Publish Source Resolver - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup Node.js - uses: actions/setup-node@v3 - with: - registry-url: "https://registry.npmjs.org" - node-version: 18.15 - - - name: Install dependencies - run: yarn install --immutable - - - name: Build noir-source-resolver - run: yarn workspace @noir-lang/source-resolver build - - - name: Publish to NPM - working-directory: ./compiler/source-resolver - run: yarn npm publish - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b579061779f..b1c61e2a5c4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -71,9 +71,10 @@ jobs: if: ${{ needs.release-please.outputs.tag-name }} runs-on: ubuntu-latest steps: - - name: Dispatch to noir_wasm + - name: Dispatch to publish-es-packages uses: benc-uk/workflow-dispatch@v1 with: workflow: publish-es-packages.yml ref: master token: ${{ secrets.NOIR_REPO_TOKEN }} + inputs: '{ "noir-ref": "${{ needs.release-please.outputs.tag-name }}", "nightly": false }' \ No newline at end of file diff --git a/acvm-repo/acvm_js/package.json b/acvm-repo/acvm_js/package.json index f7d2bf08a1a..e7de149ec5c 100644 --- a/acvm-repo/acvm_js/package.json +++ b/acvm-repo/acvm_js/package.json @@ -28,6 +28,7 @@ "test:browser": "web-test-runner", "lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0", "publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish", + "nightly:version": "jq --arg new_version \"-$(git rev-parse --short HEAD)\" '.version = .version + $new_version' package.json > package-tmp.json && mv package-tmp.json package.json", "clean": "chmod u+w web nodejs || true && rm -rf web nodejs" }, "devDependencies": { diff --git a/compiler/source-resolver/package.json b/compiler/source-resolver/package.json index 2e44246beab..7a5f29f5584 100644 --- a/compiler/source-resolver/package.json +++ b/compiler/source-resolver/package.json @@ -24,6 +24,7 @@ "test": "ava", "generate-types": "tsc src/*.ts --declaration --emitDeclarationOnly --outDir types", "clean": "rm -rf ./lib ./lib-node", + "nightly:version": "jq --arg new_version \"-$(git rev-parse --short HEAD)\" '.version = .version + $new_version' package.json > package-tmp.json && mv package-tmp.json package.json", "publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish", "lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0" }, diff --git a/compiler/wasm/package.json b/compiler/wasm/package.json index 446a189302b..00dcb8f046a 100644 --- a/compiler/wasm/package.json +++ b/compiler/wasm/package.json @@ -24,6 +24,7 @@ "test:node": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\"}' mocha", "test:browser": "web-test-runner", "clean": "chmod u+w web nodejs || true && rm -rf ./nodejs ./web ./target ./result", + "nightly:version": "jq --arg new_version \"-$(git rev-parse --short HEAD)\" '.version = .version + $new_version' package.json > package-tmp.json && mv package-tmp.json package.json", "publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish", "lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0" }, diff --git a/package.json b/package.json index 9b7cf4a04b5..42a76f2f549 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "build:noir_js": "yarn workspace @noir-lang/noir_js run build", "build:js:only": "yarn build:types && yarn build:source-resolver && yarn build:backend_barretenberg && yarn build:noir_js", "prepare:publish": "yarn clean && yarn install:from:nix && yarn build:js:only", + "nightly:version": "yarn workspaces foreach run nightly:version", "publish:all": "yarn install && yarn workspaces foreach run publish" }, "devDependencies": { diff --git a/tooling/noir_js/package.json b/tooling/noir_js/package.json index 2bd7570830a..6ad839fb993 100644 --- a/tooling/noir_js/package.json +++ b/tooling/noir_js/package.json @@ -35,6 +35,7 @@ "prettier": "prettier 'src/**/*.ts'", "prettier:fix": "prettier --write 'src/**/*.ts' 'test/**/*.ts'", "lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0", + "nightly:version": "jq --arg new_version \"-$(git rev-parse --short HEAD)\" '.version = .version + $new_version' package.json > package-tmp.json && mv package-tmp.json package.json", "publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish", "clean": "rm -rf ./lib" }, diff --git a/tooling/noir_js_backend_barretenberg/package.json b/tooling/noir_js_backend_barretenberg/package.json index 4bac7cb4ccd..9ef1ae143ab 100644 --- a/tooling/noir_js_backend_barretenberg/package.json +++ b/tooling/noir_js_backend_barretenberg/package.json @@ -27,6 +27,7 @@ "clean": "rm -rf ./lib", "prettier": "prettier 'src/**/*.ts'", "prettier:fix": "prettier --write 'src/**/*.ts' 'test/**/*.ts'", + "nightly:version": "jq --arg new_version \"-$(git rev-parse --short HEAD)\" '.version = .version + $new_version' package.json > package-tmp.json && mv package-tmp.json package.json", "publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish", "lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0" }, diff --git a/tooling/noir_js_types/package.json b/tooling/noir_js_types/package.json index d9f64ae9b31..eb913f16ca3 100644 --- a/tooling/noir_js_types/package.json +++ b/tooling/noir_js_types/package.json @@ -17,6 +17,7 @@ "build:esm": "tsc", "build:cjs": "tsc --module CommonJS --outDir lib/cjs", "build": "yarn run build:cjs && yarn run build:esm", + "nightly:version": "jq --arg new_version \"-$(git rev-parse --short HEAD)\" '.version = .version + $new_version' package.json > package-tmp.json && mv package-tmp.json package.json", "publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish", "lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0" }, diff --git a/tooling/noirc_abi_wasm/package.json b/tooling/noirc_abi_wasm/package.json index 584539ddbe4..a4e076dd0b6 100644 --- a/tooling/noirc_abi_wasm/package.json +++ b/tooling/noirc_abi_wasm/package.json @@ -26,6 +26,7 @@ "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha", "test:browser": "web-test-runner", "clean": "chmod u+w web nodejs || true && rm -rf ./nodejs ./web ./target ./result", + "nightly:version": "jq --arg new_version \"-$(git rev-parse --short HEAD)\" '.version = .version + $new_version' package.json > package-tmp.json && mv package-tmp.json package.json", "publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish", "lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0" }, From b71b15d3305e20d013433f9499717a62fa0c3fbd Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Mon, 9 Oct 2023 09:36:57 -0700 Subject: [PATCH 09/11] chore: parse fieldless structures (#3021) Co-authored-by: kevaundray --- compiler/noirc_frontend/src/parser/parser.rs | 13 ++++++++----- .../tests/execution_success/struct/src/main.nr | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/compiler/noirc_frontend/src/parser/parser.rs b/compiler/noirc_frontend/src/parser/parser.rs index 7216dd9a924..702f056adcd 100644 --- a/compiler/noirc_frontend/src/parser/parser.rs +++ b/compiler/noirc_frontend/src/parser/parser.rs @@ -228,14 +228,15 @@ fn struct_definition() -> impl NoirParser { use self::Keyword::Struct; use Token::*; - let fields = struct_fields().delimited_by(just(LeftBrace), just(RightBrace)).recover_with( - nested_delimiters( + let fields = struct_fields() + .delimited_by(just(LeftBrace), just(RightBrace)) + .recover_with(nested_delimiters( LeftBrace, RightBrace, [(LeftParen, RightParen), (LeftBracket, RightBracket)], |_| vec![], - ), - ); + )) + .or(just(Semicolon).map(|_| Vec::new())); attributes() .or_not() @@ -2281,6 +2282,7 @@ mod test { #[test] fn parse_structs() { let cases = vec![ + "struct Foo;", "struct Foo { }", "struct Bar { ident: Field, }", "struct Baz { ident: Field, other: Field }", @@ -2315,12 +2317,13 @@ mod test { #[test] fn parse_constructor() { let cases = vec![ + "Baz", "Bar { ident: 32 }", "Baz { other: 2 + 42, ident: foo() + 1 }", "Baz { other, ident: foo() + 1, foo }", ]; - parse_all(expression(), cases); + parse_all(expression(), cases); parse_with(expression(), "Foo { a + b }").unwrap_err(); } diff --git a/tooling/nargo_cli/tests/execution_success/struct/src/main.nr b/tooling/nargo_cli/tests/execution_success/struct/src/main.nr index 5e3530e8364..2bd5a1aec28 100644 --- a/tooling/nargo_cli/tests/execution_success/struct/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/struct/src/main.nr @@ -52,7 +52,11 @@ fn get_dog() -> Animal { dog } +struct Unit; + fn main(x: Field, y: Field) { + let unit = Unit {}; + let first = Foo::default(x,y); let p = Pair { first, second: 1 }; From 7e689768f4af1188e01a1a300a0d2fa152cea504 Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:40:10 +0200 Subject: [PATCH 10/11] fix: disable modulo for fields (#3009) --- compiler/noirc_frontend/src/ast/expression.rs | 4 ++++ .../noirc_frontend/src/hir/type_check/errors.rs | 5 ++++- .../noirc_frontend/src/hir/type_check/expr.rs | 15 +++++++++++---- compiler/noirc_frontend/src/hir_def/expr.rs | 4 ++++ .../tests/compile_failure/field_modulo/Nargo.toml | 7 +++++++ .../compile_failure/field_modulo/src/main.nr | 4 ++++ .../arithmetic_binary_operations/src/main.nr | 3 +-- 7 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 tooling/nargo_cli/tests/compile_failure/field_modulo/Nargo.toml create mode 100644 tooling/nargo_cli/tests/compile_failure/field_modulo/src/main.nr diff --git a/compiler/noirc_frontend/src/ast/expression.rs b/compiler/noirc_frontend/src/ast/expression.rs index 213bdd3fefb..d1d71112de0 100644 --- a/compiler/noirc_frontend/src/ast/expression.rs +++ b/compiler/noirc_frontend/src/ast/expression.rs @@ -273,6 +273,10 @@ impl BinaryOpKind { pub fn is_bit_shift(&self) -> bool { matches!(self, BinaryOpKind::ShiftRight | BinaryOpKind::ShiftLeft) } + + pub fn is_modulo(&self) -> bool { + matches!(self, BinaryOpKind::Modulo) + } } #[derive(PartialEq, PartialOrd, Eq, Ord, Hash, Debug, Copy, Clone)] diff --git a/compiler/noirc_frontend/src/hir/type_check/errors.rs b/compiler/noirc_frontend/src/hir/type_check/errors.rs index 41e0e8e0079..b2b360dc81e 100644 --- a/compiler/noirc_frontend/src/hir/type_check/errors.rs +++ b/compiler/noirc_frontend/src/hir/type_check/errors.rs @@ -78,6 +78,8 @@ pub enum TypeCheckError { IntegerTypeMismatch { typ: Type, span: Span }, #[error("Cannot use an integer and a Field in a binary operation, try converting the Field into an integer first")] IntegerAndFieldBinaryOperation { span: Span }, + #[error("Cannot do modulo on Fields, try casting to an integer first")] + FieldModulo { span: Span }, #[error("Fields cannot be compared, try casting to an integer first")] FieldComparison { span: Span }, #[error("The number of bits to use for this bitwise operation is ambiguous. Either the operand's type or return type should be specified")] @@ -195,7 +197,8 @@ impl From for Diagnostic { | TypeCheckError::FieldComparison { span, .. } | TypeCheckError::AmbiguousBitWidth { span, .. } | TypeCheckError::IntegerAndFieldBinaryOperation { span } - | TypeCheckError::OverflowingAssignment { span, .. } => { + | TypeCheckError::OverflowingAssignment { span, .. } + | TypeCheckError::FieldModulo { span } => { Diagnostic::simple_error(error.to_string(), String::new(), span) } TypeCheckError::PublicReturnType { typ, span } => Diagnostic::simple_error( diff --git a/compiler/noirc_frontend/src/hir/type_check/expr.rs b/compiler/noirc_frontend/src/hir/type_check/expr.rs index 28264970620..f5f26b79f15 100644 --- a/compiler/noirc_frontend/src/hir/type_check/expr.rs +++ b/compiler/noirc_frontend/src/hir/type_check/expr.rs @@ -11,7 +11,7 @@ use crate::{ types::Type, }, node_interner::{DefinitionKind, ExprId, FuncId, TraitMethodId}, - Signedness, TypeBinding, TypeVariableKind, UnaryOp, + BinaryOpKind, Signedness, TypeBinding, TypeVariableKind, UnaryOp, }; use super::{errors::TypeCheckError, TypeChecker}; @@ -974,8 +974,8 @@ impl<'interner> TypeChecker<'interner> { if let TypeBinding::Bound(binding) = &*int.borrow() { return self.infix_operand_type_rules(binding, op, other, span); } - - if op.is_bitwise() && (other.is_bindable() || other.is_field()) { + if (op.is_modulo() || op.is_bitwise()) && (other.is_bindable() || other.is_field()) + { let other = other.follow_bindings(); let kind = op.kind; // This will be an error if these types later resolve to a Field, or stay @@ -983,7 +983,11 @@ impl<'interner> TypeChecker<'interner> { // finishes resolving so we can still allow cases like `let x: u8 = 1 << 2;`. self.push_delayed_type_check(Box::new(move || { if other.is_field() { - Err(TypeCheckError::InvalidBitwiseOperationOnField { span }) + if kind == BinaryOpKind::Modulo { + Err(TypeCheckError::FieldModulo { span }) + } else { + Err(TypeCheckError::InvalidBitwiseOperationOnField { span }) + } } else if other.is_bindable() { Err(TypeCheckError::AmbiguousBitWidth { span }) } else if kind.is_bit_shift() && other.is_signed() { @@ -1054,6 +1058,9 @@ impl<'interner> TypeChecker<'interner> { if op.is_bitwise() { return Err(TypeCheckError::InvalidBitwiseOperationOnField { span }); } + if op.is_modulo() { + return Err(TypeCheckError::FieldModulo { span }); + } Ok(FieldElement) } diff --git a/compiler/noirc_frontend/src/hir_def/expr.rs b/compiler/noirc_frontend/src/hir_def/expr.rs index 15d4b12d30b..157e424bc95 100644 --- a/compiler/noirc_frontend/src/hir_def/expr.rs +++ b/compiler/noirc_frontend/src/hir_def/expr.rs @@ -68,6 +68,10 @@ impl HirBinaryOp { pub fn is_bit_shift(&self) -> bool { self.kind.is_bit_shift() } + + pub fn is_modulo(&self) -> bool { + self.kind.is_modulo() + } } #[derive(Debug, Clone)] diff --git a/tooling/nargo_cli/tests/compile_failure/field_modulo/Nargo.toml b/tooling/nargo_cli/tests/compile_failure/field_modulo/Nargo.toml new file mode 100644 index 00000000000..f6bc2dd70e2 --- /dev/null +++ b/tooling/nargo_cli/tests/compile_failure/field_modulo/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "field_modulo" +type = "bin" +authors = [""] +compiler_version = "0.9.0" + +[dependencies] \ No newline at end of file diff --git a/tooling/nargo_cli/tests/compile_failure/field_modulo/src/main.nr b/tooling/nargo_cli/tests/compile_failure/field_modulo/src/main.nr new file mode 100644 index 00000000000..b27ba0892b3 --- /dev/null +++ b/tooling/nargo_cli/tests/compile_failure/field_modulo/src/main.nr @@ -0,0 +1,4 @@ + +fn main(x: Field) -> pub Field { + x % 2 +} diff --git a/tooling/nargo_cli/tests/execution_success/arithmetic_binary_operations/src/main.nr b/tooling/nargo_cli/tests/execution_success/arithmetic_binary_operations/src/main.nr index 201353393a6..78c91818345 100644 --- a/tooling/nargo_cli/tests/execution_success/arithmetic_binary_operations/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/arithmetic_binary_operations/src/main.nr @@ -4,8 +4,7 @@ // Binary addition, multiplication, division, constant modulo // x = 3, y = 4, z = 5 fn main(x : Field, y : Field, z : Field) -> pub Field { - //constant modulo - assert(x % 2 == 1); + //cast assert(y as u1 == 0); let a = x + x; // 3 + 3 = 6 From 0bc2e4de224f9dae940e9f2e5aa74d62569b6ed9 Mon Sep 17 00:00:00 2001 From: kek kek kek Date: Mon, 9 Oct 2023 10:49:26 -0700 Subject: [PATCH 11/11] chore(formatter): prettier test diff output via `similar-asserts` (#3046) Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> --- Cargo.lock | 144 ++++++++++++++++++++++++++++++++--- tooling/nargo_fmt/Cargo.toml | 3 + tooling/nargo_fmt/build.rs | 2 +- tooling/nargo_fmt/src/lib.rs | 3 +- 4 files changed, 138 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87314665107..407b4771af5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -363,7 +363,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "88903cb14723e4d4003335bb7f8a14f27691649105346a0f0957466c096adfe6" dependencies = [ "anstyle", - "bstr", + "bstr 1.6.0", "doc-comment", "predicates 3.0.3", "predicates-core", @@ -586,6 +586,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "bstr" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba3569f383e8f1598449f1a423e72e99569137b47740b1da11ef19af3d5c3223" +dependencies = [ + "lazy_static", + "memchr", + "regex-automata 0.1.10", +] + [[package]] name = "bstr" version = "1.6.0" @@ -593,7 +604,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6798148dccfbff0fae41c7574d2fa8f1ef3492fba0face179de5d8d447d67b05" dependencies = [ "memchr", - "regex-automata", + "regex-automata 0.3.3", "serde", ] @@ -728,7 +739,7 @@ dependencies = [ "num-traits", "serde", "wasm-bindgen", - "windows-targets", + "windows-targets 0.48.1", ] [[package]] @@ -872,6 +883,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "console" +version = "0.15.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8" +dependencies = [ + "encode_unicode 0.3.6", + "lazy_static", + "libc", + "windows-sys 0.45.0", +] + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -1354,6 +1377,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encode_unicode" version = "1.0.0" @@ -1695,7 +1724,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1391ab1f92ffcc08911957149833e682aa3fe252b9f45f966d2ef972274c97df" dependencies = [ "aho-corasick", - "bstr", + "bstr 1.6.0", "fnv", "log", "regex", @@ -2360,6 +2389,7 @@ dependencies = [ "bytecount", "noirc_frontend", "serde", + "similar-asserts", "thiserror", "toml", ] @@ -2660,7 +2690,7 @@ dependencies = [ "libc", "redox_syscall 0.3.5", "smallvec", - "windows-targets", + "windows-targets 0.48.1", ] [[package]] @@ -2852,7 +2882,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eea25e07510aa6ab6547308ebe3c036016d162b8da920dbb079e3ba8acf3d95a" dependencies = [ "csv", - "encode_unicode", + "encode_unicode 1.0.0", "is-terminal", "lazy_static", "term", @@ -3087,10 +3117,16 @@ checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" dependencies = [ "aho-corasick", "memchr", - "regex-automata", + "regex-automata 0.3.3", "regex-syntax 0.7.4", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" + [[package]] name = "regex-automata" version = "0.3.3" @@ -3673,6 +3709,26 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" +[[package]] +name = "similar" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aeaf503862c419d66959f5d7ca015337d864e9c49485d771b732e2a20453597" +dependencies = [ + "bstr 0.2.17", + "unicode-segmentation", +] + +[[package]] +name = "similar-asserts" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e041bb827d1bfca18f213411d51b665309f1afb37a04a5d1464530e13779fc0f" +dependencies = [ + "console", + "similar", +] + [[package]] name = "siphasher" version = "0.3.11" @@ -4684,7 +4740,7 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" dependencies = [ - "windows-targets", + "windows-targets 0.48.1", ] [[package]] @@ -4700,13 +4756,37 @@ dependencies = [ "windows_x86_64_msvc 0.33.0", ] +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets 0.42.2", +] + [[package]] name = "windows-sys" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.1", +] + +[[package]] +name = "windows-targets" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +dependencies = [ + "windows_aarch64_gnullvm 0.42.2", + "windows_aarch64_msvc 0.42.2", + "windows_i686_gnu 0.42.2", + "windows_i686_msvc 0.42.2", + "windows_x86_64_gnu 0.42.2", + "windows_x86_64_gnullvm 0.42.2", + "windows_x86_64_msvc 0.42.2", ] [[package]] @@ -4715,15 +4795,21 @@ version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" dependencies = [ - "windows_aarch64_gnullvm", + "windows_aarch64_gnullvm 0.48.0", "windows_aarch64_msvc 0.48.0", "windows_i686_gnu 0.48.0", "windows_i686_msvc 0.48.0", "windows_x86_64_gnu 0.48.0", - "windows_x86_64_gnullvm", + "windows_x86_64_gnullvm 0.48.0", "windows_x86_64_msvc 0.48.0", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.48.0" @@ -4736,6 +4822,12 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd761fd3eb9ab8cc1ed81e56e567f02dd82c4c837e48ac3b2181b9ffc5060807" +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + [[package]] name = "windows_aarch64_msvc" version = "0.48.0" @@ -4748,6 +4840,12 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cab0cf703a96bab2dc0c02c0fa748491294bf9b7feb27e1f4f96340f208ada0e" +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + [[package]] name = "windows_i686_gnu" version = "0.48.0" @@ -4760,6 +4858,12 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8cfdbe89cc9ad7ce618ba34abc34bbb6c36d99e96cae2245b7943cd75ee773d0" +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + [[package]] name = "windows_i686_msvc" version = "0.48.0" @@ -4772,12 +4876,24 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4dd9b0c0e9ece7bb22e84d70d01b71c6d6248b81a3c60d11869451b4cb24784" +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + [[package]] name = "windows_x86_64_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.0" @@ -4790,6 +4906,12 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff1e4aa646495048ec7f3ffddc411e1d829c026a2ec62b39da15c1055e406eaa" +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" + [[package]] name = "windows_x86_64_msvc" version = "0.48.0" diff --git a/tooling/nargo_fmt/Cargo.toml b/tooling/nargo_fmt/Cargo.toml index 4b034ec7e3e..921c9893ab5 100644 --- a/tooling/nargo_fmt/Cargo.toml +++ b/tooling/nargo_fmt/Cargo.toml @@ -11,3 +11,6 @@ noirc_frontend.workspace = true serde.workspace = true toml.workspace = true thiserror.workspace = true + +[dev-dependencies] +similar-asserts = "1.5.0" diff --git a/tooling/nargo_fmt/build.rs b/tooling/nargo_fmt/build.rs index 0ff39e99129..f2e23f9b8c1 100644 --- a/tooling/nargo_fmt/build.rs +++ b/tooling/nargo_fmt/build.rs @@ -59,7 +59,7 @@ fn format_{test_name}() {{ let fmt_text = nargo_fmt::format(&input, parsed_module, &config); - assert_eq!(fmt_text, expected_output); + similar_asserts::assert_eq!(fmt_text, expected_output); }} "## ) diff --git a/tooling/nargo_fmt/src/lib.rs b/tooling/nargo_fmt/src/lib.rs index c938a70e7a5..9bc148ae304 100644 --- a/tooling/nargo_fmt/src/lib.rs +++ b/tooling/nargo_fmt/src/lib.rs @@ -1,8 +1,7 @@ #![forbid(unsafe_code)] -#![warn(unused_crate_dependencies, unused_extern_crates)] #![warn(unreachable_pub)] #![warn(clippy::semicolon_if_nothing_returned)] -#![warn(unused_qualifications, clippy::use_self)] +#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))] /// A Rust code formatting utility designed to manage and format untouched fragments of source code, /// including comments, whitespace, and other characters. While the module doesn't directly address comments,