From 1114871d538767c053d71c67577890dd29f0b490 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Tue, 12 Sep 2023 12:50:35 +0100 Subject: [PATCH 1/3] fix: remove duplicate file extension in stack trace (#2655) --- compiler/noirc_errors/src/reporter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_errors/src/reporter.rs b/compiler/noirc_errors/src/reporter.rs index bb8f169912..d695b2007b 100644 --- a/compiler/noirc_errors/src/reporter.rs +++ b/compiler/noirc_errors/src/reporter.rs @@ -199,7 +199,7 @@ fn stack_trace<'files>( let source = files.source(call_item.file).expect("should get file source"); let (line, column) = location(source.as_ref(), call_item.span.start()); - result += &format!("{}. {}.nr:{}:{}\n", i + 1, path, line, column); + result += &format!("{}. {}:{}:{}\n", i + 1, path, line, column); } result From 7f6fe46f8bc00f24ff8d14b3a517e27b50db4ee5 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Tue, 12 Sep 2023 12:58:59 +0100 Subject: [PATCH 2/3] fix: fix compilation using `aztec` feature flag (#2663) --- .../src/hir/def_map/aztec_library.rs | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/def_map/aztec_library.rs b/compiler/noirc_frontend/src/hir/def_map/aztec_library.rs index efd947a6c0..ef8c1872f1 100644 --- a/compiler/noirc_frontend/src/hir/def_map/aztec_library.rs +++ b/compiler/noirc_frontend/src/hir/def_map/aztec_library.rs @@ -2,12 +2,13 @@ use acvm::FieldElement; use noirc_errors::{CustomDiagnostic, Span}; use crate::graph::CrateId; +use crate::token::SecondaryAttribute; use crate::{ - hir::Context, token::Attribute, BlockExpression, CallExpression, CastExpression, Distinctness, - Expression, ExpressionKind, ForExpression, FunctionReturnType, Ident, ImportStatement, - IndexExpression, LetStatement, Literal, MemberAccessExpression, MethodCallExpression, - NoirFunction, ParsedModule, Path, PathKind, Pattern, Statement, UnresolvedType, - UnresolvedTypeData, Visibility, + hir::Context, BlockExpression, CallExpression, CastExpression, Distinctness, Expression, + ExpressionKind, ForExpression, FunctionReturnType, Ident, ImportStatement, IndexExpression, + LetStatement, Literal, MemberAccessExpression, MethodCallExpression, NoirFunction, + ParsedModule, Path, PathKind, Pattern, Statement, UnresolvedType, UnresolvedTypeData, + Visibility, }; use noirc_errors::FileDiagnostic; @@ -188,17 +189,19 @@ fn check_for_aztec_dependency( fn transform_module(functions: &mut [NoirFunction]) -> bool { let mut has_annotated_functions = false; for func in functions.iter_mut() { - if let Some(Attribute::Custom(custom_attribute)) = func.def.attribute.as_ref() { - match custom_attribute.as_str() { - "aztec(private)" => { - transform_function("Private", func); - has_annotated_functions = true; + for secondary_attribute in func.def.attributes.secondary.clone() { + if let SecondaryAttribute::Custom(custom_attribute) = secondary_attribute { + match custom_attribute.as_str() { + "aztec(private)" => { + transform_function("Private", func); + has_annotated_functions = true; + } + "aztec(public)" => { + transform_function("Public", func); + has_annotated_functions = true; + } + _ => continue, } - "aztec(public)" => { - transform_function("Public", func); - has_annotated_functions = true; - } - _ => continue, } } } From b80cd409a0f3fb8b5faa9d774f5d683499b1fba2 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Tue, 12 Sep 2023 13:32:50 +0100 Subject: [PATCH 3/3] chore: defer reporting of errors until after compilation and optimization is finished (#2659) --- .../nargo_cli/src/cli/codegen_verifier_cmd.rs | 4 +- tooling/nargo_cli/src/cli/compile_cmd.rs | 82 +++++++++++++++---- tooling/nargo_cli/src/cli/execute_cmd.rs | 4 +- tooling/nargo_cli/src/cli/info_cmd.rs | 12 ++- tooling/nargo_cli/src/cli/prove_cmd.rs | 4 +- tooling/nargo_cli/src/cli/verify_cmd.rs | 4 +- 6 files changed, 82 insertions(+), 28 deletions(-) diff --git a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs index 4589c0f7cf..6199bf0761 100644 --- a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs +++ b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use super::NargoConfig; use super::{ - compile_cmd::compile_package, + compile_cmd::compile_bin_package, fs::{create_named_dir, program::read_program_from_file, write_to_file}, }; use crate::backends::Backend; @@ -82,7 +82,7 @@ fn smart_contract_for_package( read_program_from_file(circuit_build_path)? } else { let (program, _) = - compile_package(package, compile_options, np_language, &is_opcode_supported)?; + compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?; PreprocessedProgram { backend: String::from(BACKEND_IDENTIFIER), diff --git a/tooling/nargo_cli/src/cli/compile_cmd.rs b/tooling/nargo_cli/src/cli/compile_cmd.rs index 251af05510..7a06b43937 100644 --- a/tooling/nargo_cli/src/cli/compile_cmd.rs +++ b/tooling/nargo_cli/src/cli/compile_cmd.rs @@ -11,9 +11,7 @@ use nargo::artifacts::program::PreprocessedProgram; use nargo::package::Package; use nargo::prepare_package; use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; -use noirc_driver::{ - compile_main, CompilationResult, CompileOptions, CompiledContract, CompiledProgram, -}; +use noirc_driver::{CompilationResult, CompileOptions, CompiledContract, CompiledProgram}; use noirc_frontend::graph::CrateName; use clap::Args; @@ -68,12 +66,18 @@ pub(crate) fn run( for package in &workspace { // If `contract` package type, we're compiling every function in a 'contract' rather than just 'main'. if package.is_contract() { - let contracts_with_debug_artifacts = compile_contracts( + let (file_manager, compilation_result) = compile_contracts( package, &args.compile_options, np_language, &is_opcode_supported, + ); + let contracts_with_debug_artifacts = report_errors( + compilation_result, + &file_manager, + args.compile_options.deny_warnings, )?; + save_contracts( contracts_with_debug_artifacts, package, @@ -81,8 +85,14 @@ pub(crate) fn run( args.output_debug, ); } else { - let (program, debug_artifact) = - compile_package(package, &args.compile_options, np_language, &is_opcode_supported)?; + let (file_manager, compilation_result) = + compile_program(package, &args.compile_options, np_language, &is_opcode_supported); + + let (program, debug_artifact) = report_errors( + compilation_result, + &file_manager, + args.compile_options.deny_warnings, + )?; save_program(debug_artifact, program, package, &circuit_dir, args.output_debug); } } @@ -90,7 +100,7 @@ pub(crate) fn run( Ok(()) } -pub(crate) fn compile_package( +pub(crate) fn compile_bin_package( package: &Package, compile_options: &CompileOptions, np_language: Language, @@ -100,9 +110,43 @@ pub(crate) fn compile_package( return Err(CompileError::LibraryCrate(package.name.clone()).into()); } + let (file_manager, compilation_result) = + compile_program(package, compile_options, np_language, &is_opcode_supported); + + let (program, debug_artifact) = + report_errors(compilation_result, &file_manager, compile_options.deny_warnings)?; + + Ok((program, debug_artifact)) +} + +pub(crate) fn compile_contract_package( + package: &Package, + compile_options: &CompileOptions, + np_language: Language, + is_opcode_supported: &impl Fn(&Opcode) -> bool, +) -> Result, CliError> { + let (file_manager, compilation_result) = + compile_contracts(package, compile_options, np_language, &is_opcode_supported); + let contracts_with_debug_artifacts = + report_errors(compilation_result, &file_manager, compile_options.deny_warnings)?; + Ok(contracts_with_debug_artifacts) +} + +fn compile_program( + package: &Package, + compile_options: &CompileOptions, + np_language: Language, + is_opcode_supported: &impl Fn(&Opcode) -> bool, +) -> (FileManager, CompilationResult<(CompiledProgram, DebugArtifact)>) { let (mut context, crate_id) = prepare_package(package); - let result = compile_main(&mut context, crate_id, compile_options); - let program = report_errors(result, &context.file_manager, compile_options.deny_warnings)?; + + let (program, warnings) = + match noirc_driver::compile_main(&mut context, crate_id, compile_options) { + Ok(program_and_warnings) => program_and_warnings, + Err(errors) => { + return (context.file_manager, Err(errors)); + } + }; // Apply backend specific optimizations. let optimized_program = @@ -112,22 +156,28 @@ pub(crate) fn compile_package( let debug_artifact = DebugArtifact::new(vec![optimized_program.debug.clone()], &context.file_manager); - Ok((optimized_program, debug_artifact)) + (context.file_manager, Ok(((optimized_program, debug_artifact), warnings))) } -pub(crate) fn compile_contracts( +fn compile_contracts( package: &Package, compile_options: &CompileOptions, np_language: Language, is_opcode_supported: &impl Fn(&Opcode) -> bool, -) -> Result, CliError> { +) -> (FileManager, CompilationResult>) { let (mut context, crate_id) = prepare_package(package); - let result = noirc_driver::compile_contracts(&mut context, crate_id, compile_options); - let contracts = report_errors(result, &context.file_manager, compile_options.deny_warnings)?; + let (contracts, warnings) = + match noirc_driver::compile_contracts(&mut context, crate_id, compile_options) { + Ok(contracts_and_warnings) => contracts_and_warnings, + Err(errors) => { + return (context.file_manager, Err(errors)); + } + }; let optimized_contracts = try_vecmap(contracts, |contract| { nargo::ops::optimize_contract(contract, np_language, &is_opcode_supported) - })?; + }) + .expect("Backend does not support an opcode that is in the IR"); let contracts_with_debug_artifacts = vecmap(optimized_contracts, |contract| { let debug_infos = vecmap(&contract.functions, |func| func.debug.clone()); @@ -136,7 +186,7 @@ pub(crate) fn compile_contracts( (contract, debug_artifact) }); - Ok(contracts_with_debug_artifacts) + (context.file_manager, Ok((contracts_with_debug_artifacts, warnings))) } fn save_program( diff --git a/tooling/nargo_cli/src/cli/execute_cmd.rs b/tooling/nargo_cli/src/cli/execute_cmd.rs index bce1402758..b16f700718 100644 --- a/tooling/nargo_cli/src/cli/execute_cmd.rs +++ b/tooling/nargo_cli/src/cli/execute_cmd.rs @@ -15,7 +15,7 @@ use noirc_driver::{CompileOptions, CompiledProgram}; use noirc_errors::CustomDiagnostic; use noirc_frontend::graph::CrateName; -use super::compile_cmd::compile_package; +use super::compile_cmd::compile_bin_package; use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir}; use super::NargoConfig; use crate::backends::Backend; @@ -88,7 +88,7 @@ fn execute_package( is_opcode_supported: &impl Fn(&Opcode) -> bool, ) -> Result<(Option, WitnessMap), CliError> { let (compiled_program, debug_artifact) = - compile_package(package, compile_options, np_language, &is_opcode_supported)?; + compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?; let CompiledProgram { abi, circuit, .. } = compiled_program; // Parse the initial witness values from Prover.toml diff --git a/tooling/nargo_cli/src/cli/info_cmd.rs b/tooling/nargo_cli/src/cli/info_cmd.rs index b030c21f31..e51a025642 100644 --- a/tooling/nargo_cli/src/cli/info_cmd.rs +++ b/tooling/nargo_cli/src/cli/info_cmd.rs @@ -11,9 +11,12 @@ use prettytable::{row, table, Row}; use serde::Serialize; use crate::backends::Backend; -use crate::{cli::compile_cmd::compile_package, errors::CliError}; +use crate::errors::CliError; -use super::{compile_cmd::compile_contracts, NargoConfig}; +use super::{ + compile_cmd::{compile_bin_package, compile_contract_package}, + NargoConfig, +}; /// Provides detailed information on a circuit /// @@ -172,7 +175,7 @@ fn count_opcodes_and_gates_in_program( is_opcode_supported: &impl Fn(&Opcode) -> bool, ) -> Result { let (compiled_program, _) = - compile_package(package, compile_options, np_language, &is_opcode_supported)?; + compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?; let (language, _) = backend.get_backend_info()?; Ok(ProgramInfo { @@ -190,7 +193,8 @@ fn count_opcodes_and_gates_in_contracts( np_language: Language, is_opcode_supported: &impl Fn(&Opcode) -> bool, ) -> Result, CliError> { - let contracts = compile_contracts(package, compile_options, np_language, &is_opcode_supported)?; + let contracts = + compile_contract_package(package, compile_options, np_language, &is_opcode_supported)?; let (language, _) = backend.get_backend_info()?; try_vecmap(contracts, |(contract, _)| { diff --git a/tooling/nargo_cli/src/cli/prove_cmd.rs b/tooling/nargo_cli/src/cli/prove_cmd.rs index b2a2ff1856..e953732da2 100644 --- a/tooling/nargo_cli/src/cli/prove_cmd.rs +++ b/tooling/nargo_cli/src/cli/prove_cmd.rs @@ -11,7 +11,7 @@ use noirc_abi::input_parser::Format; use noirc_driver::CompileOptions; use noirc_frontend::graph::CrateName; -use super::compile_cmd::compile_package; +use super::compile_cmd::compile_bin_package; use super::fs::{ inputs::{read_inputs_from_file, write_inputs_to_file}, program::read_program_from_file, @@ -102,7 +102,7 @@ pub(crate) fn prove_package( (program, None) } else { let (program, debug_artifact) = - compile_package(package, compile_options, np_language, &is_opcode_supported)?; + compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?; let preprocessed_program = PreprocessedProgram { backend: String::from(BACKEND_IDENTIFIER), abi: program.abi, diff --git a/tooling/nargo_cli/src/cli/verify_cmd.rs b/tooling/nargo_cli/src/cli/verify_cmd.rs index 4b57c70875..9d1a98da4d 100644 --- a/tooling/nargo_cli/src/cli/verify_cmd.rs +++ b/tooling/nargo_cli/src/cli/verify_cmd.rs @@ -1,6 +1,6 @@ use super::NargoConfig; use super::{ - compile_cmd::compile_package, + compile_cmd::compile_bin_package, fs::{inputs::read_inputs_from_file, load_hex_data, program::read_program_from_file}, }; use crate::{backends::Backend, errors::CliError}; @@ -86,7 +86,7 @@ fn verify_package( read_program_from_file(circuit_build_path)? } else { let (program, _) = - compile_package(package, compile_options, np_language, &is_opcode_supported)?; + compile_bin_package(package, compile_options, np_language, &is_opcode_supported)?; PreprocessedProgram { backend: String::from(BACKEND_IDENTIFIER),