From cb607f5787f76856a3b9907151c3de44045bc9c7 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Tue, 4 Jul 2023 13:16:48 +0100 Subject: [PATCH] fix(lsp): Ensure stdlib is always added before the `check_crate` phase (#1840) fix(lsp): Ensure that stdlib is always added to the driver during the check_crate phase --- crates/nargo_cli/src/cli/mod.rs | 1 - crates/nargo_cli/src/resolver.rs | 11 +---------- crates/noirc_driver/src/lib.rs | 9 +++++++++ crates/wasm/src/compile.rs | 3 --- 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/crates/nargo_cli/src/cli/mod.rs b/crates/nargo_cli/src/cli/mod.rs index 77fea7e258d..0066d7c9135 100644 --- a/crates/nargo_cli/src/cli/mod.rs +++ b/crates/nargo_cli/src/cli/mod.rs @@ -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(); diff --git a/crates/nargo_cli/src/resolver.rs b/crates/nargo_cli/src/resolver.rs index 2ce26a44b26..19eafb07065 100644 --- a/crates/nargo_cli/src/resolver.rs +++ b/crates/nargo_cli/src/resolver.rs @@ -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}; @@ -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) } @@ -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()); -} diff --git a/crates/noirc_driver/src/lib.rs b/crates/noirc_driver/src/lib.rs index 2ec68f69ec7..c09ffe6b43c 100644 --- a/crates/noirc_driver/src/lib.rs +++ b/crates/noirc_driver/src/lib.rs @@ -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 { + // 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); diff --git a/crates/wasm/src/compile.rs b/crates/wasm/src/compile.rs index f5a53ea402e..ba1c2bacd14 100644 --- a/crates/wasm/src/compile.rs +++ b/crates/wasm/src/compile.rs @@ -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 {