Skip to content

Commit

Permalink
chore(frontend): Replace ModuleOrigin with Location on `ModuleDat…
Browse files Browse the repository at this point in the history
…a` (#2308)
  • Loading branch information
phated authored Aug 14, 2023
1 parent 094aef1 commit ed62b5b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 60 deletions.
6 changes: 3 additions & 3 deletions crates/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl DefCollector {
let current_def_map = context.def_maps.get(&crate_id).unwrap();

errors.extend(vecmap(unresolved_imports, |(error, module_id)| {
let file_id = current_def_map.modules[module_id.0].origin.file_id();
let file_id = current_def_map.file_id(module_id);
let error = DefCollectorErrorKind::PathResolutionError(error);
error.into_file_diagnostic(file_id)
}));
Expand Down Expand Up @@ -229,7 +229,7 @@ fn collect_impls(
let path_resolver =
StandardPathResolver::new(ModuleId { local_id: *module_id, krate: crate_id });

let file = def_maps[&crate_id].module_file_id(*module_id);
let file = def_maps[&crate_id].file_id(*module_id);

for (generics, span, unresolved) in methods {
let mut resolver = Resolver::new(interner, &path_resolver, def_maps, file);
Expand Down Expand Up @@ -425,7 +425,7 @@ fn resolve_impls(
let path_resolver =
StandardPathResolver::new(ModuleId { local_id: module_id, krate: crate_id });

let file = def_maps[&crate_id].module_file_id(module_id);
let file = def_maps[&crate_id].file_id(module_id);

for (generics, _, functions) in methods {
let mut resolver = Resolver::new(interner, &path_resolver, def_maps, file);
Expand Down
7 changes: 4 additions & 3 deletions crates/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fm::FileId;
use noirc_errors::FileDiagnostic;
use noirc_errors::{FileDiagnostic, Location};

use crate::{
graph::CrateId, hir::def_collector::dc_crate::UnresolvedStruct, node_interner::StructId,
Expand All @@ -11,7 +11,7 @@ use super::{
dc_crate::{DefCollector, UnresolvedFunctions, UnresolvedGlobal, UnresolvedTypeAlias},
errors::{DefCollectorErrorKind, DuplicateType},
};
use crate::hir::def_map::{parse_file, LocalModuleId, ModuleData, ModuleId, ModuleOrigin};
use crate::hir::def_map::{parse_file, LocalModuleId, ModuleData, ModuleId};
use crate::hir::resolution::import::ImportDirective;
use crate::hir::Context;

Expand Down Expand Up @@ -315,7 +315,8 @@ impl<'a> ModCollector<'a> {
errors: &mut Vec<FileDiagnostic>,
) -> Option<LocalModuleId> {
let parent = Some(self.module_id);
let new_module = ModuleData::new(parent, ModuleOrigin::File(file_id), is_contract);
let location = Location::new(mod_name.span(), file_id);
let new_module = ModuleData::new(parent, location, is_contract);
let module_id = self.def_collector.def_map.modules.insert(new_module);

let modules = &mut self.def_collector.def_map.modules;
Expand Down
15 changes: 5 additions & 10 deletions crates/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::parser::{parse_program, ParsedModule};
use crate::token::Attribute;
use arena::{Arena, Index};
use fm::{FileId, FileManager};
use noirc_errors::FileDiagnostic;
use noirc_errors::{FileDiagnostic, Location};
use std::collections::HashMap;

mod module_def;
Expand Down Expand Up @@ -87,8 +87,8 @@ impl CrateDefMap {

// Allocate a default Module for the root, giving it a ModuleId
let mut modules: Arena<ModuleData> = Arena::default();
let origin = ModuleOrigin::CrateRoot(root_file_id);
let root = modules.insert(ModuleData::new(None, origin, false));
let location = Location::new(Default::default(), root_file_id);
let root = modules.insert(ModuleData::new(None, location, false));

let def_map = CrateDefMap {
root: LocalModuleId(root),
Expand Down Expand Up @@ -120,13 +120,8 @@ impl CrateDefMap {
root_module.find_func_with_name(&MAIN_FUNCTION.into())
}

pub fn root_file_id(&self) -> FileId {
let root_module = &self.modules()[self.root.0];
root_module.origin.into()
}

pub fn module_file_id(&self, module_id: LocalModuleId) -> FileId {
self.modules[module_id.0].origin.file_id()
pub fn file_id(&self, module_id: LocalModuleId) -> FileId {
self.modules[module_id.0].location.file
}

/// Go through all modules in this crate, and find all functions in
Expand Down
39 changes: 4 additions & 35 deletions crates/noirc_frontend/src/hir/def_map/module_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use fm::FileId;
use noirc_errors::Location;

use crate::{
node_interner::{FuncId, StmtId, StructId, TypeAliasId},
Expand All @@ -23,24 +23,20 @@ pub struct ModuleData {
/// Contains only the definitions directly defined in the current module
definitions: ItemScope,

pub origin: ModuleOrigin,
pub location: Location,

/// True if this module is a `contract Foo { ... }` module containing contract functions
pub is_contract: bool,
}

impl ModuleData {
pub fn new(
parent: Option<LocalModuleId>,
origin: ModuleOrigin,
is_contract: bool,
) -> ModuleData {
pub fn new(parent: Option<LocalModuleId>, location: Location, is_contract: bool) -> ModuleData {
ModuleData {
parent,
children: HashMap::new(),
scope: ItemScope::default(),
definitions: ItemScope::default(),
origin,
location,
is_contract,
}
}
Expand Down Expand Up @@ -99,30 +95,3 @@ impl ModuleData {
self.definitions.values().values().map(|(id, _)| *id)
}
}

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ModuleOrigin {
CrateRoot(FileId),
File(FileId),
}

impl ModuleOrigin {
pub fn file_id(&self) -> FileId {
match self {
ModuleOrigin::CrateRoot(file_id) => *file_id,
ModuleOrigin::File(file_id) => *file_id,
}
}
}

impl From<ModuleOrigin> for FileId {
fn from(origin: ModuleOrigin) -> Self {
origin.file_id()
}
}

impl Default for ModuleOrigin {
fn default() -> Self {
ModuleOrigin::CrateRoot(FileId::default())
}
}
8 changes: 5 additions & 3 deletions crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1485,8 +1485,9 @@ mod test {

use fm::FileId;
use iter_extended::vecmap;
use noirc_errors::Location;

use crate::hir::def_map::{ModuleData, ModuleId, ModuleOrigin};
use crate::hir::def_map::{ModuleData, ModuleId};
use crate::hir::resolution::errors::ResolverError;
use crate::hir::resolution::import::PathResolutionError;
use crate::hir::resolution::resolver::StmtId;
Expand Down Expand Up @@ -1520,7 +1521,8 @@ mod test {
let file = FileId::default();

let mut modules = arena::Arena::new();
modules.insert(ModuleData::new(None, ModuleOrigin::File(file), false));
let location = Location::new(Default::default(), file);
modules.insert(ModuleData::new(None, location, false));

let path_resolver = TestPathResolver(HashMap::new());

Expand Down Expand Up @@ -1924,7 +1926,7 @@ mod test {
fn main() {
let string = f"this is i: {i}";
println(string);
println(f"I want to print {0}");
let new_val = 10;
Expand Down
5 changes: 3 additions & 2 deletions crates/noirc_frontend/src/hir/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod test {
use noirc_errors::{Location, Span};

use crate::graph::CrateId;
use crate::hir::def_map::{ModuleData, ModuleId, ModuleOrigin};
use crate::hir::def_map::{ModuleData, ModuleId};
use crate::hir::resolution::import::PathResolutionError;
use crate::hir_def::expr::HirIdent;
use crate::hir_def::stmt::HirLetStatement;
Expand Down Expand Up @@ -380,7 +380,8 @@ mod test {
let file = FileId::default();

let mut modules = arena::Arena::new();
modules.insert(ModuleData::new(None, ModuleOrigin::File(file), false));
let location = Location::new(Default::default(), file);
modules.insert(ModuleData::new(None, location, false));

def_maps.insert(
CrateId::dummy_id(),
Expand Down
8 changes: 4 additions & 4 deletions crates/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,13 +1303,12 @@ mod tests {

use fm::FileId;
use iter_extended::vecmap;
use noirc_errors::Location;

use crate::{
graph::CrateId,
hir::{
def_map::{
CrateDefMap, LocalModuleId, ModuleData, ModuleDefId, ModuleId, ModuleOrigin,
},
def_map::{CrateDefMap, LocalModuleId, ModuleData, ModuleDefId, ModuleId},
resolution::{
import::PathResolutionError, path_resolver::PathResolver, resolver::Resolver,
},
Expand Down Expand Up @@ -1349,7 +1348,8 @@ mod tests {
let file = FileId::default();

let mut modules = arena::Arena::new();
modules.insert(ModuleData::new(None, ModuleOrigin::File(file), false));
let location = Location::new(Default::default(), file);
modules.insert(ModuleData::new(None, location, false));

def_maps.insert(
CrateId::dummy_id(),
Expand Down

0 comments on commit ed62b5b

Please sign in to comment.