From e42f9627664cc3c44094e1c4f985270fbfd592b1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 11:27:47 +0300 Subject: Encapsulate Attrs --- crates/ra_hir_def/src/nameres/collector.rs | 20 ++++++++------------ crates/ra_hir_def/src/nameres/raw.rs | 18 +++++++----------- 2 files changed, 15 insertions(+), 23 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index aae3dcadf..7902293e8 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -12,7 +12,7 @@ use rustc_hash::FxHashMap; use test_utils::tested_by; use crate::{ - attr::Attr, + attr::Attrs, db::DefDatabase2, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, @@ -549,7 +549,7 @@ where // `#[macro_use] extern crate` is hoisted to imports macros before collecting // any other items. for item in items { - if self.is_cfg_enabled(item.attrs()) { + if self.is_cfg_enabled(&item.attrs) { if let raw::RawItemKind::Import(import_id) = item.kind { let import = self.raw_items[import_id].clone(); if import.is_extern_crate && import.is_macro_use { @@ -560,10 +560,10 @@ where } for item in items { - if self.is_cfg_enabled(item.attrs()) { + if self.is_cfg_enabled(&item.attrs) { match item.kind { raw::RawItemKind::Module(m) => { - self.collect_module(&self.raw_items[m], item.attrs()) + self.collect_module(&self.raw_items[m], &item.attrs) } raw::RawItemKind::Import(import_id) => self .def_collector @@ -585,9 +585,9 @@ where } } - fn collect_module(&mut self, module: &raw::ModuleData, attrs: &[Attr]) { + fn collect_module(&mut self, module: &raw::ModuleData, attrs: &Attrs) { let path_attr = self.path_attr(attrs); - let is_macro_use = self.is_macro_use(attrs); + let is_macro_use = attrs.has_atom("macro_use"); match module { // inline module, just recurse raw::ModuleData::Definition { name, items, ast_id } => { @@ -779,17 +779,13 @@ where } } - fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool { + fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) } - fn path_attr<'a>(&self, attrs: &'a [Attr]) -> Option<&'a SmolStr> { + fn path_attr<'a>(&self, attrs: &'a Attrs) -> Option<&'a SmolStr> { attrs.iter().find_map(|attr| attr.as_path()) } - - fn is_macro_use<'a>(&self, attrs: &'a [Attr]) -> bool { - attrs.iter().any(|attr| attr.is_simple_atom("macro_use")) - } } fn is_macro_rules(path: &Path) -> bool { diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 7c68fd638..55a9634f8 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -16,7 +16,12 @@ use ra_syntax::{ }; use test_utils::tested_by; -use crate::{attr::Attr, db::DefDatabase2, path::Path, FileAstId, HirFileId, ModuleSource, Source}; +use crate::{ + attr::{Attr, Attrs}, + db::DefDatabase2, + path::Path, + FileAstId, HirFileId, ModuleSource, Source, +}; /// `RawItems` is a set of top-level items in a file (except for impls). /// @@ -129,21 +134,12 @@ impl Index for RawItems { } } -// Avoid heap allocation on items without attributes. -type Attrs = Option>; - #[derive(Debug, PartialEq, Eq, Clone)] pub(super) struct RawItem { - attrs: Attrs, + pub(super) attrs: Attrs, pub(super) kind: RawItemKind, } -impl RawItem { - pub(super) fn attrs(&self) -> &[Attr] { - self.attrs.as_ref().map_or(&[], |it| &*it) - } -} - #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(super) enum RawItemKind { Module(Module), -- cgit v1.2.3 From fc1e543f7abb69b8cab308410fa0a127950ee1c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 14:44:43 +0300 Subject: Get rid of DefDatabase2 --- crates/ra_hir_def/src/nameres/collector.rs | 12 ++++++------ crates/ra_hir_def/src/nameres/mod_resolution.rs | 4 ++-- crates/ra_hir_def/src/nameres/path_resolution.rs | 8 ++++---- crates/ra_hir_def/src/nameres/raw.rs | 6 +++--- crates/ra_hir_def/src/nameres/tests.rs | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 7902293e8..1894b072a 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -13,7 +13,7 @@ use test_utils::tested_by; use crate::{ attr::Attrs, - db::DefDatabase2, + db::DefDatabase, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode, @@ -24,7 +24,7 @@ use crate::{ StructOrUnionId, TraitId, TypeAliasLoc, UnionId, }; -pub(super) fn collect_defs(db: &impl DefDatabase2, mut def_map: CrateDefMap) -> CrateDefMap { +pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { let crate_graph = db.crate_graph(); // populate external prelude @@ -108,7 +108,7 @@ struct DefCollector<'a, DB> { impl DefCollector<'_, DB> where - DB: DefDatabase2, + DB: DefDatabase, { fn collect(&mut self) { let crate_graph = self.db.crate_graph(); @@ -530,7 +530,7 @@ struct ModCollector<'a, D> { impl ModCollector<'_, &'_ mut DefCollector<'_, DB>> where - DB: DefDatabase2, + DB: DefDatabase, { fn collect(&mut self, items: &[raw::RawItem]) { // Note: don't assert that inserted value is fresh: it's simply not true @@ -798,12 +798,12 @@ mod tests { use ra_db::{fixture::WithFixture, SourceDatabase}; use rustc_hash::FxHashSet; - use crate::{db::DefDatabase2, test_db::TestDB}; + use crate::{db::DefDatabase, test_db::TestDB}; use super::*; fn do_collect_defs( - db: &impl DefDatabase2, + db: &impl DefDatabase, def_map: CrateDefMap, monitor: MacroStackMonitor, ) -> CrateDefMap { diff --git a/crates/ra_hir_def/src/nameres/mod_resolution.rs b/crates/ra_hir_def/src/nameres/mod_resolution.rs index b3b1379d0..14fb8ba3a 100644 --- a/crates/ra_hir_def/src/nameres/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/mod_resolution.rs @@ -3,7 +3,7 @@ use hir_expand::name::Name; use ra_db::{FileId, RelativePathBuf}; use ra_syntax::SmolStr; -use crate::{db::DefDatabase2, HirFileId}; +use crate::{db::DefDatabase, HirFileId}; #[derive(Clone, Debug)] pub(super) struct ModDir { @@ -40,7 +40,7 @@ impl ModDir { pub(super) fn resolve_declaration( &self, - db: &impl DefDatabase2, + db: &impl DefDatabase, file_id: HirFileId, name: &Name, attr_path: Option<&SmolStr>, diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index 95692f826..102009ac7 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs @@ -15,7 +15,7 @@ use ra_db::Edition; use test_utils::tested_by; use crate::{ - db::DefDatabase2, + db::DefDatabase, nameres::{per_ns::PerNs, CrateDefMap}, path::{Path, PathKind}, AdtId, CrateModuleId, EnumVariantId, ModuleDefId, ModuleId, @@ -63,7 +63,7 @@ impl CrateDefMap { // the result. pub(super) fn resolve_path_fp_with_macro( &self, - db: &impl DefDatabase2, + db: &impl DefDatabase, mode: ResolveMode, original_module: CrateModuleId, path: &Path, @@ -216,7 +216,7 @@ impl CrateDefMap { fn resolve_name_in_module( &self, - db: &impl DefDatabase2, + db: &impl DefDatabase, module: CrateModuleId, name: &Name, ) -> PerNs { @@ -243,7 +243,7 @@ impl CrateDefMap { from_crate_root.or(from_extern_prelude) } - fn resolve_in_prelude(&self, db: &impl DefDatabase2, name: &Name) -> PerNs { + fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs { if let Some(prelude) = self.prelude { let keep; let def_map = if prelude.krate == self.krate { diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 55a9634f8..7618cb059 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -18,7 +18,7 @@ use test_utils::tested_by; use crate::{ attr::{Attr, Attrs}, - db::DefDatabase2, + db::DefDatabase, path::Path, FileAstId, HirFileId, ModuleSource, Source, }; @@ -67,14 +67,14 @@ impl ImportSourceMap { impl RawItems { pub(crate) fn raw_items_query( - db: &(impl DefDatabase2 + AstDatabase), + db: &(impl DefDatabase + AstDatabase), file_id: HirFileId, ) -> Arc { db.raw_items_with_source_map(file_id).0 } pub(crate) fn raw_items_with_source_map_query( - db: &(impl DefDatabase2 + AstDatabase), + db: &(impl DefDatabase + AstDatabase), file_id: HirFileId, ) -> (Arc, Arc) { let mut collector = RawItemsCollector { diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index 256f7d4be..b5053ba20 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs @@ -10,7 +10,7 @@ use insta::assert_snapshot; use ra_db::{fixture::WithFixture, SourceDatabase}; use test_utils::covers; -use crate::{db::DefDatabase2, nameres::*, test_db::TestDB, CrateModuleId}; +use crate::{db::DefDatabase, nameres::*, test_db::TestDB, CrateModuleId}; fn def_map(fixtute: &str) -> String { let dm = compute_crate_def_map(fixtute); -- cgit v1.2.3 From dd5c2dc5bf9e9dee863bd79105b1782b654221f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:49:05 +0300 Subject: Move ImportId --- crates/ra_hir_def/src/nameres/collector.rs | 14 +++++++------- crates/ra_hir_def/src/nameres/raw.rs | 20 ++++++++------------ 2 files changed, 15 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 1894b072a..4a671b8f3 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -20,8 +20,8 @@ use crate::{ }, path::{Path, PathKind}, AdtId, AstId, AstItemDef, ConstLoc, ContainerId, CrateModuleId, EnumId, EnumVariantId, - FunctionLoc, ImplId, Intern, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, - StructOrUnionId, TraitId, TypeAliasLoc, UnionId, + FunctionLoc, ImplId, Intern, LocalImportId, LocationCtx, ModuleDefId, ModuleId, StaticId, + StructId, StructOrUnionId, TraitId, TypeAliasLoc, UnionId, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -94,8 +94,8 @@ impl MacroStackMonitor { struct DefCollector<'a, DB> { db: &'a DB, def_map: CrateDefMap, - glob_imports: FxHashMap>, - unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, + glob_imports: FxHashMap>, + unresolved_imports: Vec<(CrateModuleId, LocalImportId, raw::ImportData)>, unexpanded_macros: Vec<(CrateModuleId, AstId, Path)>, mod_dirs: FxHashMap, @@ -293,7 +293,7 @@ where &mut self, module_id: CrateModuleId, def: PerNs, - import_id: raw::ImportId, + import_id: LocalImportId, import: &raw::ImportData, ) { if import.is_glob { @@ -388,7 +388,7 @@ where fn update( &mut self, module_id: CrateModuleId, - import: Option, + import: Option, resolutions: &[(Name, Resolution)], ) { self.update_recursive(module_id, import, resolutions, 0) @@ -397,7 +397,7 @@ where fn update_recursive( &mut self, module_id: CrateModuleId, - import: Option, + import: Option, resolutions: &[(Name, Resolution)], depth: usize, ) { diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 7618cb059..8ee6f54cd 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -20,7 +20,7 @@ use crate::{ attr::{Attr, Attrs}, db::DefDatabase, path::Path, - FileAstId, HirFileId, ModuleSource, Source, + FileAstId, HirFileId, LocalImportId, ModuleSource, Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -30,7 +30,7 @@ use crate::{ #[derive(Debug, Default, PartialEq, Eq)] pub struct RawItems { modules: Arena, - imports: Arena, + imports: Arena, defs: Arena, macros: Arena, impls: Arena, @@ -40,7 +40,7 @@ pub struct RawItems { #[derive(Debug, Default, PartialEq, Eq)] pub struct ImportSourceMap { - map: ArenaMap, + map: ArenaMap, } type ImportSourcePtr = Either, AstPtr>; @@ -51,11 +51,11 @@ fn to_node(ptr: ImportSourcePtr, file: &SourceFile) -> ImportSource { } impl ImportSourceMap { - fn insert(&mut self, import: ImportId, ptr: ImportSourcePtr) { + fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) { self.map.insert(import, ptr) } - pub fn get(&self, source: &ModuleSource, import: ImportId) -> ImportSource { + pub fn get(&self, source: &ModuleSource, import: LocalImportId) -> ImportSource { let file = match source { ModuleSource::SourceFile(file) => file.clone(), ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), @@ -106,9 +106,9 @@ impl Index for RawItems { } } -impl Index for RawItems { +impl Index for RawItems { type Output = ImportData; - fn index(&self, idx: ImportId) -> &ImportData { + fn index(&self, idx: LocalImportId) -> &ImportData { &self.imports[idx] } } @@ -143,7 +143,7 @@ pub(super) struct RawItem { #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(super) enum RawItemKind { Module(Module), - Import(ImportId), + Import(LocalImportId), Def(Def), Macro(Macro), Impl(Impl), @@ -159,10 +159,6 @@ pub(super) enum ModuleData { Definition { name: Name, ast_id: FileAstId, items: Vec }, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct ImportId(RawId); -impl_arena_id!(ImportId); - #[derive(Debug, Clone, PartialEq, Eq)] pub struct ImportData { pub(super) path: Path, -- cgit v1.2.3 From 158b1cb524d8e07aa7a6ec2342bca2ce4667d316 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:49:53 +0300 Subject: Rename CrateModuleId --- crates/ra_hir_def/src/nameres/collector.rs | 40 +++++++++++----------- crates/ra_hir_def/src/nameres/path_resolution.rs | 6 ++-- crates/ra_hir_def/src/nameres/tests.rs | 4 +-- .../ra_hir_def/src/nameres/tests/mod_resolution.rs | 2 +- 4 files changed, 26 insertions(+), 26 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 4a671b8f3..5f7697f63 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -19,9 +19,9 @@ use crate::{ per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode, }, path::{Path, PathKind}, - AdtId, AstId, AstItemDef, ConstLoc, ContainerId, CrateModuleId, EnumId, EnumVariantId, - FunctionLoc, ImplId, Intern, LocalImportId, LocationCtx, ModuleDefId, ModuleId, StaticId, - StructId, StructOrUnionId, TraitId, TypeAliasLoc, UnionId, + AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, + Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, + StructOrUnionId, TraitId, TypeAliasLoc, UnionId, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -94,10 +94,10 @@ impl MacroStackMonitor { struct DefCollector<'a, DB> { db: &'a DB, def_map: CrateDefMap, - glob_imports: FxHashMap>, - unresolved_imports: Vec<(CrateModuleId, LocalImportId, raw::ImportData)>, - unexpanded_macros: Vec<(CrateModuleId, AstId, Path)>, - mod_dirs: FxHashMap, + glob_imports: FxHashMap>, + unresolved_imports: Vec<(LocalModuleId, LocalImportId, raw::ImportData)>, + unexpanded_macros: Vec<(LocalModuleId, AstId, Path)>, + mod_dirs: FxHashMap, /// Some macro use `$tt:tt which mean we have to handle the macro perfectly /// To prevent stack overflow, we add a deep counter here for prevent that. @@ -173,7 +173,7 @@ where /// ``` fn define_macro( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, name: Name, macro_: MacroDefId, export: bool, @@ -200,7 +200,7 @@ where /// the definition of current module. /// And also, `macro_use` on a module will import all legacy macros visable inside to /// current legacy scope, with possible shadowing. - fn define_legacy_macro(&mut self, module_id: CrateModuleId, name: Name, macro_: MacroDefId) { + fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, macro_: MacroDefId) { // Always shadowing self.def_map.modules[module_id].scope.legacy_macros.insert(name, macro_); } @@ -208,7 +208,7 @@ where /// Import macros from `#[macro_use] extern crate`. fn import_macros_from_extern_crate( &mut self, - current_module_id: CrateModuleId, + current_module_id: LocalModuleId, import: &raw::ImportData, ) { log::debug!( @@ -235,7 +235,7 @@ where /// Exported macros are just all macros in the root module scope. /// Note that it contains not only all `#[macro_export]` macros, but also all aliases /// created by `use` in the root module, ignoring the visibility of `use`. - fn import_all_macros_exported(&mut self, current_module_id: CrateModuleId, krate: CrateId) { + fn import_all_macros_exported(&mut self, current_module_id: LocalModuleId, krate: CrateId) { let def_map = self.db.crate_def_map(krate); for (name, def) in def_map[def_map.root].scope.macros() { // `macro_use` only bring things into legacy scope. @@ -265,7 +265,7 @@ where fn resolve_import( &self, - module_id: CrateModuleId, + module_id: LocalModuleId, import: &raw::ImportData, ) -> (PerNs, ReachedFixedPoint) { log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); @@ -291,7 +291,7 @@ where fn record_resolved_import( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, def: PerNs, import_id: LocalImportId, import: &raw::ImportData, @@ -387,7 +387,7 @@ where fn update( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, import: Option, resolutions: &[(Name, Resolution)], ) { @@ -396,7 +396,7 @@ where fn update_recursive( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, import: Option, resolutions: &[(Name, Resolution)], depth: usize, @@ -484,7 +484,7 @@ where fn collect_macro_expansion( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, macro_call_id: MacroCallId, macro_def_id: MacroDefId, ) { @@ -522,7 +522,7 @@ where /// Walks a single module, populating defs, imports and macros struct ModCollector<'a, D> { def_collector: D, - module_id: CrateModuleId, + module_id: LocalModuleId, file_id: HirFileId, raw_items: &'a raw::RawItems, mod_dir: ModDir, @@ -647,7 +647,7 @@ where name: Name, declaration: AstId, definition: Option, - ) -> CrateModuleId { + ) -> LocalModuleId { let modules = &mut self.def_collector.def_map.modules; let res = modules.alloc(ModuleData::default()); modules[res].parent = Some(self.module_id); @@ -772,7 +772,7 @@ where self.def_collector.unexpanded_macros.push((self.module_id, ast_id, path)); } - fn import_all_legacy_macros(&mut self, module_id: CrateModuleId) { + fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) { let macros = self.def_collector.def_map[module_id].scope.legacy_macros.clone(); for (name, macro_) in macros { self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_); @@ -827,7 +827,7 @@ mod tests { let def_map = { let edition = db.crate_graph().edition(krate); - let mut modules: Arena = Arena::default(); + let mut modules: Arena = Arena::default(); let root = modules.alloc(ModuleData::default()); CrateDefMap { krate, diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index 102009ac7..93b441f96 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs @@ -18,7 +18,7 @@ use crate::{ db::DefDatabase, nameres::{per_ns::PerNs, CrateDefMap}, path::{Path, PathKind}, - AdtId, CrateModuleId, EnumVariantId, ModuleDefId, ModuleId, + AdtId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId, }; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -65,7 +65,7 @@ impl CrateDefMap { &self, db: &impl DefDatabase, mode: ResolveMode, - original_module: CrateModuleId, + original_module: LocalModuleId, path: &Path, ) -> ResolvePathResult { let mut segments = path.segments.iter().enumerate(); @@ -217,7 +217,7 @@ impl CrateDefMap { fn resolve_name_in_module( &self, db: &impl DefDatabase, - module: CrateModuleId, + module: LocalModuleId, name: &Name, ) -> PerNs { // Resolve in: diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index b5053ba20..f0b86af7c 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs @@ -10,7 +10,7 @@ use insta::assert_snapshot; use ra_db::{fixture::WithFixture, SourceDatabase}; use test_utils::covers; -use crate::{db::DefDatabase, nameres::*, test_db::TestDB, CrateModuleId}; +use crate::{db::DefDatabase, nameres::*, test_db::TestDB, LocalModuleId}; fn def_map(fixtute: &str) -> String { let dm = compute_crate_def_map(fixtute); @@ -28,7 +28,7 @@ fn render_crate_def_map(map: &CrateDefMap) -> String { go(&mut buf, map, "\ncrate", map.root()); return buf.trim().to_string(); - fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: CrateModuleId) { + fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) { *buf += path; *buf += "\n"; diff --git a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs index eb7b85c07..e11530062 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs @@ -665,7 +665,7 @@ fn unresolved_module_diagnostics() { @r###" [ UnresolvedModule { - module: CrateModuleId( + module: LocalModuleId( 0, ), declaration: AstId { -- cgit v1.2.3 From 6bdd5fa461ba0f3f3697339ffb560c577e3b0cc6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:53:16 +0300 Subject: Privatise nameres --- crates/ra_hir_def/src/nameres/collector.rs | 3 +- crates/ra_hir_def/src/nameres/path_resolution.rs | 3 +- crates/ra_hir_def/src/nameres/per_ns.rs | 74 ------------------------ 3 files changed, 4 insertions(+), 76 deletions(-) delete mode 100644 crates/ra_hir_def/src/nameres/per_ns.rs (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 5f7697f63..b02364e86 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -16,9 +16,10 @@ use crate::{ db::DefDatabase, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, - per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode, + raw, CrateDefMap, ModuleData, Resolution, ResolveMode, }, path::{Path, PathKind}, + per_ns::PerNs, AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, StructOrUnionId, TraitId, TypeAliasLoc, UnionId, diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index 93b441f96..9455f22bb 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs @@ -16,8 +16,9 @@ use test_utils::tested_by; use crate::{ db::DefDatabase, - nameres::{per_ns::PerNs, CrateDefMap}, + nameres::CrateDefMap, path::{Path, PathKind}, + per_ns::PerNs, AdtId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId, }; diff --git a/crates/ra_hir_def/src/nameres/per_ns.rs b/crates/ra_hir_def/src/nameres/per_ns.rs deleted file mode 100644 index 717ed1ef9..000000000 --- a/crates/ra_hir_def/src/nameres/per_ns.rs +++ /dev/null @@ -1,74 +0,0 @@ -//! FIXME: write short doc here - -use hir_expand::MacroDefId; - -use crate::ModuleDefId; - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct PerNs { - pub types: Option, - pub values: Option, - /// Since macros has different type, many methods simply ignore it. - /// We can only use special method like `get_macros` to access it. - pub macros: Option, -} - -impl Default for PerNs { - fn default() -> Self { - PerNs { types: None, values: None, macros: None } - } -} - -impl PerNs { - pub fn none() -> PerNs { - PerNs { types: None, values: None, macros: None } - } - - pub fn values(t: ModuleDefId) -> PerNs { - PerNs { types: None, values: Some(t), macros: None } - } - - pub fn types(t: ModuleDefId) -> PerNs { - PerNs { types: Some(t), values: None, macros: None } - } - - pub fn both(types: ModuleDefId, values: ModuleDefId) -> PerNs { - PerNs { types: Some(types), values: Some(values), macros: None } - } - - pub fn macros(macro_: MacroDefId) -> PerNs { - PerNs { types: None, values: None, macros: Some(macro_) } - } - - pub fn is_none(&self) -> bool { - self.types.is_none() && self.values.is_none() && self.macros.is_none() - } - - pub fn is_all(&self) -> bool { - self.types.is_some() && self.values.is_some() && self.macros.is_some() - } - - pub fn take_types(self) -> Option { - self.types - } - - pub fn take_values(self) -> Option { - self.values - } - - pub fn get_macros(&self) -> Option { - self.macros - } - - pub fn only_macros(&self) -> PerNs { - PerNs { types: None, values: None, macros: self.macros } - } - - pub fn or(self, other: PerNs) -> PerNs { - PerNs { - types: self.types.or(other.types), - values: self.values.or(other.values), - macros: self.macros.or(other.macros), - } - } -} -- cgit v1.2.3 From ffc2325d194d2523456484a7dec1f175c729c1b5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 17:06:04 +0300 Subject: Move ModuleSource back to hir --- crates/ra_hir_def/src/nameres/raw.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 8ee6f54cd..552cbe544 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -12,7 +12,7 @@ use hir_expand::{ use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_syntax::{ ast::{self, AttrsOwner, NameOwner}, - AstNode, AstPtr, SourceFile, + AstNode, AstPtr, }; use test_utils::tested_by; @@ -20,7 +20,7 @@ use crate::{ attr::{Attr, Attrs}, db::DefDatabase, path::Path, - FileAstId, HirFileId, LocalImportId, ModuleSource, Source, + FileAstId, HirFileId, LocalImportId, Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -44,24 +44,14 @@ pub struct ImportSourceMap { } type ImportSourcePtr = Either, AstPtr>; -type ImportSource = Either; - -fn to_node(ptr: ImportSourcePtr, file: &SourceFile) -> ImportSource { - ptr.map(|ptr| ptr.to_node(file.syntax()), |ptr| ptr.to_node(file.syntax())) -} impl ImportSourceMap { fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) { self.map.insert(import, ptr) } - pub fn get(&self, source: &ModuleSource, import: LocalImportId) -> ImportSource { - let file = match source { - ModuleSource::SourceFile(file) => file.clone(), - ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), - }; - - to_node(self.map[import], &file) + pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { + self.map[import].clone() } } -- cgit v1.2.3 From cfffea6dc81f802e32d0312b958f20fa1b1d8425 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 13:57:45 +0300 Subject: Push poison_macros down --- crates/ra_hir_def/src/nameres/collector.rs | 38 +++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index b02364e86..1d004b6a6 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -8,7 +8,7 @@ use hir_expand::{ use ra_cfg::CfgOptions; use ra_db::{CrateId, FileId}; use ra_syntax::{ast, SmolStr}; -use rustc_hash::FxHashMap; +use rustc_hash::{FxHashMap, FxHashSet}; use test_utils::tested_by; use crate::{ @@ -57,6 +57,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C unexpanded_macros: Vec::new(), mod_dirs: FxHashMap::default(), macro_stack_monitor: MacroStackMonitor::default(), + poison_macros: FxHashSet::default(), cfg_options, }; collector.collect(); @@ -103,6 +104,17 @@ struct DefCollector<'a, DB> { /// Some macro use `$tt:tt which mean we have to handle the macro perfectly /// To prevent stack overflow, we add a deep counter here for prevent that. macro_stack_monitor: MacroStackMonitor, + /// Some macros are not well-behavior, which leads to infinite loop + /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } } + /// We mark it down and skip it in collector + /// + /// FIXME: + /// Right now it only handle a poison macro in a single crate, + /// such that if other crate try to call that macro, + /// the whole process will do again until it became poisoned in that crate. + /// We should handle this macro set globally + /// However, do we want to put it as a global variable? + poison_macros: FxHashSet, cfg_options: &'a CfgOptions, } @@ -489,7 +501,7 @@ where macro_call_id: MacroCallId, macro_def_id: MacroDefId, ) { - if self.def_map.poison_macros.contains(¯o_def_id) { + if self.poison_macros.contains(¯o_def_id) { return; } @@ -509,7 +521,7 @@ where .collect(raw_items.items()); } else { log::error!("Too deep macro expansion: {:?}", macro_call_id); - self.def_map.poison_macros.insert(macro_def_id); + self.poison_macros.insert(macro_def_id); } self.macro_stack_monitor.decrease(macro_def_id); @@ -807,7 +819,7 @@ mod tests { db: &impl DefDatabase, def_map: CrateDefMap, monitor: MacroStackMonitor, - ) -> CrateDefMap { + ) -> (CrateDefMap, FxHashSet) { let mut collector = DefCollector { db, def_map, @@ -816,13 +828,18 @@ mod tests { unexpanded_macros: Vec::new(), mod_dirs: FxHashMap::default(), macro_stack_monitor: monitor, + poison_macros: FxHashSet::default(), cfg_options: &CfgOptions::default(), }; collector.collect(); - collector.finish() + (collector.def_map, collector.poison_macros) } - fn do_limited_resolve(code: &str, limit: u32, poison_limit: u32) -> CrateDefMap { + fn do_limited_resolve( + code: &str, + limit: u32, + poison_limit: u32, + ) -> (CrateDefMap, FxHashSet) { let (db, _file_id) = TestDB::with_single_file(&code); let krate = db.test_crate(); @@ -837,7 +854,6 @@ mod tests { prelude: None, root, modules, - poison_macros: FxHashSet::default(), diagnostics: Vec::new(), } }; @@ -867,7 +883,7 @@ foo!(KABOOM); #[test] fn test_macro_expand_poisoned() { - let def = do_limited_resolve( + let (_, poison_macros) = do_limited_resolve( r#" macro_rules! foo { ($ty:ty) => { foo!($ty); } @@ -878,12 +894,12 @@ foo!(KABOOM); 16, ); - assert_eq!(def.poison_macros.len(), 1); + assert_eq!(poison_macros.len(), 1); } #[test] fn test_macro_expand_normal() { - let def = do_limited_resolve( + let (_, poison_macros) = do_limited_resolve( r#" macro_rules! foo { ($ident:ident) => { struct $ident {} } @@ -894,6 +910,6 @@ foo!(Bar); 16, ); - assert_eq!(def.poison_macros.len(), 0); + assert_eq!(poison_macros.len(), 0); } } -- cgit v1.2.3 From e0b06cb672b7aae770fea24e4a5efdbec8cbf5c6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:13:56 +0300 Subject: Switch to StaticLoc for statics --- crates/ra_hir_def/src/nameres/collector.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 1d004b6a6..7b2487999 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -21,7 +21,7 @@ use crate::{ path::{Path, PathKind}, per_ns::PerNs, AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, - Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, + Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, StructOrUnionId, TraitId, TypeAliasLoc, UnionId, }; @@ -715,7 +715,10 @@ where PerNs::values(def.into()) } raw::DefKind::Static(ast_id) => { - PerNs::values(StaticId::from_ast_id(ctx, ast_id).into()) + let def = StaticLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db); + + PerNs::values(def.into()) } raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()), raw::DefKind::TypeAlias(ast_id) => { -- cgit v1.2.3 From 8e36cb586038e2c12e6eceae57f7a95684fc6c6d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:28:45 +0300 Subject: Simplify --- crates/ra_hir_def/src/nameres/collector.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 7b2487999..15941a1cb 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -7,7 +7,7 @@ use hir_expand::{ }; use ra_cfg::CfgOptions; use ra_db::{CrateId, FileId}; -use ra_syntax::{ast, SmolStr}; +use ra_syntax::ast; use rustc_hash::{FxHashMap, FxHashSet}; use test_utils::tested_by; @@ -599,7 +599,7 @@ where } fn collect_module(&mut self, module: &raw::ModuleData, attrs: &Attrs) { - let path_attr = self.path_attr(attrs); + let path_attr = attrs.find_string_value("path"); let is_macro_use = attrs.has_atom("macro_use"); match module { // inline module, just recurse @@ -612,7 +612,7 @@ where module_id, file_id: self.file_id, raw_items: self.raw_items, - mod_dir: self.mod_dir.descend_into_definition(name, path_attr), + mod_dir: self.mod_dir.descend_into_definition(name, path_attr.as_ref()), } .collect(&*items); if is_macro_use { @@ -626,7 +626,7 @@ where self.def_collector.db, self.file_id, name, - path_attr, + path_attr.as_ref(), ) { Ok((file_id, mod_dir)) => { let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id)); @@ -798,10 +798,6 @@ where fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) } - - fn path_attr<'a>(&self, attrs: &'a Attrs) -> Option<&'a SmolStr> { - attrs.iter().find_map(|attr| attr.as_path()) - } } fn is_macro_rules(path: &Path) -> bool { -- cgit v1.2.3 From 1956d57ed4896bb29dfcfaed2a5291ec69251f52 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 15:50:45 +0300 Subject: Slightly reduce code duplication --- crates/ra_hir_def/src/nameres/raw.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 552cbe544..198578753 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -17,10 +17,7 @@ use ra_syntax::{ use test_utils::tested_by; use crate::{ - attr::{Attr, Attrs}, - db::DefDatabase, - path::Path, - FileAstId, HirFileId, LocalImportId, Source, + attr::Attrs, db::DefDatabase, path::Path, FileAstId, HirFileId, LocalImportId, Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -407,6 +404,6 @@ impl RawItemsCollector { } fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { - Attr::from_attrs_owner(item, &self.hygiene) + Attrs::new(item, &self.hygiene) } } -- cgit v1.2.3 From 4b74fb1d896ce5a1c8c4c4bf73ad2940fb86abc5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 16:03:02 +0300 Subject: Nicer API for attrs --- crates/ra_hir_def/src/nameres/collector.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 15941a1cb..7a5f90327 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -599,8 +599,8 @@ where } fn collect_module(&mut self, module: &raw::ModuleData, attrs: &Attrs) { - let path_attr = attrs.find_string_value("path"); - let is_macro_use = attrs.has_atom("macro_use"); + let path_attr = attrs.by_key("path").string_value(); + let is_macro_use = attrs.by_key("macro_use").exists(); match module { // inline module, just recurse raw::ModuleData::Definition { name, items, ast_id } => { @@ -612,7 +612,7 @@ where module_id, file_id: self.file_id, raw_items: self.raw_items, - mod_dir: self.mod_dir.descend_into_definition(name, path_attr.as_ref()), + mod_dir: self.mod_dir.descend_into_definition(name, path_attr), } .collect(&*items); if is_macro_use { @@ -626,7 +626,7 @@ where self.def_collector.db, self.file_id, name, - path_attr.as_ref(), + path_attr, ) { Ok((file_id, mod_dir)) => { let module_id = self.push_child_module(name.clone(), ast_id, Some(file_id)); @@ -796,7 +796,11 @@ where } fn is_cfg_enabled(&self, attrs: &Attrs) -> bool { - attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) + // FIXME: handle cfg_attr :-) + attrs + .by_key("cfg") + .tt_values() + .all(|tt| self.def_collector.cfg_options.is_cfg_enabled(tt) != Some(false)) } } -- cgit v1.2.3 From 99af523b68d3056c0ee355821b9b8f3c6fb5f504 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 17:00:10 +0300 Subject: Cleanup --- crates/ra_hir_def/src/nameres/collector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 7a5f90327..df01a20e1 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -476,7 +476,7 @@ where path, ); - if let Some(def) = resolved_res.resolved_def.get_macros() { + if let Some(def) = resolved_res.resolved_def.take_macros() { let call_id = self.db.intern_macro(MacroCallLoc { def, ast_id: *ast_id }); resolved.push((*module_id, call_id, def)); res = ReachedFixedPoint::No; -- cgit v1.2.3 From 21cfa6d529babf868f897b943d67561ea752b9e5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 17:34:36 +0300 Subject: Some docs --- crates/ra_hir_def/src/nameres/raw.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 198578753..2ec84f2cc 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -1,4 +1,9 @@ -//! FIXME: write short doc here +//! Lowers syntax tree of a rust file into a raw representation of containing +//! items, *without* attaching them to a module structure. +//! +//! That is, raw items don't have semantics, just as syntax, but, unlike syntax, +//! they don't change with trivial source code edits, making them a great tool +//! for building salsa recomputation firewalls. use std::{ops::Index, sync::Arc}; -- cgit v1.2.3 From 855a629b1436e7d3f2a9ae47d242d69d1fd7327e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 17:49:49 +0300 Subject: Use Trace in raw_items --- crates/ra_hir_def/src/nameres/raw.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 2ec84f2cc..401af031c 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -22,7 +22,8 @@ use ra_syntax::{ use test_utils::tested_by; use crate::{ - attr::Attrs, db::DefDatabase, path::Path, FileAstId, HirFileId, LocalImportId, Source, + attr::Attrs, db::DefDatabase, path::Path, trace::Trace, FileAstId, HirFileId, LocalImportId, + Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -48,10 +49,6 @@ pub struct ImportSourceMap { type ImportSourcePtr = Either, AstPtr>; impl ImportSourceMap { - fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) { - self.map.insert(import, ptr) - } - pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { self.map[import].clone() } @@ -72,7 +69,7 @@ impl RawItems { let mut collector = RawItemsCollector { raw_items: RawItems::default(), source_ast_id_map: db.ast_id_map(file_id), - source_map: ImportSourceMap::default(), + imports: Trace::new(), file_id, hygiene: Hygiene::new(db, file_id), }; @@ -83,7 +80,11 @@ impl RawItems { collector.process_module(None, item_list); } } - (Arc::new(collector.raw_items), Arc::new(collector.source_map)) + let mut raw_items = collector.raw_items; + let (arena, map) = collector.imports.into_arena_and_map(); + raw_items.imports = arena; + let source_map = ImportSourceMap { map }; + (Arc::new(raw_items), Arc::new(source_map)) } pub(super) fn items(&self) -> &[RawItem] { @@ -207,8 +208,8 @@ pub(super) struct ImplData { struct RawItemsCollector { raw_items: RawItems, + imports: Trace, source_ast_id_map: Arc, - source_map: ImportSourceMap, file_id: HirFileId, hygiene: Hygiene, } @@ -392,8 +393,7 @@ impl RawItemsCollector { data: ImportData, source: ImportSourcePtr, ) { - let import = self.raw_items.imports.alloc(data); - self.source_map.insert(import, source); + let import = self.imports.alloc(|| source, || data); self.push_item(current_module, attrs, RawItemKind::Import(import)) } -- cgit v1.2.3 From f5e0a31eaf9ddd7788e6261d49f4d18e8463a719 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 18:05:12 +0300 Subject: Cleanup nameres --- crates/ra_hir_def/src/nameres/collector.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index df01a20e1..41becf8df 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -1,4 +1,7 @@ -//! FIXME: write short doc here +//! The core of the module-level name resolution algorithm. +//! +//! `DefCollector::collect` contains the fixed-point iteration loop which +//! resolves imports and expands macros. use hir_expand::{ builtin_macro::find_builtin_macro, -- cgit v1.2.3 From 434f108adad75b7c5e25db745a9f9fefa5cdaa31 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 18:48:29 +0300 Subject: Simplify --- crates/ra_hir_def/src/nameres/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index f0b86af7c..f502f1cb3 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs @@ -25,7 +25,7 @@ fn compute_crate_def_map(fixture: &str) -> Arc { fn render_crate_def_map(map: &CrateDefMap) -> String { let mut buf = String::new(); - go(&mut buf, map, "\ncrate", map.root()); + go(&mut buf, map, "\ncrate", map.root); return buf.trim().to_string(); fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) { -- cgit v1.2.3