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: small driver refactors #3375

Merged
merged 2 commits into from
Oct 31, 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
47 changes: 18 additions & 29 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![warn(clippy::semicolon_if_nothing_returned)]

use clap::Args;
use debug::filter_relevant_files;
use fm::FileId;
use iter_extended::vecmap;
use noirc_abi::{AbiParameter, AbiType, ContractEvent};
Expand All @@ -23,6 +22,8 @@ mod contract;
mod debug;
mod program;

use debug::filter_relevant_files;

pub use contract::{CompiledContract, ContractFunction, ContractFunctionType};
pub use debug::DebugFile;
pub use program::CompiledProgram;
Expand Down Expand Up @@ -69,13 +70,6 @@ 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) -> CompilationResult<CompiledProgram> {
let crate_id = prepare_crate(context, root_file);
compile_main(context, crate_id, &CompileOptions::default(), None, true)
}

/// Adds the file from the file system at `Path` to the crate graph as a root file
pub fn prepare_crate(context: &mut Context, file_name: &Path) -> CrateId {
let path_to_std_lib_file = Path::new(STD_CRATE_NAME).join("lib.nr");
Expand Down Expand Up @@ -167,17 +161,14 @@ pub fn compile_main(
) -> CompilationResult<CompiledProgram> {
let (_, mut warnings) = check_crate(context, crate_id, options.deny_warnings)?;

let main = match context.get_main_function(&crate_id) {
Some(m) => m,
None => {
// TODO(#2155): This error might be a better to exist in Nargo
let err = CustomDiagnostic::from_message(
"cannot compile crate into a program as it does not contain a `main` function",
)
.in_file(FileId::default());
return Err(vec![err]);
}
};
let main = context.get_main_function(&crate_id).ok_or_else(|| {
// TODO(#2155): This error might be a better to exist in Nargo
let err = CustomDiagnostic::from_message(
"cannot compile crate into a program as it does not contain a `main` function",
)
.in_file(FileId::default());
vec![err]
})?;

let compiled_program = compile_no_check(context, options, main, cached_program, force_compile)
.map_err(FileDiagnostic::from)?;
Expand Down Expand Up @@ -330,11 +321,9 @@ fn compile_contract_inner(
}
}

/// Compile the current crate. Assumes self.check_crate is called beforehand!
/// Compile the current crate using `main_function` as the entrypoint.
///
/// This function also assumes all errors in experimental_create_circuit and create_circuit
/// are not warnings.
#[allow(deprecated)]
/// This function assumes [`check_crate`] is called beforehand.
pub fn compile_no_check(
context: &Context,
options: &CompileOptions,
Expand All @@ -345,15 +334,15 @@ pub fn compile_no_check(
let program = monomorphize(main_function, &context.def_interner);

let hash = fxhash::hash64(&program);
let hashes_match = cached_program.as_ref().map_or(false, |program| program.hash == hash);

// If user has specified that they want to see intermediate steps printed then we should
// force compilation even if the program hasn't changed.
if !(force_compile || options.print_acir || options.show_brillig || options.show_ssa) {
if let Some(cached_program) = cached_program {
if hash == cached_program.hash {
return Ok(cached_program);
}
}
let force_compile =
force_compile || options.print_acir || options.show_brillig || options.show_ssa;

if !force_compile && hashes_match {
return Ok(cached_program.expect("cache must exist for hashes to match"));
}

let (circuit, debug, abi, warnings) =
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_toml/src/semver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ mod tests {
required_compiler_version: "0.2.0".to_string(),
compiler_version_found: "0.1.0".to_string(),
};
assert_eq!(got_err, expected_version_error.into());
assert_eq!(got_err, expected_version_error);
}

#[test]
Expand Down
Loading