diff --git a/compiler/noirc_arena/src/lib.rs b/compiler/noirc_arena/src/lib.rs index 9a25299d6c8..d37e62e67d5 100644 --- a/compiler/noirc_arena/src/lib.rs +++ b/compiler/noirc_arena/src/lib.rs @@ -9,22 +9,11 @@ use std::fmt; pub struct Index(usize); impl Index { - #[cfg(test)] - pub fn test_new(index: usize) -> Index { - Self(index) - } - /// Return a dummy index (max value internally). /// This should be avoided over `Option` if possible. pub fn dummy() -> Self { Self(usize::MAX) } - - /// Return the zeroed index. This is unsafe since we don't know - /// if this is a valid index for any particular map yet. - pub fn unsafe_zeroed() -> Self { - Self(0) - } } impl fmt::Display for Index { diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index c6e59a57153..820ec8c27d6 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -1544,12 +1544,12 @@ impl<'context> Elaborator<'context> { pub fn get_module(&self, module: ModuleId) -> &ModuleData { let message = "A crate should always be present for a given crate id"; - &self.def_maps.get(&module.krate).expect(message).modules[module.local_id.0] + &self.def_maps.get(&module.krate).expect(message)[module.local_id] } fn get_module_mut(def_maps: &mut DefMaps, module: ModuleId) -> &mut ModuleData { let message = "A crate should always be present for a given crate id"; - &mut def_maps.get_mut(&module.krate).expect(message).modules[module.local_id.0] + &mut def_maps.get_mut(&module.krate).expect(message)[module.local_id] } fn declare_methods_on_struct( diff --git a/compiler/noirc_frontend/src/hir/comptime/tests.rs b/compiler/noirc_frontend/src/hir/comptime/tests.rs index 88a2bc8b52a..8367c0a1a5f 100644 --- a/compiler/noirc_frontend/src/hir/comptime/tests.rs +++ b/compiler/noirc_frontend/src/hir/comptime/tests.rs @@ -1,10 +1,9 @@ #![cfg(test)] -use std::collections::{BTreeMap, HashMap}; +use std::collections::HashMap; use std::path::PathBuf; use fm::{FileId, FileManager}; -use noirc_arena::Index; use noirc_errors::Location; use super::Interpreter; @@ -13,7 +12,7 @@ use super::value::Value; use crate::elaborator::{Elaborator, ElaboratorOptions}; use crate::hir::def_collector::dc_crate::{CompilationError, DefCollector}; use crate::hir::def_collector::dc_mod::collect_defs; -use crate::hir::def_map::{CrateDefMap, LocalModuleId, ModuleData}; +use crate::hir::def_map::{CrateDefMap, ModuleData}; use crate::hir::{Context, ParsedFiles}; use crate::node_interner::FuncId; use crate::parse_program; @@ -27,19 +26,15 @@ pub(crate) fn with_interpreter( ) -> T { let file = FileId::default(); - // Can't use Index::test_new here for some reason, even with #[cfg(test)]. - let module_id = LocalModuleId(Index::unsafe_zeroed()); - let mut modules = noirc_arena::Arena::default(); let location = Location::new(Default::default(), file); - let root = LocalModuleId(modules.insert(ModuleData::new( + let root_module = ModuleData::new( None, location, Vec::new(), Vec::new(), false, // is contract false, // is struct - ))); - assert_eq!(root, module_id); + ); let file_manager = FileManager::new(&PathBuf::new()); let parsed_files = ParsedFiles::new(); @@ -52,10 +47,11 @@ pub(crate) fn with_interpreter( assert_eq!(errors.len(), 0); let ast = module.into_sorted(); - let def_map = CrateDefMap { root: module_id, modules, krate, extern_prelude: BTreeMap::new() }; + let def_map = CrateDefMap::new(krate, root_module); + let root_module_id = def_map.root(); let mut collector = DefCollector::new(def_map); - collect_defs(&mut collector, ast, FileId::dummy(), module_id, krate, &mut context); + collect_defs(&mut collector, ast, FileId::dummy(), root_module_id, krate, &mut context); context.def_maps.insert(krate, collector.def_map); let main = context.get_main_function(&krate).expect("Expected 'main' function"); 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 81b3f842487..70215e408f9 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -35,6 +35,7 @@ use iter_extended::vecmap; use rustc_hash::FxHashMap as HashMap; use std::collections::BTreeMap; use std::fmt::Write; +use std::ops::IndexMut; use std::path::PathBuf; use std::vec; @@ -313,7 +314,7 @@ impl DefCollector { options: FrontendOptions, ) -> Vec { let mut errors: Vec = vec![]; - let crate_id = def_map.krate; + let crate_id = def_map.krate(); // Recursively resolve the dependencies // @@ -328,7 +329,7 @@ impl DefCollector { let dep_def_map = context.def_map(&dep.crate_id).expect("ice: def map was just created"); - let dep_def_root = dep_def_map.root; + let dep_def_root = dep_def_map.root(); let module_id = ModuleId { krate: dep.crate_id, local_id: dep_def_root }; // Add this crate as a dependency by linking it's root module def_map.extern_prelude.insert(dep.as_name(), module_id); @@ -346,7 +347,7 @@ impl DefCollector { // At this point, all dependencies are resolved and type checked. // // It is now possible to collect all of the definitions of this crate. - let crate_root = def_map.root; + let crate_root = def_map.root(); let mut def_collector = DefCollector::new(def_map); let module_id = ModuleId { krate: crate_id, local_id: crate_root }; @@ -367,13 +368,14 @@ impl DefCollector { context, )); - let submodules = vecmap(def_collector.def_map.modules().iter(), |(index, _)| index); + let submodules = + vecmap(def_collector.def_map.modules().iter(), |(index, _)| LocalModuleId::new(index)); // Add the current crate to the collection of DefMaps context.def_maps.insert(crate_id, def_collector.def_map); inject_prelude(crate_id, context, crate_root, &mut def_collector.imports); for submodule in submodules { - inject_prelude(crate_id, context, LocalModuleId(submodule), &mut def_collector.imports); + inject_prelude(crate_id, context, submodule, &mut def_collector.imports); } // Resolve unresolved imports collected from the crate, one by one. @@ -417,7 +419,7 @@ impl DefCollector { } let visibility = visibility.min(item_visibility); - let result = current_def_map.modules[local_module_id.0].import( + let result = current_def_map.index_mut(local_module_id).import( name.clone(), visibility, module_def_id, 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 d93a5648913..eaa56397ba2 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -383,7 +383,7 @@ impl ModCollector<'_> { context.def_interner.set_doc_comments(ReferenceId::Alias(type_alias_id), doc_comments); // Add the type alias to scope so its path can be looked up later - let result = self.def_collector.def_map.modules[self.module_id.0].declare_type_alias( + let result = self.def_collector.def_map[self.module_id].declare_type_alias( name.clone(), visibility, type_alias_id, @@ -461,7 +461,7 @@ impl ModCollector<'_> { // Add the trait to scope so its path can be looked up later let visibility = trait_definition.visibility; - let result = self.def_collector.def_map.modules[self.module_id.0].declare_trait( + let result = self.def_collector.def_map[self.module_id].declare_trait( name.clone(), visibility, trait_id, @@ -542,9 +542,11 @@ impl ModCollector<'_> { ); } - match self.def_collector.def_map.modules[trait_id.0.local_id.0] - .declare_function(name.clone(), ItemVisibility::Public, func_id) - { + match self.def_collector.def_map[trait_id.0.local_id].declare_function( + name.clone(), + ItemVisibility::Public, + func_id, + ) { Ok(()) => { if let Some(body) = body { let impl_method = @@ -585,8 +587,8 @@ impl ModCollector<'_> { false, ); - if let Err((first_def, second_def)) = self.def_collector.def_map.modules - [trait_id.0.local_id.0] + if let Err((first_def, second_def)) = self.def_collector.def_map + [trait_id.0.local_id] .declare_global(name.clone(), ItemVisibility::Public, global_id) { let error = DefCollectorErrorKind::Duplicate { @@ -610,9 +612,8 @@ impl ModCollector<'_> { } } TraitItem::Type { name } => { - if let Err((first_def, second_def)) = self.def_collector.def_map.modules - [trait_id.0.local_id.0] - .declare_type_alias( + if let Err((first_def, second_def)) = + self.def_collector.def_map[trait_id.0.local_id].declare_type_alias( name.clone(), ItemVisibility::Public, TypeAliasId::dummy_id(), @@ -914,13 +915,14 @@ fn push_child_module( is_type, ); - let module_id = def_map.modules.insert(new_module); - let modules = &mut def_map.modules; + let krate = def_map.krate(); + let module_id = def_map.insert_module(new_module); + let parent_module = &mut def_map[parent]; // Update the parent module to reference the child - modules[parent.0].children.insert(mod_name.clone(), LocalModuleId(module_id)); + parent_module.children.insert(mod_name.clone(), module_id); - let mod_id = ModuleId { krate: def_map.krate, local_id: LocalModuleId(module_id) }; + let mod_id = ModuleId { krate, local_id: module_id }; // Add this child module into the scope of the parent module as a module definition // module definitions are definitions which can only exist at the module level. @@ -931,7 +933,7 @@ fn push_child_module( // the struct name. if add_to_parent_scope { if let Err((first_def, second_def)) = - modules[parent.0].declare_child_module(mod_name.to_owned(), visibility, mod_id) + parent_module.declare_child_module(mod_name.to_owned(), visibility, mod_id) { let err = DefCollectorErrorKind::Duplicate { typ: DuplicateType::Module, @@ -952,7 +954,7 @@ fn push_child_module( ); if interner.is_in_lsp_mode() { - let parent_module_id = ModuleId { krate: def_map.krate, local_id: parent }; + let parent_module_id = ModuleId { krate: def_map.krate(), local_id: parent }; interner.register_module( mod_id, location, @@ -982,7 +984,7 @@ pub fn collect_function( } } - let module_data = &mut def_map.modules[module.local_id.0]; + let module_data = &mut def_map[module.local_id]; let is_test = function.def.attributes.is_test_function(); let is_entry_point_function = if module_data.is_contract { @@ -1009,7 +1011,7 @@ pub fn collect_function( interner.set_doc_comments(ReferenceId::Function(func_id), doc_comments); // Add function to scope/ns of the module - let result = def_map.modules[module.local_id.0].declare_function(name, visibility, func_id); + let result = module_data.declare_function(name, visibility, func_id); if let Err((first_def, second_def)) = result { let error = DefCollectorErrorKind::Duplicate { typ: DuplicateType::Function, @@ -1083,7 +1085,7 @@ pub fn collect_struct( // Add the struct to scope so its path can be looked up later let visibility = unresolved.struct_def.visibility; - let result = def_map.modules[module_id.0].declare_type(name.clone(), visibility, id); + let result = def_map[module_id].declare_type(name.clone(), visibility, id); let parent_module_id = ModuleId { krate, local_id: module_id }; @@ -1174,7 +1176,7 @@ pub fn collect_enum( // Add the enum to scope so its path can be looked up later let visibility = unresolved.enum_def.visibility; - let result = def_map.modules[module_id.0].declare_type(name.clone(), visibility, id); + let result = def_map[module_id].declare_type(name.clone(), visibility, id); let parent_module_id = ModuleId { krate, local_id: module_id }; @@ -1400,7 +1402,7 @@ pub(crate) fn collect_global( ); // Add the statement to the scope so its path can be looked up later - let result = def_map.modules[module_id.0].declare_global(name.clone(), visibility, global_id); + let result = def_map[module_id].declare_global(name.clone(), visibility, global_id); // Globals marked as ABI don't have to be used. if !is_abi { diff --git a/compiler/noirc_frontend/src/hir/def_map/mod.rs b/compiler/noirc_frontend/src/hir/def_map/mod.rs index 18eddbb624c..497f66095d6 100644 --- a/compiler/noirc_frontend/src/hir/def_map/mod.rs +++ b/compiler/noirc_frontend/src/hir/def_map/mod.rs @@ -26,9 +26,13 @@ pub const MAIN_FUNCTION: &str = "main"; /// Lets first check if this is offered by any external crate /// XXX: RA has made this a crate on crates.io #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] -pub struct LocalModuleId(pub Index); +pub struct LocalModuleId(Index); impl LocalModuleId { + pub fn new(index: Index) -> LocalModuleId { + LocalModuleId(index) + } + pub fn dummy_id() -> LocalModuleId { LocalModuleId(Index::dummy()) } @@ -51,7 +55,7 @@ impl ModuleId { /// Returns this module's parent, if there's any. pub fn parent(self, def_maps: &DefMaps) -> Option { - let module_data = &def_maps[&self.krate].modules()[self.local_id.0]; + let module_data = &def_maps[&self.krate][self.local_id]; module_data.parent.map(|local_id| ModuleId { krate: self.krate, local_id }) } } @@ -63,17 +67,42 @@ pub type DefMaps = BTreeMap; /// The definitions of the crate are accessible indirectly via the scopes of each module. #[derive(Debug)] pub struct CrateDefMap { - pub(crate) root: LocalModuleId, + krate: CrateId, - pub(crate) modules: Arena, + root: LocalModuleId, - pub(crate) krate: CrateId, + modules: Arena, /// Maps an external dependency's name to its root module id. pub(crate) extern_prelude: BTreeMap, } +impl std::ops::Index for CrateDefMap { + type Output = ModuleData; + fn index(&self, local_module_id: LocalModuleId) -> &ModuleData { + &self.modules[local_module_id.0] + } +} + +impl std::ops::IndexMut for CrateDefMap { + fn index_mut(&mut self, local_module_id: LocalModuleId) -> &mut ModuleData { + &mut self.modules[local_module_id.0] + } +} + impl CrateDefMap { + /// Constructs a new `CrateDefMap`, containing only the crate's root module. + /// + /// # Arguments + /// + /// - `krate`: The [CrateId] of the crate for which this `CrateDefMap` refers to. + /// - `root_module`: The [ModuleData] for the root module of the crate. + pub fn new(krate: CrateId, root_module: ModuleData) -> CrateDefMap { + let mut modules = Arena::default(); + let root = LocalModuleId::new(modules.insert(root_module)); + CrateDefMap { krate, root, modules, extern_prelude: BTreeMap::default() } + } + /// Collect all definitions in the crate pub fn collect_defs( crate_id: CrateId, @@ -95,24 +124,17 @@ impl CrateDefMap { let (ast, parsing_errors) = context.parsed_file_results(root_file_id); let ast = ast.into_sorted(); - // Allocate a default Module for the root, giving it a ModuleId - let mut modules: Arena = Arena::default(); let location = Location::new(Default::default(), root_file_id); - let root = modules.insert(ModuleData::new( + + let root_module = ModuleData::new( None, location, Vec::new(), ast.inner_attributes.clone(), false, // is contract false, // is struct - )); - - let def_map = CrateDefMap { - root: LocalModuleId(root), - modules, - krate: crate_id, - extern_prelude: BTreeMap::new(), - }; + ); + let def_map = CrateDefMap::new(crate_id, root_module); // Now we want to populate the CrateDefMap using the DefCollector errors.extend(DefCollector::collect_crate_and_dependencies( @@ -131,6 +153,12 @@ impl CrateDefMap { pub fn root(&self) -> LocalModuleId { self.root } + + /// Returns a reference to the [ModuleData] stored at [LocalModuleId] `id` or `None` if none exists. + pub fn get(&self, id: LocalModuleId) -> Option<&ModuleData> { + self.modules.get(id.0) + } + pub fn modules(&self) -> &Arena { &self.modules } @@ -139,6 +167,10 @@ impl CrateDefMap { &mut self.modules } + pub(crate) fn insert_module(&mut self, module: ModuleData) -> LocalModuleId { + LocalModuleId::new(self.modules.insert(module)) + } + pub fn krate(&self) -> CrateId { self.krate } @@ -250,7 +282,7 @@ impl CrateDefMap { } }); - let name = self.get_module_path(id, module.parent); + let name = self.get_module_path(LocalModuleId::new(id), module.parent); Some(Contract { name, location: module.location, functions, outputs }) } else { None @@ -261,11 +293,24 @@ impl CrateDefMap { /// Find a child module's name by inspecting its parent. /// Currently required as modules do not store their own names. - pub fn get_module_path(&self, child_id: Index, parent: Option) -> String { + pub fn get_module_path( + &self, + child_id: LocalModuleId, + parent: Option, + ) -> String { self.get_module_path_with_separator(child_id, parent, ".") } pub fn get_module_path_with_separator( + &self, + child_id: LocalModuleId, + parent: Option, + separator: &str, + ) -> String { + self.get_module_path_with_separator_inner(child_id.0, parent, separator) + } + + fn get_module_path_with_separator_inner( &self, child_id: Index, parent: Option, @@ -280,7 +325,8 @@ impl CrateDefMap { .map(|(name, _)| name.as_str()) .expect("Child module was not a child of the given parent module"); - let parent_name = self.get_module_path_with_separator(id.0, parent.parent, separator); + let parent_name = + self.get_module_path_with_separator_inner(id.0, parent.parent, separator); if parent_name.is_empty() { name.to_string() } else { @@ -321,7 +367,7 @@ pub fn fully_qualified_module_path( crate_id: &CrateId, module_id: ModuleId, ) -> String { - let child_id = module_id.local_id.0; + let child_id = module_id.local_id; let def_map = def_maps.get(&module_id.krate).expect("The local crate should be analyzed already"); @@ -373,18 +419,6 @@ pub fn parse_file(fm: &FileManager, file_id: FileId) -> (ParsedModule, Vec for CrateDefMap { - type Output = ModuleData; - fn index(&self, local_module_id: LocalModuleId) -> &ModuleData { - &self.modules[local_module_id.0] - } -} -impl std::ops::IndexMut for CrateDefMap { - fn index_mut(&mut self, local_module_id: LocalModuleId) -> &mut ModuleData { - &mut self.modules[local_module_id.0] - } -} - pub struct TestFunction { id: FuncId, scope: TestScope, diff --git a/compiler/noirc_frontend/src/hir/mod.rs b/compiler/noirc_frontend/src/hir/mod.rs index 389be08205c..3d4549601c1 100644 --- a/compiler/noirc_frontend/src/hir/mod.rs +++ b/compiler/noirc_frontend/src/hir/mod.rs @@ -137,7 +137,7 @@ impl Context<'_, '_> { let module = self.module(module_id); let parent = - def_map.get_module_path_with_separator(module_id.local_id.0, module.parent, "::"); + def_map.get_module_path_with_separator(module_id.local_id, module.parent, "::"); if parent.is_empty() { name.into() } else { format!("{parent}::{name}") } } diff --git a/compiler/noirc_frontend/src/hir/resolution/import.rs b/compiler/noirc_frontend/src/hir/resolution/import.rs index f8a62e14eee..d8ac2f183e6 100644 --- a/compiler/noirc_frontend/src/hir/resolution/import.rs +++ b/compiler/noirc_frontend/src/hir/resolution/import.rs @@ -197,7 +197,7 @@ impl PathResolutionTargetResolver<'_, '_> { } fn resolve_crate_path(&mut self, path: Path) -> Result<(Path, ModuleId), PathResolutionError> { - let root_module = self.def_maps[&self.importing_module.krate].root; + let root_module = self.def_maps[&self.importing_module.krate].root(); let current_module = ModuleId { krate: self.importing_module.krate, local_id: root_module }; Ok((path, current_module)) } @@ -344,8 +344,7 @@ impl<'def_maps, 'usage_tracker, 'references_tracker> errors.push(PathResolutionError::Private(last_ident.clone())); } - current_module = - &self.def_maps[¤t_module_id.krate].modules[current_module_id.local_id.0]; + current_module = &self.def_maps[¤t_module_id.krate][current_module_id.local_id]; // Check if namespace let found_ns = current_module.find_name(current_ident); @@ -388,5 +387,5 @@ impl<'def_maps, 'usage_tracker, 'references_tracker> fn get_module(def_maps: &BTreeMap, module: ModuleId) -> &ModuleData { let message = "A crate should always be present for a given crate id"; - &def_maps.get(&module.krate).expect(message).modules[module.local_id.0] + &def_maps.get(&module.krate).expect(message)[module.local_id] } diff --git a/compiler/noirc_frontend/src/hir/resolution/visibility.rs b/compiler/noirc_frontend/src/hir/resolution/visibility.rs index 84badde9d35..b2c816a1f18 100644 --- a/compiler/noirc_frontend/src/hir/resolution/visibility.rs +++ b/compiler/noirc_frontend/src/hir/resolution/visibility.rs @@ -59,7 +59,7 @@ pub(crate) fn module_descendent_of_target( return true; } - def_map.modules[current.0] + def_map[current] .parent .is_some_and(|parent| module_descendent_of_target(def_map, target, parent)) } @@ -70,7 +70,7 @@ fn module_is_parent_of_struct_module( current: LocalModuleId, target: LocalModuleId, ) -> bool { - let module_data = &def_map.modules[target.0]; + let module_data = &def_map[target]; module_data.is_type && module_data.parent == Some(current) } diff --git a/compiler/noirc_frontend/src/tests.rs b/compiler/noirc_frontend/src/tests.rs index 5de1721ed75..17029723a9c 100644 --- a/compiler/noirc_frontend/src/tests.rs +++ b/compiler/noirc_frontend/src/tests.rs @@ -16,7 +16,7 @@ mod visibility; // XXX: These tests repeat a lot of code // what we should do is have test cases which are passed to a test harness // A test harness will allow for more expressive and readable tests -use std::collections::{BTreeMap, HashMap}; +use std::collections::HashMap; use std::path::Path; use crate::elaborator::{FrontendOptions, UnstableFeature}; @@ -31,14 +31,13 @@ use crate::hir::def_map::ModuleData; use crate::node_interner::{NodeInterner, StmtId}; use crate::hir::def_collector::dc_crate::DefCollector; -use crate::hir::def_map::{CrateDefMap, LocalModuleId}; +use crate::hir::def_map::CrateDefMap; use crate::hir_def::expr::HirExpression; use crate::hir_def::stmt::HirStatement; use crate::parser::{ItemKind, ParserErrorReason}; use crate::token::SecondaryAttribute; use crate::{ParsedModule, parse_program}; use fm::FileManager; -use noirc_arena::Arena; pub(crate) fn has_parser_error(errors: &[CompilationError]) -> bool { errors.iter().any(|e| matches!(e, CompilationError::ParseError(_))) @@ -101,24 +100,17 @@ pub(crate) fn get_program_with_options( }) .collect(); - // Allocate a default Module for the root, giving it a ModuleId - let mut modules: Arena = Arena::default(); let location = Location::new(Default::default(), root_file_id); - let root = modules.insert(ModuleData::new( + let root_module = ModuleData::new( None, location, Vec::new(), inner_attributes.clone(), false, // is contract false, // is struct - )); + ); - let def_map = CrateDefMap { - root: LocalModuleId(root), - modules, - krate: root_crate_id, - extern_prelude: BTreeMap::new(), - }; + let def_map = CrateDefMap::new(root_crate_id, root_module); // Now we want to populate the CrateDefMap using the DefCollector errors.extend(DefCollector::collect_crate_and_dependencies( diff --git a/tooling/lsp/src/attribute_reference_finder.rs b/tooling/lsp/src/attribute_reference_finder.rs index 384c575f0c6..d71a565b2f8 100644 --- a/tooling/lsp/src/attribute_reference_finder.rs +++ b/tooling/lsp/src/attribute_reference_finder.rs @@ -46,7 +46,7 @@ impl<'a> AttributeReferenceFinder<'a> { let local_id = if let Some((module_index, _)) = def_map.modules().iter().find(|(_, module_data)| module_data.location.file == file) { - LocalModuleId(module_index) + LocalModuleId::new(module_index) } else { def_map.root() }; @@ -71,7 +71,7 @@ impl Visitor for AttributeReferenceFinder<'_> { let previous_module_id = self.module_id; let def_map = &self.def_maps[&self.module_id.krate]; - if let Some(module_data) = def_map.modules().get(self.module_id.local_id.0) { + if let Some(module_data) = def_map.get(self.module_id.local_id) { if let Some(child_module) = module_data.children.get(&parsed_sub_module.name) { self.module_id = ModuleId { krate: self.module_id.krate, local_id: *child_module }; } diff --git a/tooling/lsp/src/requests/code_action.rs b/tooling/lsp/src/requests/code_action.rs index 6d74ce2d1f9..5bd85c887fa 100644 --- a/tooling/lsp/src/requests/code_action.rs +++ b/tooling/lsp/src/requests/code_action.rs @@ -120,7 +120,7 @@ impl<'a> CodeActionFinder<'a> { let local_id = if let Some((module_index, _)) = def_map.modules().iter().find(|(_, module_data)| module_data.location.file == file) { - LocalModuleId(module_index) + LocalModuleId::new(module_index) } else { def_map.root() }; @@ -250,7 +250,7 @@ impl Visitor for CodeActionFinder<'_> { let previous_module_id = self.module_id; let def_map = &self.def_maps[&self.module_id.krate]; - let Some(module_data) = def_map.modules().get(self.module_id.local_id.0) else { + let Some(module_data) = def_map.get(self.module_id.local_id) else { return false; }; if let Some(child_module) = module_data.children.get(&parsed_sub_module.name) { diff --git a/tooling/lsp/src/requests/code_action/import_trait.rs b/tooling/lsp/src/requests/code_action/import_trait.rs index c4250e4abb7..151c5d146ef 100644 --- a/tooling/lsp/src/requests/code_action/import_trait.rs +++ b/tooling/lsp/src/requests/code_action/import_trait.rs @@ -96,8 +96,7 @@ impl CodeActionFinder<'_> { }; // Check if the trait is currently imported. If yes, no need to suggest anything - let module_data = - &self.def_maps[&self.module_id.krate].modules()[self.module_id.local_id.0]; + let module_data = &self.def_maps[&self.module_id.krate][self.module_id.local_id]; if !module_data.scope().find_name(&trait_name).is_none() { return; } diff --git a/tooling/lsp/src/requests/completion.rs b/tooling/lsp/src/requests/completion.rs index 69cff723f1e..884d5b8a1da 100644 --- a/tooling/lsp/src/requests/completion.rs +++ b/tooling/lsp/src/requests/completion.rs @@ -144,7 +144,7 @@ impl<'a> NodeFinder<'a> { let local_id = if let Some((module_index, _)) = def_map.modules().iter().find(|(_, module_data)| module_data.location.file == file) { - LocalModuleId(module_index) + LocalModuleId::new(module_index) } else { def_map.root() }; @@ -864,14 +864,14 @@ impl<'a> NodeFinder<'a> { requested_items: RequestedItems, ) { let def_map = &self.def_maps[&module_id.krate]; - let Some(mut module_data) = def_map.modules().get(module_id.local_id.0) else { + let Some(mut module_data) = def_map.get(module_id.local_id) else { return; }; if at_root { match path_kind { PathKind::Crate => { - let Some(root_module_data) = def_map.modules().get(def_map.root().0) else { + let Some(root_module_data) = def_map.get(def_map.root()) else { return; }; module_data = root_module_data; @@ -880,7 +880,7 @@ impl<'a> NodeFinder<'a> { let Some(parent) = module_data.parent else { return; }; - let Some(parent_module_data) = def_map.modules().get(parent.0) else { + let Some(parent_module_data) = def_map.get(parent) else { return; }; module_data = parent_module_data; @@ -1149,8 +1149,7 @@ impl<'a> NodeFinder<'a> { let paths = paths.ok()?; // See which modules are already defined via `mod ...;` - let module_data = - &self.def_maps[&self.module_id.krate].modules()[self.module_id.local_id.0]; + let module_data = &self.def_maps[&self.module_id.krate][self.module_id.local_id]; let existing_children: HashSet = module_data.children.keys().map(|ident| ident.to_string()).collect(); @@ -1230,7 +1229,7 @@ impl Visitor for NodeFinder<'_> { let previous_module_id = self.module_id; let def_map = &self.def_maps[&self.module_id.krate]; - let Some(module_data) = def_map.modules().get(self.module_id.local_id.0) else { + let Some(module_data) = def_map.get(self.module_id.local_id) else { return false; }; if let Some(child_module) = module_data.children.get(&parsed_sub_module.name) { diff --git a/tooling/lsp/src/requests/completion/completion_items.rs b/tooling/lsp/src/requests/completion/completion_items.rs index 4478b975a2a..36bc30ec274 100644 --- a/tooling/lsp/src/requests/completion/completion_items.rs +++ b/tooling/lsp/src/requests/completion/completion_items.rs @@ -400,8 +400,7 @@ impl NodeFinder<'_> { trait_.name.clone() }; - let module_data = - &self.def_maps[&self.module_id.krate].modules()[self.module_id.local_id.0]; + let module_data = &self.def_maps[&self.module_id.krate][self.module_id.local_id]; if !module_data.scope().find_name(&trait_name).is_none() { return None; } diff --git a/tooling/lsp/src/trait_impl_method_stub_generator.rs b/tooling/lsp/src/trait_impl_method_stub_generator.rs index cf2d95c75b8..9fdf2b9ccea 100644 --- a/tooling/lsp/src/trait_impl_method_stub_generator.rs +++ b/tooling/lsp/src/trait_impl_method_stub_generator.rs @@ -184,7 +184,7 @@ impl<'a> TraitImplMethodStubGenerator<'a> { let struct_type = struct_type.borrow(); let current_module_data = - &self.def_maps[&self.module_id.krate].modules()[self.module_id.local_id.0]; + &self.def_maps[&self.module_id.krate][self.module_id.local_id]; // Check if the struct type is already imported/visible in this module let per_ns = current_module_data.find_name(&struct_type.name); @@ -219,7 +219,7 @@ impl<'a> TraitImplMethodStubGenerator<'a> { let type_alias = type_alias.borrow(); let current_module_data = - &self.def_maps[&self.module_id.krate].modules()[self.module_id.local_id.0]; + &self.def_maps[&self.module_id.krate][self.module_id.local_id]; // Check if the alias type is already imported/visible in this module let per_ns = current_module_data.find_name(&type_alias.name); @@ -257,7 +257,7 @@ impl<'a> TraitImplMethodStubGenerator<'a> { // Check if the trait type is already imported/visible in this module let current_module_data = - &self.def_maps[&self.module_id.krate].modules()[self.module_id.local_id.0]; + &self.def_maps[&self.module_id.krate][self.module_id.local_id]; if current_module_data.find_trait_in_scope(*trait_id).is_some() { self.string.push_str(trait_.name.as_str()); self.append_trait_generics(trait_generics); diff --git a/tooling/lsp/src/visibility.rs b/tooling/lsp/src/visibility.rs index 7366684a859..d244bbba8cd 100644 --- a/tooling/lsp/src/visibility.rs +++ b/tooling/lsp/src/visibility.rs @@ -48,7 +48,7 @@ pub(super) fn module_def_id_is_visible( } target_module_id = std::mem::take(&mut defining_module).or_else(|| { - let module_data = &def_maps[&module_id.krate].modules()[module_id.local_id.0]; + let module_data = &def_maps[&module_id.krate][module_id.local_id]; let parent_local_id = module_data.parent; parent_local_id.map(|local_id| ModuleId { krate: module_id.krate, local_id }) });