diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-09-01 09:26:10 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-10-08 03:04:20 +0100 |
commit | 974518fde7975b839ed4ccd4c5ce1d48cd6db3c7 (patch) | |
tree | cf25ef47c5531ca9934842a963afaabe78fc525f /crates/hir/src | |
parent | a14194b428efdb09cc45f9862ec34bef0038cd35 (diff) |
Code reorganisation and field support
Diffstat (limited to 'crates/hir/src')
-rw-r--r-- | crates/hir/src/code_model.rs | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 031c91ccf..1dd6d73f3 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -35,7 +35,7 @@ use hir_ty::{ | |||
35 | traits::SolutionVariables, | 35 | traits::SolutionVariables, |
36 | ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, | 36 | ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, |
37 | InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, | 37 | InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, |
38 | TyDefId, TyKind, TypeCtor, | 38 | TyDefId, TyKind, TypeCtor, TyLoweringContext, TypeCtor, |
39 | }; | 39 | }; |
40 | use rustc_hash::FxHashSet; | 40 | use rustc_hash::FxHashSet; |
41 | use stdx::impl_from; | 41 | use stdx::impl_from; |
@@ -186,6 +186,25 @@ impl_from!( | |||
186 | for ModuleDef | 186 | for ModuleDef |
187 | ); | 187 | ); |
188 | 188 | ||
189 | impl From<MethodOwner> for ModuleDef { | ||
190 | fn from(mowner: MethodOwner) -> Self { | ||
191 | match mowner { | ||
192 | MethodOwner::Trait(t) => t.into(), | ||
193 | MethodOwner::Adt(t) => t.into(), | ||
194 | } | ||
195 | } | ||
196 | } | ||
197 | |||
198 | impl From<VariantDef> for ModuleDef { | ||
199 | fn from(var: VariantDef) -> Self { | ||
200 | match var { | ||
201 | VariantDef::Struct(t) => Adt::from(t).into(), | ||
202 | VariantDef::Union(t) => Adt::from(t).into(), | ||
203 | VariantDef::EnumVariant(t) => t.into(), | ||
204 | } | ||
205 | } | ||
206 | } | ||
207 | |||
189 | impl ModuleDef { | 208 | impl ModuleDef { |
190 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { | 209 | pub fn module(self, db: &dyn HirDatabase) -> Option<Module> { |
191 | match self { | 210 | match self { |
@@ -752,8 +771,35 @@ impl Function { | |||
752 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 771 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
753 | hir_ty::diagnostics::validate_body(db, self.id.into(), sink) | 772 | hir_ty::diagnostics::validate_body(db, self.id.into(), sink) |
754 | } | 773 | } |
774 | |||
775 | pub fn parent_def(self, db: &dyn HirDatabase) -> Option<MethodOwner> { | ||
776 | match self.as_assoc_item(db).map(|assoc| assoc.container(db)) { | ||
777 | Some(AssocItemContainer::Trait(t)) => Some(t.into()), | ||
778 | Some(AssocItemContainer::ImplDef(imp)) => { | ||
779 | let resolver = ModuleId::from(imp.module(db)).resolver(db.upcast()); | ||
780 | let ctx = TyLoweringContext::new(db, &resolver); | ||
781 | let adt = Ty::from_hir( | ||
782 | &ctx, | ||
783 | &imp.target_trait(db).unwrap_or_else(|| imp.target_type(db)), | ||
784 | ) | ||
785 | .as_adt() | ||
786 | .map(|t| t.0) | ||
787 | .unwrap(); | ||
788 | Some(Adt::from(adt).into()) | ||
789 | } | ||
790 | None => None, | ||
791 | } | ||
792 | } | ||
755 | } | 793 | } |
756 | 794 | ||
795 | #[derive(Debug)] | ||
796 | pub enum MethodOwner { | ||
797 | Trait(Trait), | ||
798 | Adt(Adt), | ||
799 | } | ||
800 | |||
801 | impl_from!(Trait, Adt for MethodOwner); | ||
802 | |||
757 | // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. | 803 | // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. |
758 | pub enum Access { | 804 | pub enum Access { |
759 | Shared, | 805 | Shared, |