Skip to content

Commit

Permalink
chore: Add a flag to nargo to silence warnings (noir-lang#3032)
Browse files Browse the repository at this point in the history
Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
2 people authored and Sakapoi committed Oct 19, 2023
1 parent 0a8f7a0 commit 0d28621
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
6 changes: 5 additions & 1 deletion compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ pub struct CompileOptions {
pub print_acir: bool,

/// Treat all warnings as errors
#[arg(long)]
#[arg(long, conflicts_with = "silence_warnings")]
pub deny_warnings: bool,

/// Suppress warnings
#[arg(long, conflicts_with = "deny_warnings")]
pub silence_warnings: bool,
}

/// Helper type used to signify where only warnings are expected in file diagnostics
Expand Down
6 changes: 5 additions & 1 deletion compiler/noirc_errors/src/reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,15 @@ pub fn report_all<'files>(
files: &'files impl Files<'files, FileId = fm::FileId>,
diagnostics: &[FileDiagnostic],
deny_warnings: bool,
silence_warnings: bool,
) -> ReportedErrors {
// Report warnings before any errors
let (mut diagnostics, mut errors): (Vec<_>, _) =
let (warnings, mut errors): (Vec<_>, _) =
diagnostics.iter().partition(|item| item.diagnostic.is_warning());

let mut diagnostics = if silence_warnings { Vec::new() } else { warnings };
diagnostics.append(&mut errors);

let error_count =
diagnostics.iter().map(|error| error.report(files, deny_warnings) as u32).sum();

Expand Down
15 changes: 13 additions & 2 deletions tooling/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ pub(crate) fn run(
fn check_package(package: &Package, compile_options: &CompileOptions) -> Result<(), CompileError> {
let (mut context, crate_id) =
prepare_package(package, Box::new(|path| std::fs::read_to_string(path)));
check_crate_and_report_errors(&mut context, crate_id, compile_options.deny_warnings)?;
check_crate_and_report_errors(
&mut context,
crate_id,
compile_options.deny_warnings,
compile_options.silence_warnings,
)?;

if package.is_library() || package.is_contract() {
// Libraries do not have ABIs while contracts have many, so we cannot generate a `Prover.toml` file.
Expand Down Expand Up @@ -171,7 +176,13 @@ pub(crate) fn check_crate_and_report_errors(
context: &mut Context,
crate_id: CrateId,
deny_warnings: bool,
silence_warnings: bool,
) -> Result<(), CompileError> {
let result = check_crate(context, crate_id, deny_warnings);
super::compile_cmd::report_errors(result, &context.file_manager, deny_warnings)
super::compile_cmd::report_errors(
result,
&context.file_manager,
deny_warnings,
silence_warnings,
)
}
37 changes: 32 additions & 5 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,23 @@ pub(super) fn compile_workspace(
let compiled_programs: Vec<CompiledProgram> = program_results
.into_iter()
.map(|(file_manager, compilation_result)| {
report_errors(compilation_result, &file_manager, compile_options.deny_warnings)
report_errors(
compilation_result,
&file_manager,
compile_options.deny_warnings,
compile_options.silence_warnings,
)
})
.collect::<Result<_, _>>()?;
let compiled_contracts: Vec<CompiledContract> = contract_results
.into_iter()
.map(|(file_manager, compilation_result)| {
report_errors(compilation_result, &file_manager, compile_options.deny_warnings)
report_errors(
compilation_result,
&file_manager,
compile_options.deny_warnings,
compile_options.silence_warnings,
)
})
.collect::<Result<_, _>>()?;

Expand Down Expand Up @@ -164,7 +174,12 @@ pub(crate) fn compile_bin_package(
&is_opcode_supported,
);

let program = report_errors(compilation_result, &file_manager, compile_options.deny_warnings)?;
let program = report_errors(
compilation_result,
&file_manager,
compile_options.deny_warnings,
compile_options.silence_warnings,
)?;

Ok(program)
}
Expand Down Expand Up @@ -323,11 +338,23 @@ pub(crate) fn report_errors<T>(
result: CompilationResult<T>,
file_manager: &FileManager,
deny_warnings: bool,
silence_warnings: bool,
) -> Result<T, CompileError> {
let (t, warnings) = result.map_err(|errors| {
noirc_errors::reporter::report_all(file_manager.as_file_map(), &errors, deny_warnings)
noirc_errors::reporter::report_all(
file_manager.as_file_map(),
&errors,
deny_warnings,
silence_warnings,
)
})?;

noirc_errors::reporter::report_all(file_manager.as_file_map(), &warnings, deny_warnings);
noirc_errors::reporter::report_all(
file_manager.as_file_map(),
&warnings,
deny_warnings,
silence_warnings,
);

Ok(t)
}
7 changes: 6 additions & 1 deletion tooling/nargo_cli/src/cli/fmt_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ pub(crate) fn run(_args: FormatCommand, config: NargoConfig) -> Result<(), CliEr
})
.collect();

let _ = super::compile_cmd::report_errors::<()>(Err(errors), &file_manager, false);
let _ = super::compile_cmd::report_errors::<()>(
Err(errors),
&file_manager,
false,
false,
);
return Ok(());
}

Expand Down
9 changes: 8 additions & 1 deletion tooling/nargo_cli/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ fn run_tests<S: BlackBoxFunctionSolver>(
) -> Result<(), CliError> {
let (mut context, crate_id) =
prepare_package(package, Box::new(|path| std::fs::read_to_string(path)));
check_crate_and_report_errors(&mut context, crate_id, compile_options.deny_warnings)?;
check_crate_and_report_errors(
&mut context,
crate_id,
compile_options.deny_warnings,
compile_options.silence_warnings,
)?;

let test_functions = context.get_all_test_functions_in_crate_matching(&crate_id, test_name);

Expand Down Expand Up @@ -119,6 +124,7 @@ fn run_tests<S: BlackBoxFunctionSolver>(
context.file_manager.as_file_map(),
&[diag],
compile_options.deny_warnings,
compile_options.silence_warnings,
);
}
failing += 1;
Expand All @@ -128,6 +134,7 @@ fn run_tests<S: BlackBoxFunctionSolver>(
context.file_manager.as_file_map(),
&[err],
compile_options.deny_warnings,
compile_options.silence_warnings,
);
failing += 1;
}
Expand Down

0 comments on commit 0d28621

Please sign in to comment.