Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add CompilationResult helper type #2639

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/// 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 @@
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 @@
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 All @@ -158,7 +158,7 @@
let compiled_program = compile_no_check(context, options, main)?;

if options.print_acir {
println!("Compiled ACIR for main (unoptimized):");

Check warning on line 161 in crates/noirc_driver/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (unoptimized)
println!("{}", compiled_program.circuit);
}

Expand All @@ -170,8 +170,8 @@
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 All @@ -192,7 +192,7 @@
for compiled_contract in &compiled_contracts {
for contract_function in &compiled_contract.functions {
println!(
"Compiled ACIR for {}::{} (unoptimized):",

Check warning on line 195 in crates/noirc_driver/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (unoptimized)
compiled_contract.name, contract_function.name
);
println!("{}", contract_function.bytecode);
Expand All @@ -218,7 +218,7 @@
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