Skip to content

Commit

Permalink
Parent module ID is def_maps
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jul 12, 2024
1 parent 7277623 commit 3fc052b
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 99 deletions.
6 changes: 2 additions & 4 deletions aztec_macros/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
8 changes: 2 additions & 6 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -545,11 +545,7 @@ impl<'context> Elaborator<'context> {
}

fn resolve_trait_by_path(&mut self, path: Path) -> Option<TraitId> {
// 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 }) => {
Expand Down
13 changes: 2 additions & 11 deletions compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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<LocalModuleId> {
let module_id = self.module_id();
self.interner.try_module_parent(&module_id)
}

pub(super) fn resolve_path(&mut self, path: Path) -> Result<ModuleDefId, ResolverError> {
// 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 {
Expand Down
6 changes: 0 additions & 6 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,17 @@ impl UnresolvedFunctions {
pub fn resolve_trait_bounds_trait_ids(
&mut self,
def_maps: &BTreeMap<CrateId, CrateDefMap>,
interner: &mut NodeInterner,
crate_id: CrateId,
) -> Vec<DefCollectorErrorKind> {
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)) => {
Expand Down Expand Up @@ -336,7 +333,6 @@ impl DefCollector {
root_file_id,
crate_root,
crate_id,
None,
context,
macro_processors,
));
Expand Down Expand Up @@ -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,
) {
Expand All @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ pub fn collect_defs(
file_id: FileId,
module_id: LocalModuleId,
crate_id: CrateId,
parent_module_id: Option<LocalModuleId>,
context: &mut Context,
macro_processors: &[&dyn MacroProcessor],
) -> Vec<(CompilationError, FileId)> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -577,7 +574,6 @@ impl<'a> ModCollector<'a> {
file_id,
child.local_id,
crate_id,
parent_module_id,
context,
macro_processors,
));
Expand Down Expand Up @@ -654,8 +650,6 @@ impl<'a> ModCollector<'a> {
parsing_errors.iter().map(|e| (e.clone().into(), child_file_id)).collect::<Vec<_>>(),
);

let parent_module_id = Some(self.module_id);

// Add module into def collector and get a ModuleId
match self.push_child_module(
context,
Expand All @@ -674,7 +668,6 @@ impl<'a> ModCollector<'a> {
child_file_id,
child_mod_id.local_id,
crate_id,
parent_module_id,
context,
macro_processors,
));
Expand Down
5 changes: 2 additions & 3 deletions compiler/noirc_frontend/src/hir/resolution/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions compiler/noirc_frontend/src/hir/resolution/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions compiler/noirc_frontend/src/hir/resolution/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions compiler/noirc_frontend/src/hir/resolution/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use super::errors::ResolverError;
#[derive(Debug, Clone)]
pub struct ImportDirective {
pub module_id: LocalModuleId,
pub parent_module_id: Option<LocalModuleId>,
pub path: Path,
pub alias: Option<Ident>,
pub is_prelude: bool,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 5 additions & 16 deletions compiler/noirc_frontend/src/hir/resolution/path_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalModuleId>,
}

impl StandardPathResolver {
pub fn new(
module_id: ModuleId,
parent_module_id: Option<LocalModuleId>,
) -> StandardPathResolver {
Self { module_id, parent_module_id }
pub fn new(module_id: ModuleId) -> StandardPathResolver {
Self { module_id }
}
}

Expand All @@ -46,7 +41,7 @@ impl PathResolver for StandardPathResolver {
path: Path,
path_references: &mut Option<&mut Vec<Option<ReferenceId>>>,
) -> 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 {
Expand All @@ -63,18 +58,12 @@ impl PathResolver for StandardPathResolver {
pub fn resolve_path(
def_maps: &BTreeMap<CrateId, CrateDefMap>,
module_id: ModuleId,
parent_module_id: Option<LocalModuleId>,
path: Path,
path_references: &mut Option<&mut Vec<Option<ReferenceId>>>,
) -> 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;
Expand Down
3 changes: 1 addition & 2 deletions compiler/noirc_frontend/src/hir/resolution/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ fn resolve_struct_fields(
unresolved: UnresolvedStruct,
) -> (Generics, Vec<(Ident, Type)>, Vec<ResolverError>) {
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)
Expand Down
Loading

0 comments on commit 3fc052b

Please sign in to comment.