Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
kevaundray committed Dec 17, 2023
1 parent 3e181c6 commit 63acdff
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 20 deletions.
22 changes: 6 additions & 16 deletions compiler/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,27 @@ impl ReadOnlyFileManagerTrait for &FileManager {
}
}

// TODO: remove this, its only so that new still works in the compiler
impl ReadOnlyFileManagerTrait for fm::FileManager {
impl<'a> ReadOnlyFileManagerTrait for std::borrow::Cow<'a, fm::FileManager> {
type FileId = fm::FileId;

fn root(&self) -> &std::path::Path {
self.root()
fm::FileManager::root(self)
}

fn fetch_file(&self, file_id: Self::FileId) -> &str {
self.fetch_file(file_id)
fm::FileManager::fetch_file(self, file_id)
}

fn path(&self, file_id: Self::FileId) -> &std::path::Path {
self.path(file_id)
fm::FileManager::path(self, file_id)
}

fn name_to_id(&self, file_name: std::path::PathBuf) -> Option<Self::FileId> {
self.name_to_id(file_name)
fm::FileManager::name_to_id(self, file_name)
}
}

pub type Context<'a> = Context_<&'a fm::FileManager>;
pub type Context<'file> = Context_<std::borrow::Cow<'file,fm::FileManager>>;
/// Helper object which groups together several useful context objects used
/// during name resolution. Once name resolution is finished, only the
/// def_interner is required for type inference and monomorphization.
Expand Down Expand Up @@ -126,15 +125,6 @@ impl<F: ReadOnlyFileManagerTrait> Context_<F> {
file_manager,
}
}
pub fn from_ref_file_manager(file_manager: F, crate_graph: CrateGraph) -> Context_<F> {
Context_ {
def_interner: NodeInterner::default(),
def_maps: BTreeMap::new(),
visited_files: BTreeMap::new(),
crate_graph,
file_manager,
}
}

/// Returns the CrateDefMap for a given CrateId.
/// It is perfectly valid for the compiler to look
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod test {
let root = std::path::Path::new("/");
let fm = FileManager::new(root);
let graph = CrateGraph::default();
let mut context = Context::new(fm, graph);
let mut context = Context::new(std::borrow::Cow::Owned(fm), graph);
let root_file_id = FileId::dummy();
let root_crate_id = context.crate_graph.add_crate_root(root_file_id);
let (program, parser_errors) = parse_program(src);
Expand Down
2 changes: 1 addition & 1 deletion compiler/wasm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub fn compile(
let fm = file_manager_with_source_map(file_source_map);

let graph = CrateGraph::default();
let mut context = Context::new(fm, graph);
let mut context = Context::new(std::borrow::Cow::Owned(fm), graph);

let path = Path::new(&entry_point);
let crate_id = prepare_crate(&mut context, path);
Expand Down
2 changes: 1 addition & 1 deletion compiler/wasm/src/compile_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl CompilerContext {

let fm = file_manager_with_source_map(source_map);
let graph = CrateGraph::default();
CompilerContext { context: Context::new(fm, graph) }
CompilerContext { context: Context::new(std::borrow::Cow::Owned(fm), graph) }
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub fn prepare_package<'file_manager>(
package: &Package,
) -> (Context<'file_manager>, CrateId) {
let graph = CrateGraph::default();
let mut context = Context::from_ref_file_manager(file_manager, graph);
let mut context = Context::new(std::borrow::Cow::Borrowed(file_manager), graph);

let crate_id = prepare_crate(&mut context, &package.entry_path);

Expand Down

0 comments on commit 63acdff

Please sign in to comment.