From 82e9b245587046d2a1ed432225b19023adbe3245 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Dec 2019 14:34:03 +0100 Subject: Move traits to the new loc --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/from_source.rs | 6 ++++-- crates/ra_hir/src/has_source.rs | 2 +- crates/ra_hir_def/src/attr.rs | 2 +- crates/ra_hir_def/src/child_by_source.rs | 4 ++++ crates/ra_hir_def/src/data.rs | 6 +++--- 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 | 4 +++- crates/ra_hir_def/src/lib.rs | 30 +++++++++++++++++++++--------- crates/ra_hir_def/src/nameres/collector.rs | 9 +++++++-- crates/ra_hir_def/src/resolver.rs | 8 ++++---- crates/ra_hir_def/src/src.rs | 11 ++++++++++- crates/ra_hir_expand/src/builtin_macro.rs | 5 ++++- crates/ra_hir_ty/src/traits/chalk.rs | 6 ++---- 16 files changed, 70 insertions(+), 35 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 117ca2fe3..78c7792bf 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -577,7 +577,7 @@ pub struct Trait { impl Trait { pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.lookup(db).container } } pub fn name(self, db: &impl DefDatabase) -> Name { diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index b35188a21..0cf4bcae1 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -44,8 +44,10 @@ impl FromSource for Enum { impl FromSource for Trait { type Ast = ast::TraitDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - let id = from_source(db, src)?; - Some(Trait { id }) + // XXX: use `.parent()` to avoid finding ourselves + let parent = src.value.syntax().parent()?; + let container = Container::find(db, src.with_value(parent).as_ref())?; + container.child_by_source(db)[keys::TRAIT].get(&src).copied().map(Trait::from) } } impl FromSource for Function { diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index a888fe995..c4c6d66b7 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -93,7 +93,7 @@ impl HasSource for Static { impl HasSource for Trait { type Ast = ast::TraitDef; fn source(self, db: &impl DefDatabase) -> InFile { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for TypeAlias { diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 12d4e777a..3347d819c 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -60,7 +60,7 @@ impl Attrs { 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), }, - AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), + AttrDefId::TraitId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::MacroDefId(it) => { it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(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 821549bd5..88c2d3f24 100644 --- a/crates/ra_hir_def/src/child_by_source.rs +++ b/crates/ra_hir_def/src/child_by_source.rs @@ -94,6 +94,10 @@ impl ChildBySource for ModuleId { let src = ty.lookup(db).source(db); res[keys::TYPE_ALIAS].insert(src, ty) } + ModuleDefId::TraitId(trait_) => { + let src = trait_.lookup(db).source(db); + res[keys::TRAIT].insert(src, trait_) + } _ => (), } } diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 42821b9b1..b2dac183e 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -12,8 +12,8 @@ use crate::{ db::DefDatabase, src::HasSource, type_ref::{Mutability, TypeRef}, - AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, - Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, + AssocItemId, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, Intern, Lookup, + StaticId, TraitId, TypeAliasId, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -94,7 +94,7 @@ pub struct TraitData { impl TraitData { pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc { - let src = tr.source(db); + let src = tr.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let auto = src.value.is_auto(); let ast_id_map = db.ast_id_map(src.file_id); diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 8907aacca..f0c2ae559 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, TypeAliasId, - TypeAliasLoc, UnionId, + ImplId, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TraitLoc, + TypeAliasId, TypeAliasLoc, UnionId, }; #[salsa::query_group(InternDatabaseStorage)] @@ -37,7 +37,7 @@ pub trait InternDatabase: SourceDatabase { #[salsa::interned] fn intern_static(&self, loc: StaticLoc) -> StaticId; #[salsa::interned] - fn intern_trait(&self, loc: ItemLoc) -> TraitId; + fn intern_trait(&self, loc: TraitLoc) -> TraitId; #[salsa::interned] fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId; #[salsa::interned] diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs index 61727bd26..1921681fb 100644 --- a/crates/ra_hir_def/src/docs.rs +++ b/crates/ra_hir_def/src/docs.rs @@ -59,7 +59,7 @@ impl Documentation { let src = it.parent.child_source(db); docs_from_ast(&src.value[it.local_id]) } - AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value), + AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value), AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db)), AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value), AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value), diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 5d1100945..cbfc3ff04 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -86,7 +86,7 @@ impl GenericParams { src.file_id } GenericDefId::TraitId(it) => { - let src = it.source(db); + let src = it.lookup(db).source(db); // traits get the Self type as an implicit first type parameter let self_param_id = diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs index d318b2451..6ec58f5f9 100644 --- a/crates/ra_hir_def/src/keys.rs +++ b/crates/ra_hir_def/src/keys.rs @@ -8,7 +8,8 @@ use rustc_hash::FxHashMap; use crate::{ dyn_map::{DynMap, Policy}, - ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TypeAliasId, TypeParamId, + ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, TraitId, TypeAliasId, + TypeParamId, }; type Key = crate::dyn_map::Key, V, AstPtrPolicy>; @@ -18,6 +19,7 @@ pub const CONST: Key = Key::new(); 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 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 5564b166b..0fcc2cde4 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -253,12 +253,24 @@ impl Lookup for StaticId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct TraitId(salsa::InternId); impl_intern_key!(TraitId); -impl AstItemDef for TraitId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_trait(loc) + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct TraitLoc { + pub container: ModuleId, + pub ast_id: AstId, +} + +impl Intern for TraitLoc { + type ID = TraitId; + fn intern(self, db: &impl db::DefDatabase) -> TraitId { + db.intern_trait(self) } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_trait(self) +} + +impl Lookup for TraitId { + type Data = TraitLoc; + fn lookup(&self, db: &impl db::DefDatabase) -> TraitLoc { + db.lookup_intern_trait(*self) } } @@ -492,7 +504,7 @@ impl HasModule for FunctionLoc { match self.container { ContainerId::ModuleId(it) => it, ContainerId::ImplId(it) => it.lookup(db).container, - ContainerId::TraitId(it) => it.module(db), + ContainerId::TraitId(it) => it.lookup(db).container, } } } @@ -502,7 +514,7 @@ impl HasModule for TypeAliasLoc { match self.container { ContainerId::ModuleId(it) => it, ContainerId::ImplId(it) => it.lookup(db).container, - ContainerId::TraitId(it) => it.module(db), + ContainerId::TraitId(it) => it.lookup(db).container, } } } @@ -512,7 +524,7 @@ impl HasModule for ConstLoc { match self.container { ContainerId::ModuleId(it) => it, ContainerId::ImplId(it) => it.lookup(db).container, - ContainerId::TraitId(it) => it.module(db), + ContainerId::TraitId(it) => it.lookup(db).container, } } } @@ -542,7 +554,7 @@ impl HasModule for GenericDefId { match self { GenericDefId::FunctionId(it) => it.lookup(db).module(db), GenericDefId::AdtId(it) => it.module(db), - GenericDefId::TraitId(it) => it.module(db), + GenericDefId::TraitId(it) => it.lookup(db).container, GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), GenericDefId::ImplId(it) => it.lookup(db).container, GenericDefId::EnumVariantId(it) => it.parent.module(db), diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index b33507a9a..a7bdd620b 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -26,7 +26,7 @@ use crate::{ per_ns::PerNs, AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplLoc, Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, - TraitId, TypeAliasLoc, UnionId, + TraitLoc, TypeAliasLoc, UnionId, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -796,7 +796,12 @@ where PerNs::values(def.into()) } - raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()), + raw::DefKind::Trait(ast_id) => { + let def = TraitLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db); + + PerNs::types(def.into()) + } raw::DefKind::TypeAlias(ast_id) => { let def = TypeAliasLoc { container: ContainerId::ModuleId(module), diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index f87b16b44..17b2169d2 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs @@ -17,9 +17,9 @@ use crate::{ nameres::{BuiltinShadowMode, CrateDefMap}, path::{Path, PathKind}, per_ns::PerNs, - AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, - GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, - StructId, TraitId, TypeAliasId, TypeParamId, VariantId, + AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, + HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, + TypeAliasId, TypeParamId, VariantId, }; #[derive(Debug, Clone, Default)] @@ -524,7 +524,7 @@ impl HasResolver for ModuleId { impl HasResolver for TraitId { fn resolver(self, db: &impl DefDatabase) -> Resolver { - self.module(db).resolver(db).push_generic_params_scope(db, self.into()) + self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into()) } } diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs index a5c4359a7..858e4861e 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, ImplLoc, StaticLoc, TypeAliasLoc}; +use crate::{db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, TraitLoc, TypeAliasLoc}; pub trait HasSource { type Value; @@ -56,6 +56,15 @@ impl HasSource for ImplLoc { } } +impl HasSource for TraitLoc { + type Value = ast::TraitDef; + + 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; diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index ec5ace757..be5f3cbe3 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs @@ -367,6 +367,9 @@ mod tests { BuiltinFnLikeExpander::FormatArgs, ); - assert_eq!(expanded, r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#); + assert_eq!( + expanded, + r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"# + ); } } diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs index ff299f511..fc21872b2 100644 --- a/crates/ra_hir_ty/src/traits/chalk.rs +++ b/crates/ra_hir_ty/src/traits/chalk.rs @@ -9,9 +9,7 @@ use chalk_ir::{ }; use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum}; -use hir_def::{ - AssocItemId, AstItemDef, ContainerId, GenericDefId, ImplId, Lookup, TraitId, TypeAliasId, -}; +use hir_def::{AssocItemId, ContainerId, GenericDefId, ImplId, Lookup, TraitId, TypeAliasId}; use ra_db::{ salsa::{InternId, InternKey}, CrateId, @@ -593,7 +591,7 @@ pub(crate) fn trait_datum_query( let bound_vars = Substs::bound_vars(&generic_params); let flags = chalk_rust_ir::TraitFlags { auto: trait_data.auto, - upstream: trait_.module(db).krate != krate, + upstream: trait_.lookup(db).container.krate != krate, non_enumerable: true, coinductive: false, // only relevant for Chalk testing // FIXME set these flags correctly -- cgit v1.2.3 From 63c192b34a54e826afebc735ca6225b44a876eab Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Dec 2019 14:48:33 +0100 Subject: Simplify from_source --- crates/ra_hir/src/from_source.rs | 83 ++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 0cf4bcae1..091349d49 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -44,16 +44,16 @@ impl FromSource for Enum { impl FromSource for Trait { type Ast = ast::TraitDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - // XXX: use `.parent()` to avoid finding ourselves - let parent = src.value.syntax().parent()?; - let container = Container::find(db, src.with_value(parent).as_ref())?; - container.child_by_source(db)[keys::TRAIT].get(&src).copied().map(Trait::from) + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::TRAIT] + .get(&src) + .copied() + .map(Trait::from) } } impl FromSource for Function { type Ast = ast::FnDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::FUNCTION] + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::FUNCTION] .get(&src) .copied() .map(Function::from) @@ -63,7 +63,7 @@ impl FromSource for Function { impl FromSource for Const { type Ast = ast::ConstDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::CONST] + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::CONST] .get(&src) .copied() .map(Const::from) @@ -72,7 +72,7 @@ impl FromSource for Const { impl FromSource for Static { type Ast = ast::StaticDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::STATIC] + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::STATIC] .get(&src) .copied() .map(Static::from) @@ -82,8 +82,7 @@ impl FromSource for Static { impl FromSource for TypeAlias { type Ast = ast::TypeAliasDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db) - [keys::TYPE_ALIAS] + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::TYPE_ALIAS] .get(&src) .copied() .map(TypeAlias::from) @@ -109,10 +108,10 @@ impl FromSource for MacroDef { impl FromSource for ImplBlock { type Ast = ast::ImplBlock; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - // XXX: use `.parent()` to avoid finding ourselves - let parent = src.value.syntax().parent()?; - let container = Container::find(db, src.with_value(parent).as_ref())?; - container.child_by_source(db)[keys::IMPL].get(&src).copied().map(ImplBlock::from) + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::IMPL] + .get(&src) + .copied() + .map(ImplBlock::from) } } @@ -262,44 +261,30 @@ where Some(DEF::from_ast_id(ctx, item_id)) } -enum Container { - Trait(Trait), - ImplBlock(ImplBlock), - Module(Module), +fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap { + _analyze_container(db, src).unwrap_or_default() } -impl Container { - fn find(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option { - // FIXME: this doesn't try to handle nested declarations - for container in src.value.ancestors() { - let res = match_ast! { - match container { - ast::TraitDef(it) => { - let c = Trait::from_source(db, src.with_value(it))?; - Container::Trait(c) - }, - ast::ImplBlock(it) => { - let c = ImplBlock::from_source(db, src.with_value(it))?; - Container::ImplBlock(c) - }, - _ => { continue }, - } - }; - return Some(res); - } - - let module_source = ModuleSource::from_child_node(db, src); - let c = Module::from_definition(db, src.with_value(module_source))?; - Some(Container::Module(c)) +fn _analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option { + // FIXME: this doesn't try to handle nested declarations + for container in src.value.ancestors().skip(1) { + let res = match_ast! { + match container { + ast::TraitDef(it) => { + let c = Trait::from_source(db, src.with_value(it))?; + c.id.child_by_source(db) + }, + ast::ImplBlock(it) => { + let c = ImplBlock::from_source(db, src.with_value(it))?; + c.id.child_by_source(db) + }, + _ => { continue }, + } + }; + return Some(res); } -} -impl ChildBySource for Container { - fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { - match self { - Container::Trait(it) => it.id.child_by_source(db), - Container::ImplBlock(it) => it.id.child_by_source(db), - Container::Module(it) => it.id.child_by_source(db), - } - } + let module_source = ModuleSource::from_child_node(db, src); + let c = Module::from_definition(db, src.with_value(module_source))?; + Some(c.id.child_by_source(db)) } -- cgit v1.2.3 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/src/code_model.rs | 4 ++-- crates/ra_hir/src/from_source.rs | 6 ++++-- crates/ra_hir/src/has_source.rs | 2 +- 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 ++++++++++++- crates/ra_hir_ty/src/lower.rs | 2 +- 14 files changed, 66 insertions(+), 30 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 78c7792bf..8588aa5a3 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -269,7 +269,7 @@ pub struct Struct { impl Struct { pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.lookup(db).container } } pub fn krate(self, db: &impl DefDatabase) -> Option { @@ -290,7 +290,7 @@ impl Struct { } pub fn ty(self, db: &impl HirDatabase) -> Type { - Type::from_def(db, self.id.module(db).krate, self.id) + Type::from_def(db, self.id.lookup(db).container.krate, self.id) } fn variant_data(self, db: &impl DefDatabase) -> Arc { diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 091349d49..978326c74 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -23,8 +23,10 @@ pub trait FromSource: Sized { impl FromSource for Struct { type Ast = ast::StructDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - let id = from_source(db, src)?; - Some(Struct { id }) + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::STRUCT] + .get(&src) + .copied() + .map(Struct::from) } } impl FromSource for Union { diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index c4c6d66b7..8f35a3fbb 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -51,7 +51,7 @@ impl HasSource for StructField { impl HasSource for Struct { type Ast = ast::StructDef; fn source(self, db: &impl DefDatabase) -> InFile { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for Union { 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; diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index eab91229e..6af5bf50a 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs @@ -699,7 +699,7 @@ impl CallableDef { pub fn krate(self, db: &impl HirDatabase) -> CrateId { match self { CallableDef::FunctionId(f) => f.lookup(db).module(db).krate, - CallableDef::StructId(s) => s.module(db).krate, + CallableDef::StructId(s) => s.lookup(db).container.krate, CallableDef::EnumVariantId(e) => e.parent.module(db).krate, } } -- cgit v1.2.3 From 56710f119b7114efac237ac36ea21730b8bd5311 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Dec 2019 15:11:57 +0100 Subject: Move enum&union to new loc --- crates/ra_hir/src/code_model.rs | 10 +++--- crates/ra_hir/src/from_source.rs | 29 ++++++----------- crates/ra_hir/src/has_source.rs | 6 ++-- crates/ra_hir_def/src/adt.rs | 13 ++++---- crates/ra_hir_def/src/attr.rs | 7 ++--- crates/ra_hir_def/src/child_by_source.rs | 10 ++++-- crates/ra_hir_def/src/db.rs | 12 +++---- crates/ra_hir_def/src/docs.rs | 6 ++-- crates/ra_hir_def/src/generics.rs | 6 ++-- crates/ra_hir_def/src/keys.rs | 4 ++- crates/ra_hir_def/src/lib.rs | 50 ++++++++++++++++++++++-------- crates/ra_hir_def/src/nameres/collector.rs | 19 +++++++----- crates/ra_hir_def/src/src.rs | 21 ++++++++++++- crates/ra_hir_ty/src/lower.rs | 9 +++--- 14 files changed, 123 insertions(+), 79 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 8588aa5a3..c705d1630 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -11,7 +11,7 @@ use hir_def::{ per_ns::PerNs, resolver::HasResolver, type_ref::{Mutability, TypeRef}, - AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, + AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, }; @@ -309,11 +309,11 @@ impl Union { } pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.lookup(db).container } } pub fn ty(self, db: &impl HirDatabase) -> Type { - Type::from_def(db, self.id.module(db).krate, self.id) + Type::from_def(db, self.id.lookup(db).container.krate, self.id) } pub fn fields(self, db: &impl HirDatabase) -> Vec { @@ -337,7 +337,7 @@ pub struct Enum { impl Enum { pub fn module(self, db: &impl DefDatabase) -> Module { - Module { id: self.id.module(db) } + Module { id: self.id.lookup(db).container } } pub fn krate(self, db: &impl DefDatabase) -> Option { @@ -357,7 +357,7 @@ impl Enum { } pub fn ty(self, db: &impl HirDatabase) -> Type { - Type::from_def(db, self.id.module(db).krate, self.id) + Type::from_def(db, self.id.lookup(db).container.krate, self.id) } } diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 978326c74..f7411c5cf 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use hir_def::{ - child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef, - EnumVariantId, GenericDefId, LocationCtx, ModuleId, VariantId, + child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, EnumVariantId, + GenericDefId, ModuleId, VariantId, }; use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; use ra_syntax::{ @@ -32,15 +32,19 @@ impl FromSource for Struct { impl FromSource for Union { type Ast = ast::UnionDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - let id = from_source(db, src)?; - Some(Union { id }) + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::UNION] + .get(&src) + .copied() + .map(Union::from) } } impl FromSource for Enum { type Ast = ast::EnumDef; fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option { - let id = from_source(db, src)?; - Some(Enum { id }) + analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::ENUM] + .get(&src) + .copied() + .map(Enum::from) } } impl FromSource for Trait { @@ -250,19 +254,6 @@ impl Module { } } -fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile) -> Option -where - N: AstNode, - DEF: AstItemDef, -{ - let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax())); - let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?; - let ctx = LocationCtx::new(db, module.id, src.file_id); - let items = db.ast_id_map(src.file_id); - let item_id = items.ast_id(&src.value); - Some(DEF::from_ast_id(ctx, item_id)) -} - fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap { _analyze_container(db, src).unwrap_or_default() } diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs index 8f35a3fbb..72afecf26 100644 --- a/crates/ra_hir/src/has_source.rs +++ b/crates/ra_hir/src/has_source.rs @@ -4,7 +4,7 @@ use either::Either; use hir_def::{ nameres::ModuleSource, src::{HasChildSource, HasSource as _}, - AstItemDef, Lookup, VariantId, + Lookup, VariantId, }; use ra_syntax::ast; @@ -57,13 +57,13 @@ impl HasSource for Struct { impl HasSource for Union { type Ast = ast::UnionDef; fn source(self, db: &impl DefDatabase) -> InFile { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for Enum { type Ast = ast::EnumDef; fn source(self, db: &impl DefDatabase) -> InFile { - self.id.source(db) + self.id.lookup(db).source(db) } } impl HasSource for EnumVariant { diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index ef2b20f1e..ec3d57d1a 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs @@ -11,9 +11,8 @@ use ra_arena::{map::ArenaMap, Arena}; use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; use crate::{ - db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, - AstItemDef, EnumId, LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, UnionId, - VariantId, + db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, EnumId, + LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, UnionId, VariantId, }; /// Note that we use `StructData` for unions as well! @@ -58,7 +57,7 @@ impl StructData { Arc::new(StructData { name, variant_data }) } pub(crate) fn union_data_query(db: &impl DefDatabase, id: UnionId) -> 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 @@ -73,7 +72,7 @@ impl StructData { impl EnumData { pub(crate) fn enum_data_query(db: &impl DefDatabase, e: EnumId) -> Arc { - let src = e.source(db); + let src = e.lookup(db).source(db); let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); let mut trace = Trace::new_for_arena(); lower_enum(&mut trace, &src.value); @@ -90,7 +89,7 @@ impl HasChildSource for EnumId { type ChildId = LocalEnumVariantId; type Value = ast::EnumVariant; fn child_source(&self, db: &impl DefDatabase) -> InFile> { - let src = self.source(db); + let src = self.lookup(db).source(db); let mut trace = Trace::new_for_map(); lower_enum(&mut trace, &src.value); src.with_value(trace.into_map()) @@ -155,7 +154,7 @@ impl HasChildSource for VariantId { src.map(|map| map[it.local_id].kind()) } VariantId::StructId(it) => it.lookup(db).source(db).map(|it| it.kind()), - VariantId::UnionId(it) => it.source(db).map(|it| { + VariantId::UnionId(it) => it.lookup(db).source(db).map(|it| { it.record_field_def_list() .map(ast::StructKind::Record) .unwrap_or(ast::StructKind::Unit) diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index d2aa5ce8f..5bf82e191 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs @@ -12,8 +12,7 @@ use ra_syntax::{ use tt::Subtree; use crate::{ - db::DefDatabase, path::Path, src::HasChildSource, src::HasSource, AdtId, AstItemDef, AttrDefId, - Lookup, + db::DefDatabase, path::Path, src::HasChildSource, src::HasSource, AdtId, AttrDefId, Lookup, }; #[derive(Default, Debug, Clone, PartialEq, Eq)] @@ -57,8 +56,8 @@ impl Attrs { } AttrDefId::AdtId(it) => match it { 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), + AdtId::EnumId(it) => attrs_from_loc(it.lookup(db), db), + AdtId::UnionId(it) => attrs_from_loc(it.lookup(db), db), }, AttrDefId::TraitId(it) => attrs_from_loc(it.lookup(db), db), AttrDefId::MacroDefId(it) => { diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs index eba361578..3c9379b15 100644 --- a/crates/ra_hir_def/src/child_by_source.rs +++ b/crates/ra_hir_def/src/child_by_source.rs @@ -103,8 +103,14 @@ impl ChildBySource for ModuleId { let src = strukt.lookup(db).source(db); res[keys::STRUCT].insert(src, strukt) } - AdtId::UnionId(_) => (), - AdtId::EnumId(_) => (), + AdtId::UnionId(union_) => { + let src = union_.lookup(db).source(db); + res[keys::UNION].insert(src, union_) + } + AdtId::EnumId(enum_) => { + let src = enum_.lookup(db).source(db); + res[keys::ENUM].insert(src, enum_) + } }, _ => (), } diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index e120c7768..98bff6cb7 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use hir_expand::{db::AstDatabase, HirFileId}; use ra_db::{salsa, CrateId, SourceDatabase}; -use ra_syntax::{ast, SmolStr}; +use ra_syntax::SmolStr; use crate::{ adt::{EnumData, StructData}, @@ -17,9 +17,9 @@ use crate::{ raw::{ImportSourceMap, RawItems}, CrateDefMap, }, - AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId, - ImplId, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, - TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, + AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc, + GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, + TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, }; #[salsa::query_group(InternDatabaseStorage)] @@ -29,9 +29,9 @@ pub trait InternDatabase: SourceDatabase { #[salsa::interned] fn intern_struct(&self, loc: StructLoc) -> StructId; #[salsa::interned] - fn intern_union(&self, loc: ItemLoc) -> UnionId; + fn intern_union(&self, loc: UnionLoc) -> UnionId; #[salsa::interned] - fn intern_enum(&self, loc: ItemLoc) -> EnumId; + fn intern_enum(&self, loc: EnumLoc) -> EnumId; #[salsa::interned] fn intern_const(&self, loc: ConstLoc) -> ConstId; #[salsa::interned] diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs index 58143b894..b29f142e3 100644 --- a/crates/ra_hir_def/src/docs.rs +++ b/crates/ra_hir_def/src/docs.rs @@ -11,7 +11,7 @@ use ra_syntax::ast; use crate::{ db::DefDatabase, src::{HasChildSource, HasSource}, - AdtId, AstItemDef, AttrDefId, Lookup, + AdtId, AttrDefId, Lookup, }; /// Holds documentation @@ -52,8 +52,8 @@ impl Documentation { } AttrDefId::AdtId(it) => match it { 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), + AdtId::EnumId(it) => docs_from_ast(&it.lookup(db).source(db).value), + AdtId::UnionId(it) => docs_from_ast(&it.lookup(db).source(db).value), }, AttrDefId::EnumVariantId(it) => { let src = it.parent.child_source(db); diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index f1ce8d59a..e502dd798 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs @@ -21,7 +21,7 @@ use crate::{ src::HasChildSource, src::HasSource, type_ref::{TypeBound, TypeRef}, - AdtId, AstItemDef, GenericDefId, LocalTypeParamId, Lookup, TypeParamId, + AdtId, GenericDefId, LocalTypeParamId, Lookup, TypeParamId, }; /// Data about a generic parameter (to a function, struct, impl, ...). @@ -76,12 +76,12 @@ impl GenericParams { src.file_id } GenericDefId::AdtId(AdtId::UnionId(it)) => { - let src = it.source(db); + let src = it.lookup(db).source(db); generics.fill(&mut sm, &src.value); src.file_id } GenericDefId::AdtId(AdtId::EnumId(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 758cf8097..ada145379 100644 --- a/crates/ra_hir_def/src/keys.rs +++ b/crates/ra_hir_def/src/keys.rs @@ -9,7 +9,7 @@ use rustc_hash::FxHashMap; use crate::{ dyn_map::{DynMap, Policy}, ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId, - TypeAliasId, TypeParamId, + TypeAliasId, TypeParamId, EnumId, UnionId, }; type Key = crate::dyn_map::Key, V, AstPtrPolicy>; @@ -21,6 +21,8 @@ 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 UNION: Key = Key::new(); +pub const ENUM: 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 1a599706a..c9e4e6a0f 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -173,24 +173,48 @@ impl Lookup for StructId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct UnionId(salsa::InternId); impl_intern_key!(UnionId); -impl AstItemDef for UnionId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_union(loc) + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct UnionLoc { + pub container: ModuleId, + pub ast_id: AstId, +} + +impl Intern for UnionLoc { + type ID = UnionId; + fn intern(self, db: &impl db::DefDatabase) -> UnionId { + db.intern_union(self) } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_union(self) +} + +impl Lookup for UnionId { + type Data = UnionLoc; + fn lookup(&self, db: &impl db::DefDatabase) -> UnionLoc { + db.lookup_intern_union(*self) } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct EnumId(salsa::InternId); impl_intern_key!(EnumId); -impl AstItemDef for EnumId { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self { - db.intern_enum(loc) + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct EnumLoc { + pub container: ModuleId, + pub ast_id: AstId, +} + +impl Intern for EnumLoc { + type ID = EnumId; + fn intern(self, db: &impl db::DefDatabase) -> EnumId { + db.intern_enum(self) } - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc { - db.lookup_intern_enum(self) +} + +impl Lookup for EnumId { + type Data = EnumLoc; + fn lookup(&self, db: &impl db::DefDatabase) -> EnumLoc { + db.lookup_intern_enum(*self) } } @@ -545,8 +569,8 @@ impl HasModule for AdtId { fn module(&self, db: &impl db::DefDatabase) -> ModuleId { match self { AdtId::StructId(it) => it.lookup(db).container, - AdtId::UnionId(it) => it.module(db), - AdtId::EnumId(it) => it.module(db), + AdtId::UnionId(it) => it.lookup(db).container, + AdtId::EnumId(it) => it.lookup(db).container, } } } @@ -569,7 +593,7 @@ impl HasModule for GenericDefId { GenericDefId::TraitId(it) => it.lookup(db).container, GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), GenericDefId::ImplId(it) => it.lookup(db).container, - GenericDefId::EnumVariantId(it) => it.parent.module(db), + GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container, 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 602ec0911..04aadead1 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -24,9 +24,9 @@ use crate::{ }, path::{Path, PathKind}, per_ns::PerNs, - AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplLoc, - Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructLoc, - TraitLoc, TypeAliasLoc, UnionId, + AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, + LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, + TypeAliasLoc, UnionLoc, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -753,8 +753,6 @@ where 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 }; - let ctx = LocationCtx::new(self.def_collector.db, module, self.file_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 @@ -778,10 +776,15 @@ where PerNs::both(def.into(), def.into()) } raw::DefKind::Union(ast_id) => { - let id = UnionId::from_ast_id(ctx, ast_id).into(); - PerNs::both(id, id) + let def = UnionLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db); + PerNs::both(def.into(), def.into()) + } + raw::DefKind::Enum(ast_id) => { + let def = EnumLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } + .intern(self.def_collector.db); + PerNs::types(def.into()) } - raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()), raw::DefKind::Const(ast_id) => { let def = ConstLoc { container: ContainerId::ModuleId(module), diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs index 7e212adb1..20200d1db 100644 --- a/crates/ra_hir_def/src/src.rs +++ b/crates/ra_hir_def/src/src.rs @@ -5,7 +5,8 @@ use ra_arena::map::ArenaMap; use ra_syntax::ast; use crate::{ - db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, + db::DefDatabase, ConstLoc, EnumLoc, FunctionLoc, ImplLoc, StaticLoc, StructLoc, TraitLoc, + TypeAliasLoc, UnionLoc, }; pub trait HasSource { @@ -76,6 +77,24 @@ impl HasSource for StructLoc { } } +impl HasSource for UnionLoc { + type Value = ast::UnionDef; + + fn source(&self, db: &impl DefDatabase) -> InFile { + let node = self.ast_id.to_node(db); + InFile::new(self.ast_id.file_id, node) + } +} + +impl HasSource for EnumLoc { + type Value = ast::EnumDef; + + 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; diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index 6af5bf50a..5f795bc02 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs @@ -14,7 +14,7 @@ use hir_def::{ path::{GenericArg, Path, PathKind, PathSegment}, resolver::{HasResolver, Resolver, TypeNs}, type_ref::{TypeBound, TypeRef}, - AdtId, AstItemDef, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, + AdtId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, LocalStructFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, UnionId, VariantId, }; use ra_arena::map::ArenaMap; @@ -698,10 +698,11 @@ impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId); impl CallableDef { pub fn krate(self, db: &impl HirDatabase) -> CrateId { match self { - CallableDef::FunctionId(f) => f.lookup(db).module(db).krate, - CallableDef::StructId(s) => s.lookup(db).container.krate, - CallableDef::EnumVariantId(e) => e.parent.module(db).krate, + CallableDef::FunctionId(f) => f.lookup(db).module(db), + CallableDef::StructId(s) => s.lookup(db).container, + CallableDef::EnumVariantId(e) => e.parent.lookup(db).container, } + .krate } } -- cgit v1.2.3 From 7a255a2f9381ba5886cacc48c1dd0420a739a55c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 12 Dec 2019 15:13:05 +0100 Subject: Remove old location infra --- crates/ra_hir_def/src/keys.rs | 4 +-- crates/ra_hir_def/src/lib.rs | 65 +++---------------------------------------- 2 files changed, 6 insertions(+), 63 deletions(-) diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs index ada145379..4611c6e38 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, StructId, TraitId, - TypeAliasId, TypeParamId, EnumId, UnionId, + ConstId, EnumId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId, + TypeAliasId, TypeParamId, UnionId, }; type Key = crate::dyn_map::Key, V, AstPtrPolicy>; diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index c9e4e6a0f..f085bbe87 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -40,14 +40,14 @@ mod test_db; #[cfg(test)] mod marks; -use std::hash::{Hash, Hasher}; +use std::hash::Hash; -use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, InFile, MacroDefId}; +use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId}; use ra_arena::{impl_arena_id, RawId}; use ra_db::{impl_intern_key, salsa, CrateId}; -use ra_syntax::{ast, AstNode}; +use ra_syntax::ast; -use crate::{builtin_type::BuiltinType, db::InternDatabase}; +use crate::builtin_type::BuiltinType; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct LocalImportId(RawId); @@ -65,63 +65,6 @@ pub struct ModuleId { pub struct LocalModuleId(RawId); impl_arena_id!(LocalModuleId); -#[derive(Debug)] -pub struct ItemLoc { - pub(crate) module: ModuleId, - ast_id: AstId, -} - -impl PartialEq for ItemLoc { - fn eq(&self, other: &Self) -> bool { - self.module == other.module && self.ast_id == other.ast_id - } -} -impl Eq for ItemLoc {} -impl Hash for ItemLoc { - fn hash(&self, hasher: &mut H) { - self.module.hash(hasher); - self.ast_id.hash(hasher); - } -} - -impl Clone for ItemLoc { - fn clone(&self) -> ItemLoc { - ItemLoc { module: self.module, ast_id: self.ast_id } - } -} - -#[derive(Clone, Copy)] -pub struct LocationCtx { - db: DB, - module: ModuleId, - file_id: HirFileId, -} - -impl<'a, DB> LocationCtx<&'a DB> { - pub fn new(db: &'a DB, module: ModuleId, file_id: HirFileId) -> LocationCtx<&'a DB> { - LocationCtx { db, module, file_id } - } -} - -pub trait AstItemDef: salsa::InternKey + Clone { - fn intern(db: &impl InternDatabase, loc: ItemLoc) -> Self; - fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc; - - fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId) -> Self { - let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) }; - Self::intern(ctx.db, loc) - } - fn source(self, db: &(impl AstDatabase + InternDatabase)) -> InFile { - let loc = self.lookup_intern(db); - let value = loc.ast_id.to_node(db); - InFile { file_id: loc.ast_id.file_id, value } - } - fn module(self, db: &impl InternDatabase) -> ModuleId { - let loc = self.lookup_intern(db); - loc.module - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct FunctionId(salsa::InternId); impl_intern_key!(FunctionId); -- cgit v1.2.3