aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-20 11:43:19 +0000
committerGitHub <[email protected]>2019-12-20 11:43:19 +0000
commit65377620245bda207145742595a7bd878e14f7ec (patch)
treed2016e6e3ce596e6a4b3e44f4894b5a411c62789 /crates/ra_hir_def
parent6e9335d311c058986c4bbef5aadbe208b87f63c7 (diff)
parentf42697e54b9d0a040011cb04c266d51710e249f1 (diff)
Merge #2608
2608: Support for nested traits r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r--crates/ra_hir_def/src/body/lower.rs44
-rw-r--r--crates/ra_hir_def/src/data.rs12
-rw-r--r--crates/ra_hir_def/src/lib.rs45
-rw-r--r--crates/ra_hir_def/src/nameres/collector.rs17
-rw-r--r--crates/ra_hir_def/src/resolver.rs18
5 files changed, 96 insertions, 40 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 17efa10e2..853e17bae 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -25,7 +25,8 @@ use crate::{
25 path::GenericArgs, 25 path::GenericArgs,
26 path::Path, 26 path::Path,
27 type_ref::{Mutability, TypeRef}, 27 type_ref::{Mutability, TypeRef},
28 ContainerId, DefWithBodyId, FunctionLoc, Intern, 28 ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, StaticLoc,
29 StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
29}; 30};
30 31
31pub(super) fn lower( 32pub(super) fn lower(
@@ -492,14 +493,45 @@ where
492 fn collect_block_items(&mut self, block: &ast::Block) { 493 fn collect_block_items(&mut self, block: &ast::Block) {
493 let container = ContainerId::DefWithBodyId(self.def); 494 let container = ContainerId::DefWithBodyId(self.def);
494 for item in block.items() { 495 for item in block.items() {
495 match item { 496 let def: ModuleDefId = match item {
496 ast::ModuleItem::FnDef(def) => { 497 ast::ModuleItem::FnDef(def) => {
497 let ast_id = self.expander.ast_id(&def); 498 let ast_id = self.expander.ast_id(&def);
498 self.body.defs.push(FunctionLoc { container, ast_id }.intern(self.db).into()) 499 FunctionLoc { container: container.into(), ast_id }.intern(self.db).into()
499 } 500 }
500 // FIXME: handle other items 501 ast::ModuleItem::TypeAliasDef(def) => {
501 _ => (), 502 let ast_id = self.expander.ast_id(&def);
502 } 503 TypeAliasLoc { container: container.into(), ast_id }.intern(self.db).into()
504 }
505 ast::ModuleItem::ConstDef(def) => {
506 let ast_id = self.expander.ast_id(&def);
507 ConstLoc { container: container.into(), ast_id }.intern(self.db).into()
508 }
509 ast::ModuleItem::StaticDef(def) => {
510 let ast_id = self.expander.ast_id(&def);
511 StaticLoc { container, ast_id }.intern(self.db).into()
512 }
513 ast::ModuleItem::StructDef(def) => {
514 let ast_id = self.expander.ast_id(&def);
515 StructLoc { container, ast_id }.intern(self.db).into()
516 }
517 ast::ModuleItem::EnumDef(def) => {
518 let ast_id = self.expander.ast_id(&def);
519 EnumLoc { container, ast_id }.intern(self.db).into()
520 }
521 ast::ModuleItem::UnionDef(def) => {
522 let ast_id = self.expander.ast_id(&def);
523 UnionLoc { container, ast_id }.intern(self.db).into()
524 }
525 ast::ModuleItem::TraitDef(def) => {
526 let ast_id = self.expander.ast_id(&def);
527 TraitLoc { container, ast_id }.intern(self.db).into()
528 }
529 ast::ModuleItem::ImplBlock(_)
530 | ast::ModuleItem::UseItem(_)
531 | ast::ModuleItem::ExternCrateItem(_)
532 | ast::ModuleItem::Module(_) => continue,
533 };
534 self.body.defs.push(def)
503 } 535 }
504 } 536 }
505 537
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index 4f4ef57cc..14e86936b 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, ConstId, ConstLoc, ContainerId, FunctionId, FunctionLoc, ImplId, Intern, Lookup, 15 AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, ImplId, Intern,
16 StaticId, TraitId, TypeAliasId, TypeAliasLoc, 16 Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc,
17}; 17};
18 18
19#[derive(Debug, Clone, PartialEq, Eq)] 19#[derive(Debug, Clone, PartialEq, Eq)]
@@ -99,7 +99,7 @@ impl TraitData {
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);
101 101
102 let container = ContainerId::TraitId(tr); 102 let container = AssocContainerId::TraitId(tr);
103 let items = if let Some(item_list) = src.value.item_list() { 103 let items = if let Some(item_list) = src.value.item_list() {
104 item_list 104 item_list
105 .impl_items() 105 .impl_items()
@@ -180,7 +180,7 @@ impl ImplData {
180 .map(|item_node| match item_node { 180 .map(|item_node| match item_node {
181 ast::ImplItem::FnDef(it) => { 181 ast::ImplItem::FnDef(it) => {
182 let def = FunctionLoc { 182 let def = FunctionLoc {
183 container: ContainerId::ImplId(id), 183 container: AssocContainerId::ImplId(id),
184 ast_id: AstId::new(src.file_id, items.ast_id(&it)), 184 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
185 } 185 }
186 .intern(db); 186 .intern(db);
@@ -188,7 +188,7 @@ impl ImplData {
188 } 188 }
189 ast::ImplItem::ConstDef(it) => { 189 ast::ImplItem::ConstDef(it) => {
190 let def = ConstLoc { 190 let def = ConstLoc {
191 container: ContainerId::ImplId(id), 191 container: AssocContainerId::ImplId(id),
192 ast_id: AstId::new(src.file_id, items.ast_id(&it)), 192 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
193 } 193 }
194 .intern(db); 194 .intern(db);
@@ -196,7 +196,7 @@ impl ImplData {
196 } 196 }
197 ast::ImplItem::TypeAliasDef(it) => { 197 ast::ImplItem::TypeAliasDef(it) => {
198 let def = TypeAliasLoc { 198 let def = TypeAliasLoc {
199 container: ContainerId::ImplId(id), 199 container: AssocContainerId::ImplId(id),
200 ast_id: AstId::new(src.file_id, items.ast_id(&it)), 200 ast_id: AstId::new(src.file_id, items.ast_id(&it)),
201 } 201 }
202 .intern(db); 202 .intern(db);
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index 4fc3127c4..140eccf26 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -71,7 +71,7 @@ impl_intern_key!(FunctionId);
71 71
72#[derive(Debug, Clone, PartialEq, Eq, Hash)] 72#[derive(Debug, Clone, PartialEq, Eq, Hash)]
73pub struct FunctionLoc { 73pub struct FunctionLoc {
74 pub container: ContainerId, 74 pub container: AssocContainerId,
75 pub ast_id: AstId<ast::FnDef>, 75 pub ast_id: AstId<ast::FnDef>,
76} 76}
77 77
@@ -95,7 +95,7 @@ impl_intern_key!(StructId);
95 95
96#[derive(Debug, Clone, PartialEq, Eq, Hash)] 96#[derive(Debug, Clone, PartialEq, Eq, Hash)]
97pub struct StructLoc { 97pub struct StructLoc {
98 pub container: ModuleId, 98 pub container: ContainerId,
99 pub ast_id: AstId<ast::StructDef>, 99 pub ast_id: AstId<ast::StructDef>,
100} 100}
101 101
@@ -119,7 +119,7 @@ impl_intern_key!(UnionId);
119 119
120#[derive(Debug, Clone, PartialEq, Eq, Hash)] 120#[derive(Debug, Clone, PartialEq, Eq, Hash)]
121pub struct UnionLoc { 121pub struct UnionLoc {
122 pub container: ModuleId, 122 pub container: ContainerId,
123 pub ast_id: AstId<ast::UnionDef>, 123 pub ast_id: AstId<ast::UnionDef>,
124} 124}
125 125
@@ -143,7 +143,7 @@ impl_intern_key!(EnumId);
143 143
144#[derive(Debug, Clone, PartialEq, Eq, Hash)] 144#[derive(Debug, Clone, PartialEq, Eq, Hash)]
145pub struct EnumLoc { 145pub struct EnumLoc {
146 pub container: ModuleId, 146 pub container: ContainerId,
147 pub ast_id: AstId<ast::EnumDef>, 147 pub ast_id: AstId<ast::EnumDef>,
148} 148}
149 149
@@ -187,7 +187,7 @@ pub struct ConstId(salsa::InternId);
187impl_intern_key!(ConstId); 187impl_intern_key!(ConstId);
188#[derive(Debug, Clone, PartialEq, Eq, Hash)] 188#[derive(Debug, Clone, PartialEq, Eq, Hash)]
189pub struct ConstLoc { 189pub struct ConstLoc {
190 pub container: ContainerId, 190 pub container: AssocContainerId,
191 pub ast_id: AstId<ast::ConstDef>, 191 pub ast_id: AstId<ast::ConstDef>,
192} 192}
193 193
@@ -211,7 +211,7 @@ impl_intern_key!(StaticId);
211 211
212#[derive(Debug, Clone, PartialEq, Eq, Hash)] 212#[derive(Debug, Clone, PartialEq, Eq, Hash)]
213pub struct StaticLoc { 213pub struct StaticLoc {
214 pub container: ModuleId, 214 pub container: ContainerId,
215 pub ast_id: AstId<ast::StaticDef>, 215 pub ast_id: AstId<ast::StaticDef>,
216} 216}
217 217
@@ -235,7 +235,7 @@ impl_intern_key!(TraitId);
235 235
236#[derive(Debug, Clone, PartialEq, Eq, Hash)] 236#[derive(Debug, Clone, PartialEq, Eq, Hash)]
237pub struct TraitLoc { 237pub struct TraitLoc {
238 pub container: ModuleId, 238 pub container: ContainerId,
239 pub ast_id: AstId<ast::TraitDef>, 239 pub ast_id: AstId<ast::TraitDef>,
240} 240}
241 241
@@ -259,7 +259,7 @@ impl_intern_key!(TypeAliasId);
259 259
260#[derive(Debug, Clone, PartialEq, Eq, Hash)] 260#[derive(Debug, Clone, PartialEq, Eq, Hash)]
261pub struct TypeAliasLoc { 261pub struct TypeAliasLoc {
262 pub container: ContainerId, 262 pub container: AssocContainerId,
263 pub ast_id: AstId<ast::TypeAliasDef>, 263 pub ast_id: AstId<ast::TypeAliasDef>,
264} 264}
265 265
@@ -333,10 +333,16 @@ impl_arena_id!(LocalTypeParamId);
333#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 333#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
334pub enum ContainerId { 334pub enum ContainerId {
335 ModuleId(ModuleId), 335 ModuleId(ModuleId),
336 DefWithBodyId(DefWithBodyId),
337}
338
339#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
340pub enum AssocContainerId {
341 ContainerId(ContainerId),
336 ImplId(ImplId), 342 ImplId(ImplId),
337 TraitId(TraitId), 343 TraitId(TraitId),
338 DefWithBodyId(DefWithBodyId),
339} 344}
345impl_froms!(AssocContainerId: ContainerId);
340 346
341/// A Data Type 347/// A Data Type
342#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 348#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -483,13 +489,21 @@ impl HasModule for ContainerId {
483 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 489 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
484 match *self { 490 match *self {
485 ContainerId::ModuleId(it) => it, 491 ContainerId::ModuleId(it) => it,
486 ContainerId::ImplId(it) => it.lookup(db).container,
487 ContainerId::TraitId(it) => it.lookup(db).container,
488 ContainerId::DefWithBodyId(it) => it.module(db), 492 ContainerId::DefWithBodyId(it) => it.module(db),
489 } 493 }
490 } 494 }
491} 495}
492 496
497impl HasModule for AssocContainerId {
498 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
499 match *self {
500 AssocContainerId::ContainerId(it) => it.module(db),
501 AssocContainerId::ImplId(it) => it.lookup(db).container,
502 AssocContainerId::TraitId(it) => it.lookup(db).container.module(db),
503 }
504 }
505}
506
493impl HasModule for FunctionLoc { 507impl HasModule for FunctionLoc {
494 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 508 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
495 self.container.module(db) 509 self.container.module(db)
@@ -515,6 +529,7 @@ impl HasModule for AdtId {
515 AdtId::UnionId(it) => it.lookup(db).container, 529 AdtId::UnionId(it) => it.lookup(db).container,
516 AdtId::EnumId(it) => it.lookup(db).container, 530 AdtId::EnumId(it) => it.lookup(db).container,
517 } 531 }
532 .module(db)
518 } 533 }
519} 534}
520 535
@@ -533,17 +548,17 @@ impl HasModule for GenericDefId {
533 match self { 548 match self {
534 GenericDefId::FunctionId(it) => it.lookup(db).module(db), 549 GenericDefId::FunctionId(it) => it.lookup(db).module(db),
535 GenericDefId::AdtId(it) => it.module(db), 550 GenericDefId::AdtId(it) => it.module(db),
536 GenericDefId::TraitId(it) => it.lookup(db).container, 551 GenericDefId::TraitId(it) => it.lookup(db).container.module(db),
537 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db), 552 GenericDefId::TypeAliasId(it) => it.lookup(db).module(db),
538 GenericDefId::ImplId(it) => it.lookup(db).container, 553 GenericDefId::ImplId(it) => it.lookup(db).container,
539 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container, 554 GenericDefId::EnumVariantId(it) => it.parent.lookup(db).container.module(db),
540 GenericDefId::ConstId(it) => it.lookup(db).module(db), 555 GenericDefId::ConstId(it) => it.lookup(db).module(db),
541 } 556 }
542 } 557 }
543} 558}
544 559
545impl HasModule for StaticLoc { 560impl HasModule for StaticLoc {
546 fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { 561 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
547 self.container 562 self.container.module(db)
548 } 563 }
549} 564}
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs
index 8bbf7ffa2..a1ea130e0 100644
--- a/crates/ra_hir_def/src/nameres/collector.rs
+++ b/crates/ra_hir_def/src/nameres/collector.rs
@@ -760,10 +760,11 @@ where
760 self.collect_derives(attrs, def); 760 self.collect_derives(attrs, def);
761 761
762 let name = def.name.clone(); 762 let name = def.name.clone();
763 let container = ContainerId::ModuleId(module);
763 let def: PerNs = match def.kind { 764 let def: PerNs = match def.kind {
764 raw::DefKind::Function(ast_id) => { 765 raw::DefKind::Function(ast_id) => {
765 let def = FunctionLoc { 766 let def = FunctionLoc {
766 container: ContainerId::ModuleId(module), 767 container: container.into(),
767 ast_id: AstId::new(self.file_id, ast_id), 768 ast_id: AstId::new(self.file_id, ast_id),
768 } 769 }
769 .intern(self.def_collector.db); 770 .intern(self.def_collector.db);
@@ -771,23 +772,23 @@ where
771 PerNs::values(def.into()) 772 PerNs::values(def.into())
772 } 773 }
773 raw::DefKind::Struct(ast_id) => { 774 raw::DefKind::Struct(ast_id) => {
774 let def = StructLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } 775 let def = StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
775 .intern(self.def_collector.db); 776 .intern(self.def_collector.db);
776 PerNs::both(def.into(), def.into()) 777 PerNs::both(def.into(), def.into())
777 } 778 }
778 raw::DefKind::Union(ast_id) => { 779 raw::DefKind::Union(ast_id) => {
779 let def = UnionLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } 780 let def = UnionLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
780 .intern(self.def_collector.db); 781 .intern(self.def_collector.db);
781 PerNs::both(def.into(), def.into()) 782 PerNs::both(def.into(), def.into())
782 } 783 }
783 raw::DefKind::Enum(ast_id) => { 784 raw::DefKind::Enum(ast_id) => {
784 let def = EnumLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } 785 let def = EnumLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
785 .intern(self.def_collector.db); 786 .intern(self.def_collector.db);
786 PerNs::types(def.into()) 787 PerNs::types(def.into())
787 } 788 }
788 raw::DefKind::Const(ast_id) => { 789 raw::DefKind::Const(ast_id) => {
789 let def = ConstLoc { 790 let def = ConstLoc {
790 container: ContainerId::ModuleId(module), 791 container: container.into(),
791 ast_id: AstId::new(self.file_id, ast_id), 792 ast_id: AstId::new(self.file_id, ast_id),
792 } 793 }
793 .intern(self.def_collector.db); 794 .intern(self.def_collector.db);
@@ -795,20 +796,20 @@ where
795 PerNs::values(def.into()) 796 PerNs::values(def.into())
796 } 797 }
797 raw::DefKind::Static(ast_id) => { 798 raw::DefKind::Static(ast_id) => {
798 let def = StaticLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } 799 let def = StaticLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
799 .intern(self.def_collector.db); 800 .intern(self.def_collector.db);
800 801
801 PerNs::values(def.into()) 802 PerNs::values(def.into())
802 } 803 }
803 raw::DefKind::Trait(ast_id) => { 804 raw::DefKind::Trait(ast_id) => {
804 let def = TraitLoc { container: module, ast_id: AstId::new(self.file_id, ast_id) } 805 let def = TraitLoc { container, ast_id: AstId::new(self.file_id, ast_id) }
805 .intern(self.def_collector.db); 806 .intern(self.def_collector.db);
806 807
807 PerNs::types(def.into()) 808 PerNs::types(def.into())
808 } 809 }
809 raw::DefKind::TypeAlias(ast_id) => { 810 raw::DefKind::TypeAlias(ast_id) => {
810 let def = TypeAliasLoc { 811 let def = TypeAliasLoc {
811 container: ContainerId::ModuleId(module), 812 container: container.into(),
812 ast_id: AstId::new(self.file_id, ast_id), 813 ast_id: AstId::new(self.file_id, ast_id),
813 } 814 }
814 .intern(self.def_collector.db); 815 .intern(self.def_collector.db);
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index 250329271..af9d194f8 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::{ModPath, PathKind}, 18 path::{ModPath, PathKind},
19 per_ns::PerNs, 19 per_ns::PerNs,
20 AdtId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, 20 AdtId, AssocContainerId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId,
21 HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, 21 FunctionId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId,
22 TypeAliasId, TypeParamId, VariantId, 22 StaticId, StructId, TraitId, TypeAliasId, TypeParamId, VariantId,
23}; 23};
24 24
25#[derive(Debug, Clone, Default)] 25#[derive(Debug, Clone, Default)]
@@ -583,14 +583,22 @@ impl HasResolver for DefWithBodyId {
583impl HasResolver for ContainerId { 583impl HasResolver for ContainerId {
584 fn resolver(self, db: &impl DefDatabase) -> Resolver { 584 fn resolver(self, db: &impl DefDatabase) -> Resolver {
585 match self { 585 match self {
586 ContainerId::TraitId(it) => it.resolver(db),
587 ContainerId::ImplId(it) => it.resolver(db),
588 ContainerId::ModuleId(it) => it.resolver(db), 586 ContainerId::ModuleId(it) => it.resolver(db),
589 ContainerId::DefWithBodyId(it) => it.resolver(db), 587 ContainerId::DefWithBodyId(it) => it.resolver(db),
590 } 588 }
591 } 589 }
592} 590}
593 591
592impl HasResolver for AssocContainerId {
593 fn resolver(self, db: &impl DefDatabase) -> Resolver {
594 match self {
595 AssocContainerId::ContainerId(it) => it.resolver(db),
596 AssocContainerId::TraitId(it) => it.resolver(db),
597 AssocContainerId::ImplId(it) => it.resolver(db),
598 }
599 }
600}
601
594impl HasResolver for GenericDefId { 602impl HasResolver for GenericDefId {
595 fn resolver(self, db: &impl DefDatabase) -> Resolver { 603 fn resolver(self, db: &impl DefDatabase) -> Resolver {
596 match self { 604 match self {