Skip to content

Commit

Permalink
Simplify CheckOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Aug 28, 2024
1 parent 1236d2c commit b50f1da
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 54 deletions.
54 changes: 9 additions & 45 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,53 +131,15 @@ pub struct CompileOptions {
pub skip_underconstrained_check: bool,
}

/// Similar to CompileOptions but with some extra fields specific for `check_crate`.
#[derive(Clone, Debug, Default)]
pub struct CheckOptions {
pub expression_width: Option<ExpressionWidth>,
pub bounded_codegen: bool,
pub force_compile: bool,
pub show_ssa: bool,
pub emit_ssa: bool,
pub show_brillig: bool,
pub print_acir: bool,
pub benchmark_codegen: bool,
pub deny_warnings: bool,
pub silence_warnings: bool,
pub disable_macros: bool,
pub show_monomorphized: bool,
pub instrument_debug: bool,
pub force_brillig: bool,
pub debug_comptime_in_file: Option<String>,
pub show_artifact_paths: bool,
pub arithmetic_generics: bool,
pub skip_underconstrained_check: bool,
pub compile_options: CompileOptions,
pub error_on_unused_imports: bool,
}

impl CheckOptions {
pub fn from_compile_options(options: &CompileOptions, error_on_unused_imports: bool) -> Self {
Self {
expression_width: options.expression_width,
bounded_codegen: options.bounded_codegen,
force_compile: options.force_compile,
show_ssa: options.show_ssa,
emit_ssa: options.emit_ssa,
show_brillig: options.show_brillig,
print_acir: options.print_acir,
benchmark_codegen: options.benchmark_codegen,
deny_warnings: options.deny_warnings,
silence_warnings: options.silence_warnings,
disable_macros: options.disable_macros,
show_monomorphized: options.show_monomorphized,
instrument_debug: options.instrument_debug,
force_brillig: options.force_brillig,
debug_comptime_in_file: options.debug_comptime_in_file.clone(),
show_artifact_paths: options.show_artifact_paths,
arithmetic_generics: options.arithmetic_generics,
skip_underconstrained_check: options.skip_underconstrained_check,
error_on_unused_imports,
}
pub fn new(compile_options: &CompileOptions, error_on_unused_imports: bool) -> Self {
Self { compile_options: compile_options.clone(), error_on_unused_imports }
}
}

Expand Down Expand Up @@ -328,8 +290,10 @@ pub fn add_dep(
pub fn check_crate(
context: &mut Context,
crate_id: CrateId,
options: &CheckOptions,
check_options: &CheckOptions,
) -> CompilationResult<()> {
let options = &check_options.compile_options;

let macros: &[&dyn MacroProcessor] =
if options.disable_macros { &[] } else { &[&aztec_macros::AztecMacro] };

Expand All @@ -339,7 +303,7 @@ pub fn check_crate(
context,
options.debug_comptime_in_file.as_deref(),
options.arithmetic_generics,
options.error_on_unused_imports,
check_options.error_on_unused_imports,
macros,
);
errors.extend(diagnostics.into_iter().map(|(error, file_id)| {
Expand Down Expand Up @@ -374,7 +338,7 @@ pub fn compile_main(
cached_program: Option<CompiledProgram>,
) -> CompilationResult<CompiledProgram> {
let error_on_unused_imports = true;
let check_options = CheckOptions::from_compile_options(options, error_on_unused_imports);
let check_options = CheckOptions::new(options, error_on_unused_imports);

let (_, mut warnings) = check_crate(context, crate_id, &check_options)?;

Expand Down Expand Up @@ -412,7 +376,7 @@ pub fn compile_contract(
options: &CompileOptions,
) -> CompilationResult<CompiledContract> {
let error_on_unused_imports = true;
let check_options = CheckOptions::from_compile_options(options, error_on_unused_imports);
let check_options = CheckOptions::new(options, error_on_unused_imports);
let (_, warnings) = check_crate(context, crate_id, &check_options)?;

// TODO: We probably want to error if contracts is empty
Expand Down
8 changes: 4 additions & 4 deletions tooling/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ fn check_package(
) -> Result<bool, CompileError> {
let (mut context, crate_id) = prepare_package(file_manager, parsed_files, package);
let error_on_unused_imports = package.error_on_unused_imports();
let check_options =
CheckOptions::from_compile_options(compile_options, error_on_unused_imports);
let check_options = CheckOptions::new(compile_options, error_on_unused_imports);
check_crate_and_report_errors(&mut context, crate_id, &check_options)?;

if package.is_library() || package.is_contract() {
Expand Down Expand Up @@ -153,9 +152,10 @@ fn create_input_toml_template(
pub(crate) fn check_crate_and_report_errors(
context: &mut Context,
crate_id: CrateId,
options: &CheckOptions,
check_options: &CheckOptions,
) -> Result<(), CompileError> {
let result = check_crate(context, crate_id, options);
let options = &check_options.compile_options;
let result = check_crate(context, crate_id, check_options);
report_errors(result, &context.file_manager, options.deny_warnings, options.silence_warnings)
}

Expand Down
3 changes: 1 addition & 2 deletions tooling/nargo_cli/src/cli/export_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ fn compile_exported_functions(
) -> Result<(), CliError> {
let (mut context, crate_id) = prepare_package(file_manager, parsed_files, package);
let error_on_unused_imports = package.error_on_unused_imports();
let check_options =
CheckOptions::from_compile_options(compile_options, error_on_unused_imports);
let check_options = CheckOptions::new(compile_options, error_on_unused_imports);
check_crate_and_report_errors(&mut context, crate_id, &check_options)?;

let exported_functions = context.get_all_exported_functions_in_crate(&crate_id);
Expand Down
5 changes: 2 additions & 3 deletions tooling/nargo_cli/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ fn run_test<S: BlackBoxFunctionSolver<FieldElement> + Default>(

let (mut context, crate_id) = prepare_package(file_manager, parsed_files, package);
let error_on_unused_imports = package.error_on_unused_imports();
let check_options =
CheckOptions::from_compile_options(compile_options, error_on_unused_imports);
let check_options = CheckOptions::new(compile_options, error_on_unused_imports);
check_crate(&mut context, crate_id, &check_options)
.expect("Any errors should have occurred when collecting test functions");

Expand Down Expand Up @@ -214,7 +213,7 @@ fn get_tests_in_package(
) -> Result<Vec<String>, CliError> {
let (mut context, crate_id) = prepare_package(file_manager, parsed_files, package);
let error_on_unused_imports = package.error_on_unused_imports();
let check_options = CheckOptions::from_compile_options(options, error_on_unused_imports);
let check_options = CheckOptions::new(options, error_on_unused_imports);
check_crate_and_report_errors(&mut context, crate_id, &check_options)?;

Ok(context
Expand Down

0 comments on commit b50f1da

Please sign in to comment.