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.rs25
-rw-r--r--crates/ra_hir/src/code_model/src.rs2
-rw-r--r--crates/ra_hir/src/db.rs1
-rw-r--r--crates/ra_hir/src/from_source.rs25
-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, 52 insertions, 28 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
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
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 {
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 {
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 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 {
79impl FromSource for Const { 79impl 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}
86impl FromSource for Static { 105impl 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.
297fn same_source<N: AstNode>(s1: &Source<N>, s2: &Source<N>) -> bool { 316fn 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
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