Skip to content

Commit

Permalink
chore(fm): Remove the concept of FileType (#1890)
Browse files Browse the repository at this point in the history
chore: Remove the concept of FileType
  • Loading branch information
phated authored Jul 7, 2023
1 parent fb5f20b commit fc755b9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
59 changes: 21 additions & 38 deletions crates/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ use std::{

pub const FILE_EXTENSION: &str = "nr";

// XXX: Create a trait for file io
/// An enum to differentiate between the root file
/// which the compiler starts at, and the others.
/// This is so that submodules of the root, can live alongside the
/// root file as files.
pub enum FileType {
Root,
Normal,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct VirtualPath(PathBuf);

Expand All @@ -37,14 +27,14 @@ pub struct FileManager {

impl FileManager {
// XXX: Maybe use a AsRef<Path> here, for API ergonomics
pub fn add_file(&mut self, path_to_file: &Path, file_type: FileType) -> Option<FileId> {
pub fn add_file(&mut self, path_to_file: &Path) -> Option<FileId> {
// Handle both relative file paths and std/lib virtual paths.
let base = Path::new(".").canonicalize().expect("Base path canonicalize failed");
let res = path_to_file.canonicalize().unwrap_or_else(|_| path_to_file.to_path_buf());
let resolved_path = res.strip_prefix(base).unwrap_or(&res);

// Check that the resolved path already exists in the file map, if it is, we return it.
let path_to_file = virtualize_path(resolved_path, file_type);
let path_to_file = virtualize_path(resolved_path);
if let Some(file_id) = self.path_to_id.get(&path_to_file) {
return Some(*file_id);
}
Expand Down Expand Up @@ -79,12 +69,16 @@ impl FileManager {
pub fn resolve_path(&mut self, anchor: FileId, mod_name: &str) -> Result<FileId, String> {
let mut candidate_files = Vec::new();

let dir = self.path(anchor).to_path_buf();
let anchor_path = self.path(anchor).to_path_buf();
let anchor_dir = anchor_path.parent().unwrap();

candidate_files.push(dir.join(format!("{mod_name}.{FILE_EXTENSION}")));
// First we attempt to look at `base/anchor/mod_name.nr` (child of the anchor)
candidate_files.push(anchor_path.join(format!("{mod_name}.{FILE_EXTENSION}")));
// If not found, we attempt to look at `base/mod_name.nr` (sibling of the anchor)
candidate_files.push(anchor_dir.join(format!("{mod_name}.{FILE_EXTENSION}")));

for candidate in candidate_files.iter() {
if let Some(file_id) = self.add_file(candidate, FileType::Normal) {
if let Some(file_id) = self.add_file(candidate) {
return Ok(file_id);
}
}
Expand All @@ -94,23 +88,13 @@ impl FileManager {
}

/// Takes a path to a noir file. This will panic on paths to directories
/// Returns
/// For Normal filetypes, given "src/mod.nr" this method returns "src/mod"
/// For Root filetypes, given "src/mod.nr" this method returns "src"
fn virtualize_path(path: &Path, file_type: FileType) -> VirtualPath {
let mut path = path.to_path_buf();
let path = match file_type {
FileType::Root => {
path.pop();
path
}
FileType::Normal => {
let base = path.parent().unwrap();
let path_no_ext: PathBuf =
path.file_stem().expect("ice: this should have been the path to a file").into();
base.join(path_no_ext)
}
};
/// Returns the file path with the extension removed
fn virtualize_path(path: &Path) -> VirtualPath {
let path = path.to_path_buf();
let base = path.parent().unwrap();
let path_no_ext: PathBuf =
path.file_stem().expect("ice: this should have been the path to a file").into();
let path = base.join(path_no_ext);
VirtualPath(path)
}
#[cfg(test)]
Expand All @@ -132,7 +116,7 @@ mod tests {

let mut fm = FileManager::default();

let file_id = fm.add_file(&file_path, FileType::Root).unwrap();
let file_id = fm.add_file(&file_path).unwrap();

let _foo_file_path = dummy_file_path(&dir, "foo.nr");
fm.resolve_path(file_id, "foo").unwrap();
Expand All @@ -144,7 +128,7 @@ mod tests {

let mut fm = FileManager::default();

let file_id = fm.add_file(&file_path, FileType::Normal).unwrap();
let file_id = fm.add_file(&file_path).unwrap();

assert!(fm.path(file_id).ends_with("foo"));
}
Expand All @@ -157,14 +141,13 @@ mod tests {
// we now have dir/lib.nr
let file_path = dummy_file_path(&dir, "lib.nr");

let file_id = fm.add_file(&file_path, FileType::Root).unwrap();
let file_id = fm.add_file(&file_path).unwrap();

// Create a sub directory
// we now have:
// - dir/lib.nr
// - dir/sub_dir
let sub_dir = TempDir::new_in(&dir).unwrap();
std::fs::create_dir_all(sub_dir.path()).unwrap();
let sub_dir_name = sub_dir.path().file_name().unwrap().to_str().unwrap();

// Add foo.nr to the subdirectory
Expand Down Expand Up @@ -205,8 +188,8 @@ mod tests {
let second_file_path = dummy_file_path(&sub_sub_dir, "./../../lib.nr");

// Add both files to the file manager
let file_id = fm.add_file(&file_path, FileType::Root).unwrap();
let second_file_id = fm.add_file(&second_file_path, FileType::Root).unwrap();
let file_id = fm.add_file(&file_path).unwrap();
let second_file_id = fm.add_file(&second_file_path).unwrap();

assert_eq!(file_id, second_file_id);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use acvm::acir::circuit::Opcode;
use acvm::compiler::CircuitSimplifier;
use acvm::Language;
use clap::Args;
use fm::{FileId, FileType};
use fm::FileId;
use noirc_abi::FunctionSignature;
use noirc_errors::{CustomDiagnostic, FileDiagnostic};
use noirc_evaluator::{create_circuit, ssa_refactor::experimental_create_circuit};
Expand Down Expand Up @@ -90,7 +90,7 @@ pub fn create_local_crate<P: AsRef<Path>>(
crate_type: CrateType,
) -> CrateId {
let dir_path = root_file.as_ref().to_path_buf();
let root_file_id = context.file_manager.add_file(&dir_path, FileType::Root).unwrap();
let root_file_id = context.file_manager.add_file(&dir_path).unwrap();

let crate_id = context.crate_graph.add_crate_root(crate_type, root_file_id);

Expand All @@ -107,7 +107,7 @@ pub fn create_non_local_crate<P: AsRef<Path>>(
crate_type: CrateType,
) -> CrateId {
let dir_path = root_file.as_ref().to_path_buf();
let root_file_id = context.file_manager.add_file(&dir_path, FileType::Root).unwrap();
let root_file_id = context.file_manager.add_file(&dir_path).unwrap();

// The first crate is always the local crate
assert!(context.crate_graph.number_of_crates() != 0);
Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl CrateDefMap {
.path(root_file_id)
.to_str()
.expect("expected std path to be convertible to str");
assert_eq!(path_as_str, "std");
assert_eq!(path_as_str, "std/lib");
ast.module_decls.retain(|ident| ident.0.contents != "slice");
}

Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_frontend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! perform optimizations, convert to ACIR and eventually prove/verify the program.
use std::path::PathBuf;

use fm::{FileManager, FileType};
use fm::FileManager;
use hir::{def_map::CrateDefMap, Context};
use noirc_frontend::graph::{CrateGraph, CrateType};
use noirc_frontend::hir::{self, def_map::ModuleDefId};
Expand All @@ -19,7 +19,7 @@ fn main() {
//
// Add root file to file manager
let dir_path: PathBuf = PathBuf::from("example_project/lib.nr");
let root_file_id = fm.add_file(&dir_path, FileType::Root).unwrap();
let root_file_id = fm.add_file(&dir_path).unwrap();

// CrateGraph
let mut crate_graph = CrateGraph::default();
Expand Down

0 comments on commit fc755b9

Please sign in to comment.