diff options
author | Aleksey Kladov <[email protected]> | 2019-11-20 13:03:59 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-20 13:22:58 +0000 |
commit | cebeedc66fc40097eae20bf1767a285d00269966 (patch) | |
tree | e54c306ac0512e3610430574dc8bea39e4b50218 /crates/ra_hir/src/code_model.rs | |
parent | 06fa3d8389c833b01f482bf35b0f850e627612b9 (diff) |
Next gen IDs for functions
The current system with AstIds has two primaraly drawbacks:
* It is possible to manufacture IDs out of thin air.
For example, it's possible to create IDs for items which are not
considered in CrateDefMap due to cfg. Or it is possible to mixup
structs and unions, because they share ID space.
* Getting the ID of a parent requires a secondary index.
Instead, the plan is to pursue the more traditional approach, where
each items stores the id of the parent declaration. This makes
`FromSource` more awkward, but also more correct: now, to get from an
AST to HIR, we first do this recursively for the parent item, and the
just search the children of the parent for the matching def
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index f436d5d5e..c49190a0f 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -12,7 +12,8 @@ use hir_def::{ | |||
12 | builtin_type::BuiltinType, | 12 | builtin_type::BuiltinType, |
13 | traits::TraitData, | 13 | traits::TraitData, |
14 | type_ref::{Mutability, TypeRef}, | 14 | type_ref::{Mutability, TypeRef}, |
15 | AssocItemId, CrateModuleId, ImplId, LocalEnumVariantId, LocalStructFieldId, ModuleId, UnionId, | 15 | AssocItemId, CrateModuleId, FunctionContainerId, HasModule, ImplId, LocalEnumVariantId, |
16 | LocalStructFieldId, Lookup, ModuleId, UnionId, | ||
16 | }; | 17 | }; |
17 | use hir_expand::{ | 18 | use hir_expand::{ |
18 | diagnostics::DiagnosticSink, | 19 | diagnostics::DiagnosticSink, |
@@ -647,7 +648,7 @@ impl FnData { | |||
647 | 648 | ||
648 | impl Function { | 649 | impl Function { |
649 | pub fn module(self, db: &impl DefDatabase) -> Module { | 650 | pub fn module(self, db: &impl DefDatabase) -> Module { |
650 | Module { id: self.id.module(db) } | 651 | self.id.lookup(db).module(db).into() |
651 | } | 652 | } |
652 | 653 | ||
653 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 654 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
@@ -680,21 +681,25 @@ impl Function { | |||
680 | 681 | ||
681 | /// The containing impl block, if this is a method. | 682 | /// The containing impl block, if this is a method. |
682 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { | 683 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { |
683 | ImplBlock::containing(db, self.into()) | 684 | match self.container(db) { |
685 | Some(Container::ImplBlock(it)) => Some(it), | ||
686 | _ => None, | ||
687 | } | ||
684 | } | 688 | } |
685 | 689 | ||
686 | /// The containing trait, if this is a trait method definition. | 690 | /// The containing trait, if this is a trait method definition. |
687 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { | 691 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { |
688 | db.trait_items_index(self.module(db).id).get_parent_trait(self.id.into()).map(Trait::from) | 692 | match self.container(db) { |
693 | Some(Container::Trait(it)) => Some(it), | ||
694 | _ => None, | ||
695 | } | ||
689 | } | 696 | } |
690 | 697 | ||
691 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | 698 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { |
692 | if let Some(impl_block) = self.impl_block(db) { | 699 | match self.id.lookup(db).container { |
693 | Some(impl_block.into()) | 700 | FunctionContainerId::TraitId(it) => Some(Container::Trait(it.into())), |
694 | } else if let Some(trait_) = self.parent_trait(db) { | 701 | FunctionContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), |
695 | Some(trait_.into()) | 702 | FunctionContainerId::ModuleId(_) => None, |
696 | } else { | ||
697 | None | ||
698 | } | 703 | } |
699 | } | 704 | } |
700 | 705 | ||