diff --git a/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs b/tooling/nargo_cli/src/cli/codegen_verifier_cmd.rs index 4589c0f7cfb..6199bf0761d 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 251af055107..7a06b43937a 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 a1451a348ea..a08cfb09995 100644 --- a/tooling/nargo_cli/src/cli/execute_cmd.rs +++ b/tooling/nargo_cli/src/cli/execute_cmd.rs @@ -14,7 +14,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; @@ -57,7 +57,7 @@ pub(crate) fn run( let (np_language, is_opcode_supported) = backend.get_backend_info()?; for package in &workspace { let (compiled_program, debug_artifact) = - compile_package(package, &args.compile_options, np_language, &is_opcode_supported)?; + compile_bin_package(package, &args.compile_options, np_language, &is_opcode_supported)?; let (return_value, solved_witness) = execute_program_and_decode( compiled_program, diff --git a/tooling/nargo_cli/src/cli/info_cmd.rs b/tooling/nargo_cli/src/cli/info_cmd.rs index b030c21f312..e51a0256426 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 79558961371..c451b78add5 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 4b57c70875f..9d1a98da4da 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),