aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-12 14:20:02 +0000
committerGitHub <[email protected]>2019-12-12 14:20:02 +0000
commitc213c3b36b7987504c6c3703b76a637dd0d01355 (patch)
tree36f9066ab25826f9401216d6ff371954eb9f447c
parentd8dba7fbe587ba5fb6cfe939083ccd92c61f5827 (diff)
parent7a255a2f9381ba5886cacc48c1dd0420a739a55c (diff)
Merge #2539
2539: Remove old location infra r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--crates/ra_hir/src/code_model.rs16
-rw-r--r--crates/ra_hir/src/from_source.rs116
-rw-r--r--crates/ra_hir/src/has_source.rs10
-rw-r--r--crates/ra_hir_def/src/adt.rs16
-rw-r--r--crates/ra_hir_def/src/attr.rs11
-rw-r--r--crates/ra_hir_def/src/child_by_source.rs22
-rw-r--r--crates/ra_hir_def/src/data.rs6
-rw-r--r--crates/ra_hir_def/src/db.rs16
-rw-r--r--crates/ra_hir_def/src/docs.rs10
-rw-r--r--crates/ra_hir_def/src/generics.rs10
-rw-r--r--crates/ra_hir_def/src/keys.rs7
-rw-r--r--crates/ra_hir_def/src/lib.rs169
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs31
-rw-r--r--crates/ra_hir_def/src/resolver.rs8
-rw-r--r--crates/ra_hir_def/src/src.rs41
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs5
-rw-r--r--crates/ra_hir_ty/src/lower.rs9
-rw-r--r--crates/ra_hir_ty/src/traits/chalk.rs6
18 files changed, 276 insertions, 233 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 117ca2fe3..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};
@@ -269,7 +269,7 @@ pub struct Struct {
269 269
270impl Struct { 270impl Struct {
271 pub fn module(self, db: &impl DefDatabase) -> Module { 271 pub fn module(self, db: &impl DefDatabase) -> Module {
272 Module { id: self.id.module(db) } 272 Module { id: self.id.lookup(db).container }
273 } 273 }
274 274
275 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { 275 pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> {
@@ -290,7 +290,7 @@ impl Struct {
290 } 290 }
291 291
292 pub fn ty(self, db: &impl HirDatabase) -> Type { 292 pub fn ty(self, db: &impl HirDatabase) -> Type {
293 Type::from_def(db, self.id.module(db).krate, self.id) 293 Type::from_def(db, self.id.lookup(db).container.krate, self.id)
294 } 294 }
295 295
296 fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> { 296 fn variant_data(self, db: &impl DefDatabase) -> Arc<VariantData> {
@@ -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
@@ -577,7 +577,7 @@ pub struct Trait {
577 577
578impl Trait { 578impl Trait {
579 pub fn module(self, db: &impl DefDatabase) -> Module { 579 pub fn module(self, db: &impl DefDatabase) -> Module {
580 Module { id: self.id.module(db) } 580 Module { id: self.id.lookup(db).container }
581 } 581 }
582 582
583 pub fn name(self, db: &impl DefDatabase) -> Name { 583 pub fn name(self, db: &impl DefDatabase) -> Name {
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
index b35188a21..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::{
@@ -23,35 +23,43 @@ pub trait FromSource: Sized {
23impl FromSource for Struct { 23impl FromSource for Struct {
24 type Ast = ast::StructDef; 24 type Ast = ast::StructDef;
25 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 25 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
26 let id = from_source(db, src)?; 26 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::STRUCT]
27 Some(Struct { id }) 27 .get(&src)
28 .copied()
29 .map(Struct::from)
28 } 30 }
29} 31}
30impl FromSource for Union { 32impl FromSource for Union {
31 type Ast = ast::UnionDef; 33 type Ast = ast::UnionDef;
32 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> {
33 let id = from_source(db, src)?; 35 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::UNION]
34 Some(Union { id }) 36 .get(&src)
37 .copied()
38 .map(Union::from)
35 } 39 }
36} 40}
37impl FromSource for Enum { 41impl FromSource for Enum {
38 type Ast = ast::EnumDef; 42 type Ast = ast::EnumDef;
39 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> {
40 let id = from_source(db, src)?; 44 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::ENUM]
41 Some(Enum { id }) 45 .get(&src)
46 .copied()
47 .map(Enum::from)
42 } 48 }
43} 49}
44impl FromSource for Trait { 50impl FromSource for Trait {
45 type Ast = ast::TraitDef; 51 type Ast = ast::TraitDef;
46 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 52 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
47 let id = from_source(db, src)?; 53 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::TRAIT]
48 Some(Trait { id }) 54 .get(&src)
55 .copied()
56 .map(Trait::from)
49 } 57 }
50} 58}
51impl FromSource for Function { 59impl FromSource for Function {
52 type Ast = ast::FnDef; 60 type Ast = ast::FnDef;
53 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 61 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
54 Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::FUNCTION] 62 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::FUNCTION]
55 .get(&src) 63 .get(&src)
56 .copied() 64 .copied()
57 .map(Function::from) 65 .map(Function::from)
@@ -61,7 +69,7 @@ impl FromSource for Function {
61impl FromSource for Const { 69impl FromSource for Const {
62 type Ast = ast::ConstDef; 70 type Ast = ast::ConstDef;
63 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 71 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
64 Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::CONST] 72 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::CONST]
65 .get(&src) 73 .get(&src)
66 .copied() 74 .copied()
67 .map(Const::from) 75 .map(Const::from)
@@ -70,7 +78,7 @@ impl FromSource for Const {
70impl FromSource for Static { 78impl FromSource for Static {
71 type Ast = ast::StaticDef; 79 type Ast = ast::StaticDef;
72 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 80 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
73 Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db)[keys::STATIC] 81 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::STATIC]
74 .get(&src) 82 .get(&src)
75 .copied() 83 .copied()
76 .map(Static::from) 84 .map(Static::from)
@@ -80,8 +88,7 @@ impl FromSource for Static {
80impl FromSource for TypeAlias { 88impl FromSource for TypeAlias {
81 type Ast = ast::TypeAliasDef; 89 type Ast = ast::TypeAliasDef;
82 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 90 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
83 Container::find(db, src.as_ref().map(|it| it.syntax()))?.child_by_source(db) 91 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::TYPE_ALIAS]
84 [keys::TYPE_ALIAS]
85 .get(&src) 92 .get(&src)
86 .copied() 93 .copied()
87 .map(TypeAlias::from) 94 .map(TypeAlias::from)
@@ -107,10 +114,10 @@ impl FromSource for MacroDef {
107impl FromSource for ImplBlock { 114impl FromSource for ImplBlock {
108 type Ast = ast::ImplBlock; 115 type Ast = ast::ImplBlock;
109 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> { 116 fn from_source(db: &(impl DefDatabase + AstDatabase), src: InFile<Self::Ast>) -> Option<Self> {
110 // XXX: use `.parent()` to avoid finding ourselves 117 analyze_container(db, src.as_ref().map(|it| it.syntax()))[keys::IMPL]
111 let parent = src.value.syntax().parent()?; 118 .get(&src)
112 let container = Container::find(db, src.with_value(parent).as_ref())?; 119 .copied()
113 container.child_by_source(db)[keys::IMPL].get(&src).copied().map(ImplBlock::from) 120 .map(ImplBlock::from)
114 } 121 }
115} 122}
116 123
@@ -247,57 +254,30 @@ impl Module {
247 } 254 }
248} 255}
249 256
250fn from_source<N, DEF>(db: &(impl DefDatabase + AstDatabase), src: InFile<N>) -> Option<DEF> 257fn analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> DynMap {
251where 258 _analyze_container(db, src).unwrap_or_default()
252 N: AstNode,
253 DEF: AstItemDef<N>,
254{
255 let module_src = ModuleSource::from_child_node(db, src.as_ref().map(|it| it.syntax()));
256 let module = Module::from_definition(db, InFile::new(src.file_id, module_src))?;
257 let ctx = LocationCtx::new(db, module.id, src.file_id);
258 let items = db.ast_id_map(src.file_id);
259 let item_id = items.ast_id(&src.value);
260 Some(DEF::from_ast_id(ctx, item_id))
261}
262
263enum Container {
264 Trait(Trait),
265 ImplBlock(ImplBlock),
266 Module(Module),
267} 259}
268 260
269impl Container { 261fn _analyze_container(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<DynMap> {
270 fn find(db: &impl DefDatabase, src: InFile<&SyntaxNode>) -> Option<Container> { 262 // FIXME: this doesn't try to handle nested declarations
271 // FIXME: this doesn't try to handle nested declarations 263 for container in src.value.ancestors().skip(1) {
272 for container in src.value.ancestors() { 264 let res = match_ast! {
273 let res = match_ast! { 265 match container {
274 match container { 266 ast::TraitDef(it) => {
275 ast::TraitDef(it) => { 267 let c = Trait::from_source(db, src.with_value(it))?;
276 let c = Trait::from_source(db, src.with_value(it))?; 268 c.id.child_by_source(db)
277 Container::Trait(c) 269 },
278 }, 270 ast::ImplBlock(it) => {
279 ast::ImplBlock(it) => { 271 let c = ImplBlock::from_source(db, src.with_value(it))?;
280 let c = ImplBlock::from_source(db, src.with_value(it))?; 272 c.id.child_by_source(db)
281 Container::ImplBlock(c) 273 },
282 }, 274 _ => { continue },
283 _ => { continue }, 275 }
284 } 276 };
285 }; 277 return Some(res);
286 return Some(res);
287 }
288
289 let module_source = ModuleSource::from_child_node(db, src);
290 let c = Module::from_definition(db, src.with_value(module_source))?;
291 Some(Container::Module(c))
292 } 278 }
293}
294 279
295impl ChildBySource for Container { 280 let module_source = ModuleSource::from_child_node(db, src);
296 fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { 281 let c = Module::from_definition(db, src.with_value(module_source))?;
297 match self { 282 Some(c.id.child_by_source(db))
298 Container::Trait(it) => it.id.child_by_source(db),
299 Container::ImplBlock(it) => it.id.child_by_source(db),
300 Container::Module(it) => it.id.child_by_source(db),
301 }
302 }
303} 283}
diff --git a/crates/ra_hir/src/has_source.rs b/crates/ra_hir/src/has_source.rs
index a888fe995..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
@@ -51,19 +51,19 @@ impl HasSource for StructField {
51impl HasSource for Struct { 51impl HasSource for Struct {
52 type Ast = ast::StructDef; 52 type Ast = ast::StructDef;
53 fn source(self, db: &impl DefDatabase) -> InFile<ast::StructDef> { 53 fn source(self, db: &impl DefDatabase) -> InFile<ast::StructDef> {
54 self.id.source(db) 54 self.id.lookup(db).source(db)
55 } 55 }
56} 56}
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 {
@@ -93,7 +93,7 @@ impl HasSource for Static {
93impl HasSource for Trait { 93impl HasSource for Trait {
94 type Ast = ast::TraitDef; 94 type Ast = ast::TraitDef;
95 fn source(self, db: &impl DefDatabase) -> InFile<ast::TraitDef> { 95 fn source(self, db: &impl DefDatabase) -> InFile<ast::TraitDef> {
96 self.id.source(db) 96 self.id.lookup(db).source(db)
97 } 97 }
98} 98}
99impl HasSource for TypeAlias { 99impl HasSource for TypeAlias {
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 12d4e777a..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,11 +55,11 @@ 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 }
diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs
index 821549bd5..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,6 +94,24 @@ 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 }
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index 42821b9b1..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);
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs
index 8907aacca..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, TraitId, TypeAliasId, 21 GenericDefId, ImplId, ImplLoc, ModuleId, StaticId, StaticLoc, StructId, StructLoc, TraitId,
22 TypeAliasLoc, UnionId, 22 TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc,
23}; 23};
24 24
25#[salsa::query_group(InternDatabaseStorage)] 25#[salsa::query_group(InternDatabaseStorage)]
@@ -27,17 +27,17 @@ 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]
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 5d1100945..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 =
diff --git a/crates/ra_hir_def/src/keys.rs b/crates/ra_hir_def/src/keys.rs
index d318b2451..4611c6e38 100644
--- a/crates/ra_hir_def/src/keys.rs
+++ b/crates/ra_hir_def/src/keys.rs
@@ -8,7 +8,8 @@ use rustc_hash::FxHashMap;
8 8
9use crate::{ 9use crate::{
10 dyn_map::{DynMap, Policy}, 10 dyn_map::{DynMap, Policy},
11 ConstId, EnumVariantId, FunctionId, ImplId, 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>>; 15type Key<K, V> = crate::dyn_map::Key<InFile<K>, V, AstPtrPolicy<K, V>>;
@@ -18,6 +19,10 @@ pub 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 TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new(); 20pub const TYPE_ALIAS: Key<ast::TypeAliasDef, TypeAliasId> = Key::new();
20pub 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();
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();
21 26
22pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new(); 27pub const ENUM_VARIANT: Key<ast::EnumVariant, EnumVariantId> = Key::new();
23pub 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 5564b166b..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
@@ -492,7 +483,7 @@ impl HasModule for FunctionLoc {
492 match self.container { 483 match self.container {
493 ContainerId::ModuleId(it) => it, 484 ContainerId::ModuleId(it) => it,
494 ContainerId::ImplId(it) => it.lookup(db).container, 485 ContainerId::ImplId(it) => it.lookup(db).container,
495 ContainerId::TraitId(it) => it.module(db), 486 ContainerId::TraitId(it) => it.lookup(db).container,
496 } 487 }
497 } 488 }
498} 489}
@@ -502,7 +493,7 @@ impl HasModule for TypeAliasLoc {
502 match self.container { 493 match self.container {
503 ContainerId::ModuleId(it) => it, 494 ContainerId::ModuleId(it) => it,
504 ContainerId::ImplId(it) => it.lookup(db).container, 495 ContainerId::ImplId(it) => it.lookup(db).container,
505 ContainerId::TraitId(it) => it.module(db), 496 ContainerId::TraitId(it) => it.lookup(db).container,
506 } 497 }
507 } 498 }
508} 499}
@@ -512,7 +503,7 @@ impl HasModule for ConstLoc {
512 match self.container { 503 match self.container {
513 ContainerId::ModuleId(it) => it, 504 ContainerId::ModuleId(it) => it,
514 ContainerId::ImplId(it) => it.lookup(db).container, 505 ContainerId::ImplId(it) => it.lookup(db).container,
515 ContainerId::TraitId(it) => it.module(db), 506 ContainerId::TraitId(it) => it.lookup(db).container,
516 } 507 }
517 } 508 }
518} 509}
@@ -520,9 +511,9 @@ impl HasModule for ConstLoc {
520impl HasModule for AdtId { 511impl HasModule for AdtId {
521 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 512 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
522 match self { 513 match self {
523 AdtId::StructId(it) => it.module(db), 514 AdtId::StructId(it) => it.lookup(db).container,
524 AdtId::UnionId(it) => it.module(db), 515 AdtId::UnionId(it) => it.lookup(db).container,
525 AdtId::EnumId(it) => it.module(db), 516 AdtId::EnumId(it) => it.lookup(db).container,
526 } 517 }
527 } 518 }
528} 519}
@@ -542,10 +533,10 @@ impl HasModule for GenericDefId {
542 match self { 533 match self {
543 GenericDefId::FunctionId(it) => it.lookup(db).module(db), 534 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
544 GenericDefId::AdtId(it) => it.module(db), 535 GenericDefId::AdtId(it) => it.module(db),
545 GenericDefId::TraitId(it) => it.module(db), 536 GenericDefId::TraitId(it) => it.lookup(db).container,
546 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), 537 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
547 GenericDefId::ImplId(it) => it.lookup(db).container, 538 GenericDefId::ImplId(it) => it.lookup(db).container,
548 GenericDefId::EnumVariantId(it) => it.parent.module(db), 539 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container,
549 GenericDefId::ConstId(it) => it.lookup(db).module(db), 540 GenericDefId::ConstId(it) => it.lookup(db).module(db),
550 } 541 }
551 } 542 }
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index b33507a9a..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, 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 {
@@ -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
@@ -773,14 +771,20 @@ where
773 PerNs::values(def.into()) 771 PerNs::values(def.into())
774 } 772 }
775 raw::DefKind::Struct(ast_id) => { 773 raw::DefKind::Struct(ast_id) => {
776 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) }
777 PerNs::both(id, id) 775 .intern(self.def_collector.db);
776 PerNs::both(def.into(), def.into())
778 } 777 }
779 raw::DefKind::Union(ast_id) => { 778 raw::DefKind::Union(ast_id) => {
780 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) }
781 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())
782 } 787 }
783 raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()),
784 raw::DefKind::Const(ast_id) => { 788 raw::DefKind::Const(ast_id) => {
785 let def = ConstLoc { 789 let def = ConstLoc {
786 container: ContainerId::ModuleId(module), 790 container: ContainerId::ModuleId(module),
@@ -796,7 +800,12 @@ where
796 800
797 PerNs::values(def.into()) 801 PerNs::values(def.into())
798 } 802 }
799 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 }
800 raw::DefKind::TypeAlias(ast_id) => { 809 raw::DefKind::TypeAlias(ast_id) => {
801 let def = TypeAliasLoc { 810 let def = TypeAliasLoc {
802 container: ContainerId::ModuleId(module), 811 container: ContainerId::ModuleId(module),
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index f87b16b44..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
diff --git a/crates/ra_hir_def/src/src.rs b/crates/ra_hir_def/src/src.rs
index a5c4359a7..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, ImplLoc, 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;
@@ -56,6 +59,42 @@ impl HasSource for ImplLoc {
56 } 59 }
57} 60}
58 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
59pub trait HasChildSource { 98pub trait HasChildSource {
60 type ChildId; 99 type ChildId;
61 type Value; 100 type Value;
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index ec5ace757..be5f3cbe3 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -367,6 +367,9 @@ mod tests {
367 BuiltinFnLikeExpander::FormatArgs, 367 BuiltinFnLikeExpander::FormatArgs,
368 ); 368 );
369 369
370 assert_eq!(expanded, r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#); 370 assert_eq!(
371 expanded,
372 r#"std::fmt::Arguments::new_v1(&[] ,&[std::fmt::ArgumentV1::new(&(arg1(a,b,c)),std::fmt::Display::fmt),std::fmt::ArgumentV1::new(&(arg2),std::fmt::Display::fmt),])"#
373 );
371 } 374 }
372} 375}
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs
index eab91229e..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.module(db).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
diff --git a/crates/ra_hir_ty/src/traits/chalk.rs b/crates/ra_hir_ty/src/traits/chalk.rs
index ff299f511..fc21872b2 100644
--- a/crates/ra_hir_ty/src/traits/chalk.rs
+++ b/crates/ra_hir_ty/src/traits/chalk.rs
@@ -9,9 +9,7 @@ use chalk_ir::{
9}; 9};
10use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum}; 10use chalk_rust_ir::{AssociatedTyDatum, AssociatedTyValue, ImplDatum, StructDatum, TraitDatum};
11 11
12use hir_def::{ 12use hir_def::{AssocItemId, ContainerId, GenericDefId, ImplId, Lookup, TraitId, TypeAliasId};
13 AssocItemId, AstItemDef, ContainerId, GenericDefId, ImplId, Lookup, TraitId, TypeAliasId,
14};
15use ra_db::{ 13use ra_db::{
16 salsa::{InternId, InternKey}, 14 salsa::{InternId, InternKey},
17 CrateId, 15 CrateId,
@@ -593,7 +591,7 @@ pub(crate) fn trait_datum_query(
593 let bound_vars = Substs::bound_vars(&generic_params); 591 let bound_vars = Substs::bound_vars(&generic_params);
594 let flags = chalk_rust_ir::TraitFlags { 592 let flags = chalk_rust_ir::TraitFlags {
595 auto: trait_data.auto, 593 auto: trait_data.auto,
596 upstream: trait_.module(db).krate != krate, 594 upstream: trait_.lookup(db).container.krate != krate,
597 non_enumerable: true, 595 non_enumerable: true,
598 coinductive: false, // only relevant for Chalk testing 596 coinductive: false, // only relevant for Chalk testing
599 // FIXME set these flags correctly 597 // FIXME set these flags correctly