aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir/src/code_model.rs62
-rw-r--r--crates/ra_hir/src/lib.rs2
-rw-r--r--crates/ra_hir_def/src/body.rs3
-rw-r--r--crates/ra_hir_def/src/body/lower.rs1
-rw-r--r--crates/ra_hir_def/src/child_by_source.rs97
-rw-r--r--crates/ra_hir_def/src/lib.rs24
-rw-r--r--crates/ra_hir_def/src/resolver.rs1
-rw-r--r--crates/ra_hir_ty/src/infer/path.rs2
-rw-r--r--crates/ra_hir_ty/src/method_resolution.rs10
-rw-r--r--crates/ra_hir_ty/src/utils.rs2
10 files changed, 82 insertions, 122 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 7850ea9a7..8dbc0d667 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -11,9 +11,9 @@ 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, ConstId, ContainerId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, 14 AdtId, ConstId, DefWithBodyId, EnumId, FunctionId, HasModule, ImplId, LocalEnumVariantId,
15 LocalEnumVariantId, LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, 15 LocalImportId, LocalModuleId, LocalStructFieldId, Lookup, ModuleId, StaticId, StructId,
16 StaticId, StructId, TraitId, TypeAliasId, TypeParamId, UnionId, 16 TraitId, TypeAliasId, TypeParamId, UnionId,
17}; 17};
18use hir_expand::{ 18use hir_expand::{
19 diagnostics::DiagnosticSink, 19 diagnostics::DiagnosticSink,
@@ -529,30 +529,6 @@ impl Const {
529 pub fn name(self, db: &impl HirDatabase) -> Option<Name> { 529 pub fn name(self, db: &impl HirDatabase) -> Option<Name> {
530 db.const_data(self.id).name.clone() 530 db.const_data(self.id).name.clone()
531 } 531 }
532
533 /// The containing impl block, if this is a type alias.
534 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
535 match self.container(db) {
536 Some(Container::ImplBlock(it)) => Some(it),
537 _ => None,
538 }
539 }
540
541 /// The containing trait, if this is a trait type alias definition.
542 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
543 match self.container(db) {
544 Some(Container::Trait(it)) => Some(it),
545 _ => None,
546 }
547 }
548
549 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
550 match self.id.lookup(db).container {
551 ContainerId::TraitId(it) => Some(Container::Trait(it.into())),
552 ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())),
553 ContainerId::ModuleId(_) => None,
554 }
555 }
556} 532}
557 533
558#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 534#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -612,30 +588,6 @@ impl TypeAlias {
612 Some(self.module(db).krate()) 588 Some(self.module(db).krate())
613 } 589 }
614 590
615 /// The containing impl block, if this is a type alias.
616 pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> {
617 match self.container(db) {
618 Some(Container::ImplBlock(it)) => Some(it),
619 _ => None,
620 }
621 }
622
623 /// The containing trait, if this is a trait type alias definition.
624 pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> {
625 match self.container(db) {
626 Some(Container::Trait(it)) => Some(it),
627 _ => None,
628 }
629 }
630
631 pub fn container(self, db: &impl DefDatabase) -> Option<Container> {
632 match self.id.lookup(db).container {
633 ContainerId::TraitId(it) => Some(Container::Trait(it.into())),
634 ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())),
635 ContainerId::ModuleId(_) => None,
636 }
637 }
638
639 pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> { 591 pub fn type_ref(self, db: &impl DefDatabase) -> Option<TypeRef> {
640 db.type_alias_data(self.id).type_ref.clone() 592 db.type_alias_data(self.id).type_ref.clone()
641 } 593 }
@@ -654,14 +606,6 @@ pub struct MacroDef {
654 pub(crate) id: MacroDefId, 606 pub(crate) id: MacroDefId,
655} 607}
656 608
657impl MacroDef {}
658
659pub enum Container {
660 Trait(Trait),
661 ImplBlock(ImplBlock),
662}
663impl_froms!(Container: Trait, ImplBlock);
664
665#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 609#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
666pub enum AssocItem { 610pub enum AssocItem {
667 Function(Function), 611 Function(Function),
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 2e52a1f5c..7f9aef770 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -39,7 +39,7 @@ mod from_source;
39 39
40pub use crate::{ 40pub use crate::{
41 code_model::{ 41 code_model::{
42 Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Docs, Enum, 42 Adt, AssocItem, AttrDef, Const, Crate, CrateDependency, DefWithBody, Docs, Enum,
43 EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Import, Local, 43 EnumVariant, FieldSource, Function, GenericDef, HasAttrs, ImplBlock, Import, Local,
44 MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias, 44 MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, StructField, Trait, Type, TypeAlias,
45 TypeParam, Union, VariantDef, 45 TypeParam, Union, VariantDef,
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs
index d4cab0561..332c509e1 100644
--- a/crates/ra_hir_def/src/body.rs
+++ b/crates/ra_hir_def/src/body.rs
@@ -17,7 +17,7 @@ use crate::{
17 nameres::{BuiltinShadowMode, CrateDefMap}, 17 nameres::{BuiltinShadowMode, CrateDefMap},
18 path::{ModPath, Path}, 18 path::{ModPath, Path},
19 src::HasSource, 19 src::HasSource,
20 DefWithBodyId, HasModule, Lookup, ModuleId, 20 DefWithBodyId, HasModule, Lookup, ModuleDefId, ModuleId,
21}; 21};
22 22
23struct Expander { 23struct Expander {
@@ -119,6 +119,7 @@ pub struct Body {
119 pub params: Vec<PatId>, 119 pub params: Vec<PatId>,
120 /// The `ExprId` of the actual body expression. 120 /// The `ExprId` of the actual body expression.
121 pub body_expr: ExprId, 121 pub body_expr: ExprId,
122 pub defs: Vec<ModuleDefId>,
122} 123}
123 124
124pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>; 125pub type ExprPtr = Either<AstPtr<ast::Expr>, AstPtr<ast::RecordField>>;
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs
index 61193b4d8..86960186f 100644
--- a/crates/ra_hir_def/src/body/lower.rs
+++ b/crates/ra_hir_def/src/body/lower.rs
@@ -41,6 +41,7 @@ pub(super) fn lower(
41 pats: Arena::default(), 41 pats: Arena::default(),
42 params: Vec::new(), 42 params: Vec::new(),
43 body_expr: ExprId::dummy(), 43 body_expr: ExprId::dummy(),
44 defs: Vec::new(),
44 }, 45 },
45 } 46 }
46 .collect(params, body) 47 .collect(params, body)
diff --git a/crates/ra_hir_def/src/child_by_source.rs b/crates/ra_hir_def/src/child_by_source.rs
index 3c9379b15..f5a65ad40 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 AdtId, AssocItemId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId, ModuleId, 14 AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, ImplId, Lookup, ModuleDefId,
15 StructFieldId, TraitId, VariantId, 15 ModuleId, StructFieldId, TraitId, VariantId,
16}; 16};
17 17
18pub trait ChildBySource { 18pub trait ChildBySource {
@@ -76,47 +76,11 @@ impl ChildBySource for ModuleId {
76 let mut res = DynMap::default(); 76 let mut res = DynMap::default();
77 77
78 let crate_def_map = db.crate_def_map(self.krate); 78 let crate_def_map = db.crate_def_map(self.krate);
79 for item in crate_def_map[self.local_id].scope.declarations() { 79 let module_data = &crate_def_map[self.local_id];
80 match item {
81 ModuleDefId::FunctionId(func) => {
82 let src = func.lookup(db).source(db);
83 res[keys::FUNCTION].insert(src, func)
84 }
85 ModuleDefId::ConstId(konst) => {
86 let src = konst.lookup(db).source(db);
87 res[keys::CONST].insert(src, konst)
88 }
89 ModuleDefId::StaticId(statik) => {
90 let src = statik.lookup(db).source(db);
91 res[keys::STATIC].insert(src, statik)
92 }
93 ModuleDefId::TypeAliasId(ty) => {
94 let src = ty.lookup(db).source(db);
95 res[keys::TYPE_ALIAS].insert(src, ty)
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 },
115 _ => (),
116 }
117 }
118 80
119 for &impl_ in crate_def_map[self.local_id].impls.iter() { 81 module_data.scope.declarations().for_each(|item| add_module_def(db, &mut res, item));
82
83 for &impl_ in module_data.impls.iter() {
120 let src = impl_.lookup(db).source(db); 84 let src = impl_.lookup(db).source(db);
121 res[keys::IMPL].insert(src, impl_) 85 res[keys::IMPL].insert(src, impl_)
122 } 86 }
@@ -125,6 +89,46 @@ impl ChildBySource for ModuleId {
125 } 89 }
126} 90}
127 91
92fn add_module_def(db: &impl DefDatabase, map: &mut DynMap, item: ModuleDefId) {
93 match item {
94 ModuleDefId::FunctionId(func) => {
95 let src = func.lookup(db).source(db);
96 map[keys::FUNCTION].insert(src, func)
97 }
98 ModuleDefId::ConstId(konst) => {
99 let src = konst.lookup(db).source(db);
100 map[keys::CONST].insert(src, konst)
101 }
102 ModuleDefId::StaticId(statik) => {
103 let src = statik.lookup(db).source(db);
104 map[keys::STATIC].insert(src, statik)
105 }
106 ModuleDefId::TypeAliasId(ty) => {
107 let src = ty.lookup(db).source(db);
108 map[keys::TYPE_ALIAS].insert(src, ty)
109 }
110 ModuleDefId::TraitId(trait_) => {
111 let src = trait_.lookup(db).source(db);
112 map[keys::TRAIT].insert(src, trait_)
113 }
114 ModuleDefId::AdtId(adt) => match adt {
115 AdtId::StructId(strukt) => {
116 let src = strukt.lookup(db).source(db);
117 map[keys::STRUCT].insert(src, strukt)
118 }
119 AdtId::UnionId(union_) => {
120 let src = union_.lookup(db).source(db);
121 map[keys::UNION].insert(src, union_)
122 }
123 AdtId::EnumId(enum_) => {
124 let src = enum_.lookup(db).source(db);
125 map[keys::ENUM].insert(src, enum_)
126 }
127 },
128 _ => (),
129 }
130}
131
128impl ChildBySource for VariantId { 132impl ChildBySource for VariantId {
129 fn child_by_source(&self, db: &impl DefDatabase) -> DynMap { 133 fn child_by_source(&self, db: &impl DefDatabase) -> DynMap {
130 let mut res = DynMap::default(); 134 let mut res = DynMap::default();
@@ -160,3 +164,12 @@ impl ChildBySource for EnumId {
160 res 164 res
161 } 165 }
162} 166}
167
168impl ChildBySource for DefWithBodyId {
169 fn child_by_source(&self, db: &impl DefDatabase) -> DynMap {
170 let mut res = DynMap::default();
171 let body = db.body(*self);
172 body.defs.iter().copied().for_each(|item| add_module_def(db, &mut res, item));
173 res
174 }
175}
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs
index f085bbe87..4fc3127c4 100644
--- a/crates/ra_hir_def/src/lib.rs
+++ b/crates/ra_hir_def/src/lib.rs
@@ -335,6 +335,7 @@ pub enum ContainerId {
335 ModuleId(ModuleId), 335 ModuleId(ModuleId),
336 ImplId(ImplId), 336 ImplId(ImplId),
337 TraitId(TraitId), 337 TraitId(TraitId),
338 DefWithBodyId(DefWithBodyId),
338} 339}
339 340
340/// A Data Type 341/// A Data Type
@@ -478,33 +479,32 @@ pub trait HasModule {
478 fn module(&self, db: &impl db::DefDatabase) -> ModuleId; 479 fn module(&self, db: &impl db::DefDatabase) -> ModuleId;
479} 480}
480 481
481impl HasModule for FunctionLoc { 482impl HasModule for ContainerId {
482 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 483 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
483 match self.container { 484 match *self {
484 ContainerId::ModuleId(it) => it, 485 ContainerId::ModuleId(it) => it,
485 ContainerId::ImplId(it) => it.lookup(db).container, 486 ContainerId::ImplId(it) => it.lookup(db).container,
486 ContainerId::TraitId(it) => it.lookup(db).container, 487 ContainerId::TraitId(it) => it.lookup(db).container,
488 ContainerId::DefWithBodyId(it) => it.module(db),
487 } 489 }
488 } 490 }
489} 491}
490 492
493impl HasModule for FunctionLoc {
494 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
495 self.container.module(db)
496 }
497}
498
491impl HasModule for TypeAliasLoc { 499impl HasModule for TypeAliasLoc {
492 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 500 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
493 match self.container { 501 self.container.module(db)
494 ContainerId::ModuleId(it) => it,
495 ContainerId::ImplId(it) => it.lookup(db).container,
496 ContainerId::TraitId(it) => it.lookup(db).container,
497 }
498 } 502 }
499} 503}
500 504
501impl HasModule for ConstLoc { 505impl HasModule for ConstLoc {
502 fn module(&self, db: &impl db::DefDatabase) -> ModuleId { 506 fn module(&self, db: &impl db::DefDatabase) -> ModuleId {
503 match self.container { 507 self.container.module(db)
504 ContainerId::ModuleId(it) => it,
505 ContainerId::ImplId(it) => it.lookup(db).container,
506 ContainerId::TraitId(it) => it.lookup(db).container,
507 }
508 } 508 }
509} 509}
510 510
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs
index 2694c0438..250329271 100644
--- a/crates/ra_hir_def/src/resolver.rs
+++ b/crates/ra_hir_def/src/resolver.rs
@@ -586,6 +586,7 @@ impl HasResolver for ContainerId {
586 ContainerId::TraitId(it) => it.resolver(db), 586 ContainerId::TraitId(it) => it.resolver(db),
587 ContainerId::ImplId(it) => it.resolver(db), 587 ContainerId::ImplId(it) => it.resolver(db),
588 ContainerId::ModuleId(it) => it.resolver(db), 588 ContainerId::ModuleId(it) => it.resolver(db),
589 ContainerId::DefWithBodyId(it) => it.resolver(db),
589 } 590 }
590 } 591 }
591} 592}
diff --git a/crates/ra_hir_ty/src/infer/path.rs b/crates/ra_hir_ty/src/infer/path.rs
index 402a89386..31c90ea1e 100644
--- a/crates/ra_hir_ty/src/infer/path.rs
+++ b/crates/ra_hir_ty/src/infer/path.rs
@@ -237,7 +237,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
237 })); 237 }));
238 Some(substs) 238 Some(substs)
239 } 239 }
240 ContainerId::ModuleId(_) => None, 240 ContainerId::ModuleId(_) | ContainerId::DefWithBodyId(_) => None,
241 }; 241 };
242 242
243 self.write_assoc_resolution(id, item.into()); 243 self.write_assoc_resolution(id, item.into());
diff --git a/crates/ra_hir_ty/src/method_resolution.rs b/crates/ra_hir_ty/src/method_resolution.rs
index 848e306e9..d0b2b016d 100644
--- a/crates/ra_hir_ty/src/method_resolution.rs
+++ b/crates/ra_hir_ty/src/method_resolution.rs
@@ -6,8 +6,8 @@ use std::sync::Arc;
6 6
7use arrayvec::ArrayVec; 7use arrayvec::ArrayVec;
8use hir_def::{ 8use hir_def::{
9 lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocItemId, FunctionId, 9 lang_item::LangItemTarget, resolver::Resolver, type_ref::Mutability, AssocItemId, ContainerId,
10 HasModule, ImplId, Lookup, TraitId, 10 FunctionId, HasModule, ImplId, Lookup, TraitId,
11}; 11};
12use hir_expand::name::Name; 12use hir_expand::name::Name;
13use ra_db::CrateId; 13use ra_db::CrateId;
@@ -451,12 +451,12 @@ fn transform_receiver_ty(
451 self_ty: &Canonical<Ty>, 451 self_ty: &Canonical<Ty>,
452) -> Option<Ty> { 452) -> Option<Ty> {
453 let substs = match function_id.lookup(db).container { 453 let substs = match function_id.lookup(db).container {
454 hir_def::ContainerId::TraitId(_) => Substs::build_for_def(db, function_id) 454 ContainerId::TraitId(_) => Substs::build_for_def(db, function_id)
455 .push(self_ty.value.clone()) 455 .push(self_ty.value.clone())
456 .fill_with_unknown() 456 .fill_with_unknown()
457 .build(), 457 .build(),
458 hir_def::ContainerId::ImplId(impl_id) => inherent_impl_substs(db, impl_id, &self_ty)?, 458 ContainerId::ImplId(impl_id) => inherent_impl_substs(db, impl_id, &self_ty)?,
459 hir_def::ContainerId::ModuleId(_) => unreachable!(), 459 ContainerId::ModuleId(_) | ContainerId::DefWithBodyId(_) => unreachable!(),
460 }; 460 };
461 let sig = db.callable_item_signature(function_id.into()); 461 let sig = db.callable_item_signature(function_id.into());
462 Some(sig.params()[0].clone().subst(&substs)) 462 Some(sig.params()[0].clone().subst(&substs))
diff --git a/crates/ra_hir_ty/src/utils.rs b/crates/ra_hir_ty/src/utils.rs
index 29799a8cb..34defc1a2 100644
--- a/crates/ra_hir_ty/src/utils.rs
+++ b/crates/ra_hir_ty/src/utils.rs
@@ -157,6 +157,6 @@ fn parent_generic_def(db: &impl DefDatabase, def: GenericDefId) -> Option<Generi
157 match container { 157 match container {
158 ContainerId::ImplId(it) => Some(it.into()), 158 ContainerId::ImplId(it) => Some(it.into()),
159 ContainerId::TraitId(it) => Some(it.into()), 159 ContainerId::TraitId(it) => Some(it.into()),
160 ContainerId::ModuleId(_) => None, 160 ContainerId::ModuleId(_) | ContainerId::DefWithBodyId(_) => None,
161 } 161 }
162} 162}