diff options
author | Aleksey Kladov <[email protected]> | 2019-11-20 15:00:01 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-20 15:41:23 +0000 |
commit | 111891dc2dc1d2c7ea87144e8e3ddefe23fc7b6d (patch) | |
tree | 7e96d773620a3b03254d00386711cdc7c909e3ee /crates | |
parent | ee95a35664e6fe9153f6324cfc57872ca365887c (diff) |
Move constants to new ID
This allows us to get rid of trait item index
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 25 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model/src.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/db.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 25 | ||||
-rw-r--r-- | crates/ra_hir/src/impl_block.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir/src/ty.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir_def/src/db.rs | 9 | ||||
-rw-r--r-- | crates/ra_hir_def/src/impls.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 40 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir_def/src/traits.rs | 45 | ||||
-rw-r--r-- | crates/ra_ide_api/src/change.rs | 1 | ||||
-rw-r--r-- | crates/ra_ide_api/src/hover.rs | 8 |
16 files changed, 119 insertions, 89 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 669666989..920899dce 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -729,7 +729,7 @@ pub struct Const { | |||
729 | 729 | ||
730 | impl Const { | 730 | impl Const { |
731 | pub fn module(self, db: &impl DefDatabase) -> Module { | 731 | pub fn module(self, db: &impl DefDatabase) -> Module { |
732 | Module { id: self.id.module(db) } | 732 | Module { id: self.id.lookup(db).module(db) } |
733 | } | 733 | } |
734 | 734 | ||
735 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 735 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
@@ -748,22 +748,27 @@ impl Const { | |||
748 | db.infer(self.into()) | 748 | db.infer(self.into()) |
749 | } | 749 | } |
750 | 750 | ||
751 | /// The containing impl block, if this is a method. | 751 | /// The containing impl block, if this is a type alias. |
752 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { | 752 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { |
753 | ImplBlock::containing(db, self.into()) | 753 | match self.container(db) { |
754 | Some(Container::ImplBlock(it)) => Some(it), | ||
755 | _ => None, | ||
756 | } | ||
754 | } | 757 | } |
755 | 758 | ||
759 | /// The containing trait, if this is a trait type alias definition. | ||
756 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { | 760 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { |
757 | db.trait_items_index(self.module(db).id).get_parent_trait(self.id.into()).map(Trait::from) | 761 | match self.container(db) { |
762 | Some(Container::Trait(it)) => Some(it), | ||
763 | _ => None, | ||
764 | } | ||
758 | } | 765 | } |
759 | 766 | ||
760 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | 767 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { |
761 | if let Some(impl_block) = self.impl_block(db) { | 768 | match self.id.lookup(db).container { |
762 | Some(impl_block.into()) | 769 | ContainerId::TraitId(it) => Some(Container::Trait(it.into())), |
763 | } else if let Some(trait_) = self.parent_trait(db) { | 770 | ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), |
764 | Some(trait_.into()) | 771 | ContainerId::ModuleId(_) => None, |
765 | } else { | ||
766 | None | ||
767 | } | 772 | } |
768 | } | 773 | } |
769 | 774 | ||
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index 04675e08e..354d2c98f 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs | |||
@@ -120,7 +120,7 @@ impl HasSource for Function { | |||
120 | impl HasSource for Const { | 120 | impl HasSource for Const { |
121 | type Ast = ast::ConstDef; | 121 | type Ast = ast::ConstDef; |
122 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::ConstDef> { | 122 | fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::ConstDef> { |
123 | self.id.source(db) | 123 | self.id.lookup(db).source(db) |
124 | } | 124 | } |
125 | } | 125 | } |
126 | impl HasSource for Static { | 126 | impl HasSource for Static { |
diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 73859e1e9..5bb7961b3 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs | |||
@@ -26,7 +26,6 @@ pub use hir_def::db::{ | |||
26 | BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, | 26 | BodyQuery, BodyWithSourceMapQuery, CrateDefMapQuery, DefDatabase2, DefDatabase2Storage, |
27 | EnumDataQuery, ExprScopesQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, | 27 | EnumDataQuery, ExprScopesQuery, ImplDataQuery, InternDatabase, InternDatabaseStorage, |
28 | RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, TraitDataQuery, | 28 | RawItemsQuery, RawItemsWithSourceMapQuery, StructDataQuery, TraitDataQuery, |
29 | TraitItemsIndexQuery, | ||
30 | }; | 29 | }; |
31 | pub use hir_expand::db::{ | 30 | pub use hir_expand::db::{ |
32 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, | 31 | AstDatabase, AstDatabaseStorage, AstIdMapQuery, MacroArgQuery, MacroDefQuery, MacroExpandQuery, |
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index f5fdaafa3..b86307c58 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs | |||
@@ -79,8 +79,27 @@ impl FromSource for Function { | |||
79 | impl FromSource for Const { | 79 | impl FromSource for Const { |
80 | type Ast = ast::ConstDef; | 80 | type Ast = ast::ConstDef; |
81 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { | 81 | fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { |
82 | let id = from_source(db, src)?; | 82 | let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? { |
83 | Some(Const { id }) | 83 | Container::Trait(it) => it.items(db), |
84 | Container::ImplBlock(it) => it.items(db), | ||
85 | Container::Module(m) => { | ||
86 | return m | ||
87 | .declarations(db) | ||
88 | .into_iter() | ||
89 | .filter_map(|it| match it { | ||
90 | ModuleDef::Const(it) => Some(it), | ||
91 | _ => None, | ||
92 | }) | ||
93 | .find(|it| same_source(&it.source(db), &src)) | ||
94 | } | ||
95 | }; | ||
96 | items | ||
97 | .into_iter() | ||
98 | .filter_map(|it| match it { | ||
99 | AssocItem::Const(it) => Some(it), | ||
100 | _ => None, | ||
101 | }) | ||
102 | .find(|it| same_source(&it.source(db), &src)) | ||
84 | } | 103 | } |
85 | } | 104 | } |
86 | impl FromSource for Static { | 105 | impl FromSource for Static { |
@@ -292,7 +311,7 @@ impl Container { | |||
292 | /// equal if they point to exactly the same object. | 311 | /// equal if they point to exactly the same object. |
293 | /// | 312 | /// |
294 | /// In general, we do not guarantee that we have exactly one instance of a | 313 | /// In general, we do not guarantee that we have exactly one instance of a |
295 | /// syntax tree for each file. We probably should add such guanratree, but, for | 314 | /// syntax tree for each file. We probably should add such guarantee, but, for |
296 | /// the time being, we will use identity-less AstPtr comparison. | 315 | /// the time being, we will use identity-less AstPtr comparison. |
297 | fn same_source<N: AstNode>(s1: &Source<N>, s2: &Source<N>) -> bool { | 316 | fn same_source<N: AstNode>(s1: &Source<N>, s2: &Source<N>) -> bool { |
298 | s1.as_ref().map(AstPtr::new) == s2.as_ref().map(AstPtr::new) | 317 | s1.as_ref().map(AstPtr::new) == s2.as_ref().map(AstPtr::new) |
diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 0c2bb8fee..0513f28a9 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs | |||
@@ -19,14 +19,6 @@ impl HasSource for ImplBlock { | |||
19 | } | 19 | } |
20 | 20 | ||
21 | impl ImplBlock { | 21 | impl ImplBlock { |
22 | pub(crate) fn containing(db: &impl DefDatabase, item: AssocItem) -> Option<ImplBlock> { | ||
23 | let module = item.module(db); | ||
24 | let crate_def_map = db.crate_def_map(module.id.krate); | ||
25 | crate_def_map[module.id.module_id].impls.iter().copied().map(ImplBlock::from).find(|it| { | ||
26 | db.impl_data(it.id).items().iter().copied().map(AssocItem::from).any(|it| it == item) | ||
27 | }) | ||
28 | } | ||
29 | |||
30 | pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> { | 22 | pub fn target_trait(&self, db: &impl DefDatabase) -> Option<TypeRef> { |
31 | db.impl_data(self.id).target_trait().cloned() | 23 | db.impl_data(self.id).target_trait().cloned() |
32 | } | 24 | } |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index caa8d0082..fd9994098 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -71,7 +71,7 @@ fn def_with_body_from_child_node( | |||
71 | match_ast! { | 71 | match_ast! { |
72 | match node { | 72 | match node { |
73 | ast::FnDef(def) => { return Function::from_source(db, child.with_value(def)).map(DefWithBody::from); }, | 73 | ast::FnDef(def) => { return Function::from_source(db, child.with_value(def)).map(DefWithBody::from); }, |
74 | ast::ConstDef(def) => { Some(Const { id: ctx.to_def(&def) }.into()) }, | 74 | ast::ConstDef(def) => { return Const::from_source(db, child.with_value(def)).map(DefWithBody::from); }, |
75 | ast::StaticDef(def) => { Some(Static { id: ctx.to_def(&def) }.into()) }, | 75 | ast::StaticDef(def) => { Some(Static { id: ctx.to_def(&def) }.into()) }, |
76 | _ => { None }, | 76 | _ => { None }, |
77 | } | 77 | } |
@@ -428,6 +428,11 @@ impl SourceAnalyzer { | |||
428 | pub(crate) fn inference_result(&self) -> Arc<crate::ty::InferenceResult> { | 428 | pub(crate) fn inference_result(&self) -> Arc<crate::ty::InferenceResult> { |
429 | self.infer.clone().unwrap() | 429 | self.infer.clone().unwrap() |
430 | } | 430 | } |
431 | |||
432 | #[cfg(test)] | ||
433 | pub(crate) fn analyzed_declaration(&self) -> Option<DefWithBody> { | ||
434 | self.body_owner | ||
435 | } | ||
431 | } | 436 | } |
432 | 437 | ||
433 | fn scope_for( | 438 | fn scope_for( |
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 776613c7c..36ece723f 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -3,8 +3,6 @@ | |||
3 | 3 | ||
4 | mod autoderef; | 4 | mod autoderef; |
5 | pub(crate) mod primitive; | 5 | pub(crate) mod primitive; |
6 | #[cfg(test)] | ||
7 | mod tests; | ||
8 | pub(crate) mod traits; | 6 | pub(crate) mod traits; |
9 | pub(crate) mod method_resolution; | 7 | pub(crate) mod method_resolution; |
10 | mod op; | 8 | mod op; |
@@ -12,6 +10,9 @@ mod lower; | |||
12 | mod infer; | 10 | mod infer; |
13 | pub(crate) mod display; | 11 | pub(crate) mod display; |
14 | 12 | ||
13 | #[cfg(test)] | ||
14 | mod tests; | ||
15 | |||
15 | use std::ops::Deref; | 16 | use std::ops::Deref; |
16 | use std::sync::Arc; | 17 | use std::sync::Arc; |
17 | use std::{fmt, iter, mem}; | 18 | use std::{fmt, iter, mem}; |
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index abfaffb5e..74c12a0a2 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -11,6 +11,7 @@ use ra_syntax::{ | |||
11 | ast::{self, AstNode}, | 11 | ast::{self, AstNode}, |
12 | SyntaxKind::*, | 12 | SyntaxKind::*, |
13 | }; | 13 | }; |
14 | use rustc_hash::FxHashSet; | ||
14 | use test_utils::covers; | 15 | use test_utils::covers; |
15 | 16 | ||
16 | use crate::{ | 17 | use crate::{ |
@@ -2518,7 +2519,6 @@ fn test() { | |||
2518 | [167; 179) 'GLOBAL_CONST': u32 | 2519 | [167; 179) 'GLOBAL_CONST': u32 |
2519 | [189; 191) 'id': u32 | 2520 | [189; 191) 'id': u32 |
2520 | [194; 210) 'Foo::A..._CONST': u32 | 2521 | [194; 210) 'Foo::A..._CONST': u32 |
2521 | [126; 128) '99': u32 | ||
2522 | "### | 2522 | "### |
2523 | ); | 2523 | ); |
2524 | } | 2524 | } |
@@ -4742,10 +4742,13 @@ fn infer(content: &str) -> String { | |||
4742 | } | 4742 | } |
4743 | }; | 4743 | }; |
4744 | 4744 | ||
4745 | let mut analyzed = FxHashSet::default(); | ||
4745 | for node in source_file.syntax().descendants() { | 4746 | for node in source_file.syntax().descendants() { |
4746 | if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF { | 4747 | if node.kind() == FN_DEF || node.kind() == CONST_DEF || node.kind() == STATIC_DEF { |
4747 | let analyzer = SourceAnalyzer::new(&db, Source::new(file_id.into(), &node), None); | 4748 | let analyzer = SourceAnalyzer::new(&db, Source::new(file_id.into(), &node), None); |
4748 | infer_def(analyzer.inference_result(), analyzer.body_source_map()); | 4749 | if analyzed.insert(analyzer.analyzed_declaration()) { |
4750 | infer_def(analyzer.inference_result(), analyzer.body_source_map()); | ||
4751 | } | ||
4749 | } | 4752 | } |
4750 | } | 4753 | } |
4751 | 4754 | ||
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 d6d32fb8c..c6cd4369b 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs | |||
@@ -13,8 +13,8 @@ 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, 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)] |
@@ -26,7 +26,7 @@ pub trait InternDatabase: SourceDatabase { | |||
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] |
@@ -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/impls.rs b/crates/ra_hir_def/src/impls.rs index 574086ac7..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; | |||
9 | use ra_syntax::ast; | 9 | use ra_syntax::ast; |
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, ContainerId, | 12 | db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstLoc, ContainerId, |
13 | FunctionLoc, ImplId, Intern, LocationCtx, TypeAliasLoc, | 13 | FunctionLoc, ImplId, Intern, TypeAliasLoc, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | #[derive(Debug, Clone, PartialEq, Eq)] | 16 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -31,7 +31,6 @@ 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 { |
@@ -44,7 +43,12 @@ impl ImplData { | |||
44 | def.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 | let def = TypeAliasLoc { | 54 | let def = TypeAliasLoc { |
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index da6506fcd..0af41de87 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -289,12 +289,23 @@ impl_arena_id!(LocalStructFieldId); | |||
289 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 289 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
290 | pub struct ConstId(salsa::InternId); | 290 | pub struct ConstId(salsa::InternId); |
291 | impl_intern_key!(ConstId); | 291 | impl_intern_key!(ConstId); |
292 | impl AstItemDef<ast::ConstDef> for ConstId { | 292 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
293 | fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ConstDef>) -> Self { | 293 | pub struct ConstLoc { |
294 | db.intern_const(loc) | 294 | pub container: ContainerId, |
295 | pub ast_id: AstId<ast::ConstDef>, | ||
296 | } | ||
297 | |||
298 | impl Intern for ConstLoc { | ||
299 | type ID = ConstId; | ||
300 | fn intern(self, db: &impl db::DefDatabase2) -> ConstId { | ||
301 | db.intern_const(self) | ||
295 | } | 302 | } |
296 | fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ConstDef> { | 303 | } |
297 | db.lookup_intern_const(self) | 304 | |
305 | impl Lookup for ConstId { | ||
306 | type Data = ConstLoc; | ||
307 | fn lookup(&self, db: &impl db::DefDatabase2) -> ConstLoc { | ||
308 | db.lookup_intern_const(*self) | ||
298 | } | 309 | } |
299 | } | 310 | } |
300 | 311 | ||
@@ -498,6 +509,16 @@ impl HasModule for TypeAliasLoc { | |||
498 | } | 509 | } |
499 | } | 510 | } |
500 | 511 | ||
512 | impl 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), | ||
518 | } | ||
519 | } | ||
520 | } | ||
521 | |||
501 | pub trait HasSource { | 522 | pub trait HasSource { |
502 | type Value; | 523 | type Value; |
503 | fn source(&self, db: &impl db::DefDatabase2) -> Source<Self::Value>; | 524 | fn source(&self, db: &impl db::DefDatabase2) -> Source<Self::Value>; |
@@ -520,3 +541,12 @@ impl HasSource for TypeAliasLoc { | |||
520 | Source::new(self.ast_id.file_id(), node) | 541 | Source::new(self.ast_id.file_id(), node) |
521 | } | 542 | } |
522 | } | 543 | } |
544 | |||
545 | impl 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 71e01279d..aae3dcadf 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -19,7 +19,7 @@ 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, ContainerId, CrateModuleId, EnumId, EnumVariantId, | 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, TypeAliasLoc, UnionId, | 24 | StructOrUnionId, TraitId, TypeAliasLoc, UnionId, |
25 | }; | 25 | }; |
@@ -692,7 +692,15 @@ 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 | } |
diff --git a/crates/ra_hir_def/src/traits.rs b/crates/ra_hir_def/src/traits.rs index bb61e852a..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, ContainerId, FunctionLoc, Intern, | 13 | db::DefDatabase2, AssocItemId, AstItemDef, ConstLoc, ContainerId, FunctionLoc, Intern, TraitId, |
15 | LocationCtx, ModuleDefId, ModuleId, TraitId, TypeAliasLoc, | 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() { |
@@ -40,7 +37,12 @@ impl TraitData { | |||
40 | } | 37 | } |
41 | .intern(db) | 38 | .intern(db) |
42 | .into(), | 39 | .into(), |
43 | ast::ImplItem::ConstDef(it) => ConstId::from_ast(ctx, &it).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(), | ||
44 | ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc { | 46 | ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc { |
45 | container: ContainerId::TraitId(tr), | 47 | container: ContainerId::TraitId(tr), |
46 | 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)), |
@@ -55,34 +57,3 @@ impl TraitData { | |||
55 | Arc::new(TraitData { name, items, auto }) | 57 | Arc::new(TraitData { name, items, auto }) |
56 | } | 58 | } |
57 | } | 59 | } |
58 | |||
59 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
60 | pub struct TraitItemsIndex { | ||
61 | traits_by_def: FxHashMap<AssocItemId, TraitId>, | ||
62 | } | ||
63 | |||
64 | impl TraitItemsIndex { | ||
65 | pub fn trait_items_index(db: &impl DefDatabase2, module: ModuleId) -> TraitItemsIndex { | ||
66 | let mut index = TraitItemsIndex { traits_by_def: FxHashMap::default() }; | ||
67 | let crate_def_map = db.crate_def_map(module.krate); | ||
68 | for decl in crate_def_map[module.module_id].scope.declarations() { | ||
69 | if let ModuleDefId::TraitId(tr) = decl { | ||
70 | for item in db.trait_data(tr).items.iter() { | ||
71 | match item { | ||
72 | AssocItemId::FunctionId(_) => (), | ||
73 | AssocItemId::TypeAliasId(_) => (), | ||
74 | _ => { | ||
75 | let prev = index.traits_by_def.insert(*item, tr); | ||
76 | assert!(prev.is_none()); | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | index | ||
83 | } | ||
84 | |||
85 | pub fn get_parent_trait(&self, item: AssocItemId) -> Option<TraitId> { | ||
86 | self.traits_by_def.get(&item).cloned() | ||
87 | } | ||
88 | } | ||
diff --git a/crates/ra_ide_api/src/change.rs b/crates/ra_ide_api/src/change.rs index 5c9dec13e..3c607d5b5 100644 --- a/crates/ra_ide_api/src/change.rs +++ b/crates/ra_ide_api/src/change.rs | |||
@@ -309,7 +309,6 @@ impl RootDatabase { | |||
309 | hir::db::StructDataQuery | 309 | hir::db::StructDataQuery |
310 | hir::db::EnumDataQuery | 310 | hir::db::EnumDataQuery |
311 | hir::db::TraitDataQuery | 311 | hir::db::TraitDataQuery |
312 | hir::db::TraitItemsIndexQuery | ||
313 | hir::db::RawItemsWithSourceMapQuery | 312 | hir::db::RawItemsWithSourceMapQuery |
314 | hir::db::RawItemsQuery | 313 | hir::db::RawItemsQuery |
315 | hir::db::CrateDefMapQuery | 314 | hir::db::CrateDefMapQuery |
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index ae87ab9f9..9839be985 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs | |||
@@ -404,9 +404,7 @@ mod tests { | |||
404 | check_hover_result( | 404 | check_hover_result( |
405 | r#" | 405 | r#" |
406 | //- /main.rs | 406 | //- /main.rs |
407 | fn main() { | 407 | const foo<|>: u32 = 0; |
408 | const foo<|>: u32 = 0; | ||
409 | } | ||
410 | "#, | 408 | "#, |
411 | &["const foo: u32"], | 409 | &["const foo: u32"], |
412 | ); | 410 | ); |
@@ -414,9 +412,7 @@ mod tests { | |||
414 | check_hover_result( | 412 | check_hover_result( |
415 | r#" | 413 | r#" |
416 | //- /main.rs | 414 | //- /main.rs |
417 | fn main() { | 415 | static foo<|>: u32 = 0; |
418 | static foo<|>: u32 = 0; | ||
419 | } | ||
420 | "#, | 416 | "#, |
421 | &["static foo: u32"], | 417 | &["static foo: u32"], |
422 | ); | 418 | ); |