aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src')
-rw-r--r--crates/ra_hir_def/src/adt.rs16
-rw-r--r--crates/ra_hir_def/src/attr.rs13
-rw-r--r--crates/ra_hir_def/src/child_by_source.rs27
-rw-r--r--crates/ra_hir_def/src/data.rs8
-rw-r--r--crates/ra_hir_def/src/db.rs18
-rw-r--r--crates/ra_hir_def/src/docs.rs10
-rw-r--r--crates/ra_hir_def/src/generics.rs12
-rw-r--r--crates/ra_hir_def/src/keys.rs13
-rw-r--r--crates/ra_hir_def/src/lib.rs199
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs39
-rw-r--r--crates/ra_hir_def/src/path.rs110
-rw-r--r--crates/ra_hir_def/src/path/lower_use.rs115
-rw-r--r--crates/ra_hir_def/src/resolver.rs11
-rw-r--r--crates/ra_hir_def/src/src.rs50
14 files changed, 373 insertions, 268 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index db3e63ef8..ec3d57d1a 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -11,8 +11,8 @@ use ra_arena::{map::ArenaMap, Arena};
11use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; 11use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
12 12
13use crate::{ 13use crate::{
14 db::DefDatabase, src::HasChildSource, trace::Trace, type_ref::TypeRef, AstItemDef, EnumId, 14 db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, EnumId,
15 LocalEnumVariantId, LocalStructFieldId, StructId, UnionId, VariantId, 15 LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, UnionId, VariantId,
16}; 16};
17 17
18/// Note that we use `StructData` for unions as well! 18/// Note that we use `StructData` for unions as well!
@@ -50,14 +50,14 @@ pub struct StructFieldData {
50 50
51impl StructData { 51impl StructData {
52 pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc<StructData> { 52 pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc<StructData> {
53 let src = id.source(db); 53 let src = id.lookup(db).source(db);
54 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 54 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
55 let variant_data = VariantData::new(src.value.kind()); 55 let variant_data = VariantData::new(src.value.kind());
56 let variant_data = Arc::new(variant_data); 56 let variant_data = Arc::new(variant_data);
57 Arc::new(StructData { name, variant_data }) 57 Arc::new(StructData { name, variant_data })
58 } 58 }
59 pub(crate) fn union_data_query(db: &impl DefDatabase, id: UnionId) -> Arc<StructData> { 59 pub(crate) fn union_data_query(db: &impl DefDatabase, id: UnionId) -> Arc<StructData> {
60 let src = id.source(db); 60 let src = id.lookup(db).source(db);
61 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 61 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
62 let variant_data = VariantData::new( 62 let variant_data = VariantData::new(
63 src.value 63 src.value
@@ -72,7 +72,7 @@ impl StructData {
72 72
73impl EnumData { 73impl EnumData {
74 pub(crate) fn enum_data_query(db: &impl DefDatabase, e: EnumId) -> Arc<EnumData> { 74 pub(crate) fn enum_data_query(db: &impl DefDatabase, e: EnumId) -> Arc<EnumData> {
75 let src = e.source(db); 75 let src = e.lookup(db).source(db);
76 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 76 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
77 let mut trace = Trace::new_for_arena(); 77 let mut trace = Trace::new_for_arena();
78 lower_enum(&mut trace, &src.value); 78 lower_enum(&mut trace, &src.value);
@@ -89,7 +89,7 @@ impl HasChildSource for EnumId {
89 type ChildId = LocalEnumVariantId; 89 type ChildId = LocalEnumVariantId;
90 type Value = ast::EnumVariant; 90 type Value = ast::EnumVariant;
91 fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> { 91 fn child_source(&self, db: &impl DefDatabase) -> InFile<ArenaMap<Self::ChildId, Self::Value>> {
92 let src = self.source(db); 92 let src = self.lookup(db).source(db);
93 let mut trace = Trace::new_for_map(); 93 let mut trace = Trace::new_for_map();
94 lower_enum(&mut trace, &src.value); 94 lower_enum(&mut trace, &src.value);
95 src.with_value(trace.into_map()) 95 src.with_value(trace.into_map())
@@ -153,8 +153,8 @@ impl HasChildSource for VariantId {
153 let src = it.parent.child_source(db); 153 let src = it.parent.child_source(db);
154 src.map(|map| map[it.local_id].kind()) 154 src.map(|map| map[it.local_id].kind())
155 } 155 }
156 VariantId::StructId(it) => it.source(db).map(|it| it.kind()), 156 VariantId::StructId(it) => it.lookup(db).source(db).map(|it| it.kind()),
157 VariantId::UnionId(it) => it.source(db).map(|it| { 157 VariantId::UnionId(it) => it.lookup(db).source(db).map(|it| {
158 it.record_field_def_list() 158 it.record_field_def_list()
159 .map(ast::StructKind::Record) 159 .map(ast::StructKind::Record)
160 .unwrap_or(ast::StructKind::Unit) 160 .unwrap_or(ast::StructKind::Unit)
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs
index 2f8f02d82..5bf82e191 100644
--- a/crates/ra_hir_def/src/attr.rs
+++ b/crates/ra_hir_def/src/attr.rs
@@ -12,8 +12,7 @@ use ra_syntax::{
12use tt::Subtree; 12use tt::Subtree;
13 13
14use crate::{ 14use crate::{
15 db::DefDatabase, path::Path, src::HasChildSource, src::HasSource, AdtId, AstItemDef, AttrDefId, 15 db::DefDatabase, path::Path, src::HasChildSource, src::HasSource, AdtId, AttrDefId, Lookup,
16 Lookup,
17}; 16};
18 17
19#[derive(Default, Debug, Clone, PartialEq, Eq)] 18#[derive(Default, Debug, Clone, PartialEq, Eq)]
@@ -56,15 +55,15 @@ impl Attrs {
56 Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner)) 55 Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner))
57 } 56 }
58 AttrDefId::AdtId(it) => match it { 57 AttrDefId::AdtId(it) => match it {
59 AdtId::StructId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 58 AdtId::StructId(it) => attrs_from_loc(it.lookup(db), db),
60 AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 59 AdtId::EnumId(it) => attrs_from_loc(it.lookup(db), db),
61 AdtId::UnionId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 60 AdtId::UnionId(it) => attrs_from_loc(it.lookup(db), db),
62 }, 61 },
63 AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 62 AttrDefId::TraitId(it) => attrs_from_loc(it.lookup(db), db),
64 AttrDefId::MacroDefId(it) => { 63 AttrDefId::MacroDefId(it) => {
65 it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)) 64 it.ast_id.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db))
66 } 65 }
67 AttrDefId::ImplId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), 66 AttrDefId::ImplId(it) => attrs_from_loc(it.lookup(db), db),
68 AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db), 67 AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db),
69 AttrDefId::StaticId(it) => attrs_from_loc(it.lookup(db), db), 68 AttrDefId::StaticId(it) => attrs_from_loc(it.lookup(db), db),
70 AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db), 69 AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db),
diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs
index a3574a9db..3c9379b15 100644
--- a/crates/ra_hir_def/src/child_by_source.rs
+++ b/crates/ra_hir_def/src/child_by_source.rs
@@ -11,8 +11,8 @@ use crate::{
11 dyn_map::DynMap, 11 dyn_map::DynMap,
12 keys, 12 keys,
13 src::{HasChildSource, HasSource}, 13 src::{HasChildSource, HasSource},
14 AssocItemId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, ModuleId, StructFieldId, 14 AdtId, AssocItemId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, ModuleId,
15 TraitId, VariantId, 15 StructFieldId, TraitId, VariantId,
16}; 16};
17 17
18pub trait ChildBySource { 18pub trait ChildBySource {
@@ -94,10 +94,33 @@ impl ChildBySource for ModuleId {
94 let src = ty.lookup(db).source(db); 94 let src = ty.lookup(db).source(db);
95 res[keys::TYPE_ALIAS].insert(src, ty) 95 res[keys::TYPE_ALIAS].insert(src, ty)
96 } 96 }
97 ModuleDefId::TraitId(trait_) => {
98 let src = trait_.lookup(db).source(db);
99 res[keys::TRAIT].insert(src, trait_)
100 }
101 ModuleDefId::AdtId(adt) => match adt {
102 AdtId::StructId(strukt) => {
103 let src = strukt.lookup(db).source(db);
104 res[keys::STRUCT].insert(src, strukt)
105 }
106 AdtId::UnionId(union_) => {
107 let src = union_.lookup(db).source(db);
108 res[keys::UNION].insert(src, union_)
109 }
110 AdtId::EnumId(enum_) => {
111 let src = enum_.lookup(db).source(db);
112 res[keys::ENUM].insert(src, enum_)
113 }
114 },
97 _ => (), 115 _ => (),
98 } 116 }
99 } 117 }
100 118
119 for &impl_ in crate_def_map[self.local_id].impls.iter() {
120 let src = impl_.lookup(db).source(db);
121 res[keys::IMPL].insert(src, impl_)
122 }
123
101 res 124 res
102 } 125 }
103} 126}
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index 095d7064a..b2dac183e 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -12,8 +12,8 @@ use crate::{
12 db::DefDatabase, 12 db::DefDatabase,
13 src::HasSource, 13 src::HasSource,
14 type_ref::{Mutability, TypeRef}, 14 type_ref::{Mutability, TypeRef},
15 AssocItemId, AstItemDef, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, 15 AssocItemId, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, Intern, Lookup,
16 Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, 16 StaticId, TraitId, TypeAliasId, TypeAliasLoc,
17}; 17};
18 18
19#[derive(Debug, Clone, PartialEq, Eq)] 19#[derive(Debug, Clone, PartialEq, Eq)]
@@ -94,7 +94,7 @@ pub struct TraitData {
94 94
95impl TraitData { 95impl TraitData {
96 pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc<TraitData> { 96 pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc<TraitData> {
97 let src = tr.source(db); 97 let src = tr.lookup(db).source(db);
98 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); 98 let name = src.value.name().map_or_else(Name::missing, |n| n.as_name());
99 let auto = src.value.is_auto(); 99 let auto = src.value.is_auto();
100 let ast_id_map = db.ast_id_map(src.file_id); 100 let ast_id_map = db.ast_id_map(src.file_id);
@@ -167,7 +167,7 @@ pub struct ImplData {
167 167
168impl ImplData { 168impl ImplData {
169 pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc<ImplData> { 169 pub(crate) fn impl_data_query(db: &impl DefDatabase, id: ImplId) -> Arc<ImplData> {
170 let src = id.source(db); 170 let src = id.lookup(db).source(db);
171 let items = db.ast_id_map(src.file_id); 171 let items = db.ast_id_map(src.file_id);
172 172
173 let target_trait = src.value.target_trait().map(TypeRef::from_ast); 173 let target_trait = src.value.target_trait().map(TypeRef::from_ast);
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index ef5611ffc..98bff6cb7 100644
--- a/crates/ra_hir_def/src/db.rs
+++ b/crates/ra_hir_def/src/db.rs
@@ -3,7 +3,7 @@ use std::sync::Arc;
3 3
4use hir_expand::{db::AstDatabase, HirFileId}; 4use hir_expand::{db::AstDatabase, HirFileId};
5use ra_db::{salsa, CrateId, SourceDatabase}; 5use ra_db::{salsa, CrateId, SourceDatabase};
6use ra_syntax::{ast, SmolStr}; 6use ra_syntax::SmolStr;
7 7
8use crate::{ 8use crate::{
9 adt::{EnumData, StructData}, 9 adt::{EnumData, StructData},
@@ -17,9 +17,9 @@ use crate::{
17 raw::{ImportSourceMap, RawItems}, 17 raw::{ImportSourceMap, RawItems},
18 CrateDefMap, 18 CrateDefMap,
19 }, 19 },
20 AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId, 20 AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, FunctionLoc,
21 ImplId, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId, TypeAliasLoc, 21 GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId,
22 UnionId, 22 TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc,
23}; 23};
24 24
25#[salsa::query_group(InternDatabaseStorage)] 25#[salsa::query_group(InternDatabaseStorage)]
@@ -27,21 +27,21 @@ pub trait InternDatabase: SourceDatabase {
27 #[salsa::interned] 27 #[salsa::interned]
28 fn intern_function(&self, loc: FunctionLoc) -> FunctionId; 28 fn intern_function(&self, loc: FunctionLoc) -> FunctionId;
29 #[salsa::interned] 29 #[salsa::interned]
30 fn intern_struct(&self, loc: ItemLoc<ast::StructDef>) -> StructId; 30 fn intern_struct(&self, loc: StructLoc) -> StructId;
31 #[salsa::interned] 31 #[salsa::interned]
32 fn intern_union(&self, loc: ItemLoc<ast::UnionDef>) -> UnionId; 32 fn intern_union(&self, loc: UnionLoc) -> UnionId;
33 #[salsa::interned] 33 #[salsa::interned]
34 fn intern_enum(&self, loc: ItemLoc<ast::EnumDef>) -> EnumId; 34 fn intern_enum(&self, loc: EnumLoc) -> EnumId;
35 #[salsa::interned] 35 #[salsa::interned]
36 fn intern_const(&self, loc: ConstLoc) -> ConstId; 36 fn intern_const(&self, loc: ConstLoc) -> ConstId;
37 #[salsa::interned] 37 #[salsa::interned]
38 fn intern_static(&self, loc: StaticLoc) -> StaticId; 38 fn intern_static(&self, loc: StaticLoc) -> StaticId;
39 #[salsa::interned] 39 #[salsa::interned]
40 fn intern_trait(&self, loc: ItemLoc<ast::TraitDef>) -> TraitId; 40 fn intern_trait(&self, loc: TraitLoc) -> TraitId;
41 #[salsa::interned] 41 #[salsa::interned]
42 fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId; 42 fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId;
43 #[salsa::interned] 43 #[salsa::interned]
44 fn intern_impl(&self, loc: ItemLoc<ast::ImplBlock>) -> ImplId; 44 fn intern_impl(&self, loc: ImplLoc) -> ImplId;
45} 45}
46 46
47#[salsa::query_group(DefDatabaseStorage)] 47#[salsa::query_group(DefDatabaseStorage)]
diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs
index 61727bd26..b29f142e3 100644
--- a/crates/ra_hir_def/src/docs.rs
+++ b/crates/ra_hir_def/src/docs.rs
@@ -11,7 +11,7 @@ use ra_syntax::ast;
11use crate::{ 11use crate::{
12 db::DefDatabase, 12 db::DefDatabase,
13 src::{HasChildSource, HasSource}, 13 src::{HasChildSource, HasSource},
14 AdtId, AstItemDef, AttrDefId, Lookup, 14 AdtId, AttrDefId, Lookup,
15}; 15};
16 16
17/// Holds documentation 17/// Holds documentation
@@ -51,15 +51,15 @@ impl Documentation {
51 } 51 }
52 } 52 }
53 AttrDefId::AdtId(it) => match it { 53 AttrDefId::AdtId(it) => match it {
54 AdtId::StructId(it) => docs_from_ast(&it.source(db).value), 54 AdtId::StructId(it) => docs_from_ast(&it.lookup(db).source(db).value),
55 AdtId::EnumId(it) => docs_from_ast(&it.source(db).value), 55 AdtId::EnumId(it) => docs_from_ast(&it.lookup(db).source(db).value),
56 AdtId::UnionId(it) => docs_from_ast(&it.source(db).value), 56 AdtId::UnionId(it) => docs_from_ast(&it.lookup(db).source(db).value),
57 }, 57 },
58 AttrDefId::EnumVariantId(it) => { 58 AttrDefId::EnumVariantId(it) => {
59 let src = it.parent.child_source(db); 59 let src = it.parent.child_source(db);
60 docs_from_ast(&src.value[it.local_id]) 60 docs_from_ast(&src.value[it.local_id])
61 } 61 }
62 AttrDefId::TraitId(it) => docs_from_ast(&it.source(db).value), 62 AttrDefId::TraitId(it) => docs_from_ast(&it.lookup(db).source(db).value),
63 AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db)), 63 AttrDefId::MacroDefId(it) => docs_from_ast(&it.ast_id?.to_node(db)),
64 AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value), 64 AttrDefId::ConstId(it) => docs_from_ast(&it.lookup(db).source(db).value),
65 AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value), 65 AttrDefId::StaticId(it) => docs_from_ast(&it.lookup(db).source(db).value),
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs
index 976cf72d0..e502dd798 100644
--- a/crates/ra_hir_def/src/generics.rs
+++ b/crates/ra_hir_def/src/generics.rs
@@ -21,7 +21,7 @@ use crate::{
21 src::HasChildSource, 21 src::HasChildSource,
22 src::HasSource, 22 src::HasSource,
23 type_ref::{TypeBound, TypeRef}, 23 type_ref::{TypeBound, TypeRef},
24 AdtId, AstItemDef, GenericDefId, LocalTypeParamId, Lookup, TypeParamId, 24 AdtId, GenericDefId, LocalTypeParamId, Lookup, TypeParamId,
25}; 25};
26 26
27/// Data about a generic parameter (to a function, struct, impl, ...). 27/// Data about a generic parameter (to a function, struct, impl, ...).
@@ -71,22 +71,22 @@ impl GenericParams {
71 src.file_id 71 src.file_id
72 } 72 }
73 GenericDefId::AdtId(AdtId::StructId(it)) => { 73 GenericDefId::AdtId(AdtId::StructId(it)) => {
74 let src = it.source(db); 74 let src = it.lookup(db).source(db);
75 generics.fill(&mut sm, &src.value); 75 generics.fill(&mut sm, &src.value);
76 src.file_id 76 src.file_id
77 } 77 }
78 GenericDefId::AdtId(AdtId::UnionId(it)) => { 78 GenericDefId::AdtId(AdtId::UnionId(it)) => {
79 let src = it.source(db); 79 let src = it.lookup(db).source(db);
80 generics.fill(&mut sm, &src.value); 80 generics.fill(&mut sm, &src.value);
81 src.file_id 81 src.file_id
82 } 82 }
83 GenericDefId::AdtId(AdtId::EnumId(it)) => { 83 GenericDefId::AdtId(AdtId::EnumId(it)) => {
84 let src = it.source(db); 84 let src = it.lookup(db).source(db);
85 generics.fill(&mut sm, &src.value); 85 generics.fill(&mut sm, &src.value);
86 src.file_id 86 src.file_id
87 } 87 }
88 GenericDefId::TraitId(it) => { 88 GenericDefId::TraitId(it) => {
89 let src = it.source(db); 89 let src = it.lookup(db).source(db);
90 90
91 // traits get the Self type as an implicit first type parameter 91 // traits get the Self type as an implicit first type parameter
92 let self_param_id = 92 let self_param_id =
@@ -109,7 +109,7 @@ impl GenericParams {
109 // type-parameter, but rather is a type-alias for impl's target 109 // type-parameter, but rather is a type-alias for impl's target
110 // type, so this is handled by the resolver. 110 // type, so this is handled by the resolver.
111 GenericDefId::ImplId(it) => { 111 GenericDefId::ImplId(it) => {
112 let src = it.source(db); 112 let src = it.lookup(db).source(db);
113 generics.fill(&mut sm, &src.value); 113 generics.fill(&mut sm, &src.value);
114 src.file_id 114 src.file_id
115 } 115 }
diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs
index be702a4f8..d844f7a62 100644
--- a/crates/ra_hir_def/src/keys.rs
+++ b/crates/ra_hir_def/src/keys.rs
@@ -8,16 +8,23 @@ use rustc_hash::FxHashMap;
8 8
9use crate::{ 9use crate::{
10 dyn_map::{DynMap, Policy}, 10 dyn_map::{DynMap, Policy},
11 ConstId, EnumVariantId, FunctionId, StaticId, StructFieldId, TypeAliasId, TypeParamId, 11 ConstId, EnumId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId,
12 TypeAliasId, TypeParamId, UnionId,
12}; 13};
13 14
14type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>; 15pub type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
15 16
16pub const FUNCTION: Key<ast::FnDef, FunctionId> = Key::new(); 17pub const FUNCTION: Key<ast::FnDef, FunctionId> = Key::new();
17pub const CONST: Key<ast::ConstDef, ConstId> = Key::new(); 18pub const CONST: Key<ast::ConstDef, ConstId> = Key::new();
18pub const STATIC: Key<ast::StaticDef, StaticId> = Key::new(); 19pub const STATIC: Key<ast::StaticDef, StaticId> = Key::new();
19pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
20pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new(); 20pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new();
21pub const IMPL: Key<ast::ImplBlock, ImplId> = Key::new();
22pub const TRAIT: Key<ast::TraitDef, TraitId> = Key::new();
23pub const STRUCT: Key<ast::StructDef, StructId> = Key::new();
24pub const UNION: Key<ast::UnionDef, UnionId> = Key::new();
25pub const ENUM: Key<ast::EnumDef, EnumId> = Key::new();
26
27pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
21pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new(); 28pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new();
22pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new(); 29pub const RECORD_FIELD: Key<ast::RecordFieldDef, StructFieldId> = Key::new();
23pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new(); 30pub const TYPE_PARAM: Key<ast::TypeParam, TypeParamId> = Key::new();
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 569da4f28..f085bbe87 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -40,14 +40,14 @@ mod test_db;
40#[cfg(test)] 40#[cfg(test)]
41mod marks; 41mod marks;
42 42
43use std::hash::{Hash, Hasher}; 43use std::hash::Hash;
44 44
45use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, InFile, MacroDefId}; 45use hir_expand::{ast_id_map::FileAstId, AstId, HirFileId, InFile, MacroDefId};
46use ra_arena::{impl_arena_id, RawId}; 46use ra_arena::{impl_arena_id, RawId};
47use ra_db::{impl_intern_key, salsa, CrateId}; 47use ra_db::{impl_intern_key, salsa, CrateId};
48use ra_syntax::{ast, AstNode}; 48use ra_syntax::ast;
49 49
50use crate::{builtin_type::BuiltinType, db::InternDatabase}; 50use crate::builtin_type::BuiltinType;
51 51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53pub struct LocalImportId(RawId); 53pub struct LocalImportId(RawId);
@@ -65,63 +65,6 @@ pub struct ModuleId {
65pub struct LocalModuleId(RawId); 65pub struct LocalModuleId(RawId);
66impl_arena_id!(LocalModuleId); 66impl_arena_id!(LocalModuleId);
67 67
68#[derive(Debug)]
69pub struct ItemLoc<N: AstNode> {
70 pub(crate) module: ModuleId,
71 ast_id: AstId<N>,
72}
73
74impl<N: AstNode> PartialEq for ItemLoc<N> {
75 fn eq(&self, other: &Self) -> bool {
76 self.module == other.module && self.ast_id == other.ast_id
77 }
78}
79impl<N: AstNode> Eq for ItemLoc<N> {}
80impl<N: AstNode> Hash for ItemLoc<N> {
81 fn hash<H: Hasher>(&self, hasher: &mut H) {
82 self.module.hash(hasher);
83 self.ast_id.hash(hasher);
84 }
85}
86
87impl<N: AstNode> Clone for ItemLoc<N> {
88 fn clone(&self) -> ItemLoc<N> {
89 ItemLoc { module: self.module, ast_id: self.ast_id }
90 }
91}
92
93#[derive(Clone, Copy)]
94pub struct LocationCtx<DB> {
95 db: DB,
96 module: ModuleId,
97 file_id: HirFileId,
98}
99
100impl<'a, DB> LocationCtx<&'a DB> {
101 pub fn new(db: &'a DB, module: ModuleId, file_id: HirFileId) -> LocationCtx<&'a DB> {
102 LocationCtx { db, module, file_id }
103 }
104}
105
106pub trait AstItemDef<N: AstNode>: salsa::InternKey + Clone {
107 fn intern(db: &impl InternDatabase, loc: ItemLoc<N>) -> Self;
108 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<N>;
109
110 fn from_ast_id(ctx: LocationCtx<&impl InternDatabase>, ast_id: FileAstId<N>) -> Self {
111 let loc = ItemLoc { module: ctx.module, ast_id: AstId::new(ctx.file_id, ast_id) };
112 Self::intern(ctx.db, loc)
113 }
114 fn source(self, db: &(impl AstDatabase + InternDatabase)) -> InFile<N> {
115 let loc = self.lookup_intern(db);
116 let value = loc.ast_id.to_node(db);
117 InFile { file_id: loc.ast_id.file_id, value }
118 }
119 fn module(self, db: &impl InternDatabase) -> ModuleId {
120 let loc = self.lookup_intern(db);
121 loc.module
122 }
123}
124
125#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 68#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
126pub struct FunctionId(salsa::InternId); 69pub struct FunctionId(salsa::InternId);
127impl_intern_key!(FunctionId); 70impl_intern_key!(FunctionId);
@@ -149,36 +92,72 @@ impl Lookup for FunctionId {
149#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 92#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
150pub struct StructId(salsa::InternId); 93pub struct StructId(salsa::InternId);
151impl_intern_key!(StructId); 94impl_intern_key!(StructId);
152impl AstItemDef<ast::StructDef> for StructId { 95
153 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self { 96#[derive(Debug, Clone, PartialEq, Eq, Hash)]
154 db.intern_struct(loc) 97pub struct StructLoc {
98 pub container: ModuleId,
99 pub ast_id: AstId<ast::StructDef>,
100}
101
102impl Intern for StructLoc {
103 type ID = StructId;
104 fn intern(self, db: &impl db::DefDatabase) -> StructId {
105 db.intern_struct(self)
155 } 106 }
156 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> { 107}
157 db.lookup_intern_struct(self) 108
109impl Lookup for StructId {
110 type Data = StructLoc;
111 fn lookup(&self, db: &impl db::DefDatabase) -> StructLoc {
112 db.lookup_intern_struct(*self)
158 } 113 }
159} 114}
160 115
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 116#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
162pub struct UnionId(salsa::InternId); 117pub struct UnionId(salsa::InternId);
163impl_intern_key!(UnionId); 118impl_intern_key!(UnionId);
164impl AstItemDef<ast::UnionDef> for UnionId { 119
165 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::UnionDef>) -> Self { 120#[derive(Debug, Clone, PartialEq, Eq, Hash)]
166 db.intern_union(loc) 121pub struct UnionLoc {
122 pub container: ModuleId,
123 pub ast_id: AstId<ast::UnionDef>,
124}
125
126impl Intern for UnionLoc {
127 type ID = UnionId;
128 fn intern(self, db: &impl db::DefDatabase) -> UnionId {
129 db.intern_union(self)
167 } 130 }
168 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::UnionDef> { 131}
169 db.lookup_intern_union(self) 132
133impl Lookup for UnionId {
134 type Data = UnionLoc;
135 fn lookup(&self, db: &impl db::DefDatabase) -> UnionLoc {
136 db.lookup_intern_union(*self)
170 } 137 }
171} 138}
172 139
173#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
174pub struct EnumId(salsa::InternId); 141pub struct EnumId(salsa::InternId);
175impl_intern_key!(EnumId); 142impl_intern_key!(EnumId);
176impl AstItemDef<ast::EnumDef> for EnumId { 143
177 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self { 144#[derive(Debug, Clone, PartialEq, Eq, Hash)]
178 db.intern_enum(loc) 145pub struct EnumLoc {
146 pub container: ModuleId,
147 pub ast_id: AstId<ast::EnumDef>,
148}
149
150impl Intern for EnumLoc {
151 type ID = EnumId;
152 fn intern(self, db: &impl db::DefDatabase) -> EnumId {
153 db.intern_enum(self)
179 } 154 }
180 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> { 155}
181 db.lookup_intern_enum(self) 156
157impl Lookup for EnumId {
158 type Data = EnumLoc;
159 fn lookup(&self, db: &impl db::DefDatabase) -> EnumLoc {
160 db.lookup_intern_enum(*self)
182 } 161 }
183} 162}
184 163
@@ -253,12 +232,24 @@ impl Lookup for StaticId {
253#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 232#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
254pub struct TraitId(salsa::InternId); 233pub struct TraitId(salsa::InternId);
255impl_intern_key!(TraitId); 234impl_intern_key!(TraitId);
256impl AstItemDef<ast::TraitDef> for TraitId { 235
257 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::TraitDef>) -> Self { 236#[derive(Debug, Clone, PartialEq, Eq, Hash)]
258 db.intern_trait(loc) 237pub struct TraitLoc {
238 pub container: ModuleId,
239 pub ast_id: AstId<ast::TraitDef>,
240}
241
242impl Intern for TraitLoc {
243 type ID = TraitId;
244 fn intern(self, db: &impl db::DefDatabase) -> TraitId {
245 db.intern_trait(self)
259 } 246 }
260 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::TraitDef> { 247}
261 db.lookup_intern_trait(self) 248
249impl Lookup for TraitId {
250 type Data = TraitLoc;
251 fn lookup(&self, db: &impl db::DefDatabase) -> TraitLoc {
252 db.lookup_intern_trait(*self)
262 } 253 }
263} 254}
264 255
@@ -289,12 +280,24 @@ impl Lookup for TypeAliasId {
289#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 280#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
290pub struct ImplId(salsa::InternId); 281pub struct ImplId(salsa::InternId);
291impl_intern_key!(ImplId); 282impl_intern_key!(ImplId);
292impl AstItemDef<ast::ImplBlock> for ImplId { 283
293 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::ImplBlock>) -> Self { 284#[derive(Debug, Clone, PartialEq, Eq, Hash)]
294 db.intern_impl(loc) 285pub struct ImplLoc {
286 pub container: ModuleId,
287 pub ast_id: AstId<ast::ImplBlock>,
288}
289
290impl Intern for ImplLoc {
291 type ID = ImplId;
292 fn intern(self, db: &impl db::DefDatabase) -> ImplId {
293 db.intern_impl(self)
295 } 294 }
296 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::ImplBlock> { 295}
297 db.lookup_intern_impl(self) 296
297impl Lookup for ImplId {
298 type Data = ImplLoc;
299 fn lookup(&self, db: &impl db::DefDatabase) -> ImplLoc {
300 db.lookup_intern_impl(*self)
298 } 301 }
299} 302}
300 303
@@ -479,8 +482,8 @@ impl HasModule for FunctionLoc {
479 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 482 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
480 match self.container { 483 match self.container {
481 ContainerId::ModuleId(it) => it, 484 ContainerId::ModuleId(it) => it,
482 ContainerId::ImplId(it) => it.module(db), 485 ContainerId::ImplId(it) => it.lookup(db).container,
483 ContainerId::TraitId(it) => it.module(db), 486 ContainerId::TraitId(it) => it.lookup(db).container,
484 } 487 }
485 } 488 }
486} 489}
@@ -489,8 +492,8 @@ impl HasModule for TypeAliasLoc {
489 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 492 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
490 match self.container { 493 match self.container {
491 ContainerId::ModuleId(it) => it, 494 ContainerId::ModuleId(it) => it,
492 ContainerId::ImplId(it) => it.module(db), 495 ContainerId::ImplId(it) => it.lookup(db).container,
493 ContainerId::TraitId(it) => it.module(db), 496 ContainerId::TraitId(it) => it.lookup(db).container,
494 } 497 }
495 } 498 }
496} 499}
@@ -499,8 +502,8 @@ impl HasModule for ConstLoc {
499 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 502 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
500 match self.container { 503 match self.container {
501 ContainerId::ModuleId(it) => it, 504 ContainerId::ModuleId(it) => it,
502 ContainerId::ImplId(it) => it.module(db), 505 ContainerId::ImplId(it) => it.lookup(db).container,
503 ContainerId::TraitId(it) => it.module(db), 506 ContainerId::TraitId(it) => it.lookup(db).container,
504 } 507 }
505 } 508 }
506} 509}
@@ -508,9 +511,9 @@ impl HasModule for ConstLoc {
508impl HasModule for AdtId { 511impl HasModule for AdtId {
509 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 512 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
510 match self { 513 match self {
511 AdtId::StructId(it) => it.module(db), 514 AdtId::StructId(it) => it.lookup(db).container,
512 AdtId::UnionId(it) => it.module(db), 515 AdtId::UnionId(it) => it.lookup(db).container,
513 AdtId::EnumId(it) => it.module(db), 516 AdtId::EnumId(it) => it.lookup(db).container,
514 } 517 }
515 } 518 }
516} 519}
@@ -530,10 +533,10 @@ impl HasModule for GenericDefId {
530 match self { 533 match self {
531 GenericDefId::FunctionId(it) => it.lookup(db).module(db), 534 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
532 GenericDefId::AdtId(it) => it.module(db), 535 GenericDefId::AdtId(it) => it.module(db),
533 GenericDefId::TraitId(it) => it.module(db), 536 GenericDefId::TraitId(it) => it.lookup(db).container,
534 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), 537 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
535 GenericDefId::ImplId(it) => it.module(db), 538 GenericDefId::ImplId(it) => it.lookup(db).container,
536 GenericDefId::EnumVariantId(it) => it.parent.module(db), 539 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
537 GenericDefId::ConstId(it) => it.lookup(db).module(db), 540 GenericDefId::ConstId(it) => it.lookup(db).module(db),
538 } 541 }
539 } 542 }
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index a80067979..04aadead1 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -24,9 +24,9 @@ use crate::{
24 }, 24 },
25 path::{Path, PathKind}, 25 path::{Path, PathKind},
26 per_ns::PerNs, 26 per_ns::PerNs,
27 AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, 27 AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
28 Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, 28 LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc,
29 TraitId, TypeAliasLoc, UnionId, 29 TypeAliasLoc, UnionLoc,
30}; 30};
31 31
32pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { 32pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
@@ -661,9 +661,11 @@ where
661 krate: self.def_collector.def_map.krate, 661 krate: self.def_collector.def_map.krate,
662 local_id: self.module_id, 662 local_id: self.module_id,
663 }; 663 };
664 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); 664 let ast_id = self.raw_items[imp].ast_id;
665 let imp_id = ImplId::from_ast_id(ctx, self.raw_items[imp].ast_id); 665 let impl_id =
666 self.def_collector.def_map.modules[self.module_id].impls.push(imp_id) 666 ImplLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
667 .intern(self.def_collector.db);
668 self.def_collector.def_map.modules[self.module_id].impls.push(impl_id)
667 } 669 }
668 } 670 }
669 } 671 }
@@ -751,8 +753,6 @@ where
751 753
752 fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) { 754 fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) {
753 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; 755 let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id };
754 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
755
756 // FIXME: check attrs to see if this is an attribute macro invocation; 756 // FIXME: check attrs to see if this is an attribute macro invocation;
757 // in which case we don't add the invocation, just a single attribute 757 // in which case we don't add the invocation, just a single attribute
758 // macro invocation 758 // macro invocation
@@ -771,14 +771,20 @@ where
771 PerNs::values(def.into()) 771 PerNs::values(def.into())
772 } 772 }
773 raw::DefKind::Struct(ast_id) => { 773 raw::DefKind::Struct(ast_id) => {
774 let id = StructId::from_ast_id(ctx, ast_id).into(); 774 let def = StructLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
775 PerNs::both(id, id) 775 .intern(self.def_collector.db);
776 PerNs::both(def.into(), def.into())
776 } 777 }
777 raw::DefKind::Union(ast_id) => { 778 raw::DefKind::Union(ast_id) => {
778 let id = UnionId::from_ast_id(ctx, ast_id).into(); 779 let def = UnionLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
779 PerNs::both(id, id) 780 .intern(self.def_collector.db);
781 PerNs::both(def.into(), def.into())
782 }
783 raw::DefKind::Enum(ast_id) => {
784 let def = EnumLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
785 .intern(self.def_collector.db);
786 PerNs::types(def.into())
780 } 787 }
781 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()),
782 raw::DefKind::Const(ast_id) => { 788 raw::DefKind::Const(ast_id) => {
783 let def = ConstLoc { 789 let def = ConstLoc {
784 container: ContainerId::ModuleId(module), 790 container: ContainerId::ModuleId(module),
@@ -794,7 +800,12 @@ where
794 800
795 PerNs::values(def.into()) 801 PerNs::values(def.into())
796 } 802 }
797 raw::DefKind::Trait(ast_id) => PerNs::types(TraitId::from_ast_id(ctx, ast_id).into()), 803 raw::DefKind::Trait(ast_id) => {
804 let def = TraitLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) }
805 .intern(self.def_collector.db);
806
807 PerNs::types(def.into())
808 }
798 raw::DefKind::TypeAlias(ast_id) => { 809 raw::DefKind::TypeAlias(ast_id) => {
799 let def = TypeAliasLoc { 810 let def = TypeAliasLoc {
800 container: ContainerId::ModuleId(module), 811 container: ContainerId::ModuleId(module),
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
index e547b2f03..ec9d13e82 100644
--- a/crates/ra_hir_def/src/path.rs
+++ b/crates/ra_hir_def/src/path.rs
@@ -1,4 +1,5 @@
1//! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`. 1//! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`.
2mod lower_use;
2 3
3use std::{iter, sync::Arc}; 4use std::{iter, sync::Arc};
4 5
@@ -9,7 +10,7 @@ use hir_expand::{
9}; 10};
10use ra_db::CrateId; 11use ra_db::CrateId;
11use ra_syntax::{ 12use ra_syntax::{
12 ast::{self, NameOwner, TypeAscriptionOwner}, 13 ast::{self, TypeAscriptionOwner},
13 AstNode, 14 AstNode,
14}; 15};
15 16
@@ -28,8 +29,7 @@ pub struct PathSegment {
28} 29}
29 30
30/// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This 31/// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This
31/// can (in the future) also include bindings of associated types, like in 32/// also includes bindings of associated types, like in `Iterator<Item = Foo>`.
32/// `Iterator<Item = Foo>`.
33#[derive(Debug, Clone, PartialEq, Eq, Hash)] 33#[derive(Debug, Clone, PartialEq, Eq, Hash)]
34pub struct GenericArgs { 34pub struct GenericArgs {
35 pub args: Vec<GenericArg>, 35 pub args: Vec<GenericArg>,
@@ -72,7 +72,7 @@ impl Path {
72 mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>), 72 mut cb: impl FnMut(Path, &ast::UseTree, bool, Option<Name>),
73 ) { 73 ) {
74 if let Some(tree) = item_src.value.use_tree() { 74 if let Some(tree) = item_src.value.use_tree() {
75 expand_use_tree(None, tree, hygiene, &mut cb); 75 lower_use::lower_use_tree(None, tree, hygiene, &mut cb);
76 } 76 }
77 } 77 }
78 78
@@ -296,108 +296,6 @@ impl From<Name> for Path {
296 } 296 }
297} 297}
298 298
299fn expand_use_tree(
300 prefix: Option<Path>,
301 tree: ast::UseTree,
302 hygiene: &Hygiene,
303 cb: &mut dyn FnMut(Path, &ast::UseTree, bool, Option<Name>),
304) {
305 if let Some(use_tree_list) = tree.use_tree_list() {
306 let prefix = match tree.path() {
307 // E.g. use something::{{{inner}}};
308 None => prefix,
309 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
310 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
311 Some(path) => match convert_path(prefix, path, hygiene) {
312 Some(it) => Some(it),
313 None => return, // FIXME: report errors somewhere
314 },
315 };
316 for child_tree in use_tree_list.use_trees() {
317 expand_use_tree(prefix.clone(), child_tree, hygiene, cb);
318 }
319 } else {
320 let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
321 if let Some(ast_path) = tree.path() {
322 // Handle self in a path.
323 // E.g. `use something::{self, <...>}`
324 if ast_path.qualifier().is_none() {
325 if let Some(segment) = ast_path.segment() {
326 if segment.kind() == Some(ast::PathSegmentKind::SelfKw) {
327 if let Some(prefix) = prefix {
328 cb(prefix, &tree, false, alias);
329 return;
330 }
331 }
332 }
333 }
334 if let Some(path) = convert_path(prefix, ast_path, hygiene) {
335 let is_glob = tree.has_star();
336 cb(path, &tree, is_glob, alias)
337 }
338 // FIXME: report errors somewhere
339 // We get here if we do
340 }
341 }
342}
343
344fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Option<Path> {
345 let prefix = if let Some(qual) = path.qualifier() {
346 Some(convert_path(prefix, qual, hygiene)?)
347 } else {
348 prefix
349 };
350
351 let segment = path.segment()?;
352 let res = match segment.kind()? {
353 ast::PathSegmentKind::Name(name_ref) => {
354 match hygiene.name_ref_to_name(name_ref) {
355 Either::Left(name) => {
356 // no type args in use
357 let mut res = prefix.unwrap_or_else(|| Path {
358 kind: PathKind::Plain,
359 segments: Vec::with_capacity(1),
360 });
361 res.segments.push(PathSegment {
362 name,
363 args_and_bindings: None, // no type args in use
364 });
365 res
366 }
367 Either::Right(crate_id) => {
368 return Some(Path::from_simple_segments(
369 PathKind::DollarCrate(crate_id),
370 iter::empty(),
371 ))
372 }
373 }
374 }
375 ast::PathSegmentKind::CrateKw => {
376 if prefix.is_some() {
377 return None;
378 }
379 Path::from_simple_segments(PathKind::Crate, iter::empty())
380 }
381 ast::PathSegmentKind::SelfKw => {
382 if prefix.is_some() {
383 return None;
384 }
385 Path::from_simple_segments(PathKind::Self_, iter::empty())
386 }
387 ast::PathSegmentKind::SuperKw => {
388 if prefix.is_some() {
389 return None;
390 }
391 Path::from_simple_segments(PathKind::Super, iter::empty())
392 }
393 ast::PathSegmentKind::Type { .. } => {
394 // not allowed in imports
395 return None;
396 }
397 };
398 Some(res)
399}
400
401pub mod known { 299pub mod known {
402 use hir_expand::name; 300 use hir_expand::name;
403 301
diff --git a/crates/ra_hir_def/src/path/lower_use.rs b/crates/ra_hir_def/src/path/lower_use.rs
new file mode 100644
index 000000000..e2e1f716d
--- /dev/null
+++ b/crates/ra_hir_def/src/path/lower_use.rs
@@ -0,0 +1,115 @@
1//! Lowers a single complex use like `use foo::{bar, baz};` into a list of paths like
2//! `foo::bar`, `foo::baz`;
3
4use std::iter;
5
6use either::Either;
7use hir_expand::{
8 hygiene::Hygiene,
9 name::{AsName, Name},
10};
11use ra_syntax::ast::{self, NameOwner};
12
13use crate::path::{Path, PathKind, PathSegment};
14
15pub(crate) fn lower_use_tree(
16 prefix: Option<Path>,
17 tree: ast::UseTree,
18 hygiene: &Hygiene,
19 cb: &mut dyn FnMut(Path, &ast::UseTree, bool, Option<Name>),
20) {
21 if let Some(use_tree_list) = tree.use_tree_list() {
22 let prefix = match tree.path() {
23 // E.g. use something::{{{inner}}};
24 None => prefix,
25 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
26 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
27 Some(path) => match convert_path(prefix, path, hygiene) {
28 Some(it) => Some(it),
29 None => return, // FIXME: report errors somewhere
30 },
31 };
32 for child_tree in use_tree_list.use_trees() {
33 lower_use_tree(prefix.clone(), child_tree, hygiene, cb);
34 }
35 } else {
36 let alias = tree.alias().and_then(|a| a.name()).map(|a| a.as_name());
37 if let Some(ast_path) = tree.path() {
38 // Handle self in a path.
39 // E.g. `use something::{self, <...>}`
40 if ast_path.qualifier().is_none() {
41 if let Some(segment) = ast_path.segment() {
42 if segment.kind() == Some(ast::PathSegmentKind::SelfKw) {
43 if let Some(prefix) = prefix {
44 cb(prefix, &tree, false, alias);
45 return;
46 }
47 }
48 }
49 }
50 if let Some(path) = convert_path(prefix, ast_path, hygiene) {
51 let is_glob = tree.has_star();
52 cb(path, &tree, is_glob, alias)
53 }
54 // FIXME: report errors somewhere
55 // We get here if we do
56 }
57 }
58}
59
60fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Option<Path> {
61 let prefix = if let Some(qual) = path.qualifier() {
62 Some(convert_path(prefix, qual, hygiene)?)
63 } else {
64 prefix
65 };
66
67 let segment = path.segment()?;
68 let res = match segment.kind()? {
69 ast::PathSegmentKind::Name(name_ref) => {
70 match hygiene.name_ref_to_name(name_ref) {
71 Either::Left(name) => {
72 // no type args in use
73 let mut res = prefix.unwrap_or_else(|| Path {
74 kind: PathKind::Plain,
75 segments: Vec::with_capacity(1),
76 });
77 res.segments.push(PathSegment {
78 name,
79 args_and_bindings: None, // no type args in use
80 });
81 res
82 }
83 Either::Right(crate_id) => {
84 return Some(Path::from_simple_segments(
85 PathKind::DollarCrate(crate_id),
86 iter::empty(),
87 ))
88 }
89 }
90 }
91 ast::PathSegmentKind::CrateKw => {
92 if prefix.is_some() {
93 return None;
94 }
95 Path::from_simple_segments(PathKind::Crate, iter::empty())
96 }
97 ast::PathSegmentKind::SelfKw => {
98 if prefix.is_some() {
99 return None;
100 }
101 Path::from_simple_segments(PathKind::Self_, iter::empty())
102 }
103 ast::PathSegmentKind::SuperKw => {
104 if prefix.is_some() {
105 return None;
106 }
107 Path::from_simple_segments(PathKind::Super, iter::empty())
108 }
109 ast::PathSegmentKind::Type { .. } => {
110 // not allowed in imports
111 return None;
112 }
113 };
114 Some(res)
115}
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index 9484a61d5..17b2169d2 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -17,9 +17,9 @@ use crate::{
17 nameres::{BuiltinShadowMode, CrateDefMap}, 17 nameres::{BuiltinShadowMode, CrateDefMap},
18 path::{Path, PathKind}, 18 path::{Path, PathKind},
19 per_ns::PerNs, 19 per_ns::PerNs,
20 AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, 20 AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
21 GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, 21 HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
22 StructId, TraitId, TypeAliasId, TypeParamId, VariantId, 22 TypeAliasId, TypeParamId, VariantId,
23}; 23};
24 24
25#[derive(Debug, Clone, Default)] 25#[derive(Debug, Clone, Default)]
@@ -524,7 +524,7 @@ impl HasResolver for ModuleId {
524 524
525impl HasResolver for TraitId { 525impl HasResolver for TraitId {
526 fn resolver(self, db: &impl DefDatabase) -> Resolver { 526 fn resolver(self, db: &impl DefDatabase) -> Resolver {
527 self.module(db).resolver(db).push_generic_params_scope(db, self.into()) 527 self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
528 } 528 }
529} 529}
530 530
@@ -564,7 +564,8 @@ impl HasResolver for TypeAliasId {
564 564
565impl HasResolver for ImplId { 565impl HasResolver for ImplId {
566 fn resolver(self, db: &impl DefDatabase) -> Resolver { 566 fn resolver(self, db: &impl DefDatabase) -> Resolver {
567 self.module(db) 567 self.lookup(db)
568 .container
568 .resolver(db) 569 .resolver(db)
569 .push_generic_params_scope(db, self.into()) 570 .push_generic_params_scope(db, self.into())
570 .push_impl_block_scope(self) 571 .push_impl_block_scope(self)
diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs
index 27caa02cc..20200d1db 100644
--- a/crates/ra_hir_def/src/src.rs
+++ b/crates/ra_hir_def/src/src.rs
@@ -4,7 +4,10 @@ use hir_expand::InFile;
4use ra_arena::map::ArenaMap; 4use ra_arena::map::ArenaMap;
5use ra_syntax::ast; 5use ra_syntax::ast;
6 6
7use crate::{db::DefDatabase, ConstLoc, FunctionLoc, StaticLoc, TypeAliasLoc}; 7use crate::{
8 db::DefDatabase, ConstLoc, EnumLoc, FunctionLoc, ImplLoc, StaticLoc, StructLoc, TraitLoc,
9 TypeAliasLoc, UnionLoc,
10};
8 11
9pub trait HasSource { 12pub trait HasSource {
10 type Value; 13 type Value;
@@ -47,6 +50,51 @@ impl HasSource for StaticLoc {
47 } 50 }
48} 51}
49 52
53impl HasSource for ImplLoc {
54 type Value = ast::ImplBlock;
55
56 fn source(&self, db: &impl DefDatabase) -> InFile<ast::ImplBlock> {
57 let node = self.ast_id.to_node(db);
58 InFile::new(self.ast_id.file_id, node)
59 }
60}
61
62impl HasSource for TraitLoc {
63 type Value = ast::TraitDef;
64
65 fn source(&self, db: &impl DefDatabase) -> InFile<ast::TraitDef> {
66 let node = self.ast_id.to_node(db);
67 InFile::new(self.ast_id.file_id, node)
68 }
69}
70
71impl HasSource for StructLoc {
72 type Value = ast::StructDef;
73
74 fn source(&self, db: &impl DefDatabase) -> InFile<ast::StructDef> {
75 let node = self.ast_id.to_node(db);
76 InFile::new(self.ast_id.file_id, node)
77 }
78}
79
80impl HasSource for UnionLoc {
81 type Value = ast::UnionDef;
82
83 fn source(&self, db: &impl DefDatabase) -> InFile<ast::UnionDef> {
84 let node = self.ast_id.to_node(db);
85 InFile::new(self.ast_id.file_id, node)
86 }
87}
88
89impl HasSource for EnumLoc {
90 type Value = ast::EnumDef;
91
92 fn source(&self, db: &impl DefDatabase) -> InFile<ast::EnumDef> {
93 let node = self.ast_id.to_node(db);
94 InFile::new(self.ast_id.file_id, node)
95 }
96}
97
50pub trait HasChildSource { 98pub trait HasChildSource {
51 type ChildId; 99 type ChildId;
52 type Value; 100 type Value;