From 7b0644d81e52d00a7a6795b187f356213ff68225 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Dec 2019 14:09:13 +0100 Subject: Switch to the new location for impls --- crates/ra_hir_def/src/attr.rs | 2 +- crates/ra_hir_def/src/child_by_source.rs | 5 +++++ crates/ra_hir_def/src/data.rs | 2 +- crates/ra_hir_def/src/db.rs | 6 +++--- crates/ra_hir_def/src/generics.rs | 2 +- crates/ra_hir_def/src/keys.rs | 6 ++++-- crates/ra_hir_def/src/lib.rs | 30 +++++++++++++++++++++--------- crates/ra_hir_def/src/nameres/collector.rs | 10 ++++++---- crates/ra_hir_def/src/resolver.rs | 3 ++- crates/ra_hir_def/src/src.rs | 11 ++++++++++- 10 files changed, 54 insertions(+), 23 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 2f8f02d82..12d4e777a 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -64,7 +64,7 @@ impl Attrs { AttrDefId::MacroDefId(it) => { it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)) } - AttrDefId::ImplId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AttrDefId::ImplId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::StaticId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db), diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs index a3574a9db..821549bd5 100644 --- a/crates/ra_hir_def/src/child_by_source.rs +++ b/crates/ra_hir_def/src/child_by_source.rs @@ -98,6 +98,11 @@ impl ChildBySource for ModuleId { } } + for &impl_ in crate_def_map[self.local_id].impls.iter() { + let src = impl_.lookup(db).source(db); + res[keys::IMPL].insert(src, impl_) + } + res } } diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 095d7064a..42821b9b1 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -167,7 +167,7 @@ pub struct ImplData { impl ImplData { pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc { - let src = id.source(db); + let src = id.lookup(db).source(db); let items = db.ast_id_map(src.file_id); let target_trait = src.value.target_trait().map(TypeRef::from_ast); diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index ef5611ffc..8907aacca 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -18,8 +18,8 @@ use crate::{ CrateDefMap, }, AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId, - ImplId, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId, TypeAliasLoc, - UnionId, + ImplId, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId, + TypeAliasLoc, UnionId, }; #[salsa::query_group(InternDatabaseStorage)] @@ -41,7 +41,7 @@ pub trait InternDatabase: SourceDatabase { #[salsa::interned] fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId; #[salsa::interned] - fn intern_impl(&self, loc: ItemLoc) -> ImplId; + fn intern_impl(&self, loc: ImplLoc) -> ImplId; } #[salsa::query_group(DefDatabaseStorage)] diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 976cf72d0..5d1100945 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -109,7 +109,7 @@ impl GenericParams { // type-parameter, but rather is a type-alias for impl's target // type, so this is handled by the resolver. GenericDefId::ImplId(it) => { - let src = it.source(db); + let src = it.lookup(db).source(db); generics.fill(&mut sm, &src.value); src.file_id } diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs index be702a4f8..d318b2451 100644 --- a/crates/ra_hir_def/src/keys.rs +++ b/crates/ra_hir_def/src/keys.rs @@ -8,7 +8,7 @@ use rustc_hash::FxHashMap; use crate::{ dyn_map::{DynMap, Policy}, - ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, TypeParamId, + ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TypeAliasId, TypeParamId, }; type Key = crate::dyn_map::Key, V, AstPtrPolicy>; @@ -16,8 +16,10 @@ type Key = crate::dyn_map::Key, V, AstPtrPolicy>; pub const FUNCTION: Key = Key::new(); pub const CONST: Key = Key::new(); pub const STATIC: Key = Key::new(); -pub const ENUM_VARIANT: Key = Key::new(); pub const TYPE_ALIAS: Key = Key::new(); +pub const IMPL: Key = Key::new(); + +pub const ENUM_VARIANT: Key = Key::new(); pub const TUPLE_FIELD: Key = Key::new(); pub const RECORD_FIELD: Key = Key::new(); pub const TYPE_PARAM: Key = Key::new(); diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 569da4f28..5564b166b 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -289,12 +289,24 @@ impl Lookup for TypeAliasId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct ImplId(salsa::InternId); impl_intern_key!(ImplId); -impl AstItemDef for ImplId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_impl(loc) + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct ImplLoc { + pub container: ModuleId, + pub ast_id: AstId, +} + +impl Intern for ImplLoc { + type ID = ImplId; + fn intern(self, db: &impl db::DefDatabase) -> ImplId { + db.intern_impl(self) } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_impl(self) +} + +impl Lookup for ImplId { + type Data = ImplLoc; + fn lookup(&self, db: &impl db::DefDatabase) -> ImplLoc { + db.lookup_intern_impl(*self) } } @@ -479,7 +491,7 @@ impl HasModule for FunctionLoc { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { match self.container { ContainerId::ModuleId(it) => it, - ContainerId::ImplId(it) => it.module(db), + ContainerId::ImplId(it) => it.lookup(db).container, ContainerId::TraitId(it) => it.module(db), } } @@ -489,7 +501,7 @@ impl HasModule for TypeAliasLoc { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { match self.container { ContainerId::ModuleId(it) => it, - ContainerId::ImplId(it) => it.module(db), + ContainerId::ImplId(it) => it.lookup(db).container, ContainerId::TraitId(it) => it.module(db), } } @@ -499,7 +511,7 @@ impl HasModule for ConstLoc { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { match self.container { ContainerId::ModuleId(it) => it, - ContainerId::ImplId(it) => it.module(db), + ContainerId::ImplId(it) => it.lookup(db).container, ContainerId::TraitId(it) => it.module(db), } } @@ -532,7 +544,7 @@ impl HasModule for GenericDefId { GenericDefId::AdtId(it) => it.module(db), GenericDefId::TraitId(it) => it.module(db), GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), - GenericDefId::ImplId(it) => it.module(db), + GenericDefId::ImplId(it) => it.lookup(db).container, GenericDefId::EnumVariantId(it) => it.parent.module(db), GenericDefId::ConstId(it) => it.lookup(db).module(db), } diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index a80067979..b33507a9a 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -24,7 +24,7 @@ use crate::{ }, path::{Path, PathKind}, per_ns::PerNs, - AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, + AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplLoc, Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, TraitId, TypeAliasLoc, UnionId, }; @@ -661,9 +661,11 @@ where krate: self.def_collector.def_map.krate, local_id: self.module_id, }; - let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); - let imp_id = ImplId::from_ast_id(ctx, self.raw_items[imp].ast_id); - self.def_collector.def_map.modules[self.module_id].impls.push(imp_id) + let ast_id = self.raw_items[imp].ast_id; + let impl_id = + ImplLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db); + self.def_collector.def_map.modules[self.module_id].impls.push(impl_id) } } } diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 9484a61d5..f87b16b44 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -564,7 +564,8 @@ impl HasResolver for TypeAliasId { impl HasResolver for ImplId { fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.module(db) + self.lookup(db) + .container .resolver(db) .push_generic_params_scope(db, self.into()) .push_impl_block_scope(self) diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs index 27caa02cc..a5c4359a7 100644 --- a/crates/ra_hir_def/src/src.rs +++ b/crates/ra_hir_def/src/src.rs @@ -4,7 +4,7 @@ use hir_expand::InFile; use ra_arena::map::ArenaMap; use ra_syntax::ast; -use crate::{db::DefDatabase, ConstLoc, FunctionLoc, StaticLoc, TypeAliasLoc}; +use crate::{db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, TypeAliasLoc}; pub trait HasSource { type Value; @@ -47,6 +47,15 @@ impl HasSource for StaticLoc { } } +impl HasSource for ImplLoc { + type Value = ast::ImplBlock; + + fn source(&self, db: &impl DefDatabase) -> InFile { + let node = self.ast_id.to_node(db); + InFile::new(self.ast_id.file_id, node) + } +} + pub trait HasChildSource { type ChildId; type Value; -- cgit v1.2.3