Skip to content

Commit

Permalink
chore: add CompilationResult helper type (#2639)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Sep 11, 2023
1 parent 788dfb4 commit 3847e9c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
2 changes: 1 addition & 1 deletion crates/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions crates/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T>`
/// structure that is commonly used as a return result in this file.
pub(crate) fn report_errors<T>(
result: Result<(T, Warnings), ErrorsAndWarnings>,
result: CompilationResult<T>,
file_manager: &FileManager,
deny_warnings: bool,
) -> Result<T, CompileError> {
Expand Down
22 changes: 11 additions & 11 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ pub type Warnings = Vec<FileDiagnostic>;
/// Helper type used to signify where errors or warnings are expected in file diagnostics
pub type ErrorsAndWarnings = Vec<FileDiagnostic>;

/// Helper type for connecting a compilation artifact to the errors or warnings which were produced during compilation.
pub type CompilationResult<T> = 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<CompiledProgram> {
let crate_id = prepare_crate(context, root_file);
compile_main(context, crate_id, &CompileOptions::default())
}
Expand Down Expand Up @@ -107,14 +107,14 @@ pub fn check_crate(
context: &mut Context,
crate_id: CrateId,
deny_warnings: bool,
) -> Result<Warnings, ErrorsAndWarnings> {
) -> 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))
}
}

Expand All @@ -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<CompiledProgram> {
let (_, warnings) = check_crate(context, crate_id, options.deny_warnings)?;

let main = match context.get_main_function(&crate_id) {
Some(m) => m,
Expand Down Expand Up @@ -170,8 +170,8 @@ pub fn compile_contracts(
context: &mut Context,
crate_id: CrateId,
options: &CompileOptions,
) -> Result<(Vec<CompiledContract>, Warnings), ErrorsAndWarnings> {
let warnings = check_crate(context, crate_id, options.deny_warnings)?;
) -> CompilationResult<Vec<CompiledContract>> {
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);
Expand Down Expand Up @@ -218,7 +218,7 @@ fn compile_contract(
context: &Context,
contract: Contract,
options: &CompileOptions,
) -> Result<CompiledContract, Vec<FileDiagnostic>> {
) -> Result<CompiledContract, ErrorsAndWarnings> {
let mut functions = Vec::new();
let mut errors = Vec::new();
for function_id in &contract.functions {
Expand Down

0 comments on commit 3847e9c

Please sign in to comment.