From e24de2007fe1850bb5c6c289ea48f7cb4424bb0a Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 12 Jun 2020 23:24:26 +0200 Subject: collector changes WIP --- crates/ra_hir_def/src/nameres/collector.rs | 50 ++++++++++++++---------------- crates/ra_hir_def/src/nameres/raw.rs | 1 + 2 files changed, 25 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 cbce04315..034f27410 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -19,9 +19,10 @@ use test_utils::mark; use crate::{ attr::Attrs, db::DefDatabase, + item_tree::{Import, ItemTree, Mod, ModItem}, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, - raw, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, + BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, }, path::{ImportAlias, ModPath, PathKind}, per_ns::PerNs, @@ -30,6 +31,7 @@ use crate::{ FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, }; +use ra_arena::Idx; pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { let crate_graph = db.crate_graph(); @@ -104,8 +106,8 @@ impl PartialResolvedImport { #[derive(Clone, Debug, Eq, PartialEq)] struct ImportDirective { module_id: LocalModuleId, - import_id: raw::Import, - import: raw::ImportData, + import_id: Idx, + import: Import, status: PartialResolvedImport, } @@ -140,7 +142,7 @@ struct DefCollector<'a> { impl DefCollector<'_> { fn collect(&mut self) { let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id; - let raw_items = self.db.raw_items(file_id.into()); + let item_tree = self.db.item_tree(file_id.into()); let module_id = self.def_map.root; self.def_map.modules[module_id].origin = ModuleOrigin::CrateRoot { definition: file_id }; ModCollector { @@ -148,10 +150,10 @@ impl DefCollector<'_> { macro_depth: 0, module_id, file_id: file_id.into(), - raw_items: &raw_items, + item_tree: &item_tree, mod_dir: ModDir::root(), } - .collect(raw_items.items()); + .collect(item_tree.top_level_items()); // main name resolution fixed-point loop. let mut i = 0; @@ -286,7 +288,7 @@ impl DefCollector<'_> { fn import_macros_from_extern_crate( &mut self, current_module_id: LocalModuleId, - import: &raw::ImportData, + import: &Import, ) { log::debug!( "importing macros from extern crate: {:?} ({:?})", @@ -352,11 +354,7 @@ impl DefCollector<'_> { } } - fn resolve_import( - &self, - module_id: LocalModuleId, - import: &raw::ImportData, - ) -> PartialResolvedImport { + fn resolve_import(&self, module_id: LocalModuleId, import: &Import) -> PartialResolvedImport { log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); if import.is_extern_crate { let res = self.def_map.resolve_name_in_extern_prelude( @@ -649,17 +647,17 @@ impl DefCollector<'_> { depth: usize, ) { let file_id: HirFileId = macro_call_id.as_file(); - let raw_items = self.db.raw_items(file_id); + let item_tree = self.db.item_tree(file_id); let mod_dir = self.mod_dirs[&module_id].clone(); ModCollector { def_collector: &mut *self, macro_depth: depth, file_id, module_id, - raw_items: &raw_items, + item_tree: &item_tree, mod_dir, } - .collect(raw_items.items()); + .collect(item_tree.top_level_items()); } fn finish(self) -> CrateDefMap { @@ -673,12 +671,12 @@ struct ModCollector<'a, 'b> { macro_depth: usize, module_id: LocalModuleId, file_id: HirFileId, - raw_items: &'a raw::RawItems, + item_tree: &'a ItemTree, mod_dir: ModDir, } impl ModCollector<'_, '_> { - fn collect(&mut self, items: &[raw::RawItem]) { + fn collect(&mut self, items: &[ModItem]) { // Note: don't assert that inserted value is fresh: it's simply not true // for macros. self.def_collector.mod_dirs.insert(self.module_id, self.mod_dir.clone()); @@ -697,7 +695,7 @@ impl ModCollector<'_, '_> { for item in items { if self.is_cfg_enabled(&item.attrs) { if let raw::RawItemKind::Import(import_id) = item.kind { - let import = self.raw_items[import_id].clone(); + let import = self.item_tree[import_id].clone(); if import.is_extern_crate && import.is_macro_use { self.def_collector.import_macros_from_extern_crate(self.module_id, &import); } @@ -709,27 +707,27 @@ impl ModCollector<'_, '_> { 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.item_tree[m], &item.attrs) } raw::RawItemKind::Import(import_id) => { self.def_collector.unresolved_imports.push(ImportDirective { module_id: self.module_id, import_id, - import: self.raw_items[import_id].clone(), + import: self.item_tree[import_id].clone(), status: PartialResolvedImport::Unresolved, }) } raw::RawItemKind::Def(def) => { - self.define_def(&self.raw_items[def], &item.attrs) + self.define_def(&self.item_tree[def], &item.attrs) } - raw::RawItemKind::Macro(mac) => self.collect_macro(&self.raw_items[mac]), + raw::RawItemKind::Macro(mac) => self.collect_macro(&self.item_tree[mac]), raw::RawItemKind::Impl(imp) => { let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id, }; let container = ContainerId::ModuleId(module); - let ast_id = self.raw_items[imp].ast_id; + let ast_id = self.item_tree[imp].ast_id; let impl_id = ImplLoc { container, ast_id: AstId::new(self.file_id, ast_id) } .intern(self.def_collector.db); @@ -742,7 +740,7 @@ impl ModCollector<'_, '_> { } } - fn collect_module(&mut self, module: &raw::ModuleData, attrs: &Attrs) { + fn collect_module(&mut self, module: &Mod, attrs: &Attrs) { let path_attr = attrs.by_key("path").string_value(); let is_macro_use = attrs.by_key("macro_use").exists(); match module { @@ -760,7 +758,7 @@ impl ModCollector<'_, '_> { macro_depth: self.macro_depth, module_id, file_id: self.file_id, - raw_items: self.raw_items, + item_tree: self.item_tree, mod_dir: self.mod_dir.descend_into_definition(name, path_attr), } .collect(&*items); @@ -790,7 +788,7 @@ impl ModCollector<'_, '_> { macro_depth: self.macro_depth, module_id, file_id: file_id.into(), - raw_items: &raw_items, + item_tree: &raw_items, mod_dir, } .collect(raw_items.items()); diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index f44baa579..d83a5b2b5 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -46,6 +46,7 @@ pub struct RawItems { impl RawItems { pub(crate) fn raw_items_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc { let _p = profile("raw_items_query"); + db.item_tree(file_id); let mut collector = RawItemsCollector { raw_items: RawItems::default(), source_ast_id_map: db.ast_id_map(file_id), -- cgit v1.2.3 From 9d244129296a1805f890640016719afcee66a320 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 15 Jun 2020 19:16:14 +0200 Subject: Move collector --- crates/ra_hir_def/src/nameres/collector.rs | 301 ++++++++++++++++++----------- 1 file changed, 193 insertions(+), 108 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 034f27410..b899a5fb3 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -4,6 +4,7 @@ //! resolves imports and expands macros. use hir_expand::{ + ast_id_map::FileAstId, builtin_derive::find_builtin_derive, builtin_macro::find_builtin_macro, name::{name, AsName, Name}, @@ -19,14 +20,14 @@ use test_utils::mark; use crate::{ attr::Attrs, db::DefDatabase, - item_tree::{Import, ItemTree, Mod, ModItem}, + item_tree::{Import, ItemTree, MacroCall, Mod, ModItem, ModKind, StructDefKind}, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, }, path::{ImportAlias, ModPath, PathKind}, per_ns::PerNs, - visibility::Visibility, + visibility::{RawVisibility, Visibility}, AdtId, AsMacroCall, AstId, AstIdWithPath, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, @@ -125,6 +126,13 @@ struct DeriveDirective { ast_id: AstIdWithPath, } +struct DefData<'a> { + id: ModuleDefId, + name: &'a Name, + visibility: &'a RawVisibility, + has_constructor: bool, +} + /// Walks the tree of module recursively struct DefCollector<'a> { db: &'a dyn DefDatabase, @@ -693,9 +701,9 @@ impl ModCollector<'_, '_> { // `#[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 let raw::RawItemKind::Import(import_id) = item.kind { - let import = self.item_tree[import_id].clone(); + if self.is_cfg_enabled(self.item_tree.attrs(*item)) { + if let ModItem::Import(import_id) = item { + let import = self.item_tree[*import_id].clone(); if import.is_extern_crate && import.is_macro_use { self.def_collector.import_macros_from_extern_crate(self.module_id, &import); } @@ -703,13 +711,17 @@ impl ModCollector<'_, '_> { } } - for item in items { - if self.is_cfg_enabled(&item.attrs) { - match item.kind { - raw::RawItemKind::Module(m) => { - self.collect_module(&self.item_tree[m], &item.attrs) - } - raw::RawItemKind::Import(import_id) => { + for &item in items { + let attrs = self.item_tree.attrs(item); + if self.is_cfg_enabled(attrs) { + let module = + ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; + let container = ContainerId::ModuleId(module); + + let mut def = None; + match item { + ModItem::Mod(m) => self.collect_module(&self.item_tree[m], attrs), + ModItem::Import(import_id) => { self.def_collector.unresolved_imports.push(ImportDirective { module_id: self.module_id, import_id, @@ -717,11 +729,8 @@ impl ModCollector<'_, '_> { status: PartialResolvedImport::Unresolved, }) } - raw::RawItemKind::Def(def) => { - self.define_def(&self.item_tree[def], &item.attrs) - } - raw::RawItemKind::Macro(mac) => self.collect_macro(&self.item_tree[mac]), - raw::RawItemKind::Impl(imp) => { + ModItem::MacroCall(mac) => self.collect_macro(&self.item_tree[mac]), + ModItem::Impl(imp) => { let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id, @@ -735,6 +744,147 @@ impl ModCollector<'_, '_> { .scope .define_impl(impl_id) } + ModItem::Function(it) => { + let it = &self.item_tree[it]; + def = Some(DefData { + id: FunctionLoc { + container: container.into(), + ast_id: AstId::new(self.file_id, it.ast_id), + } + .intern(self.def_collector.db) + .into(), + name: &it.name, + visibility: &it.visibility, + has_constructor: false, + }); + } + ModItem::Struct(it) => { + let it = &self.item_tree[it]; + + // FIXME: check attrs to see if this is an attribute macro invocation; + // in which case we don't add the invocation, just a single attribute + // macro invocation + self.collect_derives(attrs, it.ast_id.upcast()); + + def = Some(DefData { + id: StructLoc { + container, + ast_id: AstId::new(self.file_id, it.ast_id), + } + .intern(self.def_collector.db) + .into(), + name: &it.name, + visibility: &it.visibility, + has_constructor: it.kind != StructDefKind::Record, + }); + } + ModItem::Union(it) => { + let it = &self.item_tree[it]; + + // FIXME: check attrs to see if this is an attribute macro invocation; + // in which case we don't add the invocation, just a single attribute + // macro invocation + self.collect_derives(attrs, it.ast_id.upcast()); + + def = Some(DefData { + id: UnionLoc { container, ast_id: AstId::new(self.file_id, it.ast_id) } + .intern(self.def_collector.db) + .into(), + name: &it.name, + visibility: &it.visibility, + has_constructor: false, + }); + } + ModItem::Enum(it) => { + let it = &self.item_tree[it]; + + // FIXME: check attrs to see if this is an attribute macro invocation; + // in which case we don't add the invocation, just a single attribute + // macro invocation + self.collect_derives(attrs, it.ast_id.upcast()); + + def = Some(DefData { + id: EnumLoc { container, ast_id: AstId::new(self.file_id, it.ast_id) } + .intern(self.def_collector.db) + .into(), + name: &it.name, + visibility: &it.visibility, + has_constructor: false, + }); + } + ModItem::Const(it) => { + let it = &self.item_tree[it]; + + if let Some(name) = &it.name { + def = Some(DefData { + id: ConstLoc { + container: container.into(), + ast_id: AstId::new(self.file_id, it.ast_id), + } + .intern(self.def_collector.db) + .into(), + name, + visibility: &it.visibility, + has_constructor: false, + }); + } + } + ModItem::Static(it) => { + let it = &self.item_tree[it]; + + def = Some(DefData { + id: StaticLoc { + container, + ast_id: AstId::new(self.file_id, it.ast_id), + } + .intern(self.def_collector.db) + .into(), + name: &it.name, + visibility: &it.visibility, + has_constructor: false, + }); + } + ModItem::Trait(it) => { + let it = &self.item_tree[it]; + + def = Some(DefData { + id: TraitLoc { container, ast_id: AstId::new(self.file_id, it.ast_id) } + .intern(self.def_collector.db) + .into(), + name: &it.name, + visibility: &it.visibility, + has_constructor: false, + }); + } + ModItem::TypeAlias(it) => { + let it = &self.item_tree[it]; + + def = Some(DefData { + id: TypeAliasLoc { + container: container.into(), + ast_id: AstId::new(self.file_id, it.ast_id), + } + .intern(self.def_collector.db) + .into(), + name: &it.name, + visibility: &it.visibility, + has_constructor: false, + }); + } + } + + if let Some(DefData { id, name, visibility, has_constructor }) = def { + self.def_collector.def_map.modules[self.module_id].scope.define_def(id); + let vis = self + .def_collector + .def_map + .resolve_visibility(self.def_collector.db, self.module_id, visibility) + .unwrap_or(Visibility::Public); + self.def_collector.update( + self.module_id, + &[(name.clone(), PerNs::from_def(id, vis, has_constructor))], + vis, + ) } } } @@ -743,14 +893,14 @@ impl ModCollector<'_, '_> { fn collect_module(&mut self, module: &Mod, attrs: &Attrs) { let path_attr = attrs.by_key("path").string_value(); let is_macro_use = attrs.by_key("macro_use").exists(); - match module { + match &module.kind { // inline module, just recurse - raw::ModuleData::Definition { name, visibility, items, ast_id } => { + ModKind::Inline { items } => { let module_id = self.push_child_module( - name.clone(), - AstId::new(self.file_id, *ast_id), + module.name.clone(), + AstId::new(self.file_id, module.ast_id), None, - &visibility, + &module.visibility, ); ModCollector { @@ -759,7 +909,7 @@ impl ModCollector<'_, '_> { module_id, file_id: self.file_id, item_tree: self.item_tree, - mod_dir: self.mod_dir.descend_into_definition(name, path_attr), + mod_dir: self.mod_dir.descend_into_definition(&module.name, path_attr), } .collect(&*items); if is_macro_use { @@ -767,31 +917,31 @@ impl ModCollector<'_, '_> { } } // out of line module, resolve, parse and recurse - raw::ModuleData::Declaration { name, visibility, ast_id } => { - let ast_id = AstId::new(self.file_id, *ast_id); + ModKind::Outline {} => { + let ast_id = AstId::new(self.file_id, module.ast_id); match self.mod_dir.resolve_declaration( self.def_collector.db, self.file_id, - name, + &module.name, path_attr, ) { Ok((file_id, is_mod_rs, mod_dir)) => { let module_id = self.push_child_module( - name.clone(), + module.name.clone(), ast_id, Some((file_id, is_mod_rs)), - &visibility, + &module.visibility, ); - let raw_items = self.def_collector.db.raw_items(file_id.into()); + let item_tree = self.def_collector.db.item_tree(file_id.into()); ModCollector { def_collector: &mut *self.def_collector, macro_depth: self.macro_depth, module_id, file_id: file_id.into(), - item_tree: &raw_items, + item_tree: &item_tree, mod_dir, } - .collect(raw_items.items()); + .collect(item_tree.top_level_items()); if is_macro_use { self.import_all_legacy_macros(module_id); } @@ -840,77 +990,7 @@ impl ModCollector<'_, '_> { res } - fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) { - let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; - // FIXME: check attrs to see if this is an attribute macro invocation; - // in which case we don't add the invocation, just a single attribute - // macro invocation - self.collect_derives(attrs, def); - - let name = def.name.clone(); - let container = ContainerId::ModuleId(module); - let vis = &def.visibility; - let mut has_constructor = false; - - let def: ModuleDefId = match def.kind { - raw::DefKind::Function(ast_id) => FunctionLoc { - container: container.into(), - ast_id: AstId::new(self.file_id, ast_id), - } - .intern(self.def_collector.db) - .into(), - raw::DefKind::Struct(ast_id, mode) => { - has_constructor = mode != raw::StructDefKind::Record; - StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db) - .into() - } - raw::DefKind::Union(ast_id) => { - UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db) - .into() - } - raw::DefKind::Enum(ast_id) => { - EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db) - .into() - } - raw::DefKind::Const(ast_id) => { - ConstLoc { container: container.into(), ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db) - .into() - } - raw::DefKind::Static(ast_id) => { - StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db) - .into() - } - raw::DefKind::Trait(ast_id) => { - TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db) - .into() - } - raw::DefKind::TypeAlias(ast_id) => TypeAliasLoc { - container: container.into(), - ast_id: AstId::new(self.file_id, ast_id), - } - .intern(self.def_collector.db) - .into(), - }; - self.def_collector.def_map.modules[self.module_id].scope.define_def(def); - let vis = self - .def_collector - .def_map - .resolve_visibility(self.def_collector.db, self.module_id, vis) - .unwrap_or(Visibility::Public); - self.def_collector.update( - self.module_id, - &[(name, PerNs::from_def(def, vis, has_constructor))], - vis, - ) - } - - fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { + fn collect_derives(&mut self, attrs: &Attrs, ast_id: FileAstId) { for derive_subtree in attrs.by_key("derive").tt_values() { // for #[derive(Copy, Clone)], `derive_subtree` is the `(Copy, Clone)` subtree for tt in &derive_subtree.token_trees { @@ -921,7 +1001,7 @@ impl ModCollector<'_, '_> { }; let path = ModPath::from_tt_ident(ident); - let ast_id = AstIdWithPath::new(self.file_id, def.kind.ast_id(), path); + let ast_id = AstIdWithPath::new(self.file_id, ast_id, path); self.def_collector .unexpanded_attribute_macros .push(DeriveDirective { module_id: self.module_id, ast_id }); @@ -929,11 +1009,11 @@ impl ModCollector<'_, '_> { } } - fn collect_macro(&mut self, mac: &raw::MacroData) { + fn collect_macro(&mut self, mac: &MacroCall) { let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone()); // Case 0: builtin macros - if mac.builtin { + if mac.is_builtin { if let Some(name) = &mac.name { let krate = self.def_collector.def_map.krate; if let Some(macro_id) = find_builtin_macro(name, krate, ast_id.ast_id) { @@ -941,7 +1021,7 @@ impl ModCollector<'_, '_> { self.module_id, name.clone(), macro_id, - mac.export, + mac.is_export, ); return; } @@ -955,9 +1035,14 @@ impl ModCollector<'_, '_> { ast_id: Some(ast_id.ast_id), krate: Some(self.def_collector.def_map.krate), kind: MacroDefKind::Declarative, - local_inner: mac.local_inner, + local_inner: mac.is_local_inner, }; - self.def_collector.define_macro(self.module_id, name.clone(), macro_id, mac.export); + self.def_collector.define_macro( + self.module_id, + name.clone(), + macro_id, + mac.is_export, + ); } return; } -- cgit v1.2.3 From 7054e89d187287c0547ef43961bf4969aba57dd6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 15 Jun 2020 19:16:29 +0200 Subject: Fix test --- crates/ra_hir_def/src/nameres/tests/mod_resolution.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/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs index b43b294ca..d42933eed 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs @@ -20,8 +20,11 @@ fn name_res_works_for_broken_modules() { ", ); assert_snapshot!(map, @r###" - ⋮crate - ⋮Baz: _ +crate +Baz: _ +foo: t + +crate::foo "###); } -- cgit v1.2.3 From 0e2602f75e17b35472637c8ebd8e6e436a5f2af4 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 15 Jun 2020 19:16:43 +0200 Subject: Remove raw item query --- crates/ra_hir_def/src/nameres/raw.rs | 483 ----------------------------------- 1 file changed, 483 deletions(-) delete mode 100644 crates/ra_hir_def/src/nameres/raw.rs (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 deleted file mode 100644 index d83a5b2b5..000000000 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ /dev/null @@ -1,483 +0,0 @@ -//! 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}; - -use hir_expand::{ - ast_id_map::AstIdMap, - hygiene::Hygiene, - name::{AsName, Name}, -}; -use ra_arena::{Arena, Idx}; -use ra_prof::profile; -use ra_syntax::{ - ast::{self, AttrsOwner, NameOwner, VisibilityOwner}, - AstNode, -}; -use test_utils::mark; - -use crate::{ - attr::Attrs, - db::DefDatabase, - path::{ImportAlias, ModPath}, - visibility::RawVisibility, - FileAstId, HirFileId, InFile, -}; - -/// `RawItems` is a set of top-level items in a file (except for impls). -/// -/// It is the input to name resolution algorithm. `RawItems` are not invalidated -/// on most edits. -#[derive(Debug, Default, PartialEq, Eq)] -pub struct RawItems { - modules: Arena, - imports: Arena, - defs: Arena, - macros: Arena, - impls: Arena, - /// items for top-level module - items: Vec, -} - -impl RawItems { - pub(crate) fn raw_items_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc { - let _p = profile("raw_items_query"); - db.item_tree(file_id); - let mut collector = RawItemsCollector { - raw_items: RawItems::default(), - source_ast_id_map: db.ast_id_map(file_id), - file_id, - hygiene: Hygiene::new(db.upcast(), file_id), - }; - if let Some(node) = db.parse_or_expand(file_id) { - if let Some(source_file) = ast::SourceFile::cast(node.clone()) { - collector.process_module(None, source_file); - } else if let Some(item_list) = ast::MacroItems::cast(node) { - collector.process_module(None, item_list); - } - } - let raw_items = collector.raw_items; - Arc::new(raw_items) - } - - pub(super) fn items(&self) -> &[RawItem] { - &self.items - } -} - -impl Index> for RawItems { - type Output = ModuleData; - fn index(&self, idx: Idx) -> &ModuleData { - &self.modules[idx] - } -} - -impl Index for RawItems { - type Output = ImportData; - fn index(&self, idx: Import) -> &ImportData { - &self.imports[idx] - } -} - -impl Index> for RawItems { - type Output = DefData; - fn index(&self, idx: Idx) -> &DefData { - &self.defs[idx] - } -} - -impl Index> for RawItems { - type Output = MacroData; - fn index(&self, idx: Idx) -> &MacroData { - &self.macros[idx] - } -} - -impl Index> for RawItems { - type Output = ImplData; - fn index(&self, idx: Idx) -> &ImplData { - &self.impls[idx] - } -} - -#[derive(Debug, PartialEq, Eq, Clone)] -pub(super) struct RawItem { - pub(super) attrs: Attrs, - pub(super) kind: RawItemKind, -} - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub(super) enum RawItemKind { - Module(Idx), - Import(Import), - Def(Idx), - Macro(Idx), - Impl(Idx), -} - -#[derive(Debug, PartialEq, Eq)] -pub(super) enum ModuleData { - Declaration { - name: Name, - visibility: RawVisibility, - ast_id: FileAstId, - }, - Definition { - name: Name, - visibility: RawVisibility, - ast_id: FileAstId, - items: Vec, - }, -} - -pub(crate) type Import = Idx; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ImportData { - pub(super) path: ModPath, - pub(super) alias: Option, - pub(super) is_glob: bool, - pub(super) is_prelude: bool, - pub(super) is_extern_crate: bool, - pub(super) is_macro_use: bool, - pub(super) visibility: RawVisibility, -} - -// type Def = Idx; - -#[derive(Debug, PartialEq, Eq)] -pub(super) struct DefData { - pub(super) name: Name, - pub(super) kind: DefKind, - pub(super) visibility: RawVisibility, -} - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub(super) enum StructDefKind { - Record, - Tuple, - Unit, -} - -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -pub(super) enum DefKind { - Function(FileAstId), - Struct(FileAstId, StructDefKind), - Union(FileAstId), - Enum(FileAstId), - Const(FileAstId), - Static(FileAstId), - Trait(FileAstId), - TypeAlias(FileAstId), -} - -impl DefKind { - pub fn ast_id(self) -> FileAstId { - match self { - DefKind::Function(it) => it.upcast(), - DefKind::Struct(it, _) => it.upcast(), - DefKind::Union(it) => it.upcast(), - DefKind::Enum(it) => it.upcast(), - DefKind::Const(it) => it.upcast(), - DefKind::Static(it) => it.upcast(), - DefKind::Trait(it) => it.upcast(), - DefKind::TypeAlias(it) => it.upcast(), - } - } -} - -#[derive(Debug, PartialEq, Eq)] -pub(super) struct MacroData { - pub(super) ast_id: FileAstId, - pub(super) path: ModPath, - pub(super) name: Option, - pub(super) export: bool, - pub(super) local_inner: bool, - pub(super) builtin: bool, -} - -#[derive(Debug, PartialEq, Eq)] -pub(super) struct ImplData { - pub(super) ast_id: FileAstId, -} - -struct RawItemsCollector { - raw_items: RawItems, - source_ast_id_map: Arc, - file_id: HirFileId, - hygiene: Hygiene, -} - -impl RawItemsCollector { - fn process_module( - &mut self, - current_module: Option>, - body: impl ast::ModuleItemOwner, - ) { - for item in body.items() { - self.add_item(current_module, item) - } - } - - fn add_item(&mut self, current_module: Option>, item: ast::ModuleItem) { - let attrs = self.parse_attrs(&item); - let visibility = RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene); - let (kind, name) = match item { - ast::ModuleItem::Module(module) => { - self.add_module(current_module, module); - return; - } - ast::ModuleItem::UseItem(use_item) => { - self.add_use_item(current_module, use_item); - return; - } - ast::ModuleItem::ExternCrateItem(extern_crate) => { - self.add_extern_crate_item(current_module, extern_crate); - return; - } - ast::ModuleItem::ImplDef(it) => { - self.add_impl(current_module, it); - return; - } - ast::ModuleItem::StructDef(it) => { - let kind = match it.kind() { - ast::StructKind::Record(_) => StructDefKind::Record, - ast::StructKind::Tuple(_) => StructDefKind::Tuple, - ast::StructKind::Unit => StructDefKind::Unit, - }; - let id = self.source_ast_id_map.ast_id(&it); - let name = it.name(); - (DefKind::Struct(id, kind), name) - } - ast::ModuleItem::UnionDef(it) => { - let id = self.source_ast_id_map.ast_id(&it); - let name = it.name(); - (DefKind::Union(id), name) - } - ast::ModuleItem::EnumDef(it) => { - (DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name()) - } - ast::ModuleItem::FnDef(it) => { - (DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name()) - } - ast::ModuleItem::TraitDef(it) => { - (DefKind::Trait(self.source_ast_id_map.ast_id(&it)), it.name()) - } - ast::ModuleItem::TypeAliasDef(it) => { - (DefKind::TypeAlias(self.source_ast_id_map.ast_id(&it)), it.name()) - } - ast::ModuleItem::ConstDef(it) => { - (DefKind::Const(self.source_ast_id_map.ast_id(&it)), it.name()) - } - ast::ModuleItem::StaticDef(it) => { - (DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name()) - } - ast::ModuleItem::MacroCall(it) => { - self.add_macro(current_module, it); - return; - } - ast::ModuleItem::ExternBlock(it) => { - self.add_extern_block(current_module, it); - return; - } - }; - if let Some(name) = name { - let name = name.as_name(); - let def = self.raw_items.defs.alloc(DefData { name, kind, visibility }); - self.push_item(current_module, attrs, RawItemKind::Def(def)); - } - } - - fn add_extern_block( - &mut self, - current_module: Option>, - block: ast::ExternBlock, - ) { - if let Some(items) = block.extern_item_list() { - for item in items.extern_items() { - let attrs = self.parse_attrs(&item); - let visibility = - RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene); - let (kind, name) = match item { - ast::ExternItem::FnDef(it) => { - (DefKind::Function(self.source_ast_id_map.ast_id(&it)), it.name()) - } - ast::ExternItem::StaticDef(it) => { - (DefKind::Static(self.source_ast_id_map.ast_id(&it)), it.name()) - } - }; - - if let Some(name) = name { - let name = name.as_name(); - let def = self.raw_items.defs.alloc(DefData { name, kind, visibility }); - self.push_item(current_module, attrs, RawItemKind::Def(def)); - } - } - } - } - - fn add_module(&mut self, current_module: Option>, module: ast::Module) { - let name = match module.name() { - Some(it) => it.as_name(), - None => return, - }; - let attrs = self.parse_attrs(&module); - let visibility = RawVisibility::from_ast_with_hygiene(module.visibility(), &self.hygiene); - - let ast_id = self.source_ast_id_map.ast_id(&module); - if module.semicolon_token().is_some() { - let item = - self.raw_items.modules.alloc(ModuleData::Declaration { name, visibility, ast_id }); - self.push_item(current_module, attrs, RawItemKind::Module(item)); - return; - } - - if let Some(item_list) = module.item_list() { - let item = self.raw_items.modules.alloc(ModuleData::Definition { - name, - visibility, - ast_id, - items: Vec::new(), - }); - self.process_module(Some(item), item_list); - self.push_item(current_module, attrs, RawItemKind::Module(item)); - return; - } - mark::hit!(name_res_works_for_broken_modules); - } - - fn add_use_item(&mut self, current_module: Option>, use_item: ast::UseItem) { - // FIXME: cfg_attr - let is_prelude = use_item.has_atom_attr("prelude_import"); - let attrs = self.parse_attrs(&use_item); - let visibility = RawVisibility::from_ast_with_hygiene(use_item.visibility(), &self.hygiene); - - let mut buf = Vec::new(); - ModPath::expand_use_item( - InFile { value: use_item, file_id: self.file_id }, - &self.hygiene, - |path, _use_tree, is_glob, alias| { - let import_data = ImportData { - path, - alias, - is_glob, - is_prelude, - is_extern_crate: false, - is_macro_use: false, - visibility: visibility.clone(), - }; - buf.push(import_data); - }, - ); - for import_data in buf { - self.push_import(current_module, attrs.clone(), import_data); - } - } - - fn add_extern_crate_item( - &mut self, - current_module: Option>, - extern_crate: ast::ExternCrateItem, - ) { - if let Some(name_ref) = extern_crate.name_ref() { - let path = ModPath::from_name_ref(&name_ref); - let visibility = - RawVisibility::from_ast_with_hygiene(extern_crate.visibility(), &self.hygiene); - let alias = extern_crate.alias().map(|a| { - a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias) - }); - let attrs = self.parse_attrs(&extern_crate); - // FIXME: cfg_attr - let is_macro_use = extern_crate.has_atom_attr("macro_use"); - let import_data = ImportData { - path, - alias, - is_glob: false, - is_prelude: false, - is_extern_crate: true, - is_macro_use, - visibility, - }; - self.push_import(current_module, attrs, import_data); - } - } - - fn add_macro(&mut self, current_module: Option>, m: ast::MacroCall) { - let attrs = self.parse_attrs(&m); - let path = match m.path().and_then(|path| ModPath::from_src(path, &self.hygiene)) { - Some(it) => it, - _ => return, - }; - - let name = m.name().map(|it| it.as_name()); - let ast_id = self.source_ast_id_map.ast_id(&m); - - // FIXME: cfg_attr - let export_attr = attrs.by_key("macro_export"); - - let export = export_attr.exists(); - let local_inner = if export { - export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it { - tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => { - ident.text.contains("local_inner_macros") - } - _ => false, - }) - } else { - false - }; - - let builtin = attrs.by_key("rustc_builtin_macro").exists(); - - let m = self.raw_items.macros.alloc(MacroData { - ast_id, - path, - name, - export, - local_inner, - builtin, - }); - self.push_item(current_module, attrs, RawItemKind::Macro(m)); - } - - fn add_impl(&mut self, current_module: Option>, imp: ast::ImplDef) { - let attrs = self.parse_attrs(&imp); - let ast_id = self.source_ast_id_map.ast_id(&imp); - let imp = self.raw_items.impls.alloc(ImplData { ast_id }); - self.push_item(current_module, attrs, RawItemKind::Impl(imp)) - } - - fn push_import( - &mut self, - current_module: Option>, - attrs: Attrs, - data: ImportData, - ) { - let import = self.raw_items.imports.alloc(data); - self.push_item(current_module, attrs, RawItemKind::Import(import)) - } - - fn push_item( - &mut self, - current_module: Option>, - attrs: Attrs, - kind: RawItemKind, - ) { - match current_module { - Some(module) => match &mut self.raw_items.modules[module] { - ModuleData::Definition { items, .. } => items, - ModuleData::Declaration { .. } => unreachable!(), - }, - None => &mut self.raw_items.items, - } - .push(RawItem { attrs, kind }) - } - - fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { - Attrs::new(item, &self.hygiene) - } -} -- cgit v1.2.3 From f9e5ba7d40680f8df96f33a8721ecc2eaab08ab6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 15 Jun 2020 19:23:50 +0200 Subject: Remove now-broken incremental test The ItemTree tracks impl and trait items, so the CrateDefMap now needs recomputing when those change --- crates/ra_hir_def/src/nameres/tests/incremental.rs | 38 ---------------------- 1 file changed, 38 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/tests/incremental.rs b/crates/ra_hir_def/src/nameres/tests/incremental.rs index 87165ac33..0c288a108 100644 --- a/crates/ra_hir_def/src/nameres/tests/incremental.rs +++ b/crates/ra_hir_def/src/nameres/tests/incremental.rs @@ -57,44 +57,6 @@ fn typing_inside_a_function_should_not_invalidate_def_map() { ); } -#[test] -fn adding_inner_items_should_not_invalidate_def_map() { - check_def_map_is_not_recomputed( - r" - //- /lib.rs - struct S { a: i32} - enum E { A } - trait T { - fn a() {} - } - mod foo;<|> - impl S { - fn a() {} - } - use crate::foo::bar::Baz; - //- /foo/mod.rs - pub mod bar; - - //- /foo/bar.rs - pub struct Baz; - ", - r" - struct S { a: i32, b: () } - enum E { A, B } - trait T { - fn a() {} - fn b() {} - } - mod foo;<|> - impl S { - fn a() {} - fn b() {} - } - use crate::foo::bar::Baz; - ", - ); -} - #[test] fn typing_inside_a_macro_should_not_invalidate_def_map() { let (mut db, pos) = TestDB::with_position( -- cgit v1.2.3 From 864b650f92388f4e82d130713b2de9afe637102f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 16 Jun 2020 19:20:29 +0200 Subject: ItemTree: use a newtyped ID --- crates/ra_hir_def/src/nameres/collector.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 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 b899a5fb3..c227b6da1 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -20,7 +20,9 @@ use test_utils::mark; use crate::{ attr::Attrs, db::DefDatabase, - item_tree::{Import, ItemTree, MacroCall, Mod, ModItem, ModKind, StructDefKind}, + item_tree::{ + FileItemTreeId, Import, ItemTree, MacroCall, Mod, ModItem, ModKind, StructDefKind, + }, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, @@ -32,7 +34,6 @@ use crate::{ FunctionLoc, ImplLoc, Intern, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, }; -use ra_arena::Idx; pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { let crate_graph = db.crate_graph(); @@ -107,7 +108,7 @@ impl PartialResolvedImport { #[derive(Clone, Debug, Eq, PartialEq)] struct ImportDirective { module_id: LocalModuleId, - import_id: Idx, + import_id: FileItemTreeId, import: Import, status: PartialResolvedImport, } -- cgit v1.2.3 From 4b03b39d5b4b00daffb120a4d2d9ea4a55a9a7ac Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 22 Jun 2020 15:07:06 +0200 Subject: draw the rest of the owl --- crates/ra_hir_def/src/nameres/collector.rs | 134 +++++++++++++-------- .../ra_hir_def/src/nameres/tests/mod_resolution.rs | 5 +- 2 files changed, 85 insertions(+), 54 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 c227b6da1..40aff830f 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -20,9 +20,7 @@ use test_utils::mark; use crate::{ attr::Attrs, db::DefDatabase, - item_tree::{ - FileItemTreeId, Import, ItemTree, MacroCall, Mod, ModItem, ModKind, StructDefKind, - }, + item_tree::{self, ItemTree, ItemTreeId, MacroCall, Mod, ModItem, ModKind, StructDefKind}, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, @@ -105,10 +103,48 @@ impl PartialResolvedImport { } } +#[derive(Clone, Debug, Eq, PartialEq)] +struct Import { + pub path: ModPath, + pub alias: Option, + pub visibility: RawVisibility, + pub is_glob: bool, + pub is_prelude: bool, + pub is_extern_crate: bool, + pub is_macro_use: bool, +} + +impl From for Import { + fn from(it: item_tree::Import) -> Self { + Self { + path: it.path, + alias: it.alias, + visibility: it.visibility, + is_glob: it.is_glob, + is_prelude: it.is_prelude, + is_extern_crate: false, + is_macro_use: false, + } + } +} + +impl From for Import { + fn from(it: item_tree::ExternCrate) -> Self { + Self { + path: it.path, + alias: it.alias, + visibility: it.visibility, + is_glob: false, + is_prelude: false, + is_extern_crate: true, + is_macro_use: it.is_macro_use, + } + } +} + #[derive(Clone, Debug, Eq, PartialEq)] struct ImportDirective { module_id: LocalModuleId, - import_id: FileItemTreeId, import: Import, status: PartialResolvedImport, } @@ -297,7 +333,7 @@ impl DefCollector<'_> { fn import_macros_from_extern_crate( &mut self, current_module_id: LocalModuleId, - import: &Import, + import: &item_tree::ExternCrate, ) { log::debug!( "importing macros from extern crate: {:?} ({:?})", @@ -703,9 +739,9 @@ impl ModCollector<'_, '_> { // any other items. for item in items { if self.is_cfg_enabled(self.item_tree.attrs(*item)) { - if let ModItem::Import(import_id) = item { - let import = self.item_tree[*import_id].clone(); - if import.is_extern_crate && import.is_macro_use { + if let ModItem::ExternCrate(id) = item { + let import = self.item_tree[*id].clone(); + if import.is_macro_use { self.def_collector.import_macros_from_extern_crate(self.module_id, &import); } } @@ -725,8 +761,14 @@ impl ModCollector<'_, '_> { ModItem::Import(import_id) => { self.def_collector.unresolved_imports.push(ImportDirective { module_id: self.module_id, - import_id, - import: self.item_tree[import_id].clone(), + import: self.item_tree[import_id].clone().into(), + status: PartialResolvedImport::Unresolved, + }) + } + ModItem::ExternCrate(import_id) => { + self.def_collector.unresolved_imports.push(ImportDirective { + module_id: self.module_id, + import: self.item_tree[import_id].clone().into(), status: PartialResolvedImport::Unresolved, }) } @@ -737,30 +779,28 @@ impl ModCollector<'_, '_> { local_id: self.module_id, }; let container = ContainerId::ModuleId(module); - let ast_id = self.item_tree[imp].ast_id; - let impl_id = - ImplLoc { container, ast_id: AstId::new(self.file_id, ast_id) } - .intern(self.def_collector.db); + let impl_id = ImplLoc { container, id: ItemTreeId::new(self.file_id, imp) } + .intern(self.def_collector.db); self.def_collector.def_map.modules[self.module_id] .scope .define_impl(impl_id) } - ModItem::Function(it) => { - let it = &self.item_tree[it]; + ModItem::Function(id) => { + let func = &self.item_tree[id]; def = Some(DefData { id: FunctionLoc { container: container.into(), - ast_id: AstId::new(self.file_id, it.ast_id), + id: ItemTreeId::new(self.file_id, id), } .intern(self.def_collector.db) .into(), - name: &it.name, - visibility: &it.visibility, + name: &func.name, + visibility: &func.visibility, has_constructor: false, }); } - ModItem::Struct(it) => { - let it = &self.item_tree[it]; + ModItem::Struct(id) => { + let it = &self.item_tree[id]; // FIXME: check attrs to see if this is an attribute macro invocation; // in which case we don't add the invocation, just a single attribute @@ -768,19 +808,16 @@ impl ModCollector<'_, '_> { self.collect_derives(attrs, it.ast_id.upcast()); def = Some(DefData { - id: StructLoc { - container, - ast_id: AstId::new(self.file_id, it.ast_id), - } - .intern(self.def_collector.db) - .into(), + id: StructLoc { container, id: ItemTreeId::new(self.file_id, id) } + .intern(self.def_collector.db) + .into(), name: &it.name, visibility: &it.visibility, has_constructor: it.kind != StructDefKind::Record, }); } - ModItem::Union(it) => { - let it = &self.item_tree[it]; + ModItem::Union(id) => { + let it = &self.item_tree[id]; // FIXME: check attrs to see if this is an attribute macro invocation; // in which case we don't add the invocation, just a single attribute @@ -788,7 +825,7 @@ impl ModCollector<'_, '_> { self.collect_derives(attrs, it.ast_id.upcast()); def = Some(DefData { - id: UnionLoc { container, ast_id: AstId::new(self.file_id, it.ast_id) } + id: UnionLoc { container, id: ItemTreeId::new(self.file_id, id) } .intern(self.def_collector.db) .into(), name: &it.name, @@ -796,8 +833,8 @@ impl ModCollector<'_, '_> { has_constructor: false, }); } - ModItem::Enum(it) => { - let it = &self.item_tree[it]; + ModItem::Enum(id) => { + let it = &self.item_tree[id]; // FIXME: check attrs to see if this is an attribute macro invocation; // in which case we don't add the invocation, just a single attribute @@ -805,7 +842,7 @@ impl ModCollector<'_, '_> { self.collect_derives(attrs, it.ast_id.upcast()); def = Some(DefData { - id: EnumLoc { container, ast_id: AstId::new(self.file_id, it.ast_id) } + id: EnumLoc { container, id: ItemTreeId::new(self.file_id, id) } .intern(self.def_collector.db) .into(), name: &it.name, @@ -813,14 +850,14 @@ impl ModCollector<'_, '_> { has_constructor: false, }); } - ModItem::Const(it) => { - let it = &self.item_tree[it]; + ModItem::Const(id) => { + let it = &self.item_tree[id]; if let Some(name) = &it.name { def = Some(DefData { id: ConstLoc { container: container.into(), - ast_id: AstId::new(self.file_id, it.ast_id), + id: ItemTreeId::new(self.file_id, id), } .intern(self.def_collector.db) .into(), @@ -830,26 +867,23 @@ impl ModCollector<'_, '_> { }); } } - ModItem::Static(it) => { - let it = &self.item_tree[it]; + ModItem::Static(id) => { + let it = &self.item_tree[id]; def = Some(DefData { - id: StaticLoc { - container, - ast_id: AstId::new(self.file_id, it.ast_id), - } - .intern(self.def_collector.db) - .into(), + id: StaticLoc { container, id: ItemTreeId::new(self.file_id, id) } + .intern(self.def_collector.db) + .into(), name: &it.name, visibility: &it.visibility, has_constructor: false, }); } - ModItem::Trait(it) => { - let it = &self.item_tree[it]; + ModItem::Trait(id) => { + let it = &self.item_tree[id]; def = Some(DefData { - id: TraitLoc { container, ast_id: AstId::new(self.file_id, it.ast_id) } + id: TraitLoc { container, id: ItemTreeId::new(self.file_id, id) } .intern(self.def_collector.db) .into(), name: &it.name, @@ -857,13 +891,13 @@ impl ModCollector<'_, '_> { has_constructor: false, }); } - ModItem::TypeAlias(it) => { - let it = &self.item_tree[it]; + ModItem::TypeAlias(id) => { + let it = &self.item_tree[id]; def = Some(DefData { id: TypeAliasLoc { container: container.into(), - ast_id: AstId::new(self.file_id, it.ast_id), + id: ItemTreeId::new(self.file_id, id), } .intern(self.def_collector.db) .into(), 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 d42933eed..e9a5e4cba 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs @@ -722,10 +722,7 @@ fn unresolved_module_diagnostics() { ), ), ), - value: FileAstId { - raw: Idx::(1), - _ty: PhantomData, - }, + value: FileAstId::(1), }, candidate: "bar.rs", }, -- cgit v1.2.3 From 43cad21623bc5de59598a565097be9c7d8642818 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 24 Jun 2020 15:36:18 +0200 Subject: Don't allocate common visibilities --- crates/ra_hir_def/src/nameres/collector.rs | 52 ++++++++++++++++-------------- 1 file changed, 28 insertions(+), 24 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 40aff830f..94da700ad 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -20,7 +20,9 @@ use test_utils::mark; use crate::{ attr::Attrs, db::DefDatabase, - item_tree::{self, ItemTree, ItemTreeId, MacroCall, Mod, ModItem, ModKind, StructDefKind}, + item_tree::{ + self, FileItemTreeId, ItemTree, ItemTreeId, MacroCall, Mod, ModItem, ModKind, StructDefKind, + }, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, BuiltinShadowMode, CrateDefMap, ModuleData, ModuleOrigin, ResolveMode, @@ -114,26 +116,28 @@ struct Import { pub is_macro_use: bool, } -impl From for Import { - fn from(it: item_tree::Import) -> Self { +impl Import { + fn from_use(tree: &ItemTree, id: FileItemTreeId) -> Self { + let it = &tree[id]; + let visibility = &tree[it.visibility]; Self { - path: it.path, - alias: it.alias, - visibility: it.visibility, + path: it.path.clone(), + alias: it.alias.clone(), + visibility: visibility.clone(), is_glob: it.is_glob, is_prelude: it.is_prelude, is_extern_crate: false, is_macro_use: false, } } -} -impl From for Import { - fn from(it: item_tree::ExternCrate) -> Self { + fn from_extern_crate(tree: &ItemTree, id: FileItemTreeId) -> Self { + let it = &tree[id]; + let visibility = &tree[it.visibility]; Self { - path: it.path, - alias: it.alias, - visibility: it.visibility, + path: it.path.clone(), + alias: it.alias.clone(), + visibility: visibility.clone(), is_glob: false, is_prelude: false, is_extern_crate: true, @@ -761,14 +765,14 @@ impl ModCollector<'_, '_> { ModItem::Import(import_id) => { self.def_collector.unresolved_imports.push(ImportDirective { module_id: self.module_id, - import: self.item_tree[import_id].clone().into(), + import: Import::from_use(&self.item_tree, import_id), status: PartialResolvedImport::Unresolved, }) } ModItem::ExternCrate(import_id) => { self.def_collector.unresolved_imports.push(ImportDirective { module_id: self.module_id, - import: self.item_tree[import_id].clone().into(), + import: Import::from_extern_crate(&self.item_tree, import_id), status: PartialResolvedImport::Unresolved, }) } @@ -795,7 +799,7 @@ impl ModCollector<'_, '_> { .intern(self.def_collector.db) .into(), name: &func.name, - visibility: &func.visibility, + visibility: &self.item_tree[func.visibility], has_constructor: false, }); } @@ -812,7 +816,7 @@ impl ModCollector<'_, '_> { .intern(self.def_collector.db) .into(), name: &it.name, - visibility: &it.visibility, + visibility: &self.item_tree[it.visibility], has_constructor: it.kind != StructDefKind::Record, }); } @@ -829,7 +833,7 @@ impl ModCollector<'_, '_> { .intern(self.def_collector.db) .into(), name: &it.name, - visibility: &it.visibility, + visibility: &self.item_tree[it.visibility], has_constructor: false, }); } @@ -846,7 +850,7 @@ impl ModCollector<'_, '_> { .intern(self.def_collector.db) .into(), name: &it.name, - visibility: &it.visibility, + visibility: &self.item_tree[it.visibility], has_constructor: false, }); } @@ -862,7 +866,7 @@ impl ModCollector<'_, '_> { .intern(self.def_collector.db) .into(), name, - visibility: &it.visibility, + visibility: &self.item_tree[it.visibility], has_constructor: false, }); } @@ -875,7 +879,7 @@ impl ModCollector<'_, '_> { .intern(self.def_collector.db) .into(), name: &it.name, - visibility: &it.visibility, + visibility: &self.item_tree[it.visibility], has_constructor: false, }); } @@ -887,7 +891,7 @@ impl ModCollector<'_, '_> { .intern(self.def_collector.db) .into(), name: &it.name, - visibility: &it.visibility, + visibility: &self.item_tree[it.visibility], has_constructor: false, }); } @@ -902,7 +906,7 @@ impl ModCollector<'_, '_> { .intern(self.def_collector.db) .into(), name: &it.name, - visibility: &it.visibility, + visibility: &self.item_tree[it.visibility], has_constructor: false, }); } @@ -935,7 +939,7 @@ impl ModCollector<'_, '_> { module.name.clone(), AstId::new(self.file_id, module.ast_id), None, - &module.visibility, + &self.item_tree[module.visibility], ); ModCollector { @@ -965,7 +969,7 @@ impl ModCollector<'_, '_> { module.name.clone(), ast_id, Some((file_id, is_mod_rs)), - &module.visibility, + &self.item_tree[module.visibility], ); let item_tree = self.def_collector.db.item_tree(file_id.into()); ModCollector { -- cgit v1.2.3