aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/body.rs1
-rw-r--r--crates/ra_hir_def/src/db.rs13
-rw-r--r--crates/ra_hir_def/src/generics.rs2
-rw-r--r--crates/ra_hir_def/src/impls.rs25
-rw-r--r--crates/ra_hir_def/src/lib.rs103
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs28
-rw-r--r--crates/ra_hir_def/src/traits.rs53
7 files changed, 142 insertions, 83 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index b69d4dea6..dfb79a30a 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -155,6 +155,7 @@ impl Body {
155 (src.file_id, f.module(db), src.value.body().map(ast::Expr::from)) 155 (src.file_id, f.module(db), src.value.body().map(ast::Expr::from))
156 } 156 }
157 DefWithBodyId::ConstId(c) => { 157 DefWithBodyId::ConstId(c) => {
158 let c = c.lookup(db);
158 let src = c.source(db); 159 let src = c.source(db);
159 (src.file_id, c.module(db), src.value.body()) 160 (src.file_id, c.module(db), src.value.body())
160 } 161 }
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index e4ffdebe9..c6cd4369b 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -13,26 +13,26 @@ use crate::{
13 raw::{ImportSourceMap, RawItems}, 13 raw::{ImportSourceMap, RawItems},
14 CrateDefMap, 14 CrateDefMap,
15 }, 15 },
16 traits::{TraitData, TraitItemsIndex}, 16 traits::TraitData,
17 DefWithBodyId, EnumId, FunctionLoc, ImplId, ItemLoc, ModuleId, StructOrUnionId, TraitId, 17 DefWithBodyId, EnumId, ImplId, ItemLoc, StructOrUnionId, TraitId,
18}; 18};
19 19
20#[salsa::query_group(InternDatabaseStorage)] 20#[salsa::query_group(InternDatabaseStorage)]
21pub trait InternDatabase: SourceDatabase { 21pub trait InternDatabase: SourceDatabase {
22 #[salsa::interned] 22 #[salsa::interned]
23 fn intern_function(&self, loc: FunctionLoc) -> crate::FunctionId; 23 fn intern_function(&self, loc: crate::FunctionLoc) -> crate::FunctionId;
24 #[salsa::interned] 24 #[salsa::interned]
25 fn intern_struct_or_union(&self, loc: ItemLoc<ast::StructDef>) -> crate::StructOrUnionId; 25 fn intern_struct_or_union(&self, loc: ItemLoc<ast::StructDef>) -> crate::StructOrUnionId;
26 #[salsa::interned] 26 #[salsa::interned]
27 fn intern_enum(&self, loc: ItemLoc<ast::EnumDef>) -> crate::EnumId; 27 fn intern_enum(&self, loc: ItemLoc<ast::EnumDef>) -> crate::EnumId;
28 #[salsa::interned] 28 #[salsa::interned]
29 fn intern_const(&self, loc: ItemLoc<ast::ConstDef>) -> crate::ConstId; 29 fn intern_const(&self, loc: crate::ConstLoc) -> crate::ConstId;
30 #[salsa::interned] 30 #[salsa::interned]
31 fn intern_static(&self, loc: ItemLoc<ast::StaticDef>) -> crate::StaticId; 31 fn intern_static(&self, loc: ItemLoc<ast::StaticDef>) -> crate::StaticId;
32 #[salsa::interned] 32 #[salsa::interned]
33 fn intern_trait(&self, loc: ItemLoc<ast::TraitDef>) -> crate::TraitId; 33 fn intern_trait(&self, loc: ItemLoc<ast::TraitDef>) -> crate::TraitId;
34 #[salsa::interned] 34 #[salsa::interned]
35 fn intern_type_alias(&self, loc: ItemLoc<ast::TypeAliasDef>) -> crate::TypeAliasId; 35 fn intern_type_alias(&self, loc: crate::TypeAliasLoc) -> crate::TypeAliasId;
36 #[salsa::interned] 36 #[salsa::interned]
37 fn intern_impl(&self, loc: ItemLoc<ast::ImplBlock>) -> crate::ImplId; 37 fn intern_impl(&self, loc: ItemLoc<ast::ImplBlock>) -> crate::ImplId;
38} 38}
@@ -63,9 +63,6 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
63 #[salsa::invoke(TraitData::trait_data_query)] 63 #[salsa::invoke(TraitData::trait_data_query)]
64 fn trait_data(&self, e: TraitId) -> Arc<TraitData>; 64 fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
65 65
66 #[salsa::invoke(TraitItemsIndex::trait_items_index)]
67 fn trait_items_index(&self, module: ModuleId) -> TraitItemsIndex;
68
69 #[salsa::invoke(Body::body_with_source_map_query)] 66 #[salsa::invoke(Body::body_with_source_map_query)]
70 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>); 67 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
71 68
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs
index 11dd2a948..17a5d5f43 100644
--- a/crates/ra_hir_def/src/generics.rs
+++ b/crates/ra_hir_def/src/generics.rs
@@ -72,7 +72,7 @@ impl GenericParams {
72 let self_param = TypeRef::Path(name::SELF_TYPE.into()); 72 let self_param = TypeRef::Path(name::SELF_TYPE.into());
73 generics.fill_bounds(&it.source(db).value, self_param); 73 generics.fill_bounds(&it.source(db).value, self_param);
74 } 74 }
75 GenericDefId::TypeAliasId(it) => generics.fill(&it.source(db).value, start), 75 GenericDefId::TypeAliasId(it) => generics.fill(&it.lookup(db).source(db).value, start),
76 // Note that we don't add `Self` here: in `impl`s, `Self` is not a 76 // Note that we don't add `Self` here: in `impl`s, `Self` is not a
77 // type-parameter, but rather is a type-alias for impl's target 77 // type-parameter, but rather is a type-alias for impl's target
78 // type, so this is handled by the resolver. 78 // type, so this is handled by the resolver.
diff --git a/crates/ra_hir_def/src/impls.rs b/crates/ra_hir_def/src/impls.rs
index 9be38c5e1..750a869f2 100644
--- a/crates/ra_hir_def/src/impls.rs
+++ b/crates/ra_hir_def/src/impls.rs
@@ -9,8 +9,8 @@ use hir_expand::AstId;
9use ra_syntax::ast; 9use ra_syntax::ast;
10 10
11use crate::{ 11use crate::{
12 db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, FunctionContainerId, 12 db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstLoc, ContainerId,
13 FunctionLoc, ImplId, Intern, LocationCtx, TypeAliasId, 13 FunctionLoc, ImplId, Intern, TypeAliasLoc,
14}; 14};
15 15
16#[derive(Debug, Clone, PartialEq, Eq)] 16#[derive(Debug, Clone, PartialEq, Eq)]
@@ -31,23 +31,32 @@ impl ImplData {
31 let negative = src.value.is_negative(); 31 let negative = src.value.is_negative();
32 32
33 let items = if let Some(item_list) = src.value.item_list() { 33 let items = if let Some(item_list) = src.value.item_list() {
34 let ctx = LocationCtx::new(db, id.module(db), src.file_id);
35 item_list 34 item_list
36 .impl_items() 35 .impl_items()
37 .map(|item_node| match item_node { 36 .map(|item_node| match item_node {
38 ast::ImplItem::FnDef(it) => { 37 ast::ImplItem::FnDef(it) => {
39 let func_id = FunctionLoc { 38 let def = FunctionLoc {
40 container: FunctionContainerId::ImplId(id), 39 container: ContainerId::ImplId(id),
41 ast_id: AstId::new(src.file_id, items.ast_id(&it)), 40 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
42 } 41 }
43 .intern(db); 42 .intern(db);
44 func_id.into() 43 def.into()
45 } 44 }
46 ast::ImplItem::ConstDef(it) => { 45 ast::ImplItem::ConstDef(it) => {
47 ConstId::from_ast_id(ctx, items.ast_id(&it)).into() 46 let def = ConstLoc {
47 container: ContainerId::ImplId(id),
48 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
49 }
50 .intern(db);
51 def.into()
48 } 52 }
49 ast::ImplItem::TypeAliasDef(it) => { 53 ast::ImplItem::TypeAliasDef(it) => {
50 TypeAliasId::from_ast_id(ctx, items.ast_id(&it)).into() 54 let def = TypeAliasLoc {
55 container: ContainerId::ImplId(id),
56 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
57 }
58 .intern(db);
59 def.into()
51 } 60 }
52 }) 61 })
53 .collect() 62 .collect()
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index b9a13776f..0af41de87 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -201,7 +201,7 @@ impl_intern_key!(FunctionId);
201 201
202#[derive(Debug, Clone, PartialEq, Eq, Hash)] 202#[derive(Debug, Clone, PartialEq, Eq, Hash)]
203pub struct FunctionLoc { 203pub struct FunctionLoc {
204 pub container: FunctionContainerId, 204 pub container: ContainerId,
205 pub ast_id: AstId<ast::FnDef>, 205 pub ast_id: AstId<ast::FnDef>,
206} 206}
207 207
@@ -220,13 +220,6 @@ impl Lookup for FunctionId {
220} 220}
221 221
222#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 222#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
223pub enum FunctionContainerId {
224 ModuleId(ModuleId),
225 ImplId(ImplId),
226 TraitId(TraitId),
227}
228
229#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
230pub struct StructOrUnionId(salsa::InternId); 223pub struct StructOrUnionId(salsa::InternId);
231impl_intern_key!(StructOrUnionId); 224impl_intern_key!(StructOrUnionId);
232impl AstItemDef<ast::StructDef> for StructOrUnionId { 225impl AstItemDef<ast::StructDef> for StructOrUnionId {
@@ -296,12 +289,23 @@ impl_arena_id!(LocalStructFieldId);
296#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 289#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
297pub struct ConstId(salsa::InternId); 290pub struct ConstId(salsa::InternId);
298impl_intern_key!(ConstId); 291impl_intern_key!(ConstId);
299impl AstItemDef<ast::ConstDef> for ConstId { 292#[derive(Debug, Clone, PartialEq, Eq, Hash)]
300 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self { 293pub struct ConstLoc {
301 db.intern_const(loc) 294 pub container: ContainerId,
295 pub ast_id: AstId<ast::ConstDef>,
296}
297
298impl Intern for ConstLoc {
299 type ID = ConstId;
300 fn intern(self, db: &impl db::DefDatabase2) -> ConstId {
301 db.intern_const(self)
302 } 302 }
303 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> { 303}
304 db.lookup_intern_const(self) 304
305impl Lookup for ConstId {
306 type Data = ConstLoc;
307 fn lookup(&self, db: &impl db::DefDatabase2) -> ConstLoc {
308 db.lookup_intern_const(*self)
305 } 309 }
306} 310}
307 311
@@ -332,12 +336,24 @@ impl AstItemDef<ast::TraitDef> for TraitId {
332#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 336#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
333pub struct TypeAliasId(salsa::InternId); 337pub struct TypeAliasId(salsa::InternId);
334impl_intern_key!(TypeAliasId); 338impl_intern_key!(TypeAliasId);
335impl AstItemDef<ast::TypeAliasDef> for TypeAliasId { 339
336 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TypeAliasDef>) -> Self { 340#[derive(Debug, Clone, PartialEq, Eq, Hash)]
337 db.intern_type_alias(loc) 341pub struct TypeAliasLoc {
342 pub container: ContainerId,
343 pub ast_id: AstId<ast::TypeAliasDef>,
344}
345
346impl Intern for TypeAliasLoc {
347 type ID = TypeAliasId;
348 fn intern(self, db: &impl db::DefDatabase2) -> TypeAliasId {
349 db.intern_type_alias(self)
338 } 350 }
339 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TypeAliasDef> { 351}
340 db.lookup_intern_type_alias(self) 352
353impl Lookup for TypeAliasId {
354 type Data = TypeAliasLoc;
355 fn lookup(&self, db: &impl db::DefDatabase2) -> TypeAliasLoc {
356 db.lookup_intern_type_alias(*self)
341 } 357 }
342} 358}
343 359
@@ -372,6 +388,13 @@ macro_rules! impl_froms {
372 } 388 }
373} 389}
374 390
391#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
392pub enum ContainerId {
393 ModuleId(ModuleId),
394 ImplId(ImplId),
395 TraitId(TraitId),
396}
397
375/// A Data Type 398/// A Data Type
376#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 399#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
377pub enum AdtId { 400pub enum AdtId {
@@ -469,9 +492,29 @@ pub trait HasModule {
469impl HasModule for FunctionLoc { 492impl HasModule for FunctionLoc {
470 fn module(&self, db: &impl db::DefDatabase2) -> ModuleId { 493 fn module(&self, db: &impl db::DefDatabase2) -> ModuleId {
471 match self.container { 494 match self.container {
472 FunctionContainerId::ModuleId(it) => it, 495 ContainerId::ModuleId(it) => it,
473 FunctionContainerId::ImplId(it) => it.module(db), 496 ContainerId::ImplId(it) => it.module(db),
474 FunctionContainerId::TraitId(it) => it.module(db), 497 ContainerId::TraitId(it) => it.module(db),
498 }
499 }
500}
501
502impl HasModule for TypeAliasLoc {
503 fn module(&self, db: &impl db::DefDatabase2) -> ModuleId {
504 match self.container {
505 ContainerId::ModuleId(it) => it,
506 ContainerId::ImplId(it) => it.module(db),
507 ContainerId::TraitId(it) => it.module(db),
508 }
509 }
510}
511
512impl HasModule for ConstLoc {
513 fn module(&self, db: &impl db::DefDatabase2) -> ModuleId {
514 match self.container {
515 ContainerId::ModuleId(it) => it,
516 ContainerId::ImplId(it) => it.module(db),
517 ContainerId::TraitId(it) => it.module(db),
475 } 518 }
476 } 519 }
477} 520}
@@ -489,3 +532,21 @@ impl HasSource for FunctionLoc {
489 Source::new(self.ast_id.file_id(), node) 532 Source::new(self.ast_id.file_id(), node)
490 } 533 }
491} 534}
535
536impl HasSource for TypeAliasLoc {
537 type Value = ast::TypeAliasDef;
538
539 fn source(&self, db: &impl db::DefDatabase2) -> Source<ast::TypeAliasDef> {
540 let node = self.ast_id.to_node(db);
541 Source::new(self.ast_id.file_id(), node)
542 }
543}
544
545impl HasSource for ConstLoc {
546 type Value = ast::ConstDef;
547
548 fn source(&self, db: &impl db::DefDatabase2) -> Source<ast::ConstDef> {
549 let node = self.ast_id.to_node(db);
550 Source::new(self.ast_id.file_id(), node)
551 }
552}
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index d2ed94a87..aae3dcadf 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -19,9 +19,9 @@ use crate::{
19 per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode, 19 per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode,
20 }, 20 },
21 path::{Path, PathKind}, 21 path::{Path, PathKind},
22 AdtId, AstId, AstItemDef, ConstId, CrateModuleId, EnumId, EnumVariantId, FunctionContainerId, 22 AdtId, AstId, AstItemDef, ConstLoc, ContainerId, CrateModuleId, EnumId, EnumVariantId,
23 FunctionLoc, ImplId, Intern, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, 23 FunctionLoc, ImplId, Intern, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId,
24 StructOrUnionId, TraitId, TypeAliasId, UnionId, 24 StructOrUnionId, TraitId, TypeAliasLoc, UnionId,
25}; 25};
26 26
27pub(super) fn collect_defs(db: &impl DefDatabase2, mut def_map: CrateDefMap) -> CrateDefMap { 27pub(super) fn collect_defs(db: &impl DefDatabase2, mut def_map: CrateDefMap) -> CrateDefMap {
@@ -673,13 +673,13 @@ where
673 let name = def.name.clone(); 673 let name = def.name.clone();
674 let def: PerNs = match def.kind { 674 let def: PerNs = match def.kind {
675 raw::DefKind::Function(ast_id) => { 675 raw::DefKind::Function(ast_id) => {
676 let f = FunctionLoc { 676 let def = FunctionLoc {
677 container: FunctionContainerId::ModuleId(module), 677 container: ContainerId::ModuleId(module),
678 ast_id: AstId::new(self.file_id, ast_id), 678 ast_id: AstId::new(self.file_id, ast_id),
679 } 679 }
680 .intern(self.def_collector.db); 680 .intern(self.def_collector.db);
681 681
682 PerNs::values(f.into()) 682 PerNs::values(def.into())
683 } 683 }
684 raw::DefKind::Struct(ast_id) => { 684 raw::DefKind::Struct(ast_id) => {
685 let id = StructOrUnionId::from_ast_id(ctx, ast_id).into(); 685 let id = StructOrUnionId::from_ast_id(ctx, ast_id).into();
@@ -692,13 +692,27 @@ where
692 PerNs::both(u, u) 692 PerNs::both(u, u)
693 } 693 }
694 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()), 694 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()),
695 raw::DefKind::Const(ast_id) => PerNs::values(ConstId::from_ast_id(ctx, ast_id).into()), 695 raw::DefKind::Const(ast_id) => {
696 let def = ConstLoc {
697 container: ContainerId::ModuleId(module),
698 ast_id: AstId::new(self.file_id, ast_id),
699 }
700 .intern(self.def_collector.db);
701
702 PerNs::values(def.into())
703 }
696 raw::DefKind::Static(ast_id) => { 704 raw::DefKind::Static(ast_id) => {
697 PerNs::values(StaticId::from_ast_id(ctx, ast_id).into()) 705 PerNs::values(StaticId::from_ast_id(ctx, ast_id).into())
698 } 706 }
699 raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()), 707 raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()),
700 raw::DefKind::TypeAlias(ast_id) => { 708 raw::DefKind::TypeAlias(ast_id) => {
701 PerNs::types(TypeAliasId::from_ast_id(ctx, ast_id).into()) 709 let def = TypeAliasLoc {
710 container: ContainerId::ModuleId(module),
711 ast_id: AstId::new(self.file_id, ast_id),
712 }
713 .intern(self.def_collector.db);
714
715 PerNs::types(def.into())
702 } 716 }
703 }; 717 };
704 let resolution = Resolution { def, import: None }; 718 let resolution = Resolution { def, import: None };
diff --git a/crates/ra_hir_def/src/traits.rs b/crates/ra_hir_def/src/traits.rs
index 6e36bc0d0..877d73d66 100644
--- a/crates/ra_hir_def/src/traits.rs
+++ b/crates/ra_hir_def/src/traits.rs
@@ -8,11 +8,10 @@ use hir_expand::{
8}; 8};
9 9
10use ra_syntax::ast::{self, NameOwner}; 10use ra_syntax::ast::{self, NameOwner};
11use rustc_hash::FxHashMap;
12 11
13use crate::{ 12use crate::{
14 db::DefDatabase2, AssocItemId, AstItemDef, ConstId, FunctionContainerId, FunctionLoc, Intern, 13 db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId,
15 LocationCtx, ModuleDefId, ModuleId, TraitId, TypeAliasId, 14 TypeAliasLoc,
16}; 15};
17 16
18#[derive(Debug, Clone, PartialEq, Eq)] 17#[derive(Debug, Clone, PartialEq, Eq)]
@@ -26,8 +25,6 @@ impl TraitData {
26 pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> { 25 pub(crate) fn trait_data_query(db: &impl DefDatabase2, tr: TraitId) -> Arc<TraitData> {
27 let src = tr.source(db); 26 let src = tr.source(db);
28 let name = src.value.name().map(|n| n.as_name()); 27 let name = src.value.name().map(|n| n.as_name());
29 let module = tr.module(db);
30 let ctx = LocationCtx::new(db, module, src.file_id);
31 let auto = src.value.is_auto(); 28 let auto = src.value.is_auto();
32 let ast_id_map = db.ast_id_map(src.file_id); 29 let ast_id_map = db.ast_id_map(src.file_id);
33 let items = if let Some(item_list) = src.value.item_list() { 30 let items = if let Some(item_list) = src.value.item_list() {
@@ -35,13 +32,23 @@ impl TraitData {
35 .impl_items() 32 .impl_items()
36 .map(|item_node| match item_node { 33 .map(|item_node| match item_node {
37 ast::ImplItem::FnDef(it) => FunctionLoc { 34 ast::ImplItem::FnDef(it) => FunctionLoc {
38 container: FunctionContainerId::TraitId(tr), 35 container: ContainerId::TraitId(tr),
36 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
37 }
38 .intern(db)
39 .into(),
40 ast::ImplItem::ConstDef(it) => ConstLoc {
41 container: ContainerId::TraitId(tr),
42 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
43 }
44 .intern(db)
45 .into(),
46 ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc {
47 container: ContainerId::TraitId(tr),
39 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), 48 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
40 } 49 }
41 .intern(db) 50 .intern(db)
42 .into(), 51 .into(),
43 ast::ImplItem::ConstDef(it) => ConstId::from_ast(ctx, &it).into(),
44 ast::ImplItem::TypeAliasDef(it) => TypeAliasId::from_ast(ctx, &it).into(),
45 }) 52 })
46 .collect() 53 .collect()
47 } else { 54 } else {
@@ -50,33 +57,3 @@ impl TraitData {
50 Arc::new(TraitData { name, items, auto }) 57 Arc::new(TraitData { name, items, auto })
51 } 58 }
52} 59}
53
54#[derive(Debug, Clone, PartialEq, Eq)]
55pub struct TraitItemsIndex {
56 traits_by_def: FxHashMap<AssocItemId, TraitId>,
57}
58
59impl TraitItemsIndex {
60 pub fn trait_items_index(db: &impl DefDatabase2, module: ModuleId) -> TraitItemsIndex {
61 let mut index = TraitItemsIndex { traits_by_def: FxHashMap::default() };
62 let crate_def_map = db.crate_def_map(module.krate);
63 for decl in crate_def_map[module.module_id].scope.declarations() {
64 if let ModuleDefId::TraitId(tr) = decl {
65 for item in db.trait_data(tr).items.iter() {
66 match item {
67 AssocItemId::FunctionId(_) => (),
68 _ => {
69 let prev = index.traits_by_def.insert(*item, tr);
70 assert!(prev.is_none());
71 }
72 }
73 }
74 }
75 }
76 index
77 }
78
79 pub fn get_parent_trait(&self, item: AssocItemId) -> Option<TraitId> {
80 self.traits_by_def.get(&item).cloned()
81 }
82}