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: automatically inject stdlib when resolving crate #998

Merged
merged 1 commit into from
Mar 18, 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
3 changes: 1 addition & 2 deletions crates/nargo/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
};

use super::fs::write_to_file;
use super::{add_std_lib, NargoConfig};
use super::NargoConfig;
use crate::constants::{PROVER_INPUT_FILE, VERIFIER_INPUT_FILE};

/// Checks the constraint system for errors
Expand All @@ -30,7 +30,6 @@ fn check_from_path<P: AsRef<Path>>(p: P, compile_options: &CompileOptions) -> Re
let backend = crate::backends::ConcreteBackend;

let mut driver = Resolver::resolve_root_config(p.as_ref(), backend.np_language())?;
add_std_lib(&mut driver);

driver.check_crate(compile_options).map_err(|_| CliError::CompilationError)?;

Expand Down
6 changes: 2 additions & 4 deletions crates/nargo/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{constants::TARGET_DIR, errors::CliError, resolver::Resolver};

use super::fs::program::save_program_to_file;
use super::preprocess_cmd::preprocess_with_path;
use super::{add_std_lib, NargoConfig};
use super::NargoConfig;

/// Compile the program and its secret execution trace into ACIR format
#[derive(Debug, Clone, Args)]
Expand Down Expand Up @@ -69,9 +69,7 @@ pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliEr

fn setup_driver(program_dir: &Path) -> Result<Driver, CliError> {
let backend = crate::backends::ConcreteBackend;
let mut driver = Resolver::resolve_root_config(program_dir, backend.np_language())?;
add_std_lib(&mut driver);
Ok(driver)
Resolver::resolve_root_config(program_dir, backend.np_language())
}

fn check_crate(program_dir: &Path, options: &CompileOptions) -> Result<Driver, CliError> {
Expand Down
11 changes: 2 additions & 9 deletions crates/nargo/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use clap::{Args, Parser, Subcommand};
use const_format::formatcp;
use noirc_abi::InputMap;
use noirc_driver::{CompileOptions, Driver};
use noirc_frontend::graph::{CrateName, CrateType};
use noirc_driver::CompileOptions;
use std::path::{Path, PathBuf};

use color_eyre::eyre;
Expand Down Expand Up @@ -101,12 +100,6 @@ pub fn prove_and_verify(proof_name: &str, prg_dir: &Path, show_ssa: bool) -> boo
}
}

fn add_std_lib(driver: &mut Driver) {
let std_crate_name = "std";
let path_to_std_lib_file = PathBuf::from(std_crate_name).join("lib.nr");
let std_crate = driver.create_non_local_crate(path_to_std_lib_file, CrateType::Library);
driver.propagate_dep(std_crate, &CrateName::new(std_crate_name).unwrap());
}
// FIXME: I not sure that this is the right place for this tests.
#[cfg(test)]
mod tests {
Expand All @@ -123,7 +116,7 @@ mod tests {
fn file_compiles<P: AsRef<Path>>(root_file: P) -> bool {
let mut driver = Driver::new(&acvm::Language::R1CS);
driver.create_local_crate(&root_file, CrateType::Binary);
super::add_std_lib(&mut driver);
crate::resolver::add_std_lib(&mut driver);
driver.file_compiles()
}

Expand Down
3 changes: 1 addition & 2 deletions crates/nargo/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

use crate::{errors::CliError, resolver::Resolver};

use super::{add_std_lib, NargoConfig};
use super::NargoConfig;

/// Run the tests for this program
#[derive(Debug, Clone, Args)]
Expand All @@ -34,7 +34,6 @@ fn run_tests(
let backend = crate::backends::ConcreteBackend;

let mut driver = Resolver::resolve_root_config(program_dir, backend.np_language())?;
add_std_lib(&mut driver);

driver.check_crate(compile_options).map_err(|_| CliError::CompilationError)?;

Expand Down
11 changes: 10 additions & 1 deletion crates/nargo/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use acvm::Language;
use noirc_driver::Driver;
use noirc_frontend::graph::{CrateId, CrateType};
use noirc_frontend::graph::{CrateId, CrateName, CrateType};

use crate::{
errors::CliError,
Expand Down Expand Up @@ -64,6 +64,7 @@ impl<'a> Resolver<'a> {
let mut resolver = Resolver::with_driver(&mut driver);
resolver.resolve_config(crate_id, cfg)?;

add_std_lib(&mut driver);
Ok(driver)
}

Expand Down Expand Up @@ -141,3 +142,11 @@ impl<'a> Resolver<'a> {
}
}
}

// This needs to be public to support the tests in `cli/mod.rs`.
pub(crate) fn add_std_lib(driver: &mut Driver) {
let std_crate_name = "std";
let path_to_std_lib_file = PathBuf::from(std_crate_name).join("lib.nr");
let std_crate = driver.create_non_local_crate(path_to_std_lib_file, CrateType::Library);
driver.propagate_dep(std_crate, &CrateName::new(std_crate_name).unwrap());
}