Skip to content

Commit

Permalink
fix(lsp): Ensure stdlib is always added before the check_crate phase (
Browse files Browse the repository at this point in the history
#1840)

fix(lsp): Ensure that stdlib is always added to the driver during the check_crate phase
  • Loading branch information
phated authored Jul 4, 2023
1 parent fdad81d commit cb607f5
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
1 change: 0 additions & 1 deletion crates/nargo_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ mod tests {
Box::new(acvm::pwg::default_is_opcode_supported(acvm::Language::R1CS)),
);
driver.create_local_crate(&root_file, CrateType::Binary);
crate::resolver::add_std_lib(&mut driver);

let result = driver.check_crate(false);
let success = result.is_ok();
Expand Down
11 changes: 1 addition & 10 deletions crates/nargo_cli/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use acvm::{acir::circuit::Opcode, Language};
use nargo::manifest::{Dependency, PackageManifest};
use noirc_driver::Driver;
use noirc_frontend::graph::{CrateId, CrateName, CrateType};
use noirc_frontend::graph::{CrateId, CrateType};
use thiserror::Error;

use crate::{git::clone_git_repo, InvalidPackageError};
Expand Down Expand Up @@ -85,7 +85,6 @@ impl<'a> Resolver<'a> {
let pkg_root = manifest_path.parent().expect("Every manifest path has a parent.");
resolver.resolve_manifest(crate_id, manifest, pkg_root)?;

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

Expand Down Expand Up @@ -167,11 +166,3 @@ 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());
}
9 changes: 9 additions & 0 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ impl Driver {
/// This returns a (possibly empty) vector of any warnings found on success.
/// On error, this returns a non-empty vector of warnings and error messages, with at least one error.
pub fn check_crate(&mut self, deny_warnings: bool) -> Result<Warnings, ErrorsAndWarnings> {
// Add the stdlib before we check the crate
// TODO: This should actually be done when constructing the driver and then propagated to each dependency when added;
// however, the `create_non_local_crate` panics if you add the stdlib as the first crate in the graph and other
// parts of the code expect the `0` FileID to be the crate root. See also #1681
let std_crate_name = "std";
let path_to_std_lib_file = PathBuf::from(std_crate_name).join("lib.nr");
let std_crate = self.create_non_local_crate(path_to_std_lib_file, CrateType::Library);
self.propagate_dep(std_crate, &CrateName::new(std_crate_name).unwrap());

let mut errors = vec![];
CrateDefMap::collect_defs(LOCAL_CRATE, &mut self.context, &mut errors);

Expand Down
3 changes: 0 additions & 3 deletions crates/wasm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ pub fn compile(args: JsValue) -> JsValue {
add_noir_lib(&mut driver, dependency.as_str());
}

// We are always adding std lib implicitly. It comes bundled with binary.
add_noir_lib(&mut driver, "std");

driver.check_crate(false).expect("Crate check failed");

if options.contracts {
Expand Down

0 comments on commit cb607f5

Please sign in to comment.