From 974518fde7975b839ed4ccd4c5ce1d48cd6db3c7 Mon Sep 17 00:00:00 2001 From: Zac Pullar-Strecker Date: Tue, 1 Sep 2020 20:26:10 +1200 Subject: Code reorganisation and field support --- crates/hir/src/code_model.rs | 48 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to 'crates/hir/src/code_model.rs') 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::{ traits::SolutionVariables, ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate, InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty, - TyDefId, TyKind, TypeCtor, + TyDefId, TyKind, TypeCtor, TyLoweringContext, TypeCtor, }; use rustc_hash::FxHashSet; use stdx::impl_from; @@ -186,6 +186,25 @@ impl_from!( for ModuleDef ); +impl From for ModuleDef { + fn from(mowner: MethodOwner) -> Self { + match mowner { + MethodOwner::Trait(t) => t.into(), + MethodOwner::Adt(t) => t.into(), + } + } +} + +impl From for ModuleDef { + fn from(var: VariantDef) -> Self { + match var { + VariantDef::Struct(t) => Adt::from(t).into(), + VariantDef::Union(t) => Adt::from(t).into(), + VariantDef::EnumVariant(t) => t.into(), + } + } +} + impl ModuleDef { pub fn module(self, db: &dyn HirDatabase) -> Option { match self { @@ -752,8 +771,35 @@ impl Function { pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { hir_ty::diagnostics::validate_body(db, self.id.into(), sink) } + + pub fn parent_def(self, db: &dyn HirDatabase) -> Option { + match self.as_assoc_item(db).map(|assoc| assoc.container(db)) { + Some(AssocItemContainer::Trait(t)) => Some(t.into()), + Some(AssocItemContainer::ImplDef(imp)) => { + let resolver = ModuleId::from(imp.module(db)).resolver(db.upcast()); + let ctx = TyLoweringContext::new(db, &resolver); + let adt = Ty::from_hir( + &ctx, + &imp.target_trait(db).unwrap_or_else(|| imp.target_type(db)), + ) + .as_adt() + .map(|t| t.0) + .unwrap(); + Some(Adt::from(adt).into()) + } + None => None, + } + } } +#[derive(Debug)] +pub enum MethodOwner { + Trait(Trait), + Adt(Adt), +} + +impl_from!(Trait, Adt for MethodOwner); + // Note: logically, this belongs to `hir_ty`, but we are not using it there yet. pub enum Access { Shared, -- cgit v1.2.3