diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-20 15:45:22 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-20 15:45:22 +0000 |
commit | c51dcb1c4bdd71f9f17de508bd0d47e3c06ae1d5 (patch) | |
tree | 7e96d773620a3b03254d00386711cdc7c909e3ee /crates/ra_hir/src/code_model.rs | |
parent | b7a36b54431ca5b746af53549a1b6e142570c7f4 (diff) | |
parent | 111891dc2dc1d2c7ea87144e8e3ddefe23fc7b6d (diff) |
Merge #2327
2327: Move constants to new ID r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/code_model.rs')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 59 |
1 files changed, 34 insertions, 25 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index c49190a0f..920899dce 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -12,7 +12,7 @@ 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, FunctionContainerId, HasModule, ImplId, LocalEnumVariantId, | 15 | AssocItemId, ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, |
16 | LocalStructFieldId, Lookup, ModuleId, UnionId, | 16 | LocalStructFieldId, Lookup, ModuleId, UnionId, |
17 | }; | 17 | }; |
18 | use hir_expand::{ | 18 | use hir_expand::{ |
@@ -697,9 +697,9 @@ impl Function { | |||
697 | 697 | ||
698 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | 698 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { |
699 | match self.id.lookup(db).container { | 699 | match self.id.lookup(db).container { |
700 | FunctionContainerId::TraitId(it) => Some(Container::Trait(it.into())), | 700 | ContainerId::TraitId(it) => Some(Container::Trait(it.into())), |
701 | FunctionContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), | 701 | ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), |
702 | FunctionContainerId::ModuleId(_) => None, | 702 | ContainerId::ModuleId(_) => None, |
703 | } | 703 | } |
704 | } | 704 | } |
705 | 705 | ||
@@ -729,7 +729,7 @@ pub struct Const { | |||
729 | 729 | ||
730 | impl Const { | 730 | impl Const { |
731 | pub fn module(self, db: &impl DefDatabase) -> Module { | 731 | pub fn module(self, db: &impl DefDatabase) -> Module { |
732 | Module { id: self.id.module(db) } | 732 | Module { id: self.id.lookup(db).module(db) } |
733 | } | 733 | } |
734 | 734 | ||
735 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 735 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
@@ -748,22 +748,27 @@ impl Const { | |||
748 | db.infer(self.into()) | 748 | db.infer(self.into()) |
749 | } | 749 | } |
750 | 750 | ||
751 | /// The containing impl block, if this is a method. | 751 | /// The containing impl block, if this is a type alias. |
752 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { | 752 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { |
753 | ImplBlock::containing(db, self.into()) | 753 | match self.container(db) { |
754 | Some(Container::ImplBlock(it)) => Some(it), | ||
755 | _ => None, | ||
756 | } | ||
754 | } | 757 | } |
755 | 758 | ||
759 | /// The containing trait, if this is a trait type alias definition. | ||
756 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { | 760 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { |
757 | db.trait_items_index(self.module(db).id).get_parent_trait(self.id.into()).map(Trait::from) | 761 | match self.container(db) { |
762 | Some(Container::Trait(it)) => Some(it), | ||
763 | _ => None, | ||
764 | } | ||
758 | } | 765 | } |
759 | 766 | ||
760 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | 767 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { |
761 | if let Some(impl_block) = self.impl_block(db) { | 768 | match self.id.lookup(db).container { |
762 | Some(impl_block.into()) | 769 | ContainerId::TraitId(it) => Some(Container::Trait(it.into())), |
763 | } else if let Some(trait_) = self.parent_trait(db) { | 770 | ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), |
764 | Some(trait_.into()) | 771 | ContainerId::ModuleId(_) => None, |
765 | } else { | ||
766 | None | ||
767 | } | 772 | } |
768 | } | 773 | } |
769 | 774 | ||
@@ -954,30 +959,34 @@ pub struct TypeAlias { | |||
954 | 959 | ||
955 | impl TypeAlias { | 960 | impl TypeAlias { |
956 | pub fn module(self, db: &impl DefDatabase) -> Module { | 961 | pub fn module(self, db: &impl DefDatabase) -> Module { |
957 | Module { id: self.id.module(db) } | 962 | Module { id: self.id.lookup(db).module(db) } |
958 | } | 963 | } |
959 | 964 | ||
960 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { | 965 | pub fn krate(self, db: &impl DefDatabase) -> Option<Crate> { |
961 | Some(self.module(db).krate()) | 966 | Some(self.module(db).krate()) |
962 | } | 967 | } |
963 | 968 | ||
964 | /// The containing impl block, if this is a method. | 969 | /// The containing impl block, if this is a type alias. |
965 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { | 970 | pub fn impl_block(self, db: &impl DefDatabase) -> Option<ImplBlock> { |
966 | ImplBlock::containing(db, self.into()) | 971 | match self.container(db) { |
972 | Some(Container::ImplBlock(it)) => Some(it), | ||
973 | _ => None, | ||
974 | } | ||
967 | } | 975 | } |
968 | 976 | ||
969 | /// The containing trait, if this is a trait method definition. | 977 | /// The containing trait, if this is a trait type alias definition. |
970 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { | 978 | pub fn parent_trait(self, db: &impl DefDatabase) -> Option<Trait> { |
971 | db.trait_items_index(self.module(db).id).get_parent_trait(self.id.into()).map(Trait::from) | 979 | match self.container(db) { |
980 | Some(Container::Trait(it)) => Some(it), | ||
981 | _ => None, | ||
982 | } | ||
972 | } | 983 | } |
973 | 984 | ||
974 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { | 985 | pub fn container(self, db: &impl DefDatabase) -> Option<Container> { |
975 | if let Some(impl_block) = self.impl_block(db) { | 986 | match self.id.lookup(db).container { |
976 | Some(impl_block.into()) | 987 | ContainerId::TraitId(it) => Some(Container::Trait(it.into())), |
977 | } else if let Some(trait_) = self.parent_trait(db) { | 988 | ContainerId::ImplId(it) => Some(Container::ImplBlock(it.into())), |
978 | Some(trait_.into()) | 989 | ContainerId::ModuleId(_) => None, |
979 | } else { | ||
980 | None | ||
981 | } | 990 | } |
982 | } | 991 | } |
983 | 992 | ||