From 10ce6200e02da99db280d70d106fae775ffc4f47 Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 11 Sep 2023 19:09:55 +0100 Subject: [PATCH] chore: add `CompilationResult` helper type --- crates/lsp/src/lib.rs | 2 +- crates/nargo_cli/src/cli/check_cmd.rs | 2 +- crates/nargo_cli/src/cli/compile_cmd.rs | 6 +++--- crates/noirc_driver/src/lib.rs | 22 +++++++++++----------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/crates/lsp/src/lib.rs b/crates/lsp/src/lib.rs index a67b8a93dc8..d08a604b77a 100644 --- a/crates/lsp/src/lib.rs +++ b/crates/lsp/src/lib.rs @@ -411,7 +411,7 @@ fn on_did_save_text_document( let (mut context, crate_id) = prepare_package(package); let file_diagnostics = match check_crate(&mut context, crate_id, false) { - Ok(warnings) => warnings, + Ok(((), warnings)) => warnings, Err(errors_and_warnings) => errors_and_warnings, }; diff --git a/crates/nargo_cli/src/cli/check_cmd.rs b/crates/nargo_cli/src/cli/check_cmd.rs index 37e28d4efa3..2d3a9f6a2ba 100644 --- a/crates/nargo_cli/src/cli/check_cmd.rs +++ b/crates/nargo_cli/src/cli/check_cmd.rs @@ -171,6 +171,6 @@ pub(crate) fn check_crate_and_report_errors( crate_id: CrateId, deny_warnings: bool, ) -> Result<(), CompileError> { - let result = check_crate(context, crate_id, deny_warnings).map(|warnings| ((), warnings)); + let result = check_crate(context, crate_id, deny_warnings); super::compile_cmd::report_errors(result, &context.file_manager, deny_warnings) } diff --git a/crates/nargo_cli/src/cli/compile_cmd.rs b/crates/nargo_cli/src/cli/compile_cmd.rs index 67cf5b49788..a56dbc13517 100644 --- a/crates/nargo_cli/src/cli/compile_cmd.rs +++ b/crates/nargo_cli/src/cli/compile_cmd.rs @@ -12,7 +12,7 @@ use nargo::package::Package; use nargo::prepare_package; use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection}; use noirc_driver::{ - compile_main, CompileOptions, CompiledContract, CompiledProgram, ErrorsAndWarnings, Warnings, + compile_main, CompilationResult, CompileOptions, CompiledContract, CompiledProgram, }; use noirc_errors::debug_info::DebugInfo; use noirc_frontend::graph::CrateName; @@ -203,10 +203,10 @@ fn save_contracts( } } -/// Helper function for reporting any errors in a Result<(T, Warnings), ErrorsAndWarnings> +/// Helper function for reporting any errors in a `CompilationResult` /// structure that is commonly used as a return result in this file. pub(crate) fn report_errors( - result: Result<(T, Warnings), ErrorsAndWarnings>, + result: CompilationResult, file_manager: &FileManager, deny_warnings: bool, ) -> Result { diff --git a/crates/noirc_driver/src/lib.rs b/crates/noirc_driver/src/lib.rs index 080e9d714e8..a608879ce77 100644 --- a/crates/noirc_driver/src/lib.rs +++ b/crates/noirc_driver/src/lib.rs @@ -48,12 +48,12 @@ pub type Warnings = Vec; /// Helper type used to signify where errors or warnings are expected in file diagnostics pub type ErrorsAndWarnings = Vec; +/// Helper type for connecting a compilation artifact to the errors or warnings which were produced during compilation. +pub type CompilationResult = Result<(T, Warnings), ErrorsAndWarnings>; + // This is here for backwards compatibility // with the restricted version which only uses one file -pub fn compile_file( - context: &mut Context, - root_file: &Path, -) -> Result<(CompiledProgram, Warnings), ErrorsAndWarnings> { +pub fn compile_file(context: &mut Context, root_file: &Path) -> CompilationResult { let crate_id = prepare_crate(context, root_file); compile_main(context, crate_id, &CompileOptions::default()) } @@ -107,14 +107,14 @@ pub fn check_crate( context: &mut Context, crate_id: CrateId, deny_warnings: bool, -) -> Result { +) -> CompilationResult<()> { let mut errors = vec![]; CrateDefMap::collect_defs(crate_id, context, &mut errors); if has_errors(&errors, deny_warnings) { Err(errors) } else { - Ok(errors) + Ok(((), errors)) } } @@ -140,8 +140,8 @@ pub fn compile_main( context: &mut Context, crate_id: CrateId, options: &CompileOptions, -) -> Result<(CompiledProgram, Warnings), ErrorsAndWarnings> { - let warnings = check_crate(context, crate_id, options.deny_warnings)?; +) -> CompilationResult { + let (_, warnings) = check_crate(context, crate_id, options.deny_warnings)?; let main = match context.get_main_function(&crate_id) { Some(m) => m, @@ -170,8 +170,8 @@ pub fn compile_contracts( context: &mut Context, crate_id: CrateId, options: &CompileOptions, -) -> Result<(Vec, Warnings), ErrorsAndWarnings> { - let warnings = check_crate(context, crate_id, options.deny_warnings)?; +) -> CompilationResult> { + let (_, warnings) = check_crate(context, crate_id, options.deny_warnings)?; // TODO: We probably want to error if contracts is empty let contracts = context.get_all_contracts(&crate_id); @@ -218,7 +218,7 @@ fn compile_contract( context: &Context, contract: Contract, options: &CompileOptions, -) -> Result> { +) -> Result { let mut functions = Vec::new(); let mut errors = Vec::new(); for function_id in &contract.functions {