From 3fc052b898f7bdd4afc99cad77b6f11e86953136 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Fri, 12 Jul 2024 14:07:46 -0300 Subject: [PATCH] Parent module ID is def_maps --- aztec_macros/src/utils/hir_utils.rs | 6 +-- compiler/noirc_frontend/src/elaborator/mod.rs | 8 +-- .../noirc_frontend/src/elaborator/scope.rs | 13 +---- .../src/hir/def_collector/dc_crate.rs | 6 --- .../src/hir/def_collector/dc_mod.rs | 7 --- .../src/hir/resolution/functions.rs | 5 +- .../src/hir/resolution/globals.rs | 4 +- .../src/hir/resolution/impls.rs | 6 +-- .../src/hir/resolution/import.rs | 6 +-- .../src/hir/resolution/path_resolver.rs | 21 ++------ .../src/hir/resolution/structs.rs | 3 +- .../src/hir/resolution/traits.rs | 53 ++++++++----------- .../src/hir/resolution/type_aliases.rs | 3 +- 13 files changed, 42 insertions(+), 99 deletions(-) diff --git a/aztec_macros/src/utils/hir_utils.rs b/aztec_macros/src/utils/hir_utils.rs index a4f481ab1f2..65d0e9eb305 100644 --- a/aztec_macros/src/utils/hir_utils.rs +++ b/aztec_macros/src/utils/hir_utils.rs @@ -193,9 +193,8 @@ pub fn inject_fn( let def_maps = &mut context.def_maps; let module_id = ModuleId { local_id: module_id, krate: *crate_id }; - let parent_module_id = context.def_interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let resolver = Resolver::new(&mut context.def_interner, &path_resolver, def_maps, file_id); @@ -248,9 +247,8 @@ pub fn inject_global( let def_maps = &mut context.def_maps; let module_id = ModuleId { local_id: module_id, krate: *crate_id }; - let parent_module_id = context.def_interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let mut resolver = Resolver::new(&mut context.def_interner, &path_resolver, def_maps, file_id); diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index c2120ca7270..f0d3e3cae1f 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -28,7 +28,7 @@ use crate::{ }, lexer::Lexer, macros_api::{ - BlockExpression, Ident, NodeInterner, NoirFunction, NoirStruct, PathKind, Pattern, + BlockExpression, Ident, NodeInterner, NoirFunction, NoirStruct, Pattern, SecondaryAttribute, StructId, }, node_interner::{ @@ -545,11 +545,7 @@ impl<'context> Elaborator<'context> { } fn resolve_trait_by_path(&mut self, path: Path) -> Option { - // Optimization: no need to pass a parent module ID if the path is not a super path - let parent_module_id = - if let PathKind::Super = path.kind { self.parent_module_id() } else { None }; - - let path_resolver = StandardPathResolver::new(self.module_id(), parent_module_id); + let path_resolver = StandardPathResolver::new(self.module_id()); let error = match path_resolver.resolve(self.def_maps, path.clone(), &mut None) { Ok(PathResolution { module_def_id: ModuleDefId::TraitId(trait_id), error }) => { diff --git a/compiler/noirc_frontend/src/elaborator/scope.rs b/compiler/noirc_frontend/src/elaborator/scope.rs index cc413412cb9..e01c7e997d4 100644 --- a/compiler/noirc_frontend/src/elaborator/scope.rs +++ b/compiler/noirc_frontend/src/elaborator/scope.rs @@ -5,7 +5,7 @@ use crate::hir::def_map::{LocalModuleId, ModuleId}; use crate::hir::resolution::path_resolver::{PathResolver, StandardPathResolver}; use crate::hir::resolution::resolver::SELF_TYPE_NAME; use crate::hir::scope::{Scope as GenericScope, ScopeTree as GenericScopeTree}; -use crate::macros_api::{Ident, PathKind}; +use crate::macros_api::Ident; use crate::{ hir::{ def_map::{ModuleDefId, TryFromModuleDefId}, @@ -42,17 +42,8 @@ impl<'context> Elaborator<'context> { ModuleId { krate: self.crate_id, local_id: self.local_module } } - pub(super) fn parent_module_id(&self) -> Option { - let module_id = self.module_id(); - self.interner.try_module_parent(&module_id) - } - pub(super) fn resolve_path(&mut self, path: Path) -> Result { - // Optimization: no need to pass a parent module ID if the path is not a super path - let parent_module_id = - if let PathKind::Super = path.kind { self.parent_module_id() } else { None }; - - let resolver = StandardPathResolver::new(self.module_id(), parent_module_id); + let resolver = StandardPathResolver::new(self.module_id()); let path_resolution; if self.interner.track_references { diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs index 6835dfb45e6..88811832e6f 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -72,20 +72,17 @@ impl UnresolvedFunctions { pub fn resolve_trait_bounds_trait_ids( &mut self, def_maps: &BTreeMap, - interner: &mut NodeInterner, crate_id: CrateId, ) -> Vec { let mut errors = Vec::new(); for (local_id, _, func) in &mut self.functions { let module_id = ModuleId { krate: crate_id, local_id: *local_id }; - let parent_module_id = interner.try_module_parent(&module_id); for bound in &mut func.def.where_clause { match resolve_trait_by_path( def_maps, module_id, - parent_module_id, bound.trait_bound.trait_path.clone(), ) { Ok((trait_id, warning)) => { @@ -336,7 +333,6 @@ impl DefCollector { root_file_id, crate_root, crate_id, - None, context, macro_processors, )); @@ -587,7 +583,6 @@ fn inject_prelude( if let Ok(PathResolution { module_def_id, error }) = path_resolver::resolve_path( &context.def_maps, ModuleId { krate: crate_id, local_id: crate_root }, - None, // Parent module isn't needed to solve "std::prelude" path, &mut None, ) { @@ -603,7 +598,6 @@ fn inject_prelude( 0, ImportDirective { module_id: crate_root, - parent_module_id: None, path: Path { segments, kind: PathKind::Plain, span: Span::default() }, alias: None, is_prelude: true, diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs index d909d32bbd5..62f903a7ae3 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -51,7 +51,6 @@ pub fn collect_defs( file_id: FileId, module_id: LocalModuleId, crate_id: CrateId, - parent_module_id: Option, context: &mut Context, macro_processors: &[&dyn MacroProcessor], ) -> Vec<(CompilationError, FileId)> { @@ -80,7 +79,6 @@ pub fn collect_defs( for import in ast.imports { collector.def_collector.imports.push(ImportDirective { module_id: collector.module_id, - parent_module_id, path: import.path, alias: import.alias, is_prelude: false, @@ -561,7 +559,6 @@ impl<'a> ModCollector<'a> { macro_processors: &[&dyn MacroProcessor], ) -> Vec<(CompilationError, FileId)> { let mut errors: Vec<(CompilationError, FileId)> = vec![]; - let parent_module_id = Some(self.module_id); for submodule in submodules { match self.push_child_module( context, @@ -577,7 +574,6 @@ impl<'a> ModCollector<'a> { file_id, child.local_id, crate_id, - parent_module_id, context, macro_processors, )); @@ -654,8 +650,6 @@ impl<'a> ModCollector<'a> { parsing_errors.iter().map(|e| (e.clone().into(), child_file_id)).collect::>(), ); - let parent_module_id = Some(self.module_id); - // Add module into def collector and get a ModuleId match self.push_child_module( context, @@ -674,7 +668,6 @@ impl<'a> ModCollector<'a> { child_file_id, child_mod_id.local_id, crate_id, - parent_module_id, context, macro_processors, )); diff --git a/compiler/noirc_frontend/src/hir/resolution/functions.rs b/compiler/noirc_frontend/src/hir/resolution/functions.rs index bd1a035fc78..fe46796ed24 100644 --- a/compiler/noirc_frontend/src/hir/resolution/functions.rs +++ b/compiler/noirc_frontend/src/hir/resolution/functions.rs @@ -29,13 +29,12 @@ pub(crate) fn resolve_function_set( let file_id = unresolved_functions.file_id; let where_clause_errors = - unresolved_functions.resolve_trait_bounds_trait_ids(def_maps, interner, crate_id); + unresolved_functions.resolve_trait_bounds_trait_ids(def_maps, crate_id); errors.extend(where_clause_errors.iter().cloned().map(|e| (e.into(), file_id))); vecmap(unresolved_functions.functions, |(mod_id, func_id, func)| { let module_id = ModuleId { krate: crate_id, local_id: mod_id }; - let parent_module_id = interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let mut resolver = Resolver::new(interner, &path_resolver, def_maps, file_id); // Must use set_generics here to ensure we re-use the same generics from when diff --git a/compiler/noirc_frontend/src/hir/resolution/globals.rs b/compiler/noirc_frontend/src/hir/resolution/globals.rs index 3d105fd8ef8..bcda4e75d3d 100644 --- a/compiler/noirc_frontend/src/hir/resolution/globals.rs +++ b/compiler/noirc_frontend/src/hir/resolution/globals.rs @@ -24,10 +24,8 @@ pub(crate) fn resolve_globals( ) -> ResolvedGlobals { let mut errors: Vec<(CompilationError, FileId)> = vec![]; let globals = vecmap(globals, |global| { - let interner = &context.def_interner; let module_id = ModuleId { local_id: global.module_id, krate: crate_id }; - let parent_module_id = interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let mut resolver = Resolver::new( &mut context.def_interner, diff --git a/compiler/noirc_frontend/src/hir/resolution/impls.rs b/compiler/noirc_frontend/src/hir/resolution/impls.rs index b64e6b20b72..3f61b1f0d15 100644 --- a/compiler/noirc_frontend/src/hir/resolution/impls.rs +++ b/compiler/noirc_frontend/src/hir/resolution/impls.rs @@ -35,8 +35,7 @@ pub(crate) fn collect_impls( for ((unresolved_type, local_module_id), methods) in collected_impls { let module_id = ModuleId { local_id: *local_module_id, krate: crate_id }; - let parent_module_id = interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let file = def_maps[&crate_id].file_id(*local_module_id); @@ -102,8 +101,7 @@ pub(crate) fn resolve_impls( for ((unresolved_type, local_module_id), methods) in collected_impls { let module_id = ModuleId { local_id: local_module_id, krate: crate_id }; - let parent_module_id = interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let file = def_maps[&crate_id].file_id(local_module_id); diff --git a/compiler/noirc_frontend/src/hir/resolution/import.rs b/compiler/noirc_frontend/src/hir/resolution/import.rs index 8d0fa7c9f96..10e18248dec 100644 --- a/compiler/noirc_frontend/src/hir/resolution/import.rs +++ b/compiler/noirc_frontend/src/hir/resolution/import.rs @@ -14,7 +14,6 @@ use super::errors::ResolverError; #[derive(Debug, Clone)] pub struct ImportDirective { pub module_id: LocalModuleId, - pub parent_module_id: Option, pub path: Path, pub alias: Option, pub is_prelude: bool, @@ -195,7 +194,9 @@ fn resolve_path_to_ns( ), crate::ast::PathKind::Super => { - if let Some(parent_module_id) = import_directive.parent_module_id { + if let Some(parent_module_id) = + def_maps[&crate_id].modules[import_directive.module_id.0].parent + { resolve_name_in_module( crate_id, importing_crate, @@ -362,7 +363,6 @@ fn resolve_external_dep( }; let dep_directive = ImportDirective { module_id: dep_module.local_id, - parent_module_id: None, // At this point the Path is not `super::` path, alias: directive.alias.clone(), is_prelude: false, diff --git a/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs b/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs index 07806e26159..7cd44a84018 100644 --- a/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs +++ b/compiler/noirc_frontend/src/hir/resolution/path_resolver.rs @@ -26,16 +26,11 @@ pub trait PathResolver { pub struct StandardPathResolver { // Module that we are resolving the path in module_id: ModuleId, - // The module's parent, if any - parent_module_id: Option, } impl StandardPathResolver { - pub fn new( - module_id: ModuleId, - parent_module_id: Option, - ) -> StandardPathResolver { - Self { module_id, parent_module_id } + pub fn new(module_id: ModuleId) -> StandardPathResolver { + Self { module_id } } } @@ -46,7 +41,7 @@ impl PathResolver for StandardPathResolver { path: Path, path_references: &mut Option<&mut Vec>>, ) -> PathResolutionResult { - resolve_path(def_maps, self.module_id, self.parent_module_id, path, path_references) + resolve_path(def_maps, self.module_id, path, path_references) } fn local_module_id(&self) -> LocalModuleId { @@ -63,18 +58,12 @@ impl PathResolver for StandardPathResolver { pub fn resolve_path( def_maps: &BTreeMap, module_id: ModuleId, - parent_module_id: Option, path: Path, path_references: &mut Option<&mut Vec>>, ) -> PathResolutionResult { // lets package up the path into an ImportDirective and resolve it using that - let import = ImportDirective { - module_id: module_id.local_id, - parent_module_id, - path, - alias: None, - is_prelude: false, - }; + let import = + ImportDirective { module_id: module_id.local_id, path, alias: None, is_prelude: false }; let resolved_import = resolve_import(module_id.krate, &import, def_maps, path_references)?; let namespace = resolved_import.resolved_namespace; diff --git a/compiler/noirc_frontend/src/hir/resolution/structs.rs b/compiler/noirc_frontend/src/hir/resolution/structs.rs index 4cb864a2944..69547c79f5b 100644 --- a/compiler/noirc_frontend/src/hir/resolution/structs.rs +++ b/compiler/noirc_frontend/src/hir/resolution/structs.rs @@ -73,8 +73,7 @@ fn resolve_struct_fields( unresolved: UnresolvedStruct, ) -> (Generics, Vec<(Ident, Type)>, Vec) { let module_id = ModuleId { local_id: unresolved.module_id, krate }; - let parent_module_id = context.def_interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let file_id = unresolved.file_id; let (generics, fields, errors) = Resolver::new(&mut context.def_interner, &path_resolver, &context.def_maps, file_id) diff --git a/compiler/noirc_frontend/src/hir/resolution/traits.rs b/compiler/noirc_frontend/src/hir/resolution/traits.rs index b5cf9670b88..a40deb4cf57 100644 --- a/compiler/noirc_frontend/src/hir/resolution/traits.rs +++ b/compiler/noirc_frontend/src/hir/resolution/traits.rs @@ -5,7 +5,6 @@ use iter_extended::vecmap; use noirc_errors::Location; use crate::ast::{Ident, ItemVisibility, Path, TraitItem, UnresolvedGeneric}; -use crate::hir::def_map::LocalModuleId; use crate::{ graph::CrateId, hir::{ @@ -108,9 +107,8 @@ fn resolve_trait_methods( let def_maps = &mut context.def_maps; let module_id = ModuleId { local_id: unresolved_trait.module_id, krate: crate_id }; - let parent_module_id = interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let file = def_maps[&crate_id].file_id(unresolved_trait.module_id); let mut functions = vec![]; @@ -288,35 +286,29 @@ fn collect_trait_impl( let mut errors: Vec<(CompilationError, FileId)> = vec![]; let unresolved_type = trait_impl.object_type.clone(); let module_id = ModuleId { local_id: trait_impl.module_id, krate: crate_id }; - let parent_module_id = interner.try_module_parent(&module_id); - - trait_impl.trait_id = match resolve_trait_by_path( - def_maps, - module_id, - parent_module_id, - trait_impl.trait_path.clone(), - ) { - Ok((trait_id, warning)) => { - if let Some(warning) = warning { - errors.push(( - DefCollectorErrorKind::PathResolutionError(warning).into(), - trait_impl.file_id, - )); + + trait_impl.trait_id = + match resolve_trait_by_path(def_maps, module_id, trait_impl.trait_path.clone()) { + Ok((trait_id, warning)) => { + if let Some(warning) = warning { + errors.push(( + DefCollectorErrorKind::PathResolutionError(warning).into(), + trait_impl.file_id, + )); + } + Some(trait_id) } - Some(trait_id) - } - Err(error) => { - errors.push((error.into(), trait_impl.file_id)); - None - } - }; + Err(error) => { + errors.push((error.into(), trait_impl.file_id)); + None + } + }; if let Some(trait_id) = trait_impl.trait_id { errors .extend(collect_trait_impl_methods(interner, def_maps, crate_id, trait_id, trait_impl)); - let parent_module_id = interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let file = def_maps[&crate_id].file_id(trait_impl.module_id); let mut resolver = Resolver::new(interner, &path_resolver, def_maps, file); resolver.add_generics(&trait_impl.generics); @@ -370,10 +362,9 @@ fn check_trait_impl_crate_coherence( let mut errors: Vec<(CompilationError, FileId)> = vec![]; let module_id = ModuleId { krate: current_crate, local_id: trait_impl.module_id }; - let parent_module_id = interner.try_module_parent(&module_id); let file = def_maps[¤t_crate].file_id(trait_impl.module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let mut resolver = Resolver::new(interner, &path_resolver, def_maps, file); let object_crate = match resolver.resolve_type(trait_impl.object_type.clone()) { @@ -395,10 +386,9 @@ fn check_trait_impl_crate_coherence( pub(crate) fn resolve_trait_by_path( def_maps: &BTreeMap, module_id: ModuleId, - parent_module_id: Option, path: Path, ) -> Result<(TraitId, Option), DefCollectorErrorKind> { - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); match path_resolver.resolve(def_maps, path.clone(), &mut None) { Ok(PathResolution { module_def_id: ModuleDefId::TraitId(trait_id), error }) => { @@ -422,8 +412,7 @@ pub(crate) fn resolve_trait_impls( let unresolved_type = trait_impl.object_type; let local_mod_id = trait_impl.module_id; let module_id = ModuleId { krate: crate_id, local_id: local_mod_id }; - let parent_module_id = interner.try_module_attributes(&module_id).map(|attrs| attrs.parent); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let self_type_span = unresolved_type.span; diff --git a/compiler/noirc_frontend/src/hir/resolution/type_aliases.rs b/compiler/noirc_frontend/src/hir/resolution/type_aliases.rs index 355d73af9e6..bccb222ad18 100644 --- a/compiler/noirc_frontend/src/hir/resolution/type_aliases.rs +++ b/compiler/noirc_frontend/src/hir/resolution/type_aliases.rs @@ -19,9 +19,8 @@ pub(crate) fn resolve_type_aliases( let mut errors: Vec<(CompilationError, FileId)> = vec![]; for (alias_id, unresolved_typ) in type_aliases { let module_id = ModuleId { local_id: unresolved_typ.module_id, krate: crate_id }; - let parent_module_id = context.def_interner.try_module_parent(&module_id); - let path_resolver = StandardPathResolver::new(module_id, parent_module_id); + let path_resolver = StandardPathResolver::new(module_id); let file = unresolved_typ.file_id; let (typ, generics, resolver_errors) = Resolver::new(&mut context.def_interner, &path_resolver, &context.def_maps, file)