diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-20 15:45:22 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-20 15:45:22 +0000 |
commit | c51dcb1c4bdd71f9f17de508bd0d47e3c06ae1d5 (patch) | |
tree | 7e96d773620a3b03254d00386711cdc7c909e3ee /crates/ra_hir_def/src/traits.rs | |
parent | b7a36b54431ca5b746af53549a1b6e142570c7f4 (diff) | |
parent | 111891dc2dc1d2c7ea87144e8e3ddefe23fc7b6d (diff) |
Merge #2327
2327: Move constants to new ID r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/traits.rs')
-rw-r--r-- | crates/ra_hir_def/src/traits.rs | 53 |
1 files changed, 15 insertions, 38 deletions
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 | ||
10 | use ra_syntax::ast::{self, NameOwner}; | 10 | use ra_syntax::ast::{self, NameOwner}; |
11 | use rustc_hash::FxHashMap; | ||
12 | 11 | ||
13 | use crate::{ | 12 | use 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)] | ||
55 | pub struct TraitItemsIndex { | ||
56 | traits_by_def: FxHashMap<AssocItemId, TraitId>, | ||
57 | } | ||
58 | |||
59 | impl 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 | } | ||