From ed62b5be6f246f66bb56525652d3354a176765f1 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 14 Aug 2023 13:07:26 -0700 Subject: [PATCH] chore(frontend): Replace `ModuleOrigin` with `Location` on `ModuleData` (#2308) --- .../src/hir/def_collector/dc_crate.rs | 6 +-- .../src/hir/def_collector/dc_mod.rs | 7 ++-- crates/noirc_frontend/src/hir/def_map/mod.rs | 15 +++---- .../src/hir/def_map/module_data.rs | 39 ++----------------- .../src/hir/resolution/resolver.rs | 8 ++-- .../noirc_frontend/src/hir/type_check/mod.rs | 5 ++- .../src/monomorphization/mod.rs | 8 ++-- 7 files changed, 28 insertions(+), 60 deletions(-) diff --git a/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs b/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs index e256bbd8cb..ac83e81ea7 100644 --- a/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/crates/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -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) })); @@ -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); @@ -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); diff --git a/crates/noirc_frontend/src/hir/def_collector/dc_mod.rs b/crates/noirc_frontend/src/hir/def_collector/dc_mod.rs index 545375dc64..53ed397e64 100644 --- a/crates/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/crates/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -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, @@ -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; @@ -315,7 +315,8 @@ impl<'a> ModCollector<'a> { errors: &mut Vec, ) -> Option { 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; diff --git a/crates/noirc_frontend/src/hir/def_map/mod.rs b/crates/noirc_frontend/src/hir/def_map/mod.rs index b32f332bd2..f264d3f40b 100644 --- a/crates/noirc_frontend/src/hir/def_map/mod.rs +++ b/crates/noirc_frontend/src/hir/def_map/mod.rs @@ -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; @@ -87,8 +87,8 @@ impl CrateDefMap { // Allocate a default Module for the root, giving it a ModuleId let mut modules: Arena = 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), @@ -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 diff --git a/crates/noirc_frontend/src/hir/def_map/module_data.rs b/crates/noirc_frontend/src/hir/def_map/module_data.rs index 5b93d04fea..5382ca6ad1 100644 --- a/crates/noirc_frontend/src/hir/def_map/module_data.rs +++ b/crates/noirc_frontend/src/hir/def_map/module_data.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use fm::FileId; +use noirc_errors::Location; use crate::{ node_interner::{FuncId, StmtId, StructId, TypeAliasId}, @@ -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, - origin: ModuleOrigin, - is_contract: bool, - ) -> ModuleData { + pub fn new(parent: Option, location: Location, is_contract: bool) -> ModuleData { ModuleData { parent, children: HashMap::new(), scope: ItemScope::default(), definitions: ItemScope::default(), - origin, + location, is_contract, } } @@ -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 for FileId { - fn from(origin: ModuleOrigin) -> Self { - origin.file_id() - } -} - -impl Default for ModuleOrigin { - fn default() -> Self { - ModuleOrigin::CrateRoot(FileId::default()) - } -} diff --git a/crates/noirc_frontend/src/hir/resolution/resolver.rs b/crates/noirc_frontend/src/hir/resolution/resolver.rs index 4bfb5428ed..a2f675542c 100644 --- a/crates/noirc_frontend/src/hir/resolution/resolver.rs +++ b/crates/noirc_frontend/src/hir/resolution/resolver.rs @@ -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; @@ -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()); @@ -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; diff --git a/crates/noirc_frontend/src/hir/type_check/mod.rs b/crates/noirc_frontend/src/hir/type_check/mod.rs index 8768120af2..484d668af6 100644 --- a/crates/noirc_frontend/src/hir/type_check/mod.rs +++ b/crates/noirc_frontend/src/hir/type_check/mod.rs @@ -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; @@ -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(), diff --git a/crates/noirc_frontend/src/monomorphization/mod.rs b/crates/noirc_frontend/src/monomorphization/mod.rs index 783d9f3133..998f3093d4 100644 --- a/crates/noirc_frontend/src/monomorphization/mod.rs +++ b/crates/noirc_frontend/src/monomorphization/mod.rs @@ -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, }, @@ -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(),