From 973b5cf7e20842711d59a810b268796b26241382 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 21 Dec 2019 15:04:33 +0100 Subject: Revert "Merge #2629" This reverts commit cdc9d682b066b110e0a44e5f8f1c574b38c16ba9, reversing changes made to 90ef070db3dce0a7acb9cd11d0b0d72de13c9d79. --- crates/ra_hir_def/src/db.rs | 11 ++++- crates/ra_hir_def/src/item_scope.rs | 35 ++++++++++----- crates/ra_hir_def/src/lib.rs | 4 ++ crates/ra_hir_def/src/nameres/collector.rs | 46 +++++++++++--------- crates/ra_hir_def/src/nameres/raw.rs | 68 +++++++++++++++++++++++------- crates/ra_hir_def/src/trace.rs | 8 ++++ 6 files changed, 126 insertions(+), 46 deletions(-) (limited to 'crates/ra_hir_def') diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index c55fd4111..98bff6cb7 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -13,7 +13,10 @@ use crate::{ docs::Documentation, generics::GenericParams, lang_item::{LangItemTarget, LangItems}, - nameres::{raw::RawItems, CrateDefMap}, + nameres::{ + raw::{ImportSourceMap, RawItems}, + CrateDefMap, + }, AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, @@ -43,6 +46,12 @@ pub trait InternDatabase: SourceDatabase { #[salsa::query_group(DefDatabaseStorage)] pub trait DefDatabase: InternDatabase + AstDatabase { + #[salsa::invoke(RawItems::raw_items_with_source_map_query)] + fn raw_items_with_source_map( + &self, + file_id: HirFileId, + ) -> (Arc, Arc); + #[salsa::invoke(RawItems::raw_items_query)] fn raw_items(&self, file_id: HirFileId) -> Arc; diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index 5c14fefff..6b9be8325 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs @@ -5,7 +5,7 @@ use hir_expand::name::Name; use once_cell::sync::Lazy; use rustc_hash::FxHashMap; -use crate::{per_ns::PerNs, BuiltinType, ImplId, MacroDefId, ModuleDefId, TraitId}; +use crate::{per_ns::PerNs, BuiltinType, ImplId, LocalImportId, MacroDefId, ModuleDefId, TraitId}; #[derive(Debug, Default, PartialEq, Eq)] pub struct ItemScope { @@ -30,7 +30,7 @@ static BUILTIN_SCOPE: Lazy> = Lazy::new(|| { BuiltinType::ALL .iter() .map(|(name, ty)| { - (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), declaration: false }) + (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None }) }) .collect() }); @@ -53,9 +53,11 @@ impl ItemScope { } pub fn declarations(&self) -> impl Iterator + '_ { - self.entries().filter(|(_name, res)| res.declaration).flat_map(|(_name, res)| { - res.def.take_types().into_iter().chain(res.def.take_values().into_iter()) - }) + self.entries() + .filter_map(|(_name, res)| if res.import.is_none() { Some(res.def) } else { None }) + .flat_map(|per_ns| { + per_ns.take_types().into_iter().chain(per_ns.take_values().into_iter()) + }) } pub fn impls(&self) -> impl Iterator + ExactSizeIterator + '_ { @@ -110,26 +112,38 @@ impl ItemScope { self.legacy_macros.insert(name, mac); } - pub(crate) fn push_res(&mut self, name: Name, res: &Resolution, declaration: bool) -> bool { + pub(crate) fn push_res( + &mut self, + name: Name, + res: &Resolution, + import: Option, + ) -> bool { let mut changed = false; let existing = self.items.entry(name.clone()).or_default(); if existing.def.types.is_none() && res.def.types.is_some() { existing.def.types = res.def.types; - existing.declaration |= declaration; + existing.import = import.or(res.import); changed = true; } if existing.def.values.is_none() && res.def.values.is_some() { existing.def.values = res.def.values; - existing.declaration |= declaration; + existing.import = import.or(res.import); changed = true; } if existing.def.macros.is_none() && res.def.macros.is_some() { existing.def.macros = res.def.macros; - existing.declaration |= declaration; + existing.import = import.or(res.import); changed = true; } + if existing.def.is_none() + && res.def.is_none() + && existing.import.is_none() + && res.import.is_some() + { + existing.import = res.import; + } changed } @@ -146,5 +160,6 @@ impl ItemScope { pub struct Resolution { /// None for unresolved pub def: PerNs, - pub declaration: bool, + /// ident by which this is imported into local scope. + pub import: Option, } diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index f6c7f38d1..acd4f4af1 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -51,6 +51,10 @@ use ra_syntax::{ast, AstNode}; use crate::body::Expander; use crate::builtin_type::BuiltinType; +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct LocalImportId(RawId); +impl_arena_id!(LocalImportId); + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ModuleId { pub krate: CrateId, diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 9419461a8..45199fa11 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -26,7 +26,8 @@ use crate::{ path::{ModPath, PathKind}, per_ns::PerNs, AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, - LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, + LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, + TypeAliasLoc, UnionLoc, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -92,7 +93,7 @@ impl PartialResolvedImport { #[derive(Clone, Debug, Eq, PartialEq)] struct ImportDirective { module_id: LocalModuleId, - import_id: raw::LocalImportId, + import_id: LocalImportId, import: raw::ImportData, status: PartialResolvedImport, } @@ -109,7 +110,7 @@ struct MacroDirective { struct DefCollector<'a, DB> { db: &'a DB, def_map: CrateDefMap, - glob_imports: FxHashMap>, + glob_imports: FxHashMap>, unresolved_imports: Vec, resolved_imports: Vec, unexpanded_macros: Vec, @@ -217,7 +218,8 @@ where if export { self.update( self.def_map.root, - &[(name, Resolution { def: PerNs::macros(macro_), declaration: false })], + None, + &[(name, Resolution { def: PerNs::macros(macro_), import: None })], ); } } @@ -372,7 +374,7 @@ where // Module scoped macros is included let items = scope.collect_resolutions(); - self.update(module_id, &items); + self.update(module_id, Some(import_id), &items); } else { // glob import from same crate => we do an initial // import, and then need to propagate any further @@ -382,7 +384,7 @@ where // Module scoped macros is included let items = scope.collect_resolutions(); - self.update(module_id, &items); + self.update(module_id, Some(import_id), &items); // record the glob import in case we add further items let glob = self.glob_imports.entry(m.local_id).or_default(); if !glob.iter().any(|it| *it == (module_id, import_id)) { @@ -402,12 +404,12 @@ where let variant = EnumVariantId { parent: e, local_id }; let res = Resolution { def: PerNs::both(variant.into(), variant.into()), - declaration: false, + import: Some(import_id), }; (name, res) }) .collect::>(); - self.update(module_id, &resolutions); + self.update(module_id, Some(import_id), &resolutions); } Some(d) => { log::debug!("glob import {:?} from non-module/enum {:?}", import, d); @@ -429,21 +431,27 @@ where } } - let resolution = Resolution { def, declaration: false }; - self.update(module_id, &[(name, resolution)]); + let resolution = Resolution { def, import: Some(import_id) }; + self.update(module_id, Some(import_id), &[(name, resolution)]); } None => tested_by!(bogus_paths), } } } - fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, Resolution)]) { - self.update_recursive(module_id, resolutions, 0) + fn update( + &mut self, + module_id: LocalModuleId, + import: Option, + resolutions: &[(Name, Resolution)], + ) { + self.update_recursive(module_id, import, resolutions, 0) } fn update_recursive( &mut self, module_id: LocalModuleId, + import: Option, resolutions: &[(Name, Resolution)], depth: usize, ) { @@ -454,7 +462,7 @@ where let scope = &mut self.def_map.modules[module_id].scope; let mut changed = false; for (name, res) in resolutions { - changed |= scope.push_res(name.clone(), res, depth == 0 && res.declaration); + changed |= scope.push_res(name.clone(), res, import); } if !changed { @@ -467,9 +475,9 @@ where .flat_map(|v| v.iter()) .cloned() .collect::>(); - for (glob_importing_module, _glob_import) in glob_imports { + for (glob_importing_module, glob_import) in glob_imports { // We pass the glob import so that the tracked import in those modules is that glob import - self.update_recursive(glob_importing_module, resolutions, depth + 1); + self.update_recursive(glob_importing_module, Some(glob_import), resolutions, depth + 1); } } @@ -711,9 +719,9 @@ where def: PerNs::types( ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), ), - declaration: true, + import: None, }; - self.def_collector.update(self.module_id, &[(name, resolution)]); + self.def_collector.update(self.module_id, None, &[(name, resolution)]); res } @@ -783,8 +791,8 @@ where PerNs::types(def.into()) } }; - let resolution = Resolution { def, declaration: true }; - self.def_collector.update(self.module_id, &[(name, resolution)]) + let resolution = Resolution { def, import: None }; + self.def_collector.update(self.module_id, None, &[(name, resolution)]) } fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index b10e458a2..ecb4d7c03 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -7,24 +7,24 @@ use std::{ops::Index, sync::Arc}; +use either::Either; use hir_expand::{ ast_id_map::AstIdMap, db::AstDatabase, hygiene::Hygiene, name::{AsName, Name}, }; -use ra_arena::{impl_arena_id, Arena, RawId}; +use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_syntax::{ ast::{self, AttrsOwner, NameOwner}, - AstNode, + AstNode, AstPtr, }; use test_utils::tested_by; -use crate::{attr::Attrs, db::DefDatabase, path::ModPath, FileAstId, HirFileId, InFile}; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub(super) struct LocalImportId(RawId); -impl_arena_id!(LocalImportId); +use crate::{ + attr::Attrs, db::DefDatabase, path::ModPath, trace::Trace, FileAstId, HirFileId, InFile, + LocalImportId, +}; /// `RawItems` is a set of top-level items in a file (except for impls). /// @@ -41,14 +41,35 @@ pub struct RawItems { items: Vec, } +#[derive(Debug, Default, PartialEq, Eq)] +pub struct ImportSourceMap { + map: ArenaMap, +} + +type ImportSourcePtr = Either, AstPtr>; + +impl ImportSourceMap { + pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { + self.map[import].clone() + } +} + impl RawItems { pub(crate) fn raw_items_query( 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 DefDatabase + AstDatabase), + file_id: HirFileId, + ) -> (Arc, Arc) { let mut collector = RawItemsCollector { raw_items: RawItems::default(), source_ast_id_map: db.ast_id_map(file_id), + imports: Trace::new(), file_id, hygiene: Hygiene::new(db, file_id), }; @@ -59,8 +80,11 @@ impl RawItems { collector.process_module(None, item_list); } } - let raw_items = collector.raw_items; - Arc::new(raw_items) + 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] { @@ -199,6 +223,7 @@ pub(super) struct ImplData { struct RawItemsCollector { raw_items: RawItems, + imports: Trace, source_ast_id_map: Arc, file_id: HirFileId, hygiene: Hygiene, @@ -305,7 +330,7 @@ impl RawItemsCollector { ModPath::expand_use_item( InFile { value: use_item, file_id: self.file_id }, &self.hygiene, - |path, _use_tree, is_glob, alias| { + |path, use_tree, is_glob, alias| { let import_data = ImportData { path, alias, @@ -314,11 +339,11 @@ impl RawItemsCollector { is_extern_crate: false, is_macro_use: false, }; - buf.push(import_data); + buf.push((import_data, Either::Left(AstPtr::new(use_tree)))); }, ); - for import_data in buf { - self.push_import(current_module, attrs.clone(), import_data); + for (import_data, ptr) in buf { + self.push_import(current_module, attrs.clone(), import_data, ptr); } } @@ -341,7 +366,12 @@ impl RawItemsCollector { is_extern_crate: true, is_macro_use, }; - self.push_import(current_module, attrs, import_data); + self.push_import( + current_module, + attrs, + import_data, + Either::Right(AstPtr::new(&extern_crate)), + ); } } @@ -372,8 +402,14 @@ impl RawItemsCollector { 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); + fn push_import( + &mut self, + current_module: Option, + attrs: Attrs, + data: ImportData, + source: ImportSourcePtr, + ) { + let import = self.imports.alloc(|| source, || data); self.push_item(current_module, attrs, RawItemKind::Import(import)) } diff --git a/crates/ra_hir_def/src/trace.rs b/crates/ra_hir_def/src/trace.rs index 9769e88df..2bcd707bc 100644 --- a/crates/ra_hir_def/src/trace.rs +++ b/crates/ra_hir_def/src/trace.rs @@ -18,6 +18,10 @@ pub(crate) struct Trace { } impl Trace { + pub(crate) fn new() -> Trace { + Trace { arena: Some(Arena::default()), map: Some(ArenaMap::default()), len: 0 } + } + pub(crate) fn new_for_arena() -> Trace { Trace { arena: Some(Arena::default()), map: None, len: 0 } } @@ -48,4 +52,8 @@ impl Trace { pub(crate) fn into_map(mut self) -> ArenaMap { self.map.take().unwrap() } + + pub(crate) fn into_arena_and_map(mut self) -> (Arena, ArenaMap) { + (self.arena.take().unwrap(), self.map.take().unwrap()) + } } -- cgit v1.2.3