Skip to content

Commit

Permalink
Merge branch 'master' into jb/noir-testing-workflow
Browse files Browse the repository at this point in the history
* master: (50 commits)
  chore: update stale comment on `create_circuit` (#2173)
  chore: Replace `resolve_path` function with a trait that impls normalize (#2157)
  chore: clippy fix (#2174)
  feat!: Allow specifying new package name with `--name` flag (#2144)
  chore!: remove unused flags on LSP command (#2170)
  chore: Hide the `show_ssa` and `show_brillig` flags (#2171)
  chore: bump `clap` to 4.3.19 (#2167)
  chore: Move the long line of `nargo info` to `long_about` (#2151)
  chore: Refactor `normalize_path` into an API on FileManager (#2156)
  fix: Implement slices of structs (#2150)
  chore: Refreshed ACIR artifacts (#2148)
  chore: Rebuild ACIR test artifacts (#2147)
  chore: remove short flags for `--show-ssa` and `--deny-warnings` (#2141)
  chore: replace usage of `Directive::Quotient` with brillig opcode  (#1766)
  chore: clippy fix (#2136)
  feat: Initial work on rewriting closures to regular functions with hi… (#1959)
  chore: Decouple acir blockid from ssa valueid (#2103)
  chore: Initialize copy array from previous values in `array_set` (#2106)
  chore: rename `ssa_refactor` module to `ssa` (#2129)
  chore: Use `--show-output` flag on execution rather than compilation  (#2116)
  ...
  • Loading branch information
TomAFrench committed Aug 4, 2023
2 parents 6c320e1 + 186375b commit 0d2d438
Show file tree
Hide file tree
Showing 415 changed files with 5,556 additions and 2,512 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,3 @@ result
**/target
!crates/nargo_cli/tests/test_data/*/target
!crates/nargo_cli/tests/test_data/*/target/witness.tr
!crates/nargo_cli/tests/test_data_ssa_refactor/*/target
!crates/nargo_cli/tests/test_data_ssa_refactor/*/target/witness.tr
13 changes: 8 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ noirc_evaluator = { path = "crates/noirc_evaluator" }
noirc_frontend = { path = "crates/noirc_frontend" }
noir_wasm = { path = "crates/wasm" }
cfg-if = "1.0.0"
clap = { version = "4.1.4", features = ["derive"] }
clap = { version = "4.3.19", features = ["derive"] }
codespan = { version = "0.11.1", features = ["serialization"] }
codespan-lsp = "0.11.1"
codespan-reporting = "0.11.1"
Expand Down
1 change: 1 addition & 0 deletions crates/fm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ wasm-bindgen.workspace = true

[dev-dependencies]
tempfile = "3.2.0"
iter-extended.workspace = true
90 changes: 68 additions & 22 deletions crates/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct FileManager {
impl FileManager {
pub fn new(root: &Path) -> Self {
Self {
root: normalize_path(root),
root: root.normalize(),
file_map: Default::default(),
id_to_path: Default::default(),
path_to_id: Default::default(),
Expand All @@ -44,7 +44,7 @@ impl FileManager {
// TODO: The stdlib path should probably be an absolute path rooted in something people would never create
file_name.to_path_buf()
} else {
normalize_path(&self.root.join(file_name))
self.root.join(file_name).normalize()
};

// Check that the resolved path already exists in the file map, if it is, we return it.
Expand Down Expand Up @@ -80,7 +80,7 @@ impl FileManager {
self.id_to_path.get(&file_id).unwrap().0.as_path()
}

pub fn resolve_path(&mut self, anchor: FileId, mod_name: &str) -> Result<FileId, String> {
pub fn find_module(&mut self, anchor: FileId, mod_name: &str) -> Result<FileId, String> {
let mut candidate_files = Vec::new();

let anchor_path = self.path(anchor).to_path_buf();
Expand All @@ -101,13 +101,33 @@ impl FileManager {
}
}

/// Replacement for `std::fs::canonicalize` that doesn't verify the path exists.
///
/// Plucked from https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61
/// Advice from https://www.reddit.com/r/rust/comments/hkkquy/comment/fwtw53s/
fn normalize_path(path: &Path) -> PathBuf {
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
pub trait NormalizePath {
/// Replacement for `std::fs::canonicalize` that doesn't verify the path exists.
///
/// Plucked from https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61
/// Advice from https://www.reddit.com/r/rust/comments/hkkquy/comment/fwtw53s/
fn normalize(&self) -> PathBuf;
}

impl NormalizePath for PathBuf {
fn normalize(&self) -> PathBuf {
let components = self.components();
resolve_components(components)
}
}

impl NormalizePath for &Path {
fn normalize(&self) -> PathBuf {
let components = self.components();
resolve_components(components)
}
}

fn resolve_components<'a>(components: impl Iterator<Item = Component<'a>>) -> PathBuf {
let mut components = components.peekable();

// Preserve path prefix if one exists.
let mut normalized_path = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
Expand All @@ -116,20 +136,46 @@ fn normalize_path(path: &Path) -> PathBuf {

for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::Prefix(..) => unreachable!("Path cannot contain multiple prefixes"),
Component::RootDir => {
ret.push(component.as_os_str());
normalized_path.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
ret.pop();
normalized_path.pop();
}
Component::Normal(c) => {
ret.push(c);
normalized_path.push(c);
}
}
}
ret

normalized_path
}

#[cfg(test)]
mod path_normalization {
use iter_extended::vecmap;
use std::path::PathBuf;

use crate::NormalizePath;

#[test]
fn normalizes_paths_correctly() {
// Note that tests are run on unix so prefix handling can't be tested (as these only exist on Windows)
let test_cases = vecmap(
[
("/", "/"), // Handles root
("/foo/bar/../baz/../bar", "/foo/bar"), // Handles backtracking
("/././././././././baz", "/baz"), // Removes no-ops
],
|(unnormalized, normalized)| (PathBuf::from(unnormalized), PathBuf::from(normalized)),
);

for (path, expected_result) in test_cases {
assert_eq!(path.normalize(), expected_result);
}
}
}

/// Takes a path to a noir file. This will panic on paths to directories
Expand All @@ -149,7 +195,7 @@ mod tests {

fn create_dummy_file(dir: &TempDir, file_name: &Path) {
let file_path = dir.path().join(file_name);
let _file = std::fs::File::create(file_path.clone()).unwrap();
let _file = std::fs::File::create(file_path).unwrap();
}

#[test]
Expand All @@ -165,7 +211,7 @@ mod tests {

let dep_file_name = Path::new("foo.nr");
create_dummy_file(&dir, dep_file_name);
fm.resolve_path(file_id, "foo").unwrap();
fm.find_module(file_id, "foo").unwrap();
}
#[test]
fn path_resolve_file_module_other_ext() {
Expand All @@ -175,7 +221,7 @@ mod tests {

let mut fm = FileManager::new(dir.path());

let file_id = fm.add_file(&file_name).unwrap();
let file_id = fm.add_file(file_name).unwrap();

assert!(fm.path(file_id).ends_with("foo"));
}
Expand All @@ -189,7 +235,7 @@ mod tests {
let file_name = Path::new("lib.nr");
create_dummy_file(&dir, file_name);

let file_id = fm.add_file(&file_name).unwrap();
let file_id = fm.add_file(file_name).unwrap();

// Create a sub directory
// we now have:
Expand All @@ -212,10 +258,10 @@ mod tests {
create_dummy_file(&dir, Path::new(&format!("{}.nr", sub_dir_name)));

// First check for the sub_dir.nr file and add it to the FileManager
let sub_dir_file_id = fm.resolve_path(file_id, sub_dir_name).unwrap();
let sub_dir_file_id = fm.find_module(file_id, sub_dir_name).unwrap();

// Now check for files in it's subdirectory
fm.resolve_path(sub_dir_file_id, "foo").unwrap();
fm.find_module(sub_dir_file_id, "foo").unwrap();
}

/// Tests that two identical files that have different paths are treated as the same file
Expand All @@ -238,7 +284,7 @@ mod tests {
let second_file_name = PathBuf::from(sub_sub_dir.path()).join("./../../lib.nr");

// Add both files to the file manager
let file_id = fm.add_file(&file_name).unwrap();
let file_id = fm.add_file(file_name).unwrap();
let second_file_id = fm.add_file(&second_file_name).unwrap();

assert_eq!(file_id, second_file_id);
Expand Down
6 changes: 3 additions & 3 deletions crates/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use lsp_types::{
InitializeParams, InitializeResult, InitializedParams, Position, PublishDiagnosticsParams,
Range, ServerCapabilities, TextDocumentSyncOptions,
};
use noirc_driver::{check_crate, create_local_crate};
use noirc_driver::{check_crate, prepare_crate};
use noirc_errors::{DiagnosticKind, FileDiagnostic};
use noirc_frontend::{
graph::{CrateGraph, CrateType},
Expand Down Expand Up @@ -190,7 +190,7 @@ fn on_code_lens_request(
}
};

let crate_id = create_local_crate(&mut context, file_path, CrateType::Binary);
let crate_id = prepare_crate(&mut context, file_path, CrateType::Binary);

// We ignore the warnings and errors produced by compilation for producing codelenses
// because we can still get the test functions even if compilation fails
Expand Down Expand Up @@ -283,7 +283,7 @@ fn on_did_save_text_document(
}
};

let crate_id = create_local_crate(&mut context, file_path, CrateType::Binary);
let crate_id = prepare_crate(&mut context, file_path, CrateType::Binary);

let mut diagnostics = Vec::new();

Expand Down
7 changes: 3 additions & 4 deletions crates/lsp/src/lib_hacky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use lsp_types::{
InitializedParams, Position, PublishDiagnosticsParams, Range, ServerCapabilities,
TextDocumentSyncOptions,
};
use noirc_driver::{check_crate, create_local_crate, create_non_local_crate, propagate_dep};
use noirc_driver::{check_crate, prepare_crate, propagate_dep};
use noirc_errors::{DiagnosticKind, FileDiagnostic};
use noirc_frontend::{
graph::{CrateGraph, CrateId, CrateType},
Expand Down Expand Up @@ -286,7 +286,7 @@ fn create_context_at_path(
}
let nargo_toml_path = find_nearest_parent_file(&file_path, &["Nargo.toml"]);

let current_crate_id = create_local_crate(&mut context, &file_path, CrateType::Binary);
let current_crate_id = prepare_crate(&mut context, &file_path, CrateType::Binary);

// TODO(AD): undo hacky dependency resolution
if let Some(nargo_toml_path) = nargo_toml_path {
Expand All @@ -297,8 +297,7 @@ fn create_context_at_path(
.parent()
.unwrap() // TODO
.join(PathBuf::from(&dependency_path).join("src").join("lib.nr"));
let library_crate =
create_non_local_crate(&mut context, &path_to_lib, CrateType::Library);
let library_crate = prepare_crate(&mut context, &path_to_lib, CrateType::Library);
propagate_dep(&mut context, library_crate, &crate_name.parse().unwrap());
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ rustc_version = "0.4.0"
acvm.workspace = true
noirc_abi.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
iter-extended.workspace = true
toml.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
noirc_errors.workspace = true
base64.workspace = true
base64.workspace = true
regex = "1.9.1"
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Directories
/// The directory for the `nargo contract` command output
pub(crate) const CONTRACT_DIR: &str = "contract";
pub const CONTRACT_DIR: &str = "contract";
/// The directory to store serialized circuit proofs.
pub(crate) const PROOFS_DIR: &str = "proofs";
pub const PROOFS_DIR: &str = "proofs";
/// The directory to store Noir source files
pub(crate) const SRC_DIR: &str = "src";
pub const SRC_DIR: &str = "src";
/// The directory to store circuits' serialized ACIR representations.
pub(crate) const TARGET_DIR: &str = "target";
pub const TARGET_DIR: &str = "target";

// Files
/// The file from which Nargo pulls prover inputs
pub(crate) const PROVER_INPUT_FILE: &str = "Prover";
pub const PROVER_INPUT_FILE: &str = "Prover";
/// The file from which Nargo pulls verifier inputs
pub(crate) const VERIFIER_INPUT_FILE: &str = "Verifier";
pub const VERIFIER_INPUT_FILE: &str = "Verifier";
/// The package definition file for a Noir project.
pub(crate) const PKG_FILE: &str = "Nargo.toml";
pub const PKG_FILE: &str = "Nargo.toml";

// Extensions
/// The extension for files containing circuit proofs.
pub(crate) const PROOF_EXT: &str = "proof";
pub const PROOF_EXT: &str = "proof";
/// The extension for files containing proof witnesses.
pub(crate) const WITNESS_EXT: &str = "tr";
pub const WITNESS_EXT: &str = "tr";
4 changes: 3 additions & 1 deletion crates/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
//! Noir Package Manager abbreviated is npm, which is already taken.

pub mod artifacts;
pub mod constants;
mod errors;
pub mod manifest;
pub mod ops;
pub mod package;
pub mod workspace;

pub use self::errors::NargoError;
26 changes: 0 additions & 26 deletions crates/nargo/src/manifest/errors.rs

This file was deleted.

Loading

0 comments on commit 0d2d438

Please sign in to comment.