aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model.rs59
-rw-r--r--crates/ra_hir/src/code_model/src.rs4
-rw-r--r--crates/ra_hir/src/db.rs1
-rw-r--r--crates/ra_hir/src/from_source.rs137
-rw-r--r--crates/ra_hir/src/impl_block.rs8
-rw-r--r--crates/ra_hir/src/source_binder.rs7
-rw-r--r--crates/ra_hir/src/ty.rs5
-rw-r--r--crates/ra_hir/src/ty/tests.rs7
8 files changed, 143 insertions, 85 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index c49190a0f..920899dce 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -12,7 +12,7 @@ use hir_def::{
12 builtin_type::BuiltinType, 12 builtin_type::BuiltinType,
13 traits::TraitData, 13 traits::TraitData,
14 type_ref::{Mutability, TypeRef}, 14 type_ref::{Mutability, TypeRef},
15 AssocItemId, CrateModuleId, FunctionContainerId, HasModule, ImplId, LocalEnumVariantId, 15 AssocItemId, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId,
16 LocalStructFieldId, Lookup, ModuleId, UnionId, 16 LocalStructFieldId, Lookup, ModuleId, UnionId,
17}; 17};
18use hir_expand::{ 18use hir_expand::{
@@ -697,9 +697,9 @@ impl Function {
697 697
698 pub fn container(self, db: &impl DefDatabase) -> Option<Container> { 698 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
699 match self.id.lookup(db).container { 699 match self.id.lookup(db).container {
700 FunctionContainerId::TraitId(it) => Some(Container::Trait(it.into())), 700 ContainerId::TraitId(it) => Some(Container::Trait(it.into())),
701 FunctionContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), 701 ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())),
702 FunctionContainerId::ModuleId(_) => None, 702 ContainerId::ModuleId(_) => None,
703 } 703 }
704 } 704 }
705 705
@@ -729,7 +729,7 @@ pub struct Const {
729 729
730impl Const { 730impl 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
@@ -954,30 +959,34 @@ pub struct TypeAlias {
954 959
955impl TypeAlias { 960impl TypeAlias {
956 pub fn module(self, db: &impl DefDatabase) -> Module { 961 pub fn module(self, db: &impl DefDatabase) -> Module {
957 Module { id: self.id.module(db) } 962 Module { id: self.id.lookup(db).module(db) }
958 } 963 }
959 964
960 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 965 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
961 Some(self.module(db).krate()) 966 Some(self.module(db).krate())
962 } 967 }
963 968
964 /// The containing impl block, if this is a method. 969 /// The containing impl block, if this is a type alias.
965 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { 970 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
966 ImplBlock::containing(db, self.into()) 971 match self.container(db) {
972 Some(Container::ImplBlock(it)) => Some(it),
973 _ => None,
974 }
967 } 975 }
968 976
969 /// The containing trait, if this is a trait method definition. 977 /// The containing trait, if this is a trait type alias definition.
970 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { 978 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
971 db.trait_items_index(self.module(db).id).get_parent_trait(self.id.into()).map(Trait::from) 979 match self.container(db) {
980 Some(Container::Trait(it)) => Some(it),
981 _ => None,
982 }
972 } 983 }
973 984
974 pub fn container(self, db: &impl DefDatabase) -> Option<Container> { 985 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
975 if let Some(impl_block) = self.impl_block(db) { 986 match self.id.lookup(db).container {
976 Some(impl_block.into()) 987 ContainerId::TraitId(it) => Some(Container::Trait(it.into())),
977 } else if let Some(trait_) = self.parent_trait(db) { 988 ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())),
978 Some(trait_.into()) 989 ContainerId::ModuleId(_) => None,
979 } else {
980 None
981 } 990 }
982 } 991 }
983 992
diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs
index 91cab7414..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 {
120impl HasSource for Const { 120impl 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}
126impl HasSource for Static { 126impl HasSource for Static {
@@ -138,7 +138,7 @@ impl HasSource for Trait {
138impl HasSource for TypeAlias { 138impl HasSource for TypeAlias {
139 type Ast = ast::TypeAliasDef; 139 type Ast = ast::TypeAliasDef;
140 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::TypeAliasDef> { 140 fn source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ast::TypeAliasDef> {
141 self.id.source(db) 141 self.id.lookup(db).source(db)
142 } 142 }
143} 143}
144impl HasSource for MacroDef { 144impl HasSource for MacroDef {
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};
31pub use hir_expand::db::{ 30pub 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 303d5f138..b86307c58 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -4,7 +4,7 @@ use hir_def::{ModuleId, StructId, StructOrUnionId, UnionId};
4use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; 4use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
5use ra_syntax::{ 5use ra_syntax::{
6 ast::{self, AstNode, NameOwner}, 6 ast::{self, AstNode, NameOwner},
7 match_ast, AstPtr, 7 match_ast, AstPtr, SyntaxNode,
8}; 8};
9 9
10use crate::{ 10use crate::{
@@ -52,56 +52,54 @@ impl FromSource for Trait {
52impl FromSource for Function { 52impl FromSource for Function {
53 type Ast = ast::FnDef; 53 type Ast = ast::FnDef;
54 fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { 54 fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
55 // FIXME: this doesn't try to handle nested declarations 55 let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
56 for container in src.value.syntax().ancestors() { 56 Container::Trait(it) => it.items(db),
57 let res = match_ast! { 57 Container::ImplBlock(it) => it.items(db),
58 match container { 58 Container::Module(m) => {
59 ast::TraitDef(it) => { 59 return m
60 let c = Trait::from_source(db, src.with_value(it))?; 60 .declarations(db)
61 c.items(db) 61 .into_iter()
62 .into_iter() 62 .filter_map(|it| match it {
63 .filter_map(|it| match it { 63 ModuleDef::Function(it) => Some(it),
64 AssocItem::Function(it) => Some(it), 64 _ => None,
65 _ => None 65 })
66 }) 66 .find(|it| same_source(&it.source(db), &src))
67 .find(|it| same_source(&it.source(db), &src))? 67 }
68 }, 68 };
69 ast::ImplBlock(it) => { 69 items
70 let c = ImplBlock::from_source(db, src.with_value(it))?;
71 c.items(db)
72 .into_iter()
73 .filter_map(|it| match it {
74 AssocItem::Function(it) => Some(it),
75 _ => None
76 })
77 .find(|it| same_source(&it.source(db), &src))?
78
79 },
80 _ => { continue },
81 }
82 };
83 return Some(res);
84 }
85
86 let module_source = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
87 let c = Module::from_definition(db, src.with_value(module_source))?;
88 let res = c
89 .declarations(db)
90 .into_iter() 70 .into_iter()
91 .filter_map(|it| match it { 71 .filter_map(|it| match it {
92 ModuleDef::Function(it) => Some(it), 72 AssocItem::Function(it) => Some(it),
93 _ => None, 73 _ => None,
94 }) 74 })
95 .find(|it| same_source(&it.source(db), &src)); 75 .find(|it| same_source(&it.source(db), &src))
96 res
97 } 76 }
98} 77}
99 78
100impl FromSource for Const { 79impl FromSource for Const {
101 type Ast = ast::ConstDef; 80 type Ast = ast::ConstDef;
102 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> {
103 let id = from_source(db, src)?; 82 let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
104 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))
105 } 103 }
106} 104}
107impl FromSource for Static { 105impl FromSource for Static {
@@ -114,8 +112,27 @@ impl FromSource for Static {
114impl FromSource for TypeAlias { 112impl FromSource for TypeAlias {
115 type Ast = ast::TypeAliasDef; 113 type Ast = ast::TypeAliasDef;
116 fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> { 114 fn from_source(db: &(impl DefDatabase + AstDatabase), src: Source<Self::Ast>) -> Option<Self> {
117 let id = from_source(db, src)?; 115 let items = match Container::find(db, src.as_ref().map(|it| it.syntax()))? {
118 Some(TypeAlias { id }) 116 Container::Trait(it) => it.items(db),
117 Container::ImplBlock(it) => it.items(db),
118 Container::Module(m) => {
119 return m
120 .declarations(db)
121 .into_iter()
122 .filter_map(|it| match it {
123 ModuleDef::TypeAlias(it) => Some(it),
124 _ => None,
125 })
126 .find(|it| same_source(&it.source(db), &src))
127 }
128 };
129 items
130 .into_iter()
131 .filter_map(|it| match it {
132 AssocItem::TypeAlias(it) => Some(it),
133 _ => None,
134 })
135 .find(|it| same_source(&it.source(db), &src))
119 } 136 }
120} 137}
121 138
@@ -258,11 +275,43 @@ where
258 Some(DEF::from_ast(ctx, &src.value)) 275 Some(DEF::from_ast(ctx, &src.value))
259} 276}
260 277
278enum Container {
279 Trait(Trait),
280 ImplBlock(ImplBlock),
281 Module(Module),
282}
283
284impl Container {
285 fn find(db: &impl DefDatabase, src: Source<&SyntaxNode>) -> Option<Container> {
286 // FIXME: this doesn't try to handle nested declarations
287 for container in src.value.ancestors() {
288 let res = match_ast! {
289 match container {
290 ast::TraitDef(it) => {
291 let c = Trait::from_source(db, src.with_value(it))?;
292 Container::Trait(c)
293 },
294 ast::ImplBlock(it) => {
295 let c = ImplBlock::from_source(db, src.with_value(it))?;
296 Container::ImplBlock(c)
297 },
298 _ => { continue },
299 }
300 };
301 return Some(res);
302 }
303
304 let module_source = ModuleSource::from_child_node(db, src);
305 let c = Module::from_definition(db, src.with_value(module_source))?;
306 Some(Container::Module(c))
307 }
308}
309
261/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are 310/// XXX: AST Nodes and SyntaxNodes have identity equality semantics: nodes are
262/// equal if they point to exactly the same object. 311/// equal if they point to exactly the same object.
263/// 312///
264/// 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
265/// 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
266/// the time being, we will use identity-less AstPtr comparison. 315/// the time being, we will use identity-less AstPtr comparison.
267fn same_source<N: AstNode>(s1: &Source<N>, s2: &Source<N>) -> bool { 316fn same_source<N: AstNode>(s1: &Source<N>, s2: &Source<N>) -> bool {
268 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
21impl ImplBlock { 21impl 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
433fn scope_for( 438fn 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
4mod autoderef; 4mod autoderef;
5pub(crate) mod primitive; 5pub(crate) mod primitive;
6#[cfg(test)]
7mod tests;
8pub(crate) mod traits; 6pub(crate) mod traits;
9pub(crate) mod method_resolution; 7pub(crate) mod method_resolution;
10mod op; 8mod op;
@@ -12,6 +10,9 @@ mod lower;
12mod infer; 10mod infer;
13pub(crate) mod display; 11pub(crate) mod display;
14 12
13#[cfg(test)]
14mod tests;
15
15use std::ops::Deref; 16use std::ops::Deref;
16use std::sync::Arc; 17use std::sync::Arc;
17use std::{fmt, iter, mem}; 18use 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};
14use rustc_hash::FxHashSet;
14use test_utils::covers; 15use test_utils::covers;
15 16
16use crate::{ 17use 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