From f135a8ea55c0a46c67713fb3b79b5f62ada430c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Dec 2019 14:58:04 +0100 Subject: Move structs to new loc --- crates/ra_hir_def/src/adt.rs | 9 +++++---- crates/ra_hir_def/src/attr.rs | 2 +- crates/ra_hir_def/src/child_by_source.rs | 12 ++++++++++-- crates/ra_hir_def/src/db.rs | 6 +++--- crates/ra_hir_def/src/docs.rs | 2 +- crates/ra_hir_def/src/generics.rs | 2 +- crates/ra_hir_def/src/keys.rs | 5 +++-- crates/ra_hir_def/src/lib.rs | 24 ++++++++++++++++++------ crates/ra_hir_def/src/nameres/collector.rs | 7 ++++--- crates/ra_hir_def/src/src.rs | 13 ++++++++++++- 10 files changed, 58 insertions(+), 24 deletions(-) (limited to 'crates/ra_hir_def/src') diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index db3e63ef8..ef2b20f1e 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -11,8 +11,9 @@ use ra_arena::{map::ArenaMap, Arena}; use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; use crate::{ - db::DefDatabase, src::HasChildSource, trace::Trace, type_ref::TypeRef, AstItemDef, EnumId, - LocalEnumVariantId, LocalStructFieldId, StructId, UnionId, VariantId, + db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, + AstItemDef, EnumId, LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, UnionId, + VariantId, }; /// Note that we use `StructData` for unions as well! @@ -50,7 +51,7 @@ pub struct StructFieldData { impl StructData { pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc { - let src = id.source(db); + let src = id.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let variant_data = VariantData::new(src.value.kind()); let variant_data = Arc::new(variant_data); @@ -153,7 +154,7 @@ impl HasChildSource for VariantId { let src = it.parent.child_source(db); src.map(|map| map[it.local_id].kind()) } - VariantId::StructId(it) => it.source(db).map(|it| it.kind()), + VariantId::StructId(it) => it.lookup(db).source(db).map(|it| it.kind()), VariantId::UnionId(it) => it.source(db).map(|it| { it.record_field_def_list() .map(ast::StructKind::Record) diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 3347d819c..d2aa5ce8f 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -56,7 +56,7 @@ impl Attrs { Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner)) } AttrDefId::AdtId(it) => match it { - AdtId::StructId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AdtId::StructId(it) => attrs_from_loc(it.lookup(db), db), AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), AdtId::UnionId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), }, diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs index 88c2d3f24..eba361578 100644 --- a/crates/ra_hir_def/src/child_by_source.rs +++ b/crates/ra_hir_def/src/child_by_source.rs @@ -11,8 +11,8 @@ use crate::{ dyn_map::DynMap, keys, src::{HasChildSource, HasSource}, - AssocItemId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, ModuleId, StructFieldId, - TraitId, VariantId, + AdtId, AssocItemId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, ModuleId, + StructFieldId, TraitId, VariantId, }; pub trait ChildBySource { @@ -98,6 +98,14 @@ impl ChildBySource for ModuleId { let src = trait_.lookup(db).source(db); res[keys::TRAIT].insert(src, trait_) } + ModuleDefId::AdtId(adt) => match adt { + AdtId::StructId(strukt) => { + let src = strukt.lookup(db).source(db); + res[keys::STRUCT].insert(src, strukt) + } + AdtId::UnionId(_) => (), + AdtId::EnumId(_) => (), + }, _ => (), } } diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index f0c2ae559..e120c7768 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, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TraitLoc, - TypeAliasId, TypeAliasLoc, UnionId, + ImplId, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, + TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, }; #[salsa::query_group(InternDatabaseStorage)] @@ -27,7 +27,7 @@ pub trait InternDatabase: SourceDatabase { #[salsa::interned] fn intern_function(&self, loc: FunctionLoc) -> FunctionId; #[salsa::interned] - fn intern_struct(&self, loc: ItemLoc) -> StructId; + fn intern_struct(&self, loc: StructLoc) -> StructId; #[salsa::interned] fn intern_union(&self, loc: ItemLoc) -> UnionId; #[salsa::interned] diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs index 1921681fb..58143b894 100644 --- a/crates/ra_hir_def/src/docs.rs +++ b/crates/ra_hir_def/src/docs.rs @@ -51,7 +51,7 @@ impl Documentation { } } AttrDefId::AdtId(it) => match it { - AdtId::StructId(it) => docs_from_ast(&it.source(db).value), + AdtId::StructId(it) => docs_from_ast(&it.lookup(db).source(db).value), AdtId::EnumId(it) => docs_from_ast(&it.source(db).value), AdtId::UnionId(it) => docs_from_ast(&it.source(db).value), }, diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index cbfc3ff04..f1ce8d59a 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -71,7 +71,7 @@ impl GenericParams { src.file_id } GenericDefId::AdtId(AdtId::StructId(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 6ec58f5f9..758cf8097 100644 --- a/crates/ra_hir_def/src/keys.rs +++ b/crates/ra_hir_def/src/keys.rs @@ -8,8 +8,8 @@ use rustc_hash::FxHashMap; use crate::{ dyn_map::{DynMap, Policy}, - ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TraitId, TypeAliasId, - TypeParamId, + ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId, + TypeAliasId, TypeParamId, }; type Key = crate::dyn_map::Key, V, AstPtrPolicy>; @@ -20,6 +20,7 @@ pub const STATIC: Key = Key::new(); pub const TYPE_ALIAS: Key = Key::new(); pub const IMPL: Key = Key::new(); pub const TRAIT: Key = Key::new(); +pub const STRUCT: Key = Key::new(); pub const ENUM_VARIANT: Key = Key::new(); pub const TUPLE_FIELD: Key = Key::new(); diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 0fcc2cde4..1a599706a 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -149,12 +149,24 @@ impl Lookup for FunctionId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct StructId(salsa::InternId); impl_intern_key!(StructId); -impl AstItemDef for StructId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_struct(loc) + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct StructLoc { + pub container: ModuleId, + pub ast_id: AstId, +} + +impl Intern for StructLoc { + type ID = StructId; + fn intern(self, db: &impl db::DefDatabase) -> StructId { + db.intern_struct(self) } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_struct(self) +} + +impl Lookup for StructId { + type Data = StructLoc; + fn lookup(&self, db: &impl db::DefDatabase) -> StructLoc { + db.lookup_intern_struct(*self) } } @@ -532,7 +544,7 @@ impl HasModule for ConstLoc { impl HasModule for AdtId { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { match self { - AdtId::StructId(it) => it.module(db), + AdtId::StructId(it) => it.lookup(db).container, AdtId::UnionId(it) => it.module(db), AdtId::EnumId(it) => it.module(db), } diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index a7bdd620b..602ec0911 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -25,7 +25,7 @@ use crate::{ path::{Path, PathKind}, per_ns::PerNs, AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplLoc, - Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, + Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionId, }; @@ -773,8 +773,9 @@ where PerNs::values(def.into()) } raw::DefKind::Struct(ast_id) => { - let id = StructId::from_ast_id(ctx, ast_id).into(); - PerNs::both(id, id) + let def = StructLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db); + PerNs::both(def.into(), def.into()) } raw::DefKind::Union(ast_id) => { let id = UnionId::from_ast_id(ctx, ast_id).into(); diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs index 858e4861e..7e212adb1 100644 --- a/crates/ra_hir_def/src/src.rs +++ b/crates/ra_hir_def/src/src.rs @@ -4,7 +4,9 @@ use hir_expand::InFile; use ra_arena::map::ArenaMap; use ra_syntax::ast; -use crate::{db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, TraitLoc, TypeAliasLoc}; +use crate::{ + db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, +}; pub trait HasSource { type Value; @@ -65,6 +67,15 @@ impl HasSource for TraitLoc { } } +impl HasSource for StructLoc { + type Value = ast::StructDef; + + 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