aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model.rs10
-rw-r--r--crates/ra_hir/src/from_source.rs29
-rw-r--r--crates/ra_hir/src/has_source.rs6
-rw-r--r--crates/ra_hir_def/src/adt.rs13
-rw-r--r--crates/ra_hir_def/src/attr.rs7
-rw-r--r--crates/ra_hir_def/src/child_by_source.rs10
-rw-r--r--crates/ra_hir_def/src/db.rs12
-rw-r--r--crates/ra_hir_def/src/docs.rs6
-rw-r--r--crates/ra_hir_def/src/generics.rs6
-rw-r--r--crates/ra_hir_def/src/keys.rs4
-rw-r--r--crates/ra_hir_def/src/lib.rs50
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs19
-rw-r--r--crates/ra_hir_def/src/src.rs21
-rw-r--r--crates/ra_hir_ty/src/lower.rs9
14 files changed, 123 insertions, 79 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 8588aa5a3..c705d1630 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -11,7 +11,7 @@ use hir_def::{
11 per_ns::PerNs, 11 per_ns::PerNs,
12 resolver::HasResolver, 12 resolver::HasResolver,
13 type_ref::{Mutability, TypeRef}, 13 type_ref::{Mutability, TypeRef},
14 AdtId, AstItemDef, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, 14 AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId,
15 LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, 15 LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId,
16 StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, 16 StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId,
17}; 17};
@@ -309,11 +309,11 @@ impl Union {
309 } 309 }
310 310
311 pub fn module(self, db: &impl DefDatabase) -> Module { 311 pub fn module(self, db: &impl DefDatabase) -> Module {
312 Module { id: self.id.module(db) } 312 Module { id: self.id.lookup(db).container }
313 } 313 }
314 314
315 pub fn ty(self, db: &impl HirDatabase) -> Type { 315 pub fn ty(self, db: &impl HirDatabase) -> Type {
316 Type::from_def(db, self.id.module(db).krate, self.id) 316 Type::from_def(db, self.id.lookup(db).container.krate, self.id)
317 } 317 }
318 318
319 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> { 319 pub fn fields(self, db: &impl HirDatabase) -> Vec<StructField> {
@@ -337,7 +337,7 @@ pub struct Enum {
337 337
338impl Enum { 338impl Enum {
339 pub fn module(self, db: &impl DefDatabase) -> Module { 339 pub fn module(self, db: &impl DefDatabase) -> Module {
340 Module { id: self.id.module(db) } 340 Module { id: self.id.lookup(db).container }
341 } 341 }
342 342
343 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 343 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@@ -357,7 +357,7 @@ impl Enum {
357 } 357 }
358 358
359 pub fn ty(self, db: &impl HirDatabase) -> Type { 359 pub fn ty(self, db: &impl HirDatabase) -> Type {
360 Type::from_def(db, self.id.module(db).krate, self.id) 360 Type::from_def(db, self.id.lookup(db).container.krate, self.id)
361 } 361 }
362} 362}
363 363
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index 978326c74..f7411c5cf 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -1,7 +1,7 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2use hir_def::{ 2use hir_def::{
3 child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, AstItemDef, 3 child_by_source::ChildBySource, dyn_map::DynMap, keys, nameres::ModuleSource, EnumVariantId,
4 EnumVariantId, GenericDefId, LocationCtx, ModuleId, VariantId, 4 GenericDefId, ModuleId, VariantId,
5}; 5};
6use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind}; 6use hir_expand::{name::AsName, AstId, MacroDefId, MacroDefKind};
7use ra_syntax::{ 7use ra_syntax::{
@@ -32,15 +32,19 @@ impl FromSource for Struct {
32impl FromSource for Union { 32impl FromSource for Union {
33 type Ast = ast::UnionDef; 33 type Ast = ast::UnionDef;
34 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 34 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
35 let id = from_source(db, src)?; 35 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::UNION]
36 Some(Union { id }) 36 .get(&src)
37 .copied()
38 .map(Union::from)
37 } 39 }
38} 40}
39impl FromSource for Enum { 41impl FromSource for Enum {
40 type Ast = ast::EnumDef; 42 type Ast = ast::EnumDef;
41 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 43 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
42 let id = from_source(db, src)?; 44 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::ENUM]
43 Some(Enum { id }) 45 .get(&src)
46 .copied()
47 .map(Enum::from)
44 } 48 }
45} 49}
46impl FromSource for Trait { 50impl FromSource for Trait {
@@ -250,19 +254,6 @@ impl Module {
250 } 254 }
251} 255}
252 256
253fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: InFile<N>) -> Option<DEF>
254where
255 N: AstNode,
256 DEF: AstItemDef<N>,
257{
258 let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
259 let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
260 let ctx = LocationCtx::new(db, module.id, src.file_id);
261 let items = db.ast_id_map(src.file_id);
262 let item_id = items.ast_id(&src.value);
263 Some(DEF::from_ast_id(ctx, item_id))
264}
265
266fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap { 257fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap {
267 _analyze_container(db, src).unwrap_or_default() 258 _analyze_container(db, src).unwrap_or_default()
268} 259}
diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs
index 8f35a3fbb..72afecf26 100644
--- a/crates/ra_hir/src/has_source.rs
+++ b/crates/ra_hir/src/has_source.rs
@@ -4,7 +4,7 @@ use either::Either;
4use hir_def::{ 4use hir_def::{
5 nameres::ModuleSource, 5 nameres::ModuleSource,
6 src::{HasChildSource, HasSource as _}, 6 src::{HasChildSource, HasSource as _},
7 AstItemDef, Lookup, VariantId, 7 Lookup, VariantId,
8}; 8};
9use ra_syntax::ast; 9use ra_syntax::ast;
10 10
@@ -57,13 +57,13 @@ impl HasSource for Struct {
57impl HasSource for Union { 57impl HasSource for Union {
58 type Ast = ast::UnionDef; 58 type Ast = ast::UnionDef;
59 fn source(self, db: &impl DefDatabase) -> InFile<ast::UnionDef> { 59 fn source(self, db: &impl DefDatabase) -> InFile<ast::UnionDef> {
60 self.id.source(db) 60 self.id.lookup(db).source(db)
61 } 61 }
62} 62}
63impl HasSource for Enum { 63impl HasSource for Enum {
64 type Ast = ast::EnumDef; 64 type Ast = ast::EnumDef;
65 fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumDef> { 65 fn source(self, db: &impl DefDatabase) -> InFile<ast::EnumDef> {
66 self.id.source(db) 66 self.id.lookup(db).source(db)
67 } 67 }
68} 68}
69impl HasSource for EnumVariant { 69impl HasSource for EnumVariant {
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs
index ef2b20f1e..ec3d57d1a 100644
--- a/crates/ra_hir_def/src/adt.rs
+++ b/crates/ra_hir_def/src/adt.rs
@@ -11,9 +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, src::HasSource, trace::Trace, type_ref::TypeRef, 14 db::DefDatabase, src::HasChildSource, src::HasSource, trace::Trace, type_ref::TypeRef, EnumId,
15 AstItemDef, EnumId, LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, UnionId, 15 LocalEnumVariantId, LocalStructFieldId, Lookup, StructId, UnionId, VariantId,
16 VariantId,
17}; 16};
18 17
19/// Note that we use `StructData` for unions as well! 18/// Note that we use `StructData` for unions as well!
@@ -58,7 +57,7 @@ impl StructData {
58 Arc::new(StructData { name, variant_data }) 57 Arc::new(StructData { name, variant_data })
59 } 58 }
60 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> {
61 let src = id.source(db); 60 let src = id.lookup(db).source(db);
62 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());
63 let variant_data = VariantData::new( 62 let variant_data = VariantData::new(
64 src.value 63 src.value
@@ -73,7 +72,7 @@ impl StructData {
73 72
74impl EnumData { 73impl EnumData {
75 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> {
76 let src = e.source(db); 75 let src = e.lookup(db).source(db);
77 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());
78 let mut trace = Trace::new_for_arena(); 77 let mut trace = Trace::new_for_arena();
79 lower_enum(&mut trace, &src.value); 78 lower_enum(&mut trace, &src.value);
@@ -90,7 +89,7 @@ impl HasChildSource for EnumId {
90 type ChildId = LocalEnumVariantId; 89 type ChildId = LocalEnumVariantId;
91 type Value = ast::EnumVariant; 90 type Value = ast::EnumVariant;
92 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>> {
93 let src = self.source(db); 92 let src = self.lookup(db).source(db);
94 let mut trace = Trace::new_for_map(); 93 let mut trace = Trace::new_for_map();
95 lower_enum(&mut trace, &src.value); 94 lower_enum(&mut trace, &src.value);
96 src.with_value(trace.into_map()) 95 src.with_value(trace.into_map())
@@ -155,7 +154,7 @@ impl HasChildSource for VariantId {
155 src.map(|map| map[it.local_id].kind()) 154 src.map(|map| map[it.local_id].kind())
156 } 155 }
157 VariantId::StructId(it) => it.lookup(db).source(db).map(|it| it.kind()), 156 VariantId::StructId(it) => it.lookup(db).source(db).map(|it| it.kind()),
158 VariantId::UnionId(it) => it.source(db).map(|it| { 157 VariantId::UnionId(it) => it.lookup(db).source(db).map(|it| {
159 it.record_field_def_list() 158 it.record_field_def_list()
160 .map(ast::StructKind::Record) 159 .map(ast::StructKind::Record)
161 .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 d2aa5ce8f..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)]
@@ -57,8 +56,8 @@ impl Attrs {
57 } 56 }
58 AttrDefId::AdtId(it) => match it { 57 AttrDefId::AdtId(it) => match it {
59 AdtId::StructId(it) => attrs_from_loc(it.lookup(db), 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_loc(it.lookup(db), db), 62 AttrDefId::TraitId(it) => attrs_from_loc(it.lookup(db), db),
64 AttrDefId::MacroDefId(it) => { 63 AttrDefId::MacroDefId(it) => {
diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs
index eba361578..3c9379b15 100644
--- a/crates/ra_hir_def/src/child_by_source.rs
+++ b/crates/ra_hir_def/src/child_by_source.rs
@@ -103,8 +103,14 @@ impl ChildBySource for ModuleId {
103 let src = strukt.lookup(db).source(db); 103 let src = strukt.lookup(db).source(db);
104 res[keys::STRUCT].insert(src, strukt) 104 res[keys::STRUCT].insert(src, strukt)
105 } 105 }
106 AdtId::UnionId(_) => (), 106 AdtId::UnionId(union_) => {
107 AdtId::EnumId(_) => (), 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 }
108 }, 114 },
109 _ => (), 115 _ => (),
110 } 116 }
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index e120c7768..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, ImplLoc, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId, 21 GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId,
22 TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, 22 TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc,
23}; 23};
24 24
25#[salsa::query_group(InternDatabaseStorage)] 25#[salsa::query_group(InternDatabaseStorage)]
@@ -29,9 +29,9 @@ pub trait InternDatabase: SourceDatabase {
29 #[salsa::interned] 29 #[salsa::interned]
30 fn intern_struct(&self, loc: StructLoc) -> 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]
diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs
index 58143b894..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
@@ -52,8 +52,8 @@ impl Documentation {
52 } 52 }
53 AttrDefId::AdtId(it) => match it { 53 AttrDefId::AdtId(it) => match it {
54 AdtId::StructId(it) => docs_from_ast(&it.lookup(db).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);
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs
index f1ce8d59a..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, ...).
@@ -76,12 +76,12 @@ impl GenericParams {
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 }
diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs
index 758cf8097..ada145379 100644
--- a/crates/ra_hir_def/src/keys.rs
+++ b/crates/ra_hir_def/src/keys.rs
@@ -9,7 +9,7 @@ use rustc_hash::FxHashMap;
9use crate::{ 9use crate::{
10 dyn_map::{DynMap, Policy}, 10 dyn_map::{DynMap, Policy},
11 ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId, 11 ConstId, EnumVariantId, FunctionId, ImplId, StaticId, StructFieldId, StructId, TraitId,
12 TypeAliasId, TypeParamId, 12 TypeAliasId, TypeParamId, EnumId, UnionId,
13}; 13};
14 14
15type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>; 15type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
@@ -21,6 +21,8 @@ pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new();
21pub const IMPL: Key<ast::ImplBlock, ImplId> = Key::new(); 21pub const IMPL: Key<ast::ImplBlock, ImplId> = Key::new();
22pub const TRAIT: Key<ast::TraitDef, TraitId> = Key::new(); 22pub const TRAIT: Key<ast::TraitDef, TraitId> = Key::new();
23pub const STRUCT: Key<ast::StructDef, StructId> = 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();
24 26
25pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new(); 27pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
26pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new(); 28pub const TUPLE_FIELD: Key<ast::TupleFieldDef, StructFieldId> = Key::new();
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 1a599706a..c9e4e6a0f 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -173,24 +173,48 @@ impl Lookup for StructId {
173#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 173#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
174pub struct UnionId(salsa::InternId); 174pub struct UnionId(salsa::InternId);
175impl_intern_key!(UnionId); 175impl_intern_key!(UnionId);
176impl AstItemDef<ast::UnionDef> for UnionId { 176
177 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::UnionDef>) -> Self { 177#[derive(Debug, Clone, PartialEq, Eq, Hash)]
178 db.intern_union(loc) 178pub struct UnionLoc {
179 pub container: ModuleId,
180 pub ast_id: AstId<ast::UnionDef>,
181}
182
183impl Intern for UnionLoc {
184 type ID = UnionId;
185 fn intern(self, db: &impl db::DefDatabase) -> UnionId {
186 db.intern_union(self)
179 } 187 }
180 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::UnionDef> { 188}
181 db.lookup_intern_union(self) 189
190impl Lookup for UnionId {
191 type Data = UnionLoc;
192 fn lookup(&self, db: &impl db::DefDatabase) -> UnionLoc {
193 db.lookup_intern_union(*self)
182 } 194 }
183} 195}
184 196
185#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 197#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
186pub struct EnumId(salsa::InternId); 198pub struct EnumId(salsa::InternId);
187impl_intern_key!(EnumId); 199impl_intern_key!(EnumId);
188impl AstItemDef<ast::EnumDef> for EnumId { 200
189 fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::EnumDef>) -> Self { 201#[derive(Debug, Clone, PartialEq, Eq, Hash)]
190 db.intern_enum(loc) 202pub struct EnumLoc {
203 pub container: ModuleId,
204 pub ast_id: AstId<ast::EnumDef>,
205}
206
207impl Intern for EnumLoc {
208 type ID = EnumId;
209 fn intern(self, db: &impl db::DefDatabase) -> EnumId {
210 db.intern_enum(self)
191 } 211 }
192 fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::EnumDef> { 212}
193 db.lookup_intern_enum(self) 213
214impl Lookup for EnumId {
215 type Data = EnumLoc;
216 fn lookup(&self, db: &impl db::DefDatabase) -> EnumLoc {
217 db.lookup_intern_enum(*self)
194 } 218 }
195} 219}
196 220
@@ -545,8 +569,8 @@ impl HasModule for AdtId {
545 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 569 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
546 match self { 570 match self {
547 AdtId::StructId(it) => it.lookup(db).container, 571 AdtId::StructId(it) => it.lookup(db).container,
548 AdtId::UnionId(it) => it.module(db), 572 AdtId::UnionId(it) => it.lookup(db).container,
549 AdtId::EnumId(it) => it.module(db), 573 AdtId::EnumId(it) => it.lookup(db).container,
550 } 574 }
551 } 575 }
552} 576}
@@ -569,7 +593,7 @@ impl HasModule for GenericDefId {
569 GenericDefId::TraitId(it) => it.lookup(db).container, 593 GenericDefId::TraitId(it) => it.lookup(db).container,
570 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), 594 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
571 GenericDefId::ImplId(it) => it.lookup(db).container, 595 GenericDefId::ImplId(it) => it.lookup(db).container,
572 GenericDefId::EnumVariantId(it) => it.parent.module(db), 596 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
573 GenericDefId::ConstId(it) => it.lookup(db).module(db), 597 GenericDefId::ConstId(it) => it.lookup(db).module(db),
574 } 598 }
575 } 599 }
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 602ec0911..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, ImplLoc, 27 AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
28 Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructLoc, 28 LocalImportId, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc,
29 TraitLoc, 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 {
@@ -753,8 +753,6 @@ where
753 753
754 fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) { 754 fn define_def(&mut self, def: &raw::DefData, attrs: &Attrs) {
755 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 };
756 let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id);
757
758 // 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;
759 // 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
760 // macro invocation 758 // macro invocation
@@ -778,10 +776,15 @@ where
778 PerNs::both(def.into(), def.into()) 776 PerNs::both(def.into(), def.into())
779 } 777 }
780 raw::DefKind::Union(ast_id) => { 778 raw::DefKind::Union(ast_id) => {
781 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) }
782 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())
783 } 787 }
784 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()),
785 raw::DefKind::Const(ast_id) => { 788 raw::DefKind::Const(ast_id) => {
786 let def = ConstLoc { 789 let def = ConstLoc {
787 container: ContainerId::ModuleId(module), 790 container: ContainerId::ModuleId(module),
diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs
index 7e212adb1..20200d1db 100644
--- a/crates/ra_hir_def/src/src.rs
+++ b/crates/ra_hir_def/src/src.rs
@@ -5,7 +5,8 @@ use ra_arena::map::ArenaMap;
5use ra_syntax::ast; 5use ra_syntax::ast;
6 6
7use crate::{ 7use crate::{
8 db::DefDatabase, ConstLoc, FunctionLoc, ImplLoc, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, 8 db::DefDatabase, ConstLoc, EnumLoc, FunctionLoc, ImplLoc, StaticLoc, StructLoc, TraitLoc,
9 TypeAliasLoc, UnionLoc,
9}; 10};
10 11
11pub trait HasSource { 12pub trait HasSource {
@@ -76,6 +77,24 @@ impl HasSource for StructLoc {
76 } 77 }
77} 78}
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
79pub trait HasChildSource { 98pub trait HasChildSource {
80 type ChildId; 99 type ChildId;
81 type Value; 100 type Value;
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs
index 6af5bf50a..5f795bc02 100644
--- a/crates/ra_hir_ty/src/lower.rs
+++ b/crates/ra_hir_ty/src/lower.rs
@@ -14,7 +14,7 @@ use hir_def::{
14 path::{GenericArg, Path, PathKind, PathSegment}, 14 path::{GenericArg, Path, PathKind, PathSegment},
15 resolver::{HasResolver, Resolver, TypeNs}, 15 resolver::{HasResolver, Resolver, TypeNs},
16 type_ref::{TypeBound, TypeRef}, 16 type_ref::{TypeBound, TypeRef},
17 AdtId, AstItemDef, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, 17 AdtId, ConstId, EnumId, EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId,
18 LocalStructFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, UnionId, VariantId, 18 LocalStructFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, UnionId, VariantId,
19}; 19};
20use ra_arena::map::ArenaMap; 20use ra_arena::map::ArenaMap;
@@ -698,10 +698,11 @@ impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId);
698impl CallableDef { 698impl CallableDef {
699 pub fn krate(self, db: &impl HirDatabase) -> CrateId { 699 pub fn krate(self, db: &impl HirDatabase) -> CrateId {
700 match self { 700 match self {
701 CallableDef::FunctionId(f) => f.lookup(db).module(db).krate, 701 CallableDef::FunctionId(f) => f.lookup(db).module(db),
702 CallableDef::StructId(s) => s.lookup(db).container.krate, 702 CallableDef::StructId(s) => s.lookup(db).container,
703 CallableDef::EnumVariantId(e) => e.parent.module(db).krate, 703 CallableDef::EnumVariantId(e) => e.parent.lookup(db).container,
704 } 704 }
705 .krate
705 } 706 }
706} 707}
707 708